Const   fi      ='b.inp';
        fo      ='b.out';
        maxn    =100005;
var     n       :longint;
        a,b     :array[0..maxn]of  longint;
        l1,l2   :array[0..maxn]of int64;
Procedure       Nhap;
Var     i,j     :longint;
begin
        //Assign(input,fi);reset(input);
        read(n);
        For i:=1 to 3*n do read(a[i]);
End;

Procedure       swap(var i,j:longint);
Var     tg      :longint;
Begin
        tg:=i;i:=j;j:=tg;
End;

Procedure       xl;
Var     i,j,vt  :Longint;
        res,kq  :int64;
Begin
        //assign(output,fo);Rewrite(output);
        For i:=1 to n do
        For j:=i+1 to n do
                if a[i]<a[j] then swap(a[i],a[j]);
        b[0]:=1000000005;res:=0;
        For i:=1 to n do
        begin
                res:=res+a[i];
                b[i]:=a[i];
        end;
        l1[n]:=res;
        For i:=n+1 to 2*n do
        begin
                b[n+1]:=a[i];
                vt:=n+1;
                while b[vt]>b[vt-1] do
                begin
                        swap(b[vt],b[vt-1]);
                        dec(vt);
                end;
                res:=res-b[n+1]+a[i];
                l1[i]:=res;
        end;
        For i:=2*n+1 to 3*n do
        For j:=i+1 to 3*n do
                if a[i]>a[j] then swap(a[i],a[j]);
        res:=0;b[0]:=-1000000005;
        For i:=1 to n do
        begin
                res:=res+a[i+2*n];
                b[i]:=a[i+2*n];
        end;
        l2[2*n+1]:=res;
        For i:=2*n downto n+1 do
        begin
                b[n+1]:=a[i];
                vt:=n+1;
                while b[vt]<b[vt-1] do
                begin
                        swap(b[vt],b[vt-1]);
                        dec(vt);
                end;
                res:=res+a[i]-b[n+1];
                l2[i]:=res;
        end;
        kq:=-1000000005;
        For i:=n to 2*n do
        For j:=i+1 to 2*n+1 do
                if kq<l1[i]-l2[j] then kq:=l1[i]-l2[j];
        write(kq);
End;

Begin
        Nhap;
        xl;
End.


Const   fi      ='b.inp';
        fo      ='b.out';
        maxn    =100005;
var     n       :longint;
        a,b,l1,l2       :array[0..maxn]of  longint;
Procedure       Nhap;
Var     i,j     :longint;
begin
        //Assign(input,fi);reset(input);
        read(n);
        For i:=1 to 3*n do read(a[i]);
End;

Procedure       swap(var i,j:longint);
Var     tg      :longint;
Begin
        tg:=i;i:=j;j:=tg;
End;

Procedure       xl;
Var     i,j,vt  :Longint;
        res,kq  :int64;
Begin
        //assign(output,fo);Rewrite(output);
        For i:=1 to n do
        For j:=i+1 to n do
                if a[i]<a[j] then swap(a[i],a[j]);
        b[0]:=1000000005;res:=0;
        For i:=1 to n do
        begin
                res:=res+a[i];
                b[i]:=a[i];
        end;
        l1[n]:=res;
        For i:=n+1 to 2*n do
        begin
                b[n+1]:=a[i];
                vt:=n+1;
                while b[vt]>b[vt-1] do
                begin
                        swap(b[vt],b[vt-1]);
                        dec(vt);
                end;
                res:=res-b[n+1]+a[i];
                l1[i]:=res;
        end;
        For i:=2*n+1 to 3*n do
        For j:=i+1 to 3*n do
                if a[i]>a[j] then swap(a[i],a[j]);
        res:=0;b[0]:=-1000000005;
        For i:=1 to n do
        begin
                res:=res+a[i+2*n];
                b[i]:=a[i+2*n];
        end;
        l2[2*n+1]:=res;
        For i:=2*n downto n+1 do
        begin
                b[n+1]:=a[i];
                vt:=n+1;
                while b[vt]<b[vt-1] do
                begin
                        swap(b[vt],b[vt-1]);
                        dec(vt);
                end;
                res:=res+a[i]-b[n+1];
                l2[i]:=res;
        end;
        kq:=-1000000005;
        For i:=n to 2*n do
        For j:=i+1 to 2*n+1 do
                if kq<l1[i]-l2[j] then kq:=l1[i]-l2[j];
        write(kq);
End;

Begin
        Nhap;
        xl;
End.

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


const   fi      ='';
        fo      ='';

var     f,g     :text;
        n,top   :longint;
        b,a,q,vt:array[0..300005] of longint;
        l1,l2   :array[0..300005] of int64;
        kq      :int64;
procedure       nhap;
var     i       :longint;
begin
        assign(f,fi); reset(f);
        readln(f,n);
        for i:=1 to 3*n do read(f,b[i]);
        close(f);
end;

{procedure       swap(i,j        :longint);
var     tg      :longint;
begin
        tg:=a[i]; a[i]:=a[j]; a[j]:=tg;
        tg:=vt[i]; vt[i]:=vt[j]; vt[j]:=tg;
end;

procedure       qs(d,c        :longint);
var     l,r     :longint;
        x       :longint;
begin
        x:=a[(d+c) div 2];
        l:=d; r:=c;
        repeat
                while a[l]<x do inc(l);
                while a[r]>x do dec(r);
                if l<=r then
                        begin
                                swap(l,r);
                                inc(l); dec(r);
                        end;
        until l>r;
        if l<c then qs(l,c);
        if d<r then qs(d,r);
end;       }

{procedure       traubo;
var     kq,tong :int64;
        i,j     :longint;
begin
        kq:=0;
        for i:=n to 2*n do
                begin
                        for j:=1 to 3*n do a[j]:=b[j];
                        qs(1,i);
                        qs(i+1,3*n);
                        tong:=0;
                        for j:=1 to n do tong:=tong+a[j];
                        for j:=3*n downto 2*n+1 do tong:=tong-a[j];
                        if tong>kq then kq:=tong;
                end;
        assign(g,fo); rewrite(g);
        writeln(g,kq);
        close(g);
end;  }

procedure       up(k    :longint);
var     v       :longint;
begin
        v:=q[k];
        while (b[v]<b[q[k div 2]]) do
                begin
                        q[k]:=q[k div 2];
                        vt[q[k]]:=k;
                        k:=k div 2;
                end;
        q[k]:=v;
        vt[q[k]]:=k;
end;

procedure       down(k  :longint);
var     l,v    :longint;
begin
        v:=q[k];
        while 2*k<=top do
                begin
                        l:=2*k;
                        if (l<top) and (b[q[l]]>b[q[l+1]]) then inc(l);
                        if b[q[l]]>=b[v] then break;
                        q[k]:=q[l];
                        vt[q[k]]:=k;
                        k:=l;
                end;
        q[k]:=v;
        vt[v]:=k;
end;

procedure       put(u   :longint);
begin
        inc(top); q[top]:=u; vt[u]:=top;
        up(top);
end;

function        get     :longint;
begin
        get:=q[1];
        q[1]:=q[top]; vt[q[1]]:=1;
        dec(top);
        down(1);
end;

procedure       up1(k    :longint);
var     v       :longint;
begin
        v:=q[k];
        while (b[v]>b[q[k div 2]]) do
                begin
                        q[k]:=q[k div 2];
                        vt[q[k]]:=k;
                        k:=k div 2;
                end;
        q[k]:=v;
        vt[q[k]]:=k;
end;

procedure       down1(k  :longint);
var     l,v    :longint;
begin
        v:=q[k];
        while 2*k<=top do
                begin
                        l:=2*k;
                        if (l<top) and (b[q[l]]<b[q[l+1]]) then inc(l);
                        if b[q[l]]<=b[v] then break;
                        q[k]:=q[l];
                        vt[q[k]]:=k;
                        k:=l;
                end;
        q[k]:=v;
        vt[v]:=k;
end;

procedure       put1(u   :longint);
begin
        inc(top); q[top]:=u; vt[u]:=top;
        up1(top);
end;

function        get1     :longint;
begin
        get1:=q[1];
        q[1]:=q[top]; vt[q[1]]:=1;
        dec(top);
        down1(1);
end;

procedure       xuli;
var     i,j     :longint;
        tong    :int64;
begin
        b[0]:=-1; q[0]:=0; top:=0; tong:=0;
        for i:=1 to n do
                begin
                        tong:=tong+b[i];
                        put(i);
                end;
        l1[n]:=tong;
        for i:=n+1 to 2*n do
                begin
                        if (b[i]>b[q[1]]) and (top>0) then
                                begin
                                        j:=get;
                                        tong:=tong-b[j];
                                        tong:=tong+b[i];
                                        put(i);
                                end;
                        l1[i]:=tong;
                end;
        tong:=0;
        b[0]:=round(1e9)+1; q[0]:=0; top:=0;
        for i:=3*n downto 2*n+1 do
                begin
                        tong:=tong+b[i];
                        put1(i);
                end;
        l2[2*n+1]:=tong;
        for i:=2*n downto n do
                begin
                        if (b[i]<b[q[1]]) and (top>0) then
                                begin
                                        j:=get1;
                                        tong:=tong-b[j];
                                        tong:=tong+b[i];
                                        put1(i);
                                end;
                        l2[i]:=tong;
                end;
        kq:=-round(1e18);
        for i:=n to 2*n do if l1[i]-l2[i+1]>kq then kq:=l1[i]-l2[i+1];
        assign(g,fo); rewrite(g);
        writeln(g,kq);
        close(g);
end;

begin
        nhap;
       // traubo;
       xuli;
end.


Const   fi='';
        fo='';
Var     f:Text;
        n:Longint;
        A:Array[0..300000] of Longint;
        L1,L2:Array[0..300000] of Int64;
        Heapmin,Heapmax:Array[0..100000] of Longint;
        nheapmin,nheapmax:Longint;   sol:Int64;
Function max(i,j:Int64):Int64; Begin If i<j then Exit(j); Exit(i); End;
Procedure Inp;
Var     i:Longint;
Begin
        Assign(f,fi);   Reset(f);
        Read(f,n);
        For i:=1 to 3*n do Read(f,A[i]);
        Close(f);
End;
Procedure Swap(Var i,j:Longint);
Var     tg:Longint;
Begin
        tg:=i; i:=j; j:=tg;
End;
Procedure Upheapmin(x:Longint);
Begin
        If (x=1) or (A[Heapmin[x]]>=A[Heapmin[x div 2]]) then Exit
        Else Begin
                Swap(Heapmin[x],Heapmin[x div 2]);
                Upheapmin(x div 2);
        End;
End;
Procedure Downheapmin(x:Longint);
Var     vt:Longint;
Begin
        If (x>nheapmin div 2) then Exit;
        If (A[Heapmin[x*2]]>=A[Heapmin[x*2+1]]) and (x*2+1<=nheapmin) then vt:=x*2+1 Else vt:=x*2;
        If (A[Heapmin[x]]<=A[heapmin[vt]]) then Exit
        Else Begin
                Swap(a[heapmin[x]],a[heapmin[vt]]);
                Downheapmin(vt);
        End;
End;
Procedure Pushmin(x:Longint);
Begin
        Inc(nheapmin);
        heapmin[nheapmin]:=x;
        upheapmin(nheapmin);
End;
Function popmin:Longint;
Begin
        Popmin:=Heapmin[1];
        heapmin[1]:=heapmin[nheapmin];
        Dec(nheapmin);
        downheapmin(1);
End;
Procedure Upheapmax(x:Longint);
Begin
        If (x=1) or (A[Heapmax[x]]<=A[Heapmax[x div 2]]) then Exit
        Else Begin
                Swap(Heapmax[x],Heapmax[x div 2]);
                Upheapmax(x div 2);
        End;
End;
Procedure Downheapmax(x:Longint);
Var     vt:Longint;
Begin
        If (x>nheapmax div 2) then Exit;
        If (A[Heapmax[x*2]]<=A[Heapmax[x*2+1]]) and (x*2+1<=nheapmax) then vt:=x*2+1 Else vt:=x*2;
        If (A[Heapmax[x]]<=A[heapmax[vt]]) then Exit
        Else Begin
                Swap(a[heapmax[x]],a[heapmax[vt]]);
                Downheapmax(vt);
        End;
End;
Procedure Pushmax(x:Longint);
Begin
        Inc(nheapmax);
        heapmax[nheapmax]:=x;
        upheapmax(nheapmin);
End;
Function popmax:Longint;
Begin
        Popmax:=Heapmax[1];
        Heapmax[1]:=heapmax[nheapmax];
        Dec(nheapmax);
        downheapmax(1);
End;
Procedure tongmax;
Var     i,j,k:Longint;
Begin
        For i:=1 to n do
        Begin
                L1[i]:=L1[i-1]+A[i];
                Pushmin(i);
        End;
        For i:=n+1 to 3*n do
        Begin
                k:=Popmin;
                If A[i]>A[k] then Begin L1[i]:=L1[i-1]-A[k]+A[i]; Pushmin(i); End
                Else Begin Pushmin(k); L1[i]:=L1[i-1]; End;
        End;
End;
Procedure tongmin;
Var     i,j,k:Longint;
Begin
        For i:=3*n downto 2*n+1 do
        Begin
                L2[i]:=L2[i+1]+A[i];
                Pushmax(i);
        End;
        For i:=2*n downto 1 do
        Begin
                k:=Popmax;
                If A[i]<A[k] then Begin L2[i]:=L2[i+1]-A[k]+A[i]; Pushmax(i); End
                Else Begin Pushmax(k); L2[i]:=L2[i+1]; End;
        End;
End;
Procedure Solve;
Var     i,j:Longint;
Begin
        tongmax;
        tongmin;
        sol:=-trunc(1e18);
        For i:=n to 2*n do sol:=max(sol,L1[i]-L2[i+1]);
        Assign(f,fo);   Rewrite(f);
        Write(f,sol);
        Close(f);
End;
BEGIN
        Inp;
        Solve;
END.


var i,n:longint;
    a,b:array[1..200000] of longint;
    tmp,k:qword;
procedure sort(l,r:longint);
var i,j,x,y: longint;
begin
  i:=l; j:=r; x:=a[(l + r) >> 1];
  repeat
    while a[i] < x do i:=i + 1;
    while x < a[j] do j:=j - 1;
    if not(i > j) then
      begin
        y:=a[i];
        a[i]:=a[j];
        a[j]:=y;
        y:=b[i];
        b[i]:=b[j];
        b[j]:=y;
        i:=i + 1; j:=j - 1;
      end;
 until i > j;
 if l < j then sort(l,j);
 if i < r then sort(i,r);
end;
begin
  readln(n,k);
  for i:=1 to n do readln(a[i],b[i]);
  sort(1,n);
  for i:=1 to n do
    begin
      tmp+=b[i];
      if tmp >= k then
        begin
          write(a[i]);
          break;
        end;
    end;
end.

Score : 300 points

Problem Statement

There is an empty array.
The following N operations will be performed to insert integers into the array.
In the i-th operation (1≤i≤N), b_i copies of an integer a_i are inserted into the array.
Find the K-th smallest integer in the array after the N operations.
For example, the 4-th smallest integer in the array \{1,2,2,3,3,3\} is 3.

Constraints

1≤N≤10^5

1≤a_i,b_i≤10^5

1≤K≤b_1…+…b_n

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
a_1 b_1
:
a_N b_N

Output

Print the K-th smallest integer in the array after the N operations.

Sample Input 1

3 4
1 1
2 2
3 3

Sample Output 1

3

The resulting array is the same as the one in the problem statement.

Sample Input 2

10 500000
1 100000
1 100000
1 100000
1 100000
1 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000

Sample Output 2

1


var i,n:longint;
    a,b:array[1..200000] of longint;
    tmp,k:qword;
procedure sort(l,r:longint);
var i,j,x,y: longint;
begin
  i:=l; j:=r; x:=a[(l + r) >> 1];
  repeat
    while a[i] < x do i:=i + 1;
    while x < a[j] do j:=j - 1;
    if not(i > j) then
      begin
        y:=a[i];
        a[i]:=a[j];
        a[j]:=y;
        y:=b[i];
        b[i]:=b[j];
        b[j]:=y;
        i:=i + 1; j:=j - 1;
      end;
 until i > j;
 if l < j then sort(l,j);
 if i < r then sort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do readln(a[i],b[i]);
  readln(k);
  sort(1,n);
  for i:=1 to n do
    begin
      tmp+=b[i];
      if tmp >= k then
        begin
          write(a[i]);
          break;
        end;
    end;
end.


var i,n:longint;
    a,b:array[1..200000] of longint;
    tmp,k:qword;
procedure sort(l,r:longint);
var i,j,x,y: longint;
begin
  i:=l; j:=r; x:=a[(l + r) >> 1];
  repeat
    while a[i] < x do i:=i + 1;
    while x < a[j] do j:=j - 1;
    if not(i > j) then
      begin
        y:=a[i];
        a[i]:=a[j];
        a[j]:=y;
        y:=b[i];
        b[i]:=b[j];
        b[j]:=y;
        i:=i + 1; j:=j - 1;
      end;
 until i > j;
 if l < j then sort(l,j);
 if i < r then sort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do readln(a[i],b[i]);
  readln(k);
  sort(1,n);
  for i:=1 to n do
    begin
      if b[i] + tmp < k then tmp+=b[i]
      else if b[i] + tmp >= k then
        begin
          write(a[i]);
          break;
        end;
    end;
end.


var b,bb:array[1..800,1..800]of boolean;
    a:array[1..800,1..800]of longint;
    x,y,i,j,h,w,k,sx,sy,ans:longint;
    ch:char;
function min(a,b,c,d,e:longint):longint;
begin
    if(a<b)and(a<c)and(a<d)and(a<e)then exit(a)
    else if(b<c)and(b<d)and(b<e)then exit(b)
    else if(c<d)and(c<e)then exit(c)
    else if d<e then exit(d)else exit(e);
end;
procedure pp(x,y,p:longint);
begin
    ans:=min(ans,x-1,y-1,h-x,w-y);bb[x,y]:=true;
    if(x+1)<=h then if(not b[x+1,y])and(p<k)and(not bb[x+1,y])then pp(x+1,y,p+1);
    if(x-1)>0 then if(not b[x-1,y])and(p<k)and(not bb[x-1,y])then pp(x-1,y,p+1);
    if(y+1)<=w then if(not b[x,y+1])and(p<k)and(not bb[x,y+1])then pp(x,y+1,p+1);
    if(y-1)>0 then if(not b[x,y-1])and(p<k)and(not bb[x,y-1])then pp(x,y-1,p+1);
end;
procedure init;
begin
    readln(h,w,k);
    for i:=1 to h do begin
    for j:=1 to w do begin
        read(ch);
        if ch='S'then begin
            sx:=i;sy:=j;
        end else if ch='#'then b[i,j]:=true;
    end;readln;
    end;
end;
begin
    fillchar(b,sizeof(b),false);
    fillchar(bb,sizeof(bb),false);
    init;
    ans:=maxlongint;
    pp(sx,sy,0);
    if(sx=1)or(sx=h)or(sy=w)or(sy=1)then writeln('0')else
    if ans mod k<>0 then writeln((ans+k) div k+1) else writeln(ans div k+1);
end.

Galaxy Online Judge System

Score : 700 points

Problem Statement

Takahashi is locked within a building.

This building consists of H×W rooms, arranged in H rows and W columns.
We will denote the room at the i-th row and j-th column as (i,j). The state of this room is represented by a character A_{i,j}. If A_{i,j}= #, the room is locked and cannot be entered; if A_{i,j}= ., the room is not locked and can be freely entered.
Takahashi is currently at the room where A_{i,j}= S, which can also be freely entered.

Each room in the 1-st row, 1-st column, H-th row or W-th column, has an exit.
Each of the other rooms (i,j) is connected to four rooms: (i-1,j), (i+1,j), (i,j-1) and (i,j+1).

Takahashi will use his magic to get out of the building. In one cast, he can do the following:

Move to an adjacent room at most K times, possibly zero. Here, locked rooms cannot be entered.

Then, select and unlock at most K locked rooms, possibly zero. Those rooms will remain unlocked from then on.

His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.

It is guaranteed that Takahashi is initially at a room without an exit.

Constraints

3 ≤ H ≤ 800

3 ≤ W ≤ 800

1 ≤ K ≤ H×W

Each A_{i,j} is # , . or S.

There uniquely exists (i,j) such that A_{i,j}= S, and it satisfies 2 ≤ i ≤ H-1 and 2 ≤ j ≤ W-1.

Input

Input is given from Standard Input in the following format:

H W K
A_{1,1}A_{1,2}...A_{1,W}
:
A_{H,1}A_{H,2}...A_{H,W}

Output

Print the minimum necessary number of casts.

Sample Input 1

3 3 3
#.#
#S.
###

Sample Output 1

1

Takahashi can move to room (1,2) in one cast.

Sample Input 2

3 3 3
###
#S#
###

Sample Output 2

2

Takahashi cannot move in the first cast, but can unlock room (1,2).
Then, he can move to room (1,2) in the next cast, achieving the objective in two casts.

Sample Input 3

7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######

Sample Output 3

2


var
  n,m,k,i,j,x,y,head,tail,tot,ans:longint;
  map:array[0..801,0..801]of char;
  cost,dx,dy:array[0..650000]of longint;
  p:array[0..801,0..801]of boolean;
  s:ansistring;

function min(x,y:longint):longint;
begin
    if x<y then exit(x) else exit(y);
end;

begin
    readln(n,m,k);
    for i:=1 to n do begin
        readln(s);
        for j:=1 to m do begin
            map[i,j]:=s[j];
            if map[i,j]='S' then begin
                x:=i;
                y:=j;
                if (x=1)or(x=n)or(y=1)or(y=n) then begin
                    writeln(0);
                    halt;
                end;
            end;
            if map[i,j]='.' then p[i,j]:=true;
        end;
    end;
    head:=1; tail:=1;
    dx[1]:=x; dy[1]:=y;
    while head<=tail do begin
        x:=dx[head]; y:=dy[head];
        if cost[head]=k+1 then break;
        if p[x-1,y] and (x>1) then begin
            inc(tail);
            dx[tail]:=x-1; dy[tail]:=y;
            cost[tail]:=cost[head]+1;
            p[x-1,y]:=false;
        end;
        if p[x+1,y] and (x<n) then begin
            inc(tail);
            dx[tail]:=x+1; dy[tail]:=y;
            cost[tail]:=cost[head]+1;
            p[x+1,y]:=false;
        end;
        if p[x,y-1] and (y>1) then begin
            inc(tail);
            dx[tail]:=x; dy[tail]:=y-1;
            cost[tail]:=cost[head]+1;
            p[x,y-1]:=false;
        end;
        if p[x,y+1] and (y<m) then begin
            inc(tail);
            dx[tail]:=x; dy[tail]:=y+1;
            cost[tail]:=cost[head]+1;
            p[x,y+1]:=false;
        end;
        inc(head);
    end;
    ans:=maxlongint;
    for i:=1 to head-1 do begin
        x:=dx[i]; y:=dy[i];
        tot:=min(min(x-1,y-1),min(n-x,m-y));
        if tot mod k=0 then ans:=min(ans,tot div k)
        else ans:=min(ans,(tot div k)+1);
    end;
    writeln(ans+1);
end.


var
  f:array[0..1000] of longint;
  w,v:array[0..1001] of longint;
  i,j,n,m:longint;
begin
  readln(m,n);
  for i:=1 to n do readln(w[i],v[i]);
  for i:=1 to n do
    for j:=m downto w[i] do
      if f[j-w[i]]+v[i]>f[j] then f[j]:=f[j-w[i]]+v[i];
  writeln(f[m]);
end.

Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.


var
	N,W,wi,w0,vi,i,j,k,l:Longint;
	v:array[0..3,0..100]of Longint;
	c:array[0..3]of Longint;
	ans,now,noww:int64;
begin
	read(N,W);
	for i:=1 to N do begin
		read(wi,vi);
		if i=1 then w0:=wi;
		inc(c[wi-w0]);
		v[wi-w0,c[wi-w0]]:=vi;
	end;
	for i:=0 to 3 do begin
		for j:=1 to c[i] do begin
			for k:=j+1 to c[i] do begin
				if v[i,j]<v[i,k]then begin
					l:=v[i,j];
					v[i,j]:=v[i,k];
					v[i,k]:=l;
				end;
			end;
		end;
		v[i,0]:=0;
		for j:=1 to c[i] do inc(v[i,j],v[i,j-1]);
	end;
	ans:=0;
	now:=0;
	noww:=0;
	for i:=0 to c[0] do begin
		for j:=0 to c[1] do begin
			for k:=0 to c[2] do begin
				for l:=0 to c[3] do begin
					if(w0*(i+j+k+l)+j+k*2+l*3<=W)and(v[0,i]+v[1,j]+v[2,k]+v[3,l]>ans)then begin
						ans:=v[0,i]+v[1,j]+v[2,k]+v[3,l];
					end;
				end;
			end;
		end;
	end;
	writeln(ans);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes;
var
  h,w,i,j,count,x,y,sw,m,n:Integer;
  s123,s1,s2,s3,a,b:string;
  tf:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(a);
    Readln(b);

    if Length(a)>Length(b) then begin
      Writeln('GREATER');
      Readln;
      exit;
    end else if Length(a)<Length(b) then begin
      Writeln('LESS');
      Readln;
      exit;
    end;

    for i := 1 to Length(a) do begin
      if StrToInt(a[i])>StrToInt(b[i]) then begin
        Writeln('GREATER');
        Readln;
        exit;
      end else if StrToInt(a[i])<StrToInt(b[i]) then begin
        Writeln('LESS');
        Readln;
        exit;
      end;
    end;
    Writeln('EQUAL');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

You are given two positive integers A and B. Compare the magnitudes of these numbers.

Constraints

1 ≤ A, B ≤ 10^{100}

Neither A nor B begins with a 0.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print GREATER if A>B, LESS if A<B and EQUAL if A=B.

Sample Input 1

36
24

Sample Output 1

GREATER

Since 36>24, print GREATER.

Sample Input 2

850
3777

Sample Output 2

LESS

Sample Input 3

9720246
22516266

Sample Output 3

LESS

Sample Input 4

123456789012345678901234567890
234567890123456789012345678901

Sample Output 4

LESS


program ec12;
var
	ans,sum1,sum2,ans1:int64;
	a:array[0..100000] of int64;
	n,m,i,j:longint;
begin
	readln(n);
	ans:=0;
	ans1:=0;
	for i:=1 to n do
	read(a[i]);
	if a[1]>0 then
	begin
		sum1:=a[1];
		ans1:=a[1]+1;
		sum2:=-1;
	end
	else
	begin
		if a[1]=0 then
		begin
			sum1:=1;
			sum2:=-1;
			ans:=1;
			ans1:=1;
		end
		else
		begin
			ans:=abs(a[1])+1;
			sum1:=1;
			sum2:=a[1];
		end;
	end;
	for i:=2 to n do
	begin
		if sum1>0 then
		begin
			if sum1+a[i]>=0 then
			begin
				inc(ans,sum1+a[i]+1);
				sum1:=-1;
			end
			else
			sum1:=sum1+a[i];
		end
		else
		begin
			if sum1+a[i]<=0 then
			begin
				inc(ans,abs(sum1+a[i])+1);
				sum1:=1;
			end
			else
			sum1:=sum1+a[i];
		end;
		if sum2>0 then
		begin
			if sum2+a[i]>=0 then
			begin
				inc(ans1,sum2+a[i]+1);
				sum2:=-1;
			end
			else
			sum2:=sum2+a[i];
		end
		else
		begin
			if sum2+a[i]<=0 then
			begin
				inc(ans1,abs(sum2+a[i])+1);
				sum2:=1;
			end
			else
			sum2:=sum2+a[i];
		end;
	end;
	if sum1=0 then
	inc(ans);
	if sum2=0 then
	inc(ans1);
	if ans<ans1 then
	writeln(ans)
	else
	writeln(ans1);
end.

Score : 300 points

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.

At least how many operations are necessary to satisfy the following conditions?

For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.

For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8


program ec12;
var
	a:array[0..100000] of longint;
	n,m,i,j,ans,sum1,sum2,ans1:longint;
begin
	readln(n);
	ans:=0;
	ans1:=0;
	for i:=1 to n do
	read(a[i]);
	if a[1]>0 then
	begin
		sum1:=a[1];
		ans1:=a[1]+1;
		sum2:=-1;
	end
	else
	begin
		if a[1]=0 then
		begin
			sum1:=1;
			sum2:=-1;
			ans:=1;
			ans1:=1;
		end
		else
		begin
			ans:=abs(a[1])+1;
			sum1:=1;
			sum2:=a[1];
		end;
	end;
	for i:=2 to n do
	begin
		if sum1>0 then
		begin
			if sum1+a[i]>=0 then
			begin
				inc(ans,sum1+a[i]+1);
				sum1:=-1;
			end
			else
			sum1:=sum1+a[i];
		end
		else
		begin
			if sum1+a[i]<=0 then
			begin
				inc(ans,abs(sum1+a[i])+1);
				sum1:=1;
			end
			else
			sum1:=sum1+a[i];
		end;
		if sum2>0 then
		begin
			if sum2+a[i]>=0 then
			begin
				inc(ans1,sum2+a[i]+1);
				sum2:=-1;
			end
			else
			sum2:=sum2+a[i];
		end
		else
		begin
			if sum2+a[i]<=0 then
			begin
				inc(ans1,abs(sum2+a[i])+1);
				sum2:=1;
			end
			else
			sum2:=sum2+a[i];
		end;
	end;
	if sum1=0 then
	inc(ans);
	if sum2=0 then
	inc(ans1);
	if ans<ans1 then
	writeln(ans)
	else
	writeln(ans1);
end.


program ec12;
var
	a,s:array[0..100000] of longint;
	n,m,i,j,ans,sum1,sum2,ans1:longint;
begin
	readln(n);
	ans:=0;
	ans1:=0;
	s[0]:=0;
	for i:=1 to n do
	read(a[i]);
	if a[1]>0 then
	begin
		sum1:=a[1];
		ans:=0;
		ans1:=a[1]+1;
		sum2:=-1;
	end
	else
	begin
		if a[1]=0 then
		begin
			sum1:=1;
			sum2:=-1;
			ans:=1;
			ans1:=1;
		end
		else
		begin
			ans:=abs(a[1])+1;
			sum1:=1;
			sum2:=a[1];
			ans1:=0;
		end;
	end;
	for i:=2 to n do
	begin
		if sum1>0 then
		begin
			if sum1+a[i]>=0 then
			begin
				inc(ans,sum1+a[i]+1);
				sum1:=-1;
			end
			else
			sum1:=sum1+a[i];
		end
		else
		begin
			if sum1+a[i]<=0 then
			begin
				inc(ans,abs(sum1+a[i])+1);
				sum1:=1;
			end
			else
			sum1:=sum1+a[i];
		end;
		if sum2>0 then
		begin
			if sum2+a[i]>=0 then
			begin
				inc(ans1,sum2+a[i]+1);
				sum2:=-1;
			end
			else
			sum2:=sum2+a[i];
		end
		else
		begin
			if sum2+a[i]<=0 then
			begin
				inc(ans1,abs(sum2+a[i])+1);
				sum2:=1;
			end
			else
			sum2:=sum2+a[i];
		end;
	end;
	if sum1=0 then
	inc(ans);
	if sum2=0 then
	inc(ans1);
	if ans<ans1 then
	writeln(ans)
	else
	writeln(ans1);
end.


program ec12;
var
	a,s:array[0..100000] of longint;
	n,m,i,j,ans,sum1,sum2,ans1:longint;
begin
	readln(n);
	ans:=0;
	ans1:=0;
	s[0]:=0;
	for i:=1 to n do
	read(a[i]);
	if a[1]>0 then
	begin
		sum1:=a[1];
		ans:=0;
		ans1:=a[1]+1;
		sum2:=-1;
	end
	else
	begin
		if a[1]=0 then
		begin
			sum1:=1;
			sum2:=-1;
			ans:=1;
			ans1:=1;
		end
		else
		begin
			ans:=(-a[1])+1;
			sum1:=1;
			ans1:=0;
		end;
	end;
	for i:=2 to n do
	begin
		if sum1>0 then
		begin
			if sum1+a[i]>=0 then
			begin
				inc(ans,sum1+a[i]+1);
				sum1:=-1;
			end
			else
			sum1:=sum1+a[i];
		end
		else
		begin
			if sum1+a[i]<=0 then
			begin
				inc(ans,abs(sum1+a[i])+1);
				sum1:=1;
			end
			else
			sum1:=sum1+a[i];
		end;
		if sum2>0 then
		begin
			if sum2+a[i]>=0 then
			begin
				inc(ans1,sum2+a[i]+1);
				sum2:=-1;
			end
			else
			sum2:=sum2+a[i];
		end
		else
		begin
			if sum2+a[i]<=0 then
			begin
				inc(ans1,abs(sum2+a[i])+1);
				sum2:=1;
			end
			else
			sum2:=sum2+a[i];
		end;
	end;
	if ans<ans1 then
	writeln(ans)
	else
	writeln(ans1);
end.


program ec12;
var
	a,s:array[0..100000] of longint;
	n,m,i,j,ans:longint;
begin
	readln(n);
	ans:=0;
	s[0]:=0;
	for i:=1 to n do
	read(a[i]);
	for i:=1 to n do
	begin
		s[i]:=s[i-1]+a[i];
		if i>1 then
		begin
			if s[i-1]<0 then
			begin
				if s[i]<=0 then
				begin
					if s[i]=0 then
					begin
						inc(ans);
						s[i]:=1;
					end
					else
					inc(ans,(-s[i])+1);
				end;
			end
			else
			begin
				if s[i]>=0 then
				begin
					if s[i]=0 then
					begin
						inc(ans);
						s[i]:=-1;
					end
					else
					begin
						inc(ans,s[i]+1);
						s[i]:=-1;
					end;
				end;
			end;
		end
		else
		begin
			if a[1]=0 then
			begin
				if a[2]>0 then
				begin
					inc(ans,a[2]+1);
					s[1]:=-1;
				end
				else
				begin
					inc(ans,(-a[2])+1);
					s[1]:=1;
				end;
			end;
		end;
	end;
	writeln(ans);
end.


program ec12;
var
	a,s:array[0..100000] of longint;
	n,m,i,j,ans:longint;
begin
	readln(n);
	ans:=0;
	s[0]:=0;
	for i:=1 to n do
	begin
		read(a[i]);
		s[i]:=s[i-1]+a[i];
		if i>1 then
		begin
			if s[i-1]<0 then
			begin
				if s[i]<=0 then
				begin
					if s[i]=0 then
					begin
						inc(ans);
						s[i]:=1;
					end
					else
					inc(ans,(-s[i])+1);
				end;
			end
			else
			begin
				if s[i]>=0 then
				begin
					if s[i]=0 then
					begin
						inc(ans);
						s[i]:=-1;
					end
					else
					begin
						inc(ans,s[i]+1);
						s[i]:=-1;
					end;
				end;
			end;
		end;
	end;
	writeln(ans);
end.


var n,i,t,x,y,ans:longint;
procedure fresh;inline;
begin
  t:=0;inc(ans);
end;
begin
  read(n,x);
  for i:=2 to n do
  begin
    read(y);
    case t of
      0:if x>y then t:=1 else
        if x<y then t:=2;
      1:if x<y then fresh;
      2:if x>y then fresh;
    end;
    x:=y;
  end;
  writeln(ans+1);
end.

Score : 300 points

Problem Statement

You are given an array A of length N.
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3


var
  i,j,a1,a2,n,r:integer;
  a:array[1..100005] of integer;
begin
  r:=0;
  read(n);
  for i:=1 to n do read(a[i]);
  while j<=n do
  begin
    r:=r+1;
    a1:=a[j];
    j:=j+1;
    while (a[j]=a1) and (j<=n) do j:=j+1;
    a2:=a[j];
    j:=j+1;
    while ((a1-a2) * (a[j-1]-a[j]) >= 0) and (j<=n) do j:=j+1;
  end;
  writeln(r);
end.


var
  a,b,c:longint;
begin
  readln(a,b,c);
  if (abs(a-b)=abs(b-c)) then writeln('YES')
    else writeln('NO');
end.

Score : 100 points

Problem Statement

Three poles stand evenly spaced along a line. Their heights are a, b and c meters, from left to right.
We will call the arrangement of the poles beautiful if the tops of the poles lie on the same line, that is, b-a = c-b.

Determine whether the arrangement of the poles is beautiful.

Constraints

1 \leq a,b,c \leq 100

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print YES if the arrangement of the poles is beautiful; print NO otherwise.

Sample Input 1

2 4 6

Sample Output 1

YES

Since 4-2 = 6-4, this arrangement of poles is beautiful.

Sample Input 2

2 5 6

Sample Output 2

NO

Since 5-2 \neq 6-5, this arrangement of poles is not beautiful.

Sample Input 3

3 2 1

Sample Output 3

YES

Since 1-2 = 2-3, this arrangement of poles is beautiful.


// luogu-judger-enable-o2
var
 a:array[0..300005] of longint;
 i,j,n:longint;
 s:qword;
 procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]<x do
            inc(i);
           while x<a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
begin
 readln(n);
 n:=n*3;
 for i:=1 to n do read(a[i]);
 sort(1,n);
 i:=n-1;
 s:=s+a[i];
 while i>3 do
  begin
   i:=i-2;
   s:=s+a[i];
  end;
 writeln(s);
end.

Score : 300 points

Problem Statement

There are 3N participants in AtCoder Group Contest.
The strength of the i-th participant is represented by an integer a_i.
They will form N teams, each consisting of three participants.
No participant may belong to multiple teams.

The strength of a team is defined as the second largest strength among its members.
For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3.

Find the maximum possible sum of the strengths of N teams.

Constraints

1 ≤ N ≤ 10^5

1 ≤ a_i ≤ 10^{9}

a_i are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the answer.

Sample Input 1

2
5 2 8 5 1 5

Sample Output 1

10

The following is one formation of teams that maximizes the sum of the strengths of teams:

Team 1: consists of the first, fourth and fifth participants.

Team 2: consists of the second, third and sixth participants.

Sample Input 2

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

10000000000

The sum of the strengths can be quite large.


var n,i,x:longint;
    s:int64;
    a:array[0..100003] of longint;
    fi,fo:text;
procedure start;
begin
    assign(fi,''); reset(fi);
    assign(fo,''); rewrite(fo);
    readln(fi,n);
    n:= 3*n;
    for i:= 1 to n do read(fi,a[i]);
end;
procedure sort(l,r:longint);
var i,j,mid,tmp:longint;
begin
    i:= l; j:= r; mid:= a[(l+r)div 2];
    repeat
        while a[i]<mid do inc(i);
        while a[j]>mid do dec(j);
        if i<=j then
        begin
            tmp:= a[i]; a[i]:= a[j]; a[j]:= tmp;
            inc(i); dec(j);
        end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
end;
procedure main;
begin
    s:= 0;
    x:= n+1;
    for i:= 1 to n div 3 do s:= s+a[x-2*i];
end;
begin
    start;
    sort(1,n);
    main;
    writeln(fo,s);
    close(fi); close(fo);
end.


var i,j,k,l,m,n,q,w,x,y,t:longint;
    st,ed,first,next,d,v,color,fw,c:array[1..200000] of longint;
    b:array[1..200000]of boolean;
procedure ad(p,o:longint);
begin
    t:=t+1;  ed[t]:=o; next[t]:=first[p];first[p]:=t;
end;
procedure dfs(p,o,cl:longint);
var i,j:longint;
begin
    if fw[p]>=o then exit;
    i:=first[p]; fw[p]:=o;  b[p]:=true;
    if color[p]=0 then color[p]:=cl;
    while (i>=1) do begin
        if b[ed[i]]=false then
               dfs(ed[i],o-1,cl);
        i:=next[i];
    end;
   b[p]:=false;
end;
begin
    readln(n,m);
    for i:=1 to m do first[i]:=-1;
    fillchar(fw,sizeof(fw),255);
    for i:=1 to m do begin readln(x,y); ad(x,y);ad(y,x); end;
    readln(q);
    for i:=1 to q do readln(v[i],d[i],c[i]);
    for i:=q downto 1 do begin dfs(v[i],d[i],c[i]); end;
    for i:=1 to n do writeln(color[i]);
end.

Score : 700 points

Problem Statement

Squid loves painting vertices in graphs.

There is a simple undirected graph consisting of N vertices numbered 1 through N, and M edges.
Initially, all the vertices are painted in color 0. The i-th edge bidirectionally connects two vertices a_i and b_i. The length of every edge is 1.

Squid performed Q operations on this graph. In the i-th operation, he repaints all the vertices within a distance of d_i from vertex v_i, in color c_i.

Find the color of each vertex after the Q operations.

Constraints

1 ≤ N,M,Q ≤ 10^5

1 ≤ a_i,b_i,v_i ≤ N

a_i ≠ b_i

0 ≤ d_i ≤ 10

1 ≤ c_i ≤10^5

d_i and c_i are all integers.

There are no self-loops or multiple edges in the given graph.

Partial Score

200 points will be awarded for passing the testset satisfying 1 ≤ N,M,Q ≤ 2{,}000.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{M} b_{M}
Q
v_1 d_1 c_1
:
v_{Q} d_{Q} c_{Q}

Output

Print the answer in N lines.
In the i-th line, print the color of vertex i after the Q operations.

Sample Input 1

7 7
1 2
1 3
1 4
4 5
5 6
5 7
2 3
2
6 1 1
1 2 2

Sample Output 1

2
2
2
2
2
1
0

Initially, each vertex is painted in color 0.
In the first operation, vertices 5 and 6 are repainted in color 1.
In the second operation, vertices 1, 2, 3, 4 and 5 are repainted in color 2.

Sample Input 2

14 10
1 4
5 7
7 11
4 10
14 7
14 3
6 14
8 11
5 13
8 3
8
8 6 2
9 7 85
6 9 3
6 7 5
10 3 1
12 9 4
9 6 6
8 2 3

Sample Output 2

1
0
3
1
5
5
3
3
6
1
3
4
5
3

The given graph may not be connected.


var
  n,m,x,y,i,q,total:longint;
  b,p,d,v,tot,que,first,next,ans,c:array[0..200010]of longint;

procedure add(x,y:longint);
begin
    inc(total);
    next[total]:=first[x];
    first[x]:=total;
    b[total]:=y;
end;

procedure bfs(k:longint);
var
  head,tail:longint;
  s:longint;
begin
    head:=1;
    tail:=1;
    que[1]:=k;
    while head<=tail do begin
        s:=first[que[head]];
        if ans[que[head]]=0 then ans[que[head]]:=c[i];
        if tot[head]>p[que[head]] then begin
            p[que[head]]:=tot[head];
            if tot[head]>0 then begin
                while s<>-1 do begin
                    inc(tail);
                    tot[tail]:=tot[head]-1;
                    que[tail]:=b[s];
                    s:=next[s];
                end;
            end;
        end;
        inc(head);
    end;
end;

begin
    read(n,m);
    for i:=1 to n*2  do first[i]:=-1;
    for i:=1 to m do begin
        read(x,y);
        add(x,y);
        add(y,x);
    end;
    read(q);
    for i:=1 to q do read(v[i],d[i],c[i]);
    for i:=q downto 1 do begin
        if p[v[i]]>=d[i] then continue;
        if (ans[v[i]]<>0)and(d[i]=0) then continue;
        tot[1]:=d[i];
        bfs(v[i]);
    end;
    for i:=1 to n do writeln(ans[i]);
end.


type arr=array[1..200000]of longint;
var n,q,m,i,j,a,b:longint;
    v,d,c,max,cl,head,next,start,endd:arr;
    bb,bc:array[1..100000]of boolean;
procedure dfs(k,dis,col:longint);
var i:longint;
begin
    bb[k]:=true;
    if(max[k]>=dis)and(bc[k])then exit;
    max[k]:=dis;
    if not bc[k]then begin
        cl[k]:=col;
        bc[k]:=true;
    end;
    if dis>0 then for i:=start[k] to endd[k] do
    if not bb[next[i]]then dfs(next[i],dis-1,col);
    bb[k]:=false;
end;
procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
    i:=l;j:=r;x:=head[(l+r) div 2];
    repeat
        while head[i]<x do inc(i);
        while x<head[j] do dec(j);
        if not(i>j) then begin
            y:=head[i];head[i]:=head[j];head[j]:=y;
            y:=next[i];next[i]:=next[j];next[j]:=y;
            inc(i);j:=j-1;
        end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
end;
begin
    readln(n,m);
    fillchar(cl,sizeof(cl),0);
    fillchar(max,sizeof(max),0);
    fillchar(bb,sizeof(bb),false);
    fillchar(bc,sizeof(bc),false);
    for i:=1 to m do begin
        readln(a,b);
        head[i]:=a;head[i+m]:=b;
        next[i]:=b;next[i+m]:=a;
    end;
    m:=m*2;sort(1,m);start[head[1]]:=1;
    for i:=2 to m do if head[i]<>head[i-1]then begin
        endd[head[i-1]]:=i-1;
        start[head[i]]:=i;
    end;
    endd[head[m]]:=m;
    readln(q);
    for i:=q downto 1 do readln(v[i],d[i],c[i]);
    for i:=1 to q do dfs(v[i],d[i],c[i]);
    for i:=1 to n do writeln(cl[i]);
end.


type arrb=array[1..100000]of boolean;
var n,q,m,i,j,a,b:longint;
    v,d,c,l,max,cl:array[1..100000]of longint;
    lian:array[1..100000,1..5000]of longint;
    bb,bc:arrb;
procedure dfs(k,dis,col:longint);
var i:longint;
begin
    bb[k]:=true;
    if(max[k]>=dis)and(bc[k])then exit;
    max[k]:=dis;
    if not bc[k]then begin
        cl[k]:=col;
        bc[k]:=true;
    end;
    if dis>0 then for i:=1 to l[k] do
    if not bb[lian[k,i]]then dfs(lian[k,i],dis-1,col);
    bb[k]:=false;
end;
begin
    readln(n,m);
    fillchar(l,sizeof(l),0);
    fillchar(cl,sizeof(cl),0);
    fillchar(max,sizeof(max),0);
    fillchar(bb,sizeof(bb),false);
    fillchar(bc,sizeof(bc),false);
    for i:=1 to m do begin
        readln(a,b);
        inc(l[a]);lian[a,l[a]]:=b;
        inc(l[b]);lian[b,l[b]]:=a;
    end;
    readln(q);
    for i:=q downto 1 do readln(v[i],d[i],c[i]);
    for i:=1 to q do dfs(v[i],d[i],c[i]);
    for i:=1 to n do writeln(cl[i]);
end.


type arrb=array[1..100000]of boolean;
var n,q,m,i,j,a,b:longint;
    v,d,c,l,max,cl:array[1..100000]of longint;
    lian:array[1..100000,1..1000]of longint;
    bb,bc:arrb;
procedure dfs(k,dis,col:longint);
var i:longint;
begin
    bb[k]:=true;
    if(max[k]>=dis)and(bc[k])then exit;
    max[k]:=dis;
    if not bc[k]then begin
        cl[k]:=col;
        bc[k]:=true;
    end;
    if dis>0 then for i:=1 to l[k] do
    if not bb[lian[k,i]]then dfs(lian[k,i],dis-1,col);
    bb[k]:=false;
end;
begin
    readln(n,m);
    fillchar(l,sizeof(l),0);
    fillchar(cl,sizeof(cl),0);
    fillchar(max,sizeof(max),0);
    fillchar(bb,sizeof(bb),false);
    fillchar(bc,sizeof(bc),false);
    for i:=1 to m do begin
        readln(a,b);
        inc(l[a]);lian[a,l[a]]:=b;
        inc(l[b]);lian[b,l[b]]:=a;
    end;
    readln(q);
    for i:=q downto 1 do readln(v[i],d[i],c[i]);
    for i:=1 to q do dfs(v[i],d[i],c[i]);
    for i:=1 to n do writeln(cl[i]);
end.


var n,ans:int64;
    s,i:longint;
begin
  readln(n);
  for i:=trunc(sqrt(n)) downto 1 do
    if n mod i=0 then
    begin
      ans:=n div i;
      break;
    end;
  while ans>0 do
  begin
    inc(s);
    ans:=ans div 10;
  end;
  writeln(s);
end.

Score : 300 points

Problem Statement

You are given an integer N.

For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B.

For example, F(3,11) = 2 since 3 has one digit and 11 has two digits.

Find the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Constraints

1 \leq N \leq 10^{10}

N is an integer.

Input

The input is given from Standard Input in the following format:

N

Output

Print the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Sample Input 1

10000

Sample Output 1

3

F(A,B) has a minimum value of 3 at (A,B)=(100,100).

Sample Input 2

1000003

Sample Output 2

7

There are two pairs (A,B) that satisfy the condition: (1,1000003) and (1000003,1). For these pairs, F(1,1000003)=F(1000003,1)=7.

Sample Input 3

9876543210

Sample Output 3

6


var
 n,m:int64;
 i:longint;
 s:string;
begin
 readln(n);
 for i:=trunc(sqrt(n)) downto 1 do
  if n mod i=0 then begin m:=n div i; str(m,s); writeln(length(s)); break; end;
end.


var
 n:int64;
 i,j,s:longint;
begin
 read(n);
 for i:=trunc(sqrt(n)) downto 1 do
  if n mod i=0 then
   begin
    j:=n div i;
    break;
   end;
   while j>0 do
    begin
     inc(s);
     j:=j div 10;
    end;
    writeln(s);
end.


{AT2523}
var
n:int64;
i:longint;
s:string;
begin
read(n);
for i:=trunc(sqrt(n)) downto 1 do
 if n mod i=0 then
 begin
 str(n div i,s);
 writeln(length(s));
 halt;
 end;
end.


var
	dp1:array[0..5000]of Boolean;
	dp2:array[0..5000,0..5000]of Longint;
	sum:array[0..5001]of Longint;
	n,k,i,j,id,cnt:Longint;
	flag:Boolean;
	a:array[1..5000]of Longint;
begin
	read(n,k);
	for i:=1 to n do read(a[i]);
	dp2[n][0]:=1;
	for i:=n downto 1 do begin
		for j:=0 to k do dp2[i-1][j]:=dp2[i][j];
		for j:=0 to k-a[i] do if dp2[i][j]=1 then dp2[i-1][j+a[i]]:=1;
	end;
	dp1[0]:=true;
	for i:=1 to n do begin
		sum[0]:=0;
		for j:=0 to k do sum[j+1]:=sum[j]+dp2[i][j];
		flag:=false;
		for j:=0 to k do if dp1[j]then begin
			id:=k-a[i]-j;
			if id<0 then id:=0;
			if sum[k-j]-sum[id]>0 then begin
				flag:=true;
				break;
			end;
		end;
		if not flag then inc(cnt);
		for j:=k downto a[i] do dp1[j]:=dp1[j]or dp1[j-a[i]];
	end;
	writeln(cnt);
end.

Score : 600 points

Problem Statement

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i.
Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.

Then, for each card i, he judges whether it is unnecessary or not, as follows:

If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.

Otherwise, card i is NOT unnecessary.

Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

Constraints

All input values are integers.

1≤N≤5000

1≤K≤5000

1≤a_i≤10^9 (1≤i≤N)

Partial Score

300 points will be awarded for passing the test set satisfying N,K≤400.

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the number of the unnecessary cards.

Sample Input 1

3 6
1 4 3

Sample Output 1

1

There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.

Neither is card 3 for a similar reason, hence the answer is 1.

Sample Input 2

5 400
3 1 4 1 5

Sample Output 2

5

In this case, there is no good set. Therefore, all the cards are unnecessary.

Sample Input 3

6 20
10 4 3 10 25 2

Sample Output 3

3


program j01;
var a,sum:array[0..100086]of int64;
    n,i:longint;

procedure qsort(l,r:longint);
var i,j,x,y:int64;
begin
  i:=l;j:=r;x:=a[(i+j)div 2];
  repeat
    while a[i]<x do inc(i);
    while x<a[j] do dec(j);
    if i<=j then
    begin
      y:=a[i];a[i]:=a[j];a[j]:=y;
      inc(i);dec(j);
    end;
  until i>j;
  if i<r then qsort(i,r);
  if l<j then qsort(l,j);
end;

begin
  readln(n);
  for i:=1 to n do read(a[i]);
  qsort(1,n);
  for i:=1 to n do sum[i]:=a[i]+sum[i-1];
  for i:=n-1 downto 1 do
    if sum[i]*2<a[i+1] then break;
  if (i=n-1)and(sum[i]*2>=a[n]) then writeln(n) else writeln(n-i);
end.

Score : 400 points

Problem Statement

Snuke found N strange creatures.
Each creature has a fixed color and size. The color and size of the i-th creature are represented by i and A_i, respectively.

Every creature can absorb another creature whose size is at most twice the size of itself.
When a creature of size A and color B absorbs another creature of size C and color D (C \leq 2 \times A), they will merge into one creature of size A+C and color B.
Here, depending on the sizes of two creatures, it is possible that both of them can absorb the other.

Snuke has been watching these creatures merge over and over and ultimately become one creature.
Find the number of the possible colors of this creature.

Constraints

2 \leq N \leq 100000

1 \leq A_i \leq 10^9

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

Print the number of the possible colors of the last remaining creature after the N creatures repeatedly merge and ultimately become one creature.

Sample Input 1

3
3 1 4

Sample Output 1

2

The possible colors of the last remaining creature are colors 1 and 3.
For example, when the creature of color 3 absorbs the creature of color 2, then the creature of color 1 absorbs the creature of color 3, the color of the last remaining creature will be color 1.

Sample Input 2

5
1 1 1 1 1

Sample Output 2

5

There may be multiple creatures of the same size.

Sample Input 3

6
40 1 30 2 7 20

Sample Output 3

4


var
 n,i,s:longint;
begin
 readln(n);
 s:=1;
 for i:=1 to n do
  begin
   s:=s*i;
   s:=s mod 1000000007;
  end;
 write(s);
end.

Score : 200 points

Problem Statement

Snuke loves working out. He is now exercising N times.

Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.

Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7.

Constraints

1 ≤ N ≤ 10^{5}

Input

The input is given from Standard Input in the following format:

N

Output

Print the answer modulo 10^{9}+7.

Sample Input 1

3

Sample Output 1

6

After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.

After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.

After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6.

Sample Input 2

10

Sample Output 2

3628800

Sample Input 3

100000

Sample Output 3

457992974

Print the answer modulo 10^{9}+7.


var
	n,i:Longint;
	s:String;
	a:array[0..99999]of Boolean;
function f(x,y:Boolean):Boolean;
var j:Longint;flag:Boolean;
begin
	a[0]:=x;
	a[1]:=y;
	for j:=1 to n-2 do if(s[j+1]='x')xor a[j]then a[j+1]:=a[j-1]else a[j+1]:=not a[j-1];
	flag:=true;
	if(s[1]='x')xor a[0]then flag:=flag and(a[n-1]=a[1])else flag:=flag and(a[n-1]<>a[1]);
	if(s[n]='x')xor a[n-1]then flag:=flag and(a[n-2]=a[0])else flag:=flag and(a[n-2]<>a[0]);
	f:=flag;
end;
procedure print();
var j:Longint;
begin
	for j:=0 to n-1 do if a[j] then write('S')else write('W');
	writeln;
end;
begin
	readln(n);
	readln(s);
	if f(true,true)then print()else if f(true,false)then print()else if f(false,true)then print()else if f(false,false)then print()else writeln(-1);
end.

Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1.

There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.

Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i. Here, if s_i is o, the animal said that the two neighboring animals are of the same species, and if s_i is x, the animal said that the two neighboring animals are of different species.

More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.

Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1.

Constraints

3 ≤ N ≤ 10^{5}

s is a string of length N consisting of o and x.

Input

The input is given from Standard Input in the following format:

N
s

Output

If there does not exist an valid assignment that is consistent with s, print -1.
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.

t is a string of length N consisting of S and W.

If t_i is S, it indicates that the animal numbered i is a sheep. If t_i is W, it indicates that the animal numbered i is a wolf.

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.

Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x. If the neiboring animals are of different species, a sheep answers x and a wolf answers o.

Sample Input 2

3
oox

Sample Output 2

-1

Print -1 if there is no valid assignment of species.

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS


program project1;
type la=record
      sum,dist,le:longint;
     end;
var a:array[1..50000,0..1] of longint;
    layer:array[1..50000] of la;
    ans:array[1..50000]of longint;
    n,i,t,m,k,sum,min:longint;

procedure qs(l,r:longint);//假设被排序的数组是a,且快排后按升序排列）
var i,j,m,t,c:longint;
begin
  i:=l;
  j:=r;//(l（left）,r（right）表示快排的左右区间)
  m:=a[(l+r) div 2,1];//注意：本句不能写成：m:=(l+r)div2;
  repeat
  while a[i,1]>m do inc(i);
  while a[j,1]<m do dec(j);//若是降序把'<'与‘>'互换;
  if i<=j then
    begin
    t:=a[i,1];
    a[i,1]:=a[j,1];
    a[j,1]:=t;
    c:=a[i,0];
    a[i,0]:= a[j,0];
    a[j,0]:= c;
    inc(i);
    dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);//递归查找左区间
  if i<r then qs(i,r);//递归查找右区间
end;

begin
   readln(n);
   for i:=1 to n do
   read(a[i,1]);
   for i:=1 to n do
   a[i,0]:=i;
   qs(1,n);
   t:=a[1,1];;
   m:=1;
   sum:=1;
   min:=a[1,0];
   for i:=2 to n do
   if a[i,1]=t then
   begin
     inc(sum);
     if a[i,0]<min  then
     min:=a[i,0];
   end
   else begin
     layer[m].dist:=t-a[i,1];
     layer[m].sum:=sum;
     inc(m);
     inc(sum);
     t:=a[i,1];
     layer[m-1].le:=min;
     if a[i,0]<min then min:=a[i,0];
   end;

   layer[m].dist:=t;
   layer[m].sum:=sum;
   layer[m].le:=min;
   for i:=1 to m do
   ans[layer[i].le]:=ans[layer[i].le]+layer[i].dist*layer[i].sum;
   for i:=1 to n do
   writeln(ans[i]);
end.

Score : 700 points

Problem Statement

Snuke loves constructing integer sequences.

There are N piles of stones, numbered 1 through N.
The pile numbered i consists of a_i stones.

Snuke will construct an integer sequence s of length Σa_i, as follows:

Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.

Select a pile with one or more stones remaining, and remove a stone from that pile.

If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.

We are interested in the lexicographically smallest sequence that can be constructed. For each of the integers 1,2,3,...,N, how many times does it occur in the lexicographically smallest sequence?

Constraints

1 ≤ N ≤ 10^{5}

1 ≤ a_i ≤ 10^{9}

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print N lines. The i-th line should contain the number of the occurrences of the integer i in the lexicographically smallest sequence that can be constructed.

Sample Input 1

2
1 2

Sample Output 1

2
1

The lexicographically smallest sequence is constructed as follows:

Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.

Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.

Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.

The resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2 occurs once.

Sample Input 2

10
1 2 1 3 2 4 2 5 8 1

Sample Output 2

10
7
0
4
0
3
0
2
3
0


program j01;
type xx=record id:longint;w:int64; end;
var a:array[0..100086]of xx;
    n,i,last,pre:longint;
    sum:int64;
    ans:array[0..100086]of int64;

function cmp(a,b:xx):boolean;
begin
  if a.w<>b.w then exit(a.w>b.w) else exit(a.id>b.id);
end;

procedure sort(l,r:longint);
var i,j:longint;x,y:xx;
begin
  i:=l;j:=r;x:=a[(i+j)div 2];
  repeat
    while cmp(a[i],x) do inc(i);
    while cmp(x,a[j]) do dec(j);
    if i<=j then
    begin
      y:=a[i];a[i]:=a[j];a[j]:=y;
      inc(i);dec(j);
    end;
  until i>j;
  if i<r then sort(i,r);
  if l<j then sort(l,j);
end;

begin
  readln(n);
  for i:=1 to n do
  begin
    read(a[i].w);a[i].id:=i;
  end;
  sort(1,n);sum:=0;pre:=n+1;
  for i:=1 to n do
  begin
    if a[i].id<pre then
    begin
      ans[pre]:=sum-a[i].w*(i-1);
      sum:=a[i].w*(i-1);pre:=a[i].id;
    end;
    inc(sum,a[i].w);
  end;
  inc(ans[a[pre].id],sum);
  for i:=1 to n do writeln(ans[i]);
end.


program j01;
uses math;
var a:array[0..100000]of longint;
    n,x,i:longint;
begin
  readln(n);
  for i:=1 to n do
  begin
    read(x);inc(a[min(n,x)]);
  end;
  for i:=n-1 downto 1 do
    inc(a[i],a[i+1]);
  for i:=1 to n do writeln(a[i]);
end.


var m,n,i,j,tx,ty:longint;
    flagg,ans:boolean;
	c1,c2:array[0..57,0..57] of char;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to n do
		read(c1[i,j]);
	readln;
  end;
  for i:=1 to m do
  begin
    for j:=1 to m do
		read(c2[i,j]);
	readln;
  end;
  ans:=false;
  for tx:=0 to n-m do
	for ty:=0 to n-m do
	begin
	  flagg:=true;
	  for i:=1 to m do
		for j:=1 to m do
		  if (c2[i,j]<>c1[i+tx,j+ty]) then flagg:=false;
	  if flagg then ans:=true;
	end;
  if ans then writeln('Yes') else writeln('No');
end.

Score : 200 points

Problem Statement

You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.

A pixel is the smallest element of an image, and in this problem it is a square of size 1×1.

Also, the given images are binary images, and the color of each pixel is either white or black.

In the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.

The image A is given as N strings A_1,...,A_N.

The j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1≦i,j≦N).

Similarly, the template image B is given as M strings B_1,...,B_M.

The j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1≦i,j≦M).

Determine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.

Constraints

1≦M≦N≦50

A_i is a string of length N consisting of # and ..

B_i is a string of length M consisting of # and ..

Input

The input is given from Standard Input in the following format:

N M
A_1
A_2
:
A_N
B_1
B_2
:
B_M

Output

Print Yes if the template image B is contained in the image A. Print No otherwise.

Sample Input 1

3 2
#.#
.#.
#.#
#.
.#

Sample Output 1

Yes

The template image B is identical to the upper-left 2 × 2 subimage and the lower-right 2 × 2 subimage of A. Thus, the output should be Yes.

Sample Input 2

4 1
....
....
....
....
#

Sample Output 2

No

The template image B, composed of a black pixel, is not contained in the image A composed of white pixels.


var
	n,m,i,a,b:Longint;
	used:array[1..8]of Boolean;
	c:array[1..8]of Longint;
	G:array[1..8,1..8]of Longint;
function f(u,cnt:Longint):Longint;
var i,ret:Longint;
begin
	if cnt=n then begin
		f:=1;
		exit;
	end;
	ret:=0;
	for i:=1 to c[u] do begin
		if not used[G[u,i]]then begin
			used[G[u,i]]:=True;
			inc(ret,f(G[u,i],cnt+1));
			used[G[u,i]]:=False;
		end;
	end;
	f:=ret;
end;
begin
	read(n,m);
	for i:=1 to m do begin
		read(a,b);
		if b<>1 then begin
			inc(c[a]);
			G[a,c[a]]:=b;
		end;
		if a<>1 then begin
			inc(c[b]);
			G[b,c[b]]:=a;
		end;
	end;
	used[1]:=True;
	writeln(f(1,1));
end.

Score : 300 points

Problem Statement

You are given an undirected unweighted graph with N vertices and M edges that contains neither self-loops nor double edges.

Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).

How many different paths start from vertex 1 and visit all the vertices exactly once?

Here, the endpoints of a path are considered visited.

For example, let us assume that the following undirected graph shown in Figure 1 is given.

Figure 1: an example of an undirected graph

The following path shown in Figure 2 satisfies the condition.

Figure 2: an example of a path that satisfies the condition

However, the following path shown in Figure 3 does not satisfy the condition, because it does not visit all the vertices.

Figure 3: an example of a path that does not satisfy the condition

Neither the following path shown in Figure 4, because it does not start from vertex 1.

Figure 4: another example of a path that does not satisfy the condition

Constraints

2≦N≦8

0≦M≦N(N-1)/2

1≦a_i<b_i≦N

The given graph contains neither self-loops nor double edges.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of the different paths that start from vertex 1 and visit all the vertices exactly once.

Sample Input 1

3 3
1 2
1 3
2 3

Sample Output 1

2

The given graph is shown in the following figure:

The following two paths satisfy the condition:

Sample Input 2

7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7

Sample Output 2

1

This test case is the same as the one described in the problem statement.


var
	n,m,i,a,b:Longint;
	used:array[1..8]of Boolean;
	c:array[1..8]of Longint;
	G:array[1..8,1..7]of Longint;
function f(u,cnt:Longint):Longint;
var i,ret:Longint;
begin
	if cnt=n then begin
		f:=1;
		exit;
	end;
	ret:=0;
	for i:=1 to c[u] do begin
		if not used[G[u,i]]then begin
			used[G[u,i]]:=True;
			inc(ret,f(G[u,i],cnt+1));
			used[G[u,i]]:=False;
		end;
	end;
	f:=ret;
end;
begin
	read(n,m);
	for i:=1 to m do begin
		read(a,b);
		inc(c[a]);
		inc(c[b]);
		G[a,c[a]]:=b;
		G[b,c[b]]:=a;
	end;
	used[1]:=True;
	writeln(f(1,1));
end.


var
	n,m,i,a,b,ans:Longint;
	used:array[1..8]of Boolean;
	c:array[1..8]of Longint;
	G:array[1..8,1..8]of Longint;
procedure f(u,cnt:Longint);
var i:Longint;
begin
	if cnt=n then begin
		inc(ans);
		exit;
	end;
	for i:=1 to c[u] do begin
		if not used[G[u,i]]then begin
			used[G[u,i]]:=True;
			f(G[u,i],cnt+1);
			used[G[u,i]]:=False;
		end;
	end;
end;
begin
	read(n,m);
	ans:=0;
	for i:=1 to m do begin
		read(a,b);
		inc(c[a]);
		inc(c[b]);
		G[a,c[a]]:=b;
		G[b,c[b]]:=a;
	end;
	used[1]:=True;
	f(1,1);
	writeln(ans);
end.


var ans,n,m,i,j,tot,u,v:longint;
    last,pre,other:array[0..100007] of longint;
	vis:array[0..100007] of boolean;
procedure insert(u,v:longint);
begin
  inc(tot);
  other[tot]:=v;
  pre[tot]:=last[u];
  last[u]:=tot;
end;
procedure dfs(x,u:longint);
var q,v:longint;
begin
  if (x=n) then inc(ans) else
  begin
    q:=last[u];
    while (q>0) do
	begin
	  v:=other[q];
	  if not(vis[v]) then
	  begin
	    vis[v]:=true;
	    dfs(x+1,v);
	    vis[v]:=false;
	  end;
	  q:=pre[q];
	end;
  end;
end;
begin
  readln(n,m);
  tot:=0;
  fillchar(last,sizeof(last),0);
  fillchar(pre,sizeof(pre),0);
  fillchar(other,sizeof(other),0);
  for i:=1 to m do
  begin
    readln(u,v);
    insert(u,v);
    insert(v,u);
  end;
  ans:=0;
  fillchar(vis,sizeof(vis),false);
  vis[1]:=true;
  dfs(1,1);
  writeln(ans);
end.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..200]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 200 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			mp:=min(mp+x,200);
			for j:=mp-x downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
		end else if x<0 then begin
			mm:=min(mm-x,200);
			for j:=mm+x downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 100 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.

Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Constraints

1≦N≦40

1≦a_i,b_i≦10

1≦c_i≦100

1≦M_a,M_b≦10

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

The input is given from Standard Input in the following format:

N M_a M_b
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..200]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 200 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			mp:=min(mp+x,200);
			for j:=mp-x downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
		end else if x<0 then begin
			mm:=min(mm-x,200);
			for j:=mm+x downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 200 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..220]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 200 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			for j:=mp downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
			mp:=min(mp+x,200);
		end else if x<0 then begin
			for j:=mm downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
			mm:=min(mm-x,200);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 200 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..300]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 200 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			for j:=mp downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
			mp:=min(mp+x,200);
		end else if x<0 then begin
			for j:=mm downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
			mm:=min(mm-x,200);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 200 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..200]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 200 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			for j:=mp downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
			mp:=min(mp+x,200);
		end else if x<0 then begin
			for j:=mm downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
			mm:=min(mm-x,200);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 200 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.


var a,b,c:array[0..50] of integer;
    dp:array[0..50, 0..500, 0..500] of integer;
    n,ma,mb,i,ca,cb,ans:integer;

Function min(a,b:integer):integer;
    Begin
        if a < b then exit(a)
        else exit(b);
    End;

BEGIN
    readln(n,ma,mb);
    for i:=0 to n-1 do
        readln(a[i],b[i],c[i]);

    for i:=0 to n do
        for ca:=0 to 400 do
            for cb:=0 to 400 do
                dp[i,ca,cb] := maxint;

    dp[0,0,0] := 0;
    for i:=0 to n-1 do
        for ca:=0 to 400 do
            for cb:=0 to 400 do
                if dp[i,ca,cb] <> maxint then
                    Begin
                        dp[i+1, ca, cb] := min(dp[i,ca,cb], dp[i+1, ca, cb]);
                        dp[i+1, ca+a[i], cb+b[i]] := dp[i,ca,cb] + c[i];
                    End;

    ans := maxint;
    for ca:=1 to 400 do
        for cb:=1 to 400 do
            if ma * cb = mb * ca then
                ans := min(ans, dp[n,ca,cb]);

    if ans = maxint then writeln(-1)
    else writeln(ans);
END.


uses math;
var n,x,y,i,j:longint;
    a,b,c:array[0..107] of longint;
	f:array[0..57,-10007..10007] of longint;
begin
  readln(n,x,y);
  fillchar(a,sizeof(a),0);
  fillchar(b,sizeof(b),0);
  fillchar(c,sizeof(c),0);
  for i:=1 to n do
  begin
    readln(a[i],b[i],c[i]);
    a[i]:=a[i]*y;
    b[i]:=b[i]*x;
  end;
  for i:=0 to n do
	for j:=-10000 to 10000 do
	  f[i,j]:=maxlongint div 2;
  for i:=1 to n do
    f[i,a[i]-b[i]]:=c[i];
  for i:=2 to n do
	for j:=-10000 to 10000 do
	  f[i,j]:=min(f[i,j],min(f[i-1,j],f[i-1,j-a[i]+b[i]]+c[i]));
  if (f[n,0]<maxlongint div 2) then writeln(f[n,0]) else writeln(-1);
end.


var
  n,i,x:longint;
  s:int64;
begin
  readln(n);
  for i:=1 to n do
    begin
      readln(x);
      s:=s+x;
    end;
  if odd(s) then writeln('NO') else writeln('YES');
end.

Score : 300 points

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i.

Takahashi will repeatedly perform the following operation on these numbers:

Select a pair of integers, A_i and A_j, that have the same parity (that is, both are even or both are odd) and erase them.

Then, write a new integer on the blackboard that is equal to the sum of those integers, A_i+A_j.

Determine whether it is possible to have only one integer on the blackboard.

Constraints

2 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

If it is possible to have only one integer on the blackboard, print YES. Otherwise, print NO.

Sample Input 1

3
1 2 3

Sample Output 1

YES

It is possible to have only one integer on the blackboard, as follows:

Erase 1 and 3 from the blackboard, then write 4. Now, there are two integers on the blackboard: 2 and 4.

Erase 2 and 4 from the blackboard, then write 6. Now, there is only one integer on the blackboard: 6.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

NO


var
  n,i,x,s:longint;
begin
  readln(n);
  for i:=1 to n do
    begin
      readln(x);
      s:=(s+x mod 2) mod 2;
    end;
  if s=1 then writeln('NO') else writeln('YES');
end.


var
  i,j,n,a:integer;
begin
  read(n);
  i:=0;
  for j:=1 to n do
  begin
    read(a);
    if a mod 2 = 1 then i:=i+1;
  end;
  if i mod 2 = 0 then writeln('YES') else  writeln('NO');
end.


var x:longint;
begin
  readln(x);
  if (x<1200) then writeln('ABC') else writeln('ARC');
end.

Score : 100 points

Problem Statement

Smeke has decided to participate in AtCoder Beginner Contest (ABC) if his current rating is less than 1200, and participate in AtCoder Regular Contest (ARC) otherwise.

You are given Smeke's current rating, x. Print ABC if Smeke will participate in ABC, and print ARC otherwise.

Constraints

1 ≦ x ≦ 3{,}000

x is an integer.

Input

The input is given from Standard Input in the following format:

x

Output

Print the answer.

Sample Input 1

1000

Sample Output 1

ABC

Smeke's current rating is less than 1200, thus the output should be ABC.

Sample Input 2

2000

Sample Output 2

ARC

Smeke's current rating is not less than 1200, thus the output should be ARC.


var
  n,i,j,s:longint;
  a:array[0..100000] of longint;
 procedure kp(l,r:longint);
  var
    i,j,t,mid:longint;
  begin
    i:=l; j:=r; mid:=a[(i+j) div 2];
    repeat
      while a[i]<mid do inc(i);
      while a[j]>mid do dec(j);
      if i<=j then
        begin
          t:=a[i]; a[i]:=a[j]; a[j]:=t;
          inc(i); dec(j);
        end;
    until i>j;
    if i<r then kp(i,r);
    if j>l then kp(l,j);
  end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  kp(1,n);
  for i:=2 to n do if a[i]=a[i-1] then inc(s);
  if odd(s) then inc(s);
  writeln(n-s);
end.

Score : 400 points

Problem Statement

Snuke has decided to play a game using cards.
He has a deck consisting of N cards. On the i-th card from the top, an integer A_i is written.

He will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, N is odd, which guarantees that at least one card can be kept.

Operation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.

Constraints

3 ≦ N ≦ 10^{5}

N is odd.

1 ≦ A_i ≦ 10^{5}

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_{N}

Output

Print the answer.

Sample Input 1

5
1 2 1 3 7

Sample Output 1

3

One optimal solution is to perform the operation once, taking out two cards with 1 and one card with 2. One card with 1 and another with 2 will be eaten, and the remaining card with 1 will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: 1, 3 and 7.

Sample Input 2

15
1 3 5 2 1 3 2 8 8 6 2 6 11 1 1

Sample Output 2

7


var
  n,i,j,s:longint;
  a:array[0..100000] of longint;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  for i:=1 to n-1 do
    for j:=i+1 to n do
      if a[i]>a[j] then
        begin
          a[0]:=a[i];a[i]:=a[j];a[j]:=a[0];
        end;
  for i:=2 to n do if a[i]=a[i-1] then inc(s);
  if odd(s) then inc(s);
  writeln(n-s);
end.


var e:array[1..100000] of boolean;
    ans,i,a,n:longint;

begin
  readln(n);
  for i:=1 to n do begin
    read(a);
    e[a]:=true
  end;
  ans:=0;
  for i:=1 to 100000 do if e[i] then ans:=ans+1;
  if ans mod 2 = 0 then ans:=ans-1;
  writeln(ans)
end.


var n,i,j,tot,ans:longint;
    a:array[0..100007] of longint;
procedure qsort(lx,rx:longint);
var i,j,m,t:longint;
begin
  i:=lx;j:=rx;m:=a[random(rx-lx+1)+lx];
  repeat
	while (a[i]<m) do inc(i);
	while (a[j]>m) do dec(j);
	if (i<=j) then
	begin
	  t:=a[i];a[i]:=a[j];a[j]:=t;
	  inc(i);dec(j);
	end;
  until (i>j);
  if (i<rx) then qsort(i,rx);
  if (j>lx) then qsort(lx,j);
end;
begin
  randomize;
  readln(n);
  fillchar(a,sizeof(a),0);
  for i:=1 to n do
	read(a[i]);
  readln;
  qsort(1,n);
  tot:=0;
  for i:=2 to n do
	if (a[i]=a[i-1]) then inc(tot);
  ans:=n-tot;
  tot:=tot mod 2;
  if (tot=1) then dec(ans);
  writeln(ans);
end.


var n,i,j:longint;
    ans,now:int64;
	a,b:array[0..100007] of longint;
begin
  readln(n);
  for i:=1 to n do
	readln(a[i],b[i]);
  ans:=0;
  for i:=n downto 1 do
  begin
    now:=(b[i]-(a[i]+ans) mod b[i]) mod b[i];
    ans:=ans+now;
  end;
  writeln(ans);
end.

Score : 300 points

Problem Statement

There are an integer sequence A_1,...,A_N consisting of N terms, and N buttons.
When the i-th (1 ≦ i ≦ N) button is pressed, the values of the i terms from the first through the i-th are all incremented by 1.

There is also another integer sequence B_1,...,B_N. Takahashi will push the buttons some number of times so that for every i, A_i will be a multiple of B_i.

Find the minimum number of times Takahashi will press the buttons.

Constraints

All input values are integers.

1 ≦ N ≦ 10^5

0 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)

1 ≦ B_i ≦ 10^9(1 ≦ i ≦ N)

Input

The input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print an integer representing the minimum number of times Takahashi will press the buttons.

Sample Input 1

3
3 5
2 7
9 4

Sample Output 1

7

Press the first button twice, the second button twice and the third button three times.

Sample Input 2

7
3 1
4 1
5 9
2 6
5 3
5 8
9 7

Sample Output 2

22


var n,i,tot:longint;
	last,pre,other,anss,a,now:array[0..500007] of longint;
procedure insert(u,v:longint);
begin
  inc(tot);
  other[tot]:=v;
  pre[tot]:=last[u];
  last[u]:=tot;
end;
procedure qsort(lx,rx:longint);
var i,j,m,t:longint;
begin
  i:=lx;j:=rx;m:=now[lx+random(rx-lx+1)];
  repeat
	while (now[i]<m) do inc(i);
	while (now[j]>m) do dec(j);
	if (i<=j) then
	begin
	  t:=now[i];now[i]:=now[j];now[j]:=t;
	  inc(i);dec(j);
	end;
  until (i>j);
  if (i<rx) then qsort(i,rx);
  if (j>lx) then qsort(lx,j);
end;
procedure dfs(u,father:longint);
var q,v,i,tott:longint;
    noww:array[0..500007] of longint;
begin
  tott:=0;
  q:=last[u];
  while (q>0) do
  begin
    v:=other[q];
    if (v<>father) then
	begin
	  dfs(v,u);
	  inc(tott);
	  noww[tott]:=anss[v];
	end;
	q:=pre[q];
  end;
if (tott<>0) then
begin
  for i:=1 to tott do
	now[i]:=noww[i];
  qsort(1,tott);
  anss[u]:=0;
  for i:=1 to tott do
	if (now[i]+tott+1-i>anss[u]) then anss[u]:=now[i]+tott+1-i;
end else anss[u]:=0;
end;
begin
  randomize;
  readln(n);
  fillchar(a,sizeof(a),0);
  tot:=0;
  for i:=2 to n do
  begin
	readln(a[i]);
    insert(i,a[i]);insert(a[i],i);
  end;
  fillchar(anss,sizeof(anss),0);
  dfs(1,0);
  writeln(anss[1]);
end.

Score : 800 points

Problem Statement

N contestants participated in a competition. The total of N-1 matches were played in a knockout tournament.
For some reasons, the tournament may not be "fair" for all the contestants.
That is, the number of the matches that must be played in order to win the championship may be different for each contestant. The structure of the tournament is formally described at the end of this statement.

After each match, there were always one winner and one loser. The last contestant standing was declared the champion.

Figure: an example of a tournament

For convenience, the contestants were numbered 1 through N. The contestant numbered 1 was the champion, and the contestant numbered i(2 ≦ i ≦ N) was defeated in a match against the contestant numbered a_i.

We will define the depth of the tournament as the maximum number of the matches that must be played in order to win the championship over all the contestants.

Find the minimum possible depth of the tournament.

The formal description of the structure of the tournament is as follows. In the i-th match, one of the following played against each other:

Two predetermined contestants

One predetermined contestant and the winner of the j-th match, where j(j<i) was predetermined

The winner of the j-th match and the winner of the k-th match, where j and k (j,k<i, j ≠ k) were predetermined

Such structure is valid structure of the tournament, if and only if no contestant who has already been defeated in a match will never play in a match, regardless of the outcome of the matches.

Constraints

2 ≦ N ≦ 10^5

1 ≦ a_i ≦ N(2 ≦ i ≦ N)

It is guaranteed that the input is consistent (that is, there exists at least one tournament that matches the given information).

Input

The input is given from Standard Input in the following format:

N
a_2
:
a_N

Output

Print the minimum possible depth of the tournament.

Sample Input 1

5
1
1
2
4

Sample Output 1

3

The following tournament and the result of the matches are consistent with the given information:

In the first match, contestants 4 and 5 played against each other, and contestant 4 won.

In the second match, contestants 2 and 4 played against each other, and contestant 2 won.

In the third match, contestants 1 and 3 played against each other, and contestant 1 won.

In the fourth match, contestants 1 and 2 played against each other, and contestant 1 won.

The depth of this tournament is 3, since contestant 5 must play three matches in order to win the championship.
There is no tournament with depth 2 or less that is consistent with the given information, thus the output should be 3.

Sample Input 2

7
1
2
1
3
1
4

Sample Output 2

3

Sample Input 3

4
4
4
1

Sample Output 3

3


var n,i,tot:longint;
	last,pre,other,anss,a,t,now:array[0..300007] of longint;
procedure insert(u,v:longint);
begin
  inc(tot);
  other[tot]:=v;
  pre[tot]:=last[u];
  last[u]:=tot;
end;
procedure qsort(lx,rx:longint);
var i,j,m,t:longint;
begin
  i:=lx;j:=rx;m:=now[lx+random(rx-lx+1)];
  repeat
	while (now[i]<m) do inc(i);
	while (now[j]>m) do dec(j);
	if (i<=j) then
	begin
	  t:=now[i];now[i]:=now[j];now[j]:=t;
	  inc(i);dec(j);
	end;
  until (i>j);
  if (i<rx) then qsort(i,rx);
  if (j>lx) then qsort(lx,j);
end;
procedure dfs(u,father:longint);
var q,v,i,tott:longint;
    noww:array[0..300007] of longint;
begin
  tott:=0;
  q:=last[u];
  while (q>0) do
  begin
    v:=other[q];
    if (v<>father) then
	begin
	  dfs(v,u);
	  inc(tott);
	  noww[tott]:=anss[v];
	end;
	q:=pre[q];
  end;
if (tott<>0) then
begin
  for i:=1 to tott do
	now[i]:=noww[i];
  qsort(1,tott);
  anss[u]:=0;
  for i:=1 to tott do
	if (now[i]+tott+1-i>anss[u]) then anss[u]:=now[i]+tott+1-i;
end else anss[u]:=0;
end;
begin
  randomize;
  readln(n);
  fillchar(a,sizeof(a),0);
  fillchar(t,sizeof(t),0);
  tot:=0;
  for i:=2 to n do
  begin
	readln(a[i]);
    t[a[i]]:=t[a[i]]+1;
    insert(i,a[i]);insert(a[i],i);
  end;
  fillchar(anss,sizeof(anss),0);
  dfs(1,0);
  writeln(anss[1]);
end.


var n,i,tot,ans:longint;
    a,t,f:array[0..100007] of longint;
	last,pre,other,depth:array[0..300007] of longint;
procedure insert(u,v:longint);
begin
  inc(tot);
  other[tot]:=v;
  pre[tot]:=last[u];
  last[u]:=tot;
end;
procedure dfs(u,father:longint);
var q,v:longint;
begin
  depth[u]:=depth[father]+1;
  if (depth[u]+t[u]>ans) then ans:=depth[u]+t[u];
  q:=last[u];
  while (q>0) do
  begin
    v:=other[q];
    if (v<>father) then dfs(v,u);
	q:=pre[q];
  end;
end;
begin
  readln(n);
  fillchar(a,sizeof(a),0);
  fillchar(t,sizeof(t),0);
  tot:=0;
  for i:=2 to n do
  begin
	readln(a[i]);
    t[a[i]]:=t[a[i]]+1;
    inc(tot);
    insert(i,a[i]);insert(a[i],i);
  end;
  ans:=0;
  dfs(1,0);
  writeln(ans-1);
end.


uses math;
var i,a,t,m:longint;
    c:ansistring;
begin
  readln(a);
  readln(c);
  for i:=1 to a do
  begin
    if c[i]='I' then
      inc(t)
    else
      dec(t);
    m:=max(m,t);
  end;
  writeln(m);
end.

Score : 200 points

Problem Statement

You have an integer variable x.
Initially, x=0.

Some person gave you a string S of length N, and using the string you performed the following operation N times.
In the i-th operation, you incremented the value of x by 1 if S_i=I, and decremented the value of x by 1 if S_i=D.

Find the maximum value taken by x during the operations (including before the first operation, and after the last operation).

Constraints

1≤N≤100

|S|=N

No characters except I and D occur in S.

Input

The input is given from Standard Input in the following format:

N
S

Output

Print the maximum value taken by x during the operations.

Sample Input 1

5
IIDID

Sample Output 1

2

After each operation, the value of x becomes 1, 2, 1, 2 and 1, respectively. Thus, the output should be 2, the maximum value.

Sample Input 2

7
DDIDDII

Sample Output 2

0

The initial value x=0 is the maximum value taken by x, thus the output should be 0.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  n,i,x,maxx:Integer;
  s:string;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(n);
    Readln(s);

    x:=0;
    for i := 1 to n do begin
      if s[i]='I' then begin
        Inc(x);
        maxx:=(maxx+x+abs(maxx-x))div 2;
      end else
        x:=x-1;
    end;

    Writeln(maxx);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


program ec12;
var
	n,m,i,j,k,step,tot:longint;
	ans:int64;
	vis:array[1..1000] of boolean;
	prime,sum:array[1..500] of longint;
begin
	fillchar(sum,sizeof(sum),0);
	fillchar(vis,sizeof(vis),true);
	readln(n);
	tot:=0;
	for i:=2 to n do
	begin
		if vis[i] then
		begin
			inc(tot);
			prime[tot]:=i;
		end;
		for j:=1 to tot do
		begin
			if prime[j]*i>1000 then
			break;
			vis[prime[j]*i]:=false;
			if i mod prime[j]=0 then
			break;
		end;
	end;
	for i:=2 to n do
	begin
		if vis[i] then
		begin
			for j:=1 to tot do
			begin
				if prime[j]=i then
				break;
			end;
			if prime[j]=i then
			inc(sum[j]);
		end
		else
		begin
			step:=1;
			k:=i;
			while k>=prime[step] do
			begin
				if k mod prime[step]=0 then
				begin
					inc(sum[step]);
					k:=k div prime[step];
				end
				else
				inc(step);
			end;
		end;
	end;
	ans:=1;
	for i:=1 to tot do
	begin
		if sum[i]>0 then
		ans:=(ans*(sum[i]+1)) mod 1000000007;
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

You are given an integer N.
Find the number of the positive divisors of N!, modulo 10^9+7.

Constraints

1≤N≤10^3

Input

The input is given from Standard Input in the following format:

N

Output

Print the number of the positive divisors of N!, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

4

There are four divisors of 3! =6: 1, 2, 3 and 6. Thus, the output should be 4.

Sample Input 2

6

Sample Output 2

30

Sample Input 3

1000

Sample Output 3

972926972


var n,a,b,x,i,p,t,s:integer;
begin
  read(n,a,b,t);
  s:=0;
  for i:=2 to n do begin
    read(x);
	p:=(x-t)*a;
	if b<p then
	  s:=s+b
	else
	  s:=s+p;
	t:=x;
  end;
  writeln(s);
end.

Score : 500 points

Problem Statement

There are N towns on a line running east-west.
The towns are numbered 1 through N, in order from west to east.
Each point on the line has a one-dimensional coordinate, and a point that is farther east has a greater coordinate value.
The coordinate of town i is X_i.

You are now at town 1, and you want to visit all the other towns.
You have two ways to travel:

Walk on the line.
Your fatigue level increases by A each time you travel a distance of 1, regardless of direction.

Teleport to any location of your choice.
Your fatigue level increases by B, regardless of the distance covered.

Find the minimum possible total increase of your fatigue level when you visit all the towns in these two ways.

Constraints

All input values are integers.

2≤N≤10^5

1≤X_i≤10^9

For all i(1≤i≤N-1), X_i<X_{i+1}.

1≤A≤10^9

1≤B≤10^9

Input

The input is given from Standard Input in the following format:

N A B
X_1 X_2 ... X_N

Output

Print the minimum possible total increase of your fatigue level when you visit all the towns.

Sample Input 1

4 2 5
1 2 5 7

Sample Output 1

11

From town 1, walk a distance of 1 to town 2, then teleport to town 3, then walk a distance of 2 to town 4.
The total increase of your fatigue level in this case is 2×1+5+2×2=11, which is the minimum possible value.

Sample Input 2

7 1 100
40 43 45 105 108 115 124

Sample Output 2

84

From town 1, walk all the way to town 7.
The total increase of your fatigue level in this case is 84, which is the minimum possible value.

Sample Input 3

7 1 2
24 35 40 68 72 99 103

Sample Output 3

12

Visit all the towns in any order by teleporting six times.
The total increase of your fatigue level in this case is 12, which is the minimum possible value.


var sx,sy,tx,ty,i:Integer;
begin
	read(sx,sy,tx,ty);
	for i:=sy+1 to ty do write('U');
	for i:=sx+1 to tx do write('R');
	for i:=sy+1 to ty do write('D');
	for i:=sx to tx do write('L');
	for i:=sy to ty do write('U');
	for i:=sx to tx do write('R');
	write('DR');
	for i:=sy to ty do write('D');
	for i:=sx to tx do write('L');
	write('U');
end.

Score : 300 points

Problem Statement

Dolphin resides in two-dimensional Cartesian plane, with the positive x-axis pointing right and the positive y-axis pointing up.

Currently, he is located at the point (sx,sy). In each second, he can move up, down, left or right by a distance of 1.

Here, both the x- and y-coordinates before and after each movement must be integers.

He will first visit the point (tx,ty) where sx < tx and sy < ty, then go back to the point (sx,sy), then visit the point (tx,ty) again, and lastly go back to the point (sx,sy).

Here, during the whole travel, he is not allowed to pass through the same point more than once, except the points (sx,sy) and (tx,ty).

Under this condition, find a shortest path for him.

Constraints

-1000 ≤ sx < tx ≤ 1000

-1000 ≤ sy < ty ≤ 1000

sx,sy,tx and ty are integers.

Input

The input is given from Standard Input in the following format:

sx sy tx ty

Output

Print a string S that represents a shortest path for Dolphin.

The i-th character in S should correspond to his i-th movement.

The directions of the movements should be indicated by the following characters:

U: Up

D: Down

L: Left

R: Right

If there exist multiple shortest paths under the condition, print any of them.

Sample Input 1

0 0 1 2

Sample Output 1

UURDDLLUUURRDRDDDLLU

One possible shortest path is:

Going from (sx,sy) to (tx,ty) for the first time: (0,0) → (0,1) → (0,2) → (1,2)

Going from (tx,ty) to (sx,sy) for the first time: (1,2) → (1,1) → (1,0) → (0,0)

Going from (sx,sy) to (tx,ty) for the second time: (0,0) → (-1,0) → (-1,1) → (-1,2) → (-1,3) → (0,3) → (1,3) → (1,2)

Going from (tx,ty) to (sx,sy) for the second time: (1,2) → (2,2) → (2,1) → (2,0) → (2,-1) → (1,-1) → (0,-1) → (0,0)

Sample Input 2

-2 -2 1 1

Sample Output 2

UURRURRDDDLLDLLULUUURRURRDDDLLDL


var sx,sy,tx,ty,i:Integer;
begin
	read(sx,sy,tx,ty);
	inc(sx);
	inc(sy);
	for i:=sy to ty do write('U');
	for i:=sx to tx do write('R');
	for i:=sy to ty do write('D');
	for i:=sx to tx do write('L');
	write('L');
	for i:=sy to ty do write('U');
	write('UR');
	for i:=sx to tx do write('R');
	write('DR');
	for i:=sy to ty do write('D');
	write('DL');
	for i:=sx to tx do write('L');
	write('U');
end.


var sx,sy,tx,ty,i:Integer;
begin
	read(sx,sy,tx,ty);
	for i:=1 to ty-sy do write('U');
	for i:=1 to tx-sx do write('R');
	for i:=1 to ty-sy do write('D');
	for i:=1 to tx-sx do write('L');
	write('L');
	for i:=1 to ty-sy do write('U');
	write('UR');
	for i:=1 to tx-sx do write('R');
	write('DR');
	for i:=1 to ty-sy do write('D');
	write('DL');
	for i:=1 to tx-sx do write('L');
	write('U');
end.


var sx,sy,tx,ty,i:Integer;
begin
	read(sx,sy,tx,ty);
	dec(tx,sx);
	dec(ty,sy);
	for i:=1 to ty do write('U');
	for i:=1 to tx do write('R');
	for i:=1 to ty do write('D');
	for i:=1 to tx do write('L');
	write('L');
	for i:=1 to ty do write('U');
	write('UR');
	for i:=1 to tx do write('R');
	write('DR');
	for i:=1 to ty do write('D');
	write('DL');
	for i:=1 to tx do write('L');
	write('U');
end.


var
	N,K,i:Longint;
	s,a,b:Array[0..100000]of int64;
	ans,sum:int64;
function my(d:Longint):Longint;
begin
	if d>0 then my:=d else my:=0;
end;
begin
	read(N,K);
	for i:=1 to N do read(s[i]);
	a[1]:=s[1];
	b[1]:=my(s[1]);
	for i:=2 to N do begin
		a[i]:=s[i]+a[i-1];
		b[i]:=my(s[i])+b[i-1];
	end;
	ans:=0;
	for i:=0 to N-K do begin
		sum:=my(a[i+K]-a[i]);
		inc(sum,b[i]-b[0]+b[N]-b[i+K]);
		if ans<sum then ans:=sum;
	end;
	writeln(ans);
end.

Score : 400 points

Problem Statement

There are N squares aligned in a row.
The i-th square from the left contains an integer a_i.

Initially, all the squares are white.
Snuke will perform the following operation some number of times:

Select K consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares are overwritten.

After Snuke finishes performing the operation, the score will be calculated as the sum of the integers contained in the black squares.
Find the maximum possible score.

Constraints

1≤N≤10^5

1≤K≤N

a_i is an integer.

|a_i|≤10^9

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the maximum possible score.

Sample Input 1

5 3
-10 10 -10 10 -10

Sample Output 1

10

Paint the following squares black: the second, third and fourth squares from the left.

Sample Input 2

4 2
10 -10 -10 10

Sample Output 2

20

One possible way to obtain the maximum score is as follows:

Paint the following squares black: the first and second squares from the left.

Paint the following squares black: the third and fourth squares from the left.

Paint the following squares white: the second and third squares from the left.

Sample Input 3

1 1
-10

Sample Output 3

0

Sample Input 4

10 5
5 -4 -5 -8 -4 7 2 -4 0 7

Sample Output 4

17


program task1016;
var
    a,b,c : ansistring;
    x,y,i,t : int64;
    sum : int64;
begin
    read(a);
    sum := 0;
    b := a;
    c := a;
    delete(b,pos(' ',a),length(a)-pos(' ',a)+1);
    val(b,x,i);
    //writeln(b);
    delete(a,1,pos(' ',a)+2);
    val(a,y,t);
    //writeln(a);
    if c[pos(' ',c) + 1] = '+' then
        sum := x + y
    else
        sum := x - y;
    writeln(sum);
end.

Score : 100 points

Problem Statement

Joisino wants to evaluate the formula "A op B".
Here, A and B are integers, and the binary operator op is either + or -.
Your task is to evaluate the formula instead of her.

Constraints

1≦A,B≦10^9

op is either + or -.

Input

The input is given from Standard Input in the following format:

A op B

Output

Evaluate the formula and print the result.

Sample Input 1

1 + 2

Sample Output 1

3

Since 1 + 2 = 3, the output should be 3.

Sample Input 2

5 - 7

Sample Output 2

-2


var
    sum : int64;
    e,f,g,h,l : char;
    i,b,c,x,y : longint;
begin
    sum := 0;
    read(e);
    read(f);
    read(g);
    read(h);
    read(l);
    val(e,b,x);
    val(l,c,y);
    if g = '+' then
        sum := b + c
    else if g = '-' then
        sum := b - c;
    writeln(sum);
end.


var

  A, B : longint;
  s, hi, ss : char;

begin

  readln(A, s, hi, ss, B);

  if hi = '+' then
    writeln(A + B)
  else
    writeln(A - B);

end.


Uses sysutils;
var a,b:longint;
    c:string;
    s:ansistring;
begin
    read(s);
    a:=strtoint(copy(s,1,pos(' ',s)-1));
    c:=copy(s,pos(' ',s)+1,1);
    b:=strtoint(copy(s,pos(' ',s)+3,length(s)-pos(' ',s)-2));
    if c='+' then
        writeln(a+b)
    else
        writeln(a-b);
end.


program  ec12;
var
	n,m,i,j:longint;
	ans:int64;
	a:array[1..100001] of longint;
	vis:array[0..100001] of longint;
begin
	fillchar(vis,sizeof(vis),0);
	readln(n);
	for i:=1 to n do
	begin
		read(a[i]);
		if a[i]>=n then
		begin
			writeln(0);
			halt;
		end;
		inc(vis[a[i]]);
		if (a[i]=0) and (vis[0]>=2) then
		begin
			writeln(0);
			halt;
		end;
	end;
	ans:=1;
	for i:=0 to n-1 do
	begin
		if vis[i]=2 then
		ans:=(ans*2) mod 1000000007
		else
		begin
			if (vis[i]=1) and (i>0) then
			begin
				writeln(0);
				halt;
			end;
		end;
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

There are N people, conveniently numbered 1 through N.
They were standing in a row yesterday, but now they are unsure of the order in which they were standing.
However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person.
According to their reports, the difference above for person i is A_i.

Based on these reports, find the number of the possible orders in which they were standing.
Since it can be extremely large, print the answer modulo 10^9+7.
Note that the reports may be incorrect and thus there may be no consistent order.
In such a case, print 0.

Constraints

1≦N≦10^5

0≦A_i≦N-1

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of the possible orders in which they were standing, modulo 10^9+7.

Sample Input 1

5
2 4 4 0 2

Sample Output 1

4

There are four possible orders, as follows:

2,1,4,5,3

2,5,4,1,3

3,1,4,5,2

3,5,4,1,2

Sample Input 2

7
6 4 0 2 4 0 2

Sample Output 2

0

Any order would be inconsistent with the reports, thus the answer is 0.

Sample Input 3

8
7 5 1 1 7 3 5 3

Sample Output 3

16