const fi='';
      fo='';
      oo=trunc(1e18);
      maxn=trunc(1e5);
var   n:longint;
      sol:int64;
      nheapmin,nheapmax:longint;
      heapmax,heapmin:array[0..2*maxn]of longint;
      a:array[0..3*maxn]of longint;
      l,r:array[0..3*maxn]of int64;
function min(i,j:int64):int64; begin if i<j then exit(i); exit(j); end;
function max(i,j:int64):int64; begin if i>j then exit(i); exit(j); end;
procedure swap(var a,b:Longint);
var temp:longint;
begin
      temp:=a;
      a:=b;
      b:=temp;
end;
procedure upmax(i:longint);
begin
      if (i=1)or(heapmax[i div 2]>=heapmax[i]) then exit
      else begin
                  swap(heapmax[i],heapmax[i div 2]);
                  upmax(i div 2);
           end;
end;
procedure upmin(i:longint);
begin
      if (i=1)or(heapmin[i div 2]<=heapmin[i]) then exit
      else begin
                  swap(heapmin[i div 2],heapmin[i]);
                  upmin(i div 2);
           end;
end;
procedure downmax(i:longint);
var gt:longint;
begin
      gt:=i*2;
      if gt>nheapmax then exit;
      if (gt+1<=nheapmax)and(heapmax[gt]<heapmax[gt+1]) then inc(gt);
      if heapmax[i]>=heapmax[gt] then exit;
      begin
                    swap(heapmax[gt],heapmax[i]);
                    downmax(gt);
      end;
end;
procedure downmin(i:longint);
var gt:longint;
begin
      gt:=i*2;
      if gt>nheapmin then exit;
      if (gt+1<=nheapmin)and(heapmin[gt]>heapmin[gt+1]) then inc(gt);
      if heapmin[i]<=heapmin[gt] then exit;
      begin
                  swap(heapmin[gt],heapmin[i]);
                  downmin(gt);
      end;
end;
procedure pushmin(x:longint);
begin
       inc(nheapmin);
       heapmin[nheapmin]:=x;
       upmin(nheapmin);
end;
procedure pushmax(x:longint);
begin
      inc(nheapmax);
      heapmax[nheapmax]:=x;
      upmax(nheapmax);
end;
procedure popmin(x:longint);
begin
      heapmin[1]:=x;
      downmin(1);
end;
procedure popmax(x:longint);
begin
       heapmax[1]:=x;
       downmax(1);
end;
procedure inp;
var i:longint;
begin
      read(n);
      for i:=1 to 3*n do read(a[i]);
end;
procedure main;
var i,j:longint;
    res:int64;
    nmax,nmin:longint;
begin
      res:=0;
      for i:=1 to n do
      begin
          res:=res+a[i];
          pushmin(a[i]);
      end;
      l[n]:=res;
      for i:=n+1 to 2*n do
      begin
          if a[i]>heapmin[1] then
          begin
             res:=res-heapmin[1]+a[i];
             popmin(a[i]);
          end;
          l[i]:=res;
      end;
      res:=0;
      for i:=3*n downto 2*n+1 do
      begin
          res:=res+a[i];
          pushmax(a[i]);
      end;
      r[2*n+1]:=res;
      for i:=2*n downto n+1 do
      begin
          if a[i]<heapmax[1] then
          begin
             res:=res-heapmax[1]+a[i];
             popmax(a[i]);
          end;
          r[i]:=res;
      end;
      sol:=-oo;
      for i:=n to 2*n do sol:=max(sol,l[i]-r[i+1]);
      write(sol);
end;
begin
      assign(input,fi);
      reset(input);
      assign(output,fo);
      rewrite(output);
      inp;
      main;
      close(output);
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..300000] 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(heapmin[x],heapmin[vt]);
                Downheapmin(vt);
        End;
End;
Procedure Pushmin(x:Longint);
Begin
        Inc(nheapmin);
        heapmin[nheapmin]:=x;
        upheapmin(nheapmin);
End;
Procedure popmin(x:Longint);
Begin
        heapmin[1]:=x;
        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(heapmax[x],heapmax[vt]);
                Downheapmax(vt);
        End;
End;
Procedure Pushmax(x:Longint);
Begin
        Inc(nheapmax);
        heapmax[nheapmax]:=x;
        upheapmax(nheapmin);
End;
Procedure popmax(x:Longint);
Begin
        heapmax[1]:=x;
        downheapmax(1);
End;
Procedure tongmax;
Var     i,j,k:Longint;
Begin
        k:=0;
        For i:=1 to n do
        Begin
                k:=k+A[i];
                Pushmin(i);
        End;
        L1[n]:=k;
        For i:=n+1 to 3*n do
        Begin
                If A[i]>A[heapmin[1]] then Begin L1[i]:=L1[i-1]-A[heapmin[1]]+A[i]; Popmin(i); End
                Else L1[i]:=L1[i-1];
        End;
End;
Procedure tongmin;
Var     i,j,k:Longint;
Begin
        k:=0;
        For i:=3*n downto 2*n+1 do
        Begin
                k:=k+A[i];
                Pushmax(i);
        End;
        L2[2*n+1]:=k;
        For i:=2*n downto 1 do
        Begin
                If A[i]<A[heapmax[1]] then Begin L2[i]:=L2[i+1]-A[heapmax[1]]+A[i]; Popmax(i); End
                Else L2[i]:=L2[i+1];
        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.

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='';//B.inp';
        fo='';//B.out';
var     f:text;
        i,j,n,top,top1:longint;
        kq:int64;
        heap,heap1,a:array[0..5000000] of longint;
        l,g:array[0..5000000] of int64;
(*===================================*)
procedure inp;
Begin
assign(f,fi);
reset(f);
readln(f,n);
for i:=1 to 3*n do read(f,a[i]);
close(f);
End;
(*===================================*)
procedure swap(var a,b:longint);
var y:longint;
Begin
y:=a;
a:=b;
b:=y;
End;
(*===================================*)
procedure upheap(i:longint);
Begin
if (i=1) or (heap[i]<heap[i div 2]) then exit;
swap(heap[i],heap[i div 2]);
upheap(i div 2);
End;
(*===================================*)
procedure downheap(i:longint);
var j:longint;
Begin
j:=i*2;
if j>top then exit;
if (j<top) and (heap[j]<heap[j+1]) then inc(j);
if heap[i]<heap[j] then
 begin
  swap(heap[i],heap[j]);
  downheap(j);
 end;
End;
(*===================================*)
procedure push(x:longint);
Begin
inc(top);
heap[top]:=x;
upheap(top);
End;
(*===================================*)
function pop:longint;
Begin
pop:=heap[1];
heap[1]:=heap[top];
dec(top);
downheap(1);
End;
(*===================================*)
procedure upheap1(i:longint);
Begin
if (i=1) or (heap1[i]>heap1[i div 2]) then exit;
swap(heap1[i],heap1[i div 2]);
upheap1(i div 2);
End;
(*===================================*)
procedure downheap1(i:longint);
var j:longint;
Begin
j:=i*2;
if j>top1 then exit;
if (j<top1) and (heap1[j]>heap1[j+1]) then inc(j);
if heap1[i]>heap1[j] then
 begin
  swap(heap1[i],heap1[j]);
  downheap1(j);
 end;
End;
(*===================================*)
procedure push1(x:longint);
Begin
inc(top1);
heap1[top1]:=x;
upheap1(top1);
End;
(*===================================*)
function pop1:longint;
Begin
pop1:=heap1[1];
heap1[1]:=heap1[top1];
dec(top1);
downheap1(1);
End;
(*===============================*)
procedure trong;
Begin
for i:=1 to n do begin l[i]:=l[i-1]+a[i]; push1(a[i]); end;
for i:=n+1 to 3*n do
  begin
   l[i]:=l[i-1]+a[i];
   push1(a[i]);
   l[i]:=l[i]-pop1;
  end;

for i:=3*n downto 2*n+1 do begin g[i]:=g[i+1]+a[i]; push(a[i]); end;
for i:=2*n downto 1 do
 begin
  g[i]:=g[i+1]+a[i];
  push(a[i]);
  g[i]:=g[i]-pop;
 end;
End;
(*===============================*)
BEGIN
inp;
assign(f,fo);
rewrite(f);
trong;
kq:=-maxlongint*100000;
for i:=n to 2*n do
 if l[i]-g[i+1]>kq then kq:=l[i]-g[i+1];
writeln(f,kq);
close(f);
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..300000] 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(heapmin[x],heapmin[vt]);
                Downheapmin(vt);
        End;
End;
Procedure Pushmin(x:Longint);
Begin
        Inc(nheapmin);
        heapmin[nheapmin]:=x;
        upheapmin(nheapmin);
End;
Procedure popmin(x:Longint);
Begin
        heapmin[1]:=x;
        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(heapmax[x],heapmax[vt]);
                Downheapmax(vt);
        End;
End;
Procedure Pushmax(x:Longint);
Begin
        Inc(nheapmax);
        heapmax[nheapmax]:=x;
        upheapmax(nheapmin);
End;
Procedure popmax(x:Longint);
Begin
        heapmax[1]:=x;
        downheapmax(1);
End;
Procedure tongmax;
Var     i,j,k:Longint;
Begin
        k:=0;
        For i:=1 to n do
        Begin
                k:=k+A[i];
                Pushmin(i);
        End;
        L1[n]:=k;
        For i:=n+1 to 3*n do
        Begin
                If A[i]>A[heapmin[1]] then Begin L1[i]:=L1[i-1]-A[heapmin[1]]+A[i]; Popmin(i); End
                Else L1[i]:=L1[i-1];
        End;
End;
Procedure tongmin;
Var     i,j,k:Longint;
Begin
        k:=0;
        For i:=3*n downto 2*n+1 do
        Begin
                k:=k+A[i];
                Pushmax(i);
        End;
        L2[2*n+1]:=k;
        For i:=2*n downto 1 do
        Begin
                If A[i]<A[heapmax[1]] then Begin L2[i]:=L2[i+1]-A[heapmax[1]]+A[i]; Popmax(i); End
                Else L2[i]:=L2[i+1];
        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.


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
                b[i]:=a[i+2*n];
                res:=res+b[i];
        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:=-100000000000000005;
        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';
var     f:text;
        i,j,n,top,top1:longint;
        kq:int64;
        heap,heap1,a,l,g:array[0..5000000] of longint;
(*===================================*)
procedure inp;
Begin
assign(f,fi);
reset(f);
readln(f,n);
for i:=1 to 3*n do read(f,a[i]);
close(f);
End;
(*===================================*)
procedure swap(var a,b:longint);
var y:longint;
Begin
y:=a;
a:=b;
b:=y;
End;
(*===================================*)
procedure upheap(i:longint);
Begin
if (i=1) or (heap[i]<heap[i div 2]) then exit;
swap(heap[i],heap[i div 2]);
upheap(i div 2);
End;
(*===================================*)
procedure downheap(i:longint);
var j:longint;
Begin
j:=i*2;
if j>top then exit;
if (j<top) and (heap[j]<heap[j+1]) then inc(j);
if heap[i]<heap[j] then
 begin
  swap(heap[i],heap[j]);
  downheap(j);
 end;
End;
(*===================================*)
procedure push(x:longint);
Begin
inc(top);
heap[top]:=x;
upheap(top);
End;
(*===================================*)
function pop:longint;
Begin
pop:=heap[1];
heap[1]:=heap[top];
dec(top);
downheap(1);
End;
(*===================================*)
procedure upheap1(i:longint);
Begin
if (i=1) or (heap1[i]>heap1[i div 2]) then exit;
swap(heap1[i],heap1[i div 2]);
upheap1(i div 2);
End;
(*===================================*)
procedure downheap1(i:longint);
var j:longint;
Begin
j:=i*2;
if j>top1 then exit;
if (j<top1) and (heap1[j]>heap1[j+1]) then inc(j);
if heap1[i]>heap1[j] then
 begin
  swap(heap1[i],heap1[j]);
  downheap1(j);
 end;
End;
(*===================================*)
procedure push1(x:longint);
Begin
inc(top1);
heap1[top1]:=x;
upheap1(top1);
End;
(*===================================*)
function pop1:longint;
Begin
pop1:=heap1[1];
heap1[1]:=heap1[top1];
dec(top1);
downheap1(1);
End;
(*===============================*)
procedure trong;
Begin
for i:=1 to n do begin l[i]:=l[i-1]+a[i]; push1(a[i]); end;
for i:=n+1 to 3*n do
  begin
   l[i]:=l[i-1]+a[i];
   push1(a[i]);
   l[i]:=l[i]-pop1;
  end;

for i:=3*n downto 2*n+1 do begin g[i]:=g[i+1]+a[i]; push(a[i]); end;
for i:=2*n downto 1 do
 begin
  g[i]:=g[i+1]+a[i];
  push(a[i]);
  g[i]:=g[i]-pop;
 end;
End;
(*===============================*)
BEGIN
inp;
assign(f,fo);
rewrite(f);
trong;
kq:=-maxlongint*100000;
for i:=n to 2*n do
 if l[i]-g[i+1]>kq then kq:=l[i]-g[i+1];
writeln(f,kq);
close(f);
END.


uses math;
var  a,heap,heap1:array[0..500000] of longint;
     res:int64;
     n,nheap:longint;
     f,g:array[0..500000] of int64;
procedure nhap;
var i:longint;
begin
 read(n);
 for i:=1 to 3*n do read(a[i]);
end;
procedure doicho(var x,y:longint);
var tg:longint;
begin
 tg:=x;
 x:=y;
 y:=tg;
end;
procedure upheap(i:longint);
var j:longint;
begin
 j:=i div 2;
 if (i=1) or (a[heap[i]]>a[heap[j]]) then exit;
 doicho(heap[i],heap[j]);
 upheap(j);
end;
procedure downheap(i:longint);
var j:longint;
begin
 j:=2*i;
 if j>nheap then exit;
 if (j<nheap) and (A[heap[j]]>a[heap[j+1]]) then inc(j);
 if a[heap[i]]>a[heap[j]] then
  begin
   doicho(heap[i],heap[j]);
   downheap(j);
  end;
end;
procedure push(x:longint);
begin
 inc(nheap);
 heap[nheap]:=x;
 upheap(nheap);
end;
function pop:longint;
begin
 pop:=heap[1];
 heap[1]:=heap[nheap];
 dec(nheap);
 downheap(1);
end;
procedure upheap1(i:longint);
var j:longint;
begin
 j:=i div 2;
 if (i=1) or (a[heap1[i]]<a[heap1[j]]) then exit;
 doicho(heap1[i],heap1[j]);
 upheap1(j);
end;
procedure downheap1(i:longint);
var j:longint;
begin
 j:=2*i;
 if j>nheap then exit;
 if (j<nheap) and (A[heap1[j]]<a[heap1[j+1]]) then inc(j);
 if a[heap1[i]]<a[heap1[j]] then
  begin
   doicho(heap1[i],heap1[j]);
   downheap1(j);
  end;
end;
procedure push1(x:longint);
begin
 inc(nheap);
 heap1[nheap]:=x;
 upheap1(nheap);
end;
function pop1:longint;
begin
 pop1:=heap1[1];
 heap1[1]:=heap1[nheap];
 dec(nheap);
 downheap1(1);
end;
procedure xuli;
var i,k:longint;
begin
 for i:=1 to n do
  begin
   push(i);
   f[i]:=f[i-1]+a[i];
  end;
 for i:=n+1 to 3*n do
  begin
   k:=pop;
   f[i]:=f[i-1]-a[k]+a[i];
   push(i);
  end;
 nheap:=0;
 for i:=3*n downto 2*n+1 do
  begin
   push1(i);
   g[i]:=g[i+1]+a[i];
  end;
 for i:=2*n downto 1 do
  begin
   k:=pop1;
   g[i]:=g[i+1]-a[k]+a[i];
   push1(i);
  end;
 res:=-high(int64);
 for i:=n to 2*n do res:=max(Res,f[i]-g[i+1]);
 write(REs);
end;
begin
// assign(input,'b.inp');reset(input);
// assign(output,'b.out');rewrite(output);
 nhap;
 xuli;
// close(input); close(output);
end.


program Project1;

{$APPTYPE CONSOLE}

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

    if (c>=a) and (c<=b) then
      Writeln('Yes')
    else
      Writeln('No');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

You are given three integers A, B and C.
Determine whether C is not less than A and not greater than B.

Constraints

-100≤A,B,C≤100

A, B and C are all integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

If the condition is satisfied, print Yes; otherwise, print No.

Sample Input 1

1 3 2

Sample Output 1

Yes

C=2 is not less than A=1 and not greater than B=3, and thus the output should be Yes.

Sample Input 2

6 5 4

Sample Output 2

No

C=4 is less than A=6, and thus the output should be No.

Sample Input 3

2 2 2

Sample Output 3

Yes


Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:

When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.

The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

Constraints

2≤N≤1000

1≤M≤min(N(N-1),2000)

1≤a_i,b_i≤N (1≤i≤M)

a_i≠b_i (1≤i≤M)

a_i≠a_j or b_i≠b_j (1≤i<j≤M)

-10^9≤c_i≤10^9 (1≤i≤M)

c_i is an integer.

In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

Sample Input 1

3 3
1 2 4
2 3 3
1 3 5

Sample Output 1

7

There are two ways to move the piece to vertex N=3:

vertex 1 → vertex 2 → vertex 3 : score 4+3=7

vertex 1 → vertex 3 : score 5

Thus, the maximum possible score at the end of the game is 7.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

inf

It is possible to increase the score indefinitely by alternating between vertex 1 and 2.

Sample Input 3

6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000

Sample Output 3

-5000000000


program j01;
var f:array[0..1000000]of longint;
     n,m,u,v,i:longint;
begin
  readln(n,m);
  for i:=1 to m do
  begin
     readln(u,v);inc(f[u]);inc(f[v]);
  end;
for i:=1 to n do
if f[i] mod 2=1 then
begin
 writeln('NO');halt;end;
writeln('YES');
end.

Score : 500 points

Problem Statement

Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.

First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.

Then, Aoki gave him M queries. The i-th of them is as follows:

Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.

After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number.
However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.

Determine whether there exists a tree that has the property mentioned by Takahashi.

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

1 ≤ a_i,b_i ≤ N

a_i ≠ b_i

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

Print YES if there exists a tree that has the property mentioned by Takahashi; print NO otherwise.

Sample Input 1

4 4
1 2
2 4
1 3
3 4

Sample Output 1

YES

For example, Takahashi's graph has the property mentioned by him if it has the following edges: 1-2, 1-3 and 1-4.
In this case, the number written at every edge will become 2.

Sample Input 2

5 5
1 2
3 5
5 1
3 4
2 3

Sample Output 2

NO


program j01;
var f,g,u,v:array[0..100000]of longint;
     n,i:longint;
begin
  readln(n);
for i:=1 to n-1 do
begin
  readln(u[i],v[i]);inc(f[u[i]]);inc(f[v[i]]);
end;
for i:=1 to n-1 do
begin
  if u[i]=1 then inc(g[v[i]]);
  if v[i]=1 then inc(g[u[i]]);
end;
for i:=1 to n do if g[i]>1 then begin writeln('First');halt;end;writeln('Second');
end.

Score : 900 points

Problem Statement

There is a tree with N vertices numbered 1 through N.
The i-th of the N-1 edges connects vertices a_i and b_i.

Initially, each vertex is uncolored.

Takahashi and Aoki is playing a game by painting the vertices. In this game, they alternately perform the following operation, starting from Takahashi:

Select a vertex that is not painted yet.

If it is Takahashi who is performing this operation, paint the vertex white; paint it black if it is Aoki.

Then, after all the vertices are colored, the following procedure takes place:

Repaint every white vertex that is adjacent to a black vertex, in black.

Note that all such white vertices are repainted simultaneously, not one at a time.

If there are still one or more white vertices remaining, Takahashi wins; if all the vertices are now black, Aoki wins.
Determine the winner of the game, assuming that both persons play optimally.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i,b_i ≤ N

a_i ≠ b_i

The input graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

Print First if Takahashi wins; print Second if Aoki wins.

Sample Input 1

3
1 2
2 3

Sample Output 1

First

Below is a possible progress of the game:

First, Takahashi paint vertex 2 white.

Then, Aoki paint vertex 1 black.

Lastly, Takahashi paint vertex 3 white.

In this case, the colors of vertices 1, 2 and 3 after the final procedure are black, black and white, resulting in Takahashi's victory.

Sample Input 2

4
1 2
2 3
2 4

Sample Output 2

First

Sample Input 3

6
1 2
2 3
3 4
2 5
5 6

Sample Output 3

Second


program ec12;
var
        ans:int64;
	a,s:array[0..100000] of int64;
	n,m,i,j: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.

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
	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.


var
	N,i:Longint;
	A:Array[1..100000]of Longint;
	ans,now,sum:int64;
begin
	read(N);
	for i:=1 to N do read(A[i]);
	now:=0;
	sum:=0;
	for i:=1 to N do begin
		inc(sum,A[i]);
		if(i mod 2=1)and(sum<=0)then begin
			inc(now,1-sum);
			sum:=1;
		end else if(i mod 2=0)and(sum>=0)then begin
			inc(now,sum+1);
			sum:=-1;
		end;
	end;
	ans:=now;
	now:=0;
	sum:=0;
	for i:=1 to N do begin
		inc(sum,A[i]);
		if(i mod 2=0)and(sum<=0)then begin
			inc(now,1-sum);
			sum:=1;
		end else if(i mod 2=1)and(sum>=0)then begin
			inc(now,sum+1);
			sum:=-1;
		end;
	end;
	if ans>now then ans:=now;
	writeln(ans);
end.


uses math ;
var a:array[1..100000] of int64 ;
      i,n:longint ;
			ans1,ans2,sum1,sum2:int64 ;
begin
   readln(n) ;
	 for i:=1 to n do read(a[i]) ;
	 if a[1]>0 then
	 begin
	   sum1:=a[1];
		 ans2:=a[1]+1 ;
		 sum2:=-1 ;
	 end
	 else if (a[1]=0) then
	 begin
	   sum1:=1;
		 ans1:=1;ans2:=1 ;
		 sum2:=-1 ;
	 end
	 else
	 begin
	   sum1:=1;
		 ans1:=abs(a[1])+1 ;
		 sum2:=a[1] ;
	 end;
	 for i:=2 to n do
	 begin
	   if sum1>0 then
		 begin
		   if a[i]+sum1>=0 then
			 begin
			   ans1:=ans1+a[i]+sum1+1 ;
				 sum1:=-1 ;
			 end
			 else sum1:=sum1+a[i] ;
		 end
		 else
		 begin
		   if a[i]+sum1<=0 then
			 begin
			   ans1:=ans1+abs(sum1+a[i])+1 ;
				 sum1:=1 ;
			 end
			 else sum1:=sum1+a[i] ;
		 end;
	 end;
	 if sum1=0 then inc(ans1) ;
	 for i:=2 to n do //这段跟上段一模一样
	 begin
	   if sum2>0 then
		 begin
		   if a[i]+sum2>=0 then
			 begin
			   ans2:=ans2+a[i]+sum2+1 ;
				 sum2:=-1 ;
			 end
			 else sum2:=sum2+a[i] ;
		 end
		 else
		 begin
		   if a[i]+sum2<=0 then
			 begin
			   ans2:=ans2+abs(sum2+a[i])+1 ;
				 sum2:=1 ;
			 end
			 else sum2:=sum2+a[i] ;
		 end;
	 end;
	 if sum2=0 then inc(ans2) ;
	 writeln(min(ans1,ans2)) ;
end.


var n,i,t,x,y,ans:longint;
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
        begin
          t:=0;
          inc(ans);
        end;
      2:if x>y then
        begin
          t:=0;
          inc(ans);
        end;
    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
	N,i,ans,now:Longint;
	A:Array[1..100001]of Longint;
function f(i,j:Longint):Longint;
begin
	if A[i]<A[j] then f:=1 else if A[i]>A[j] then f:=-1 else f:=0;
end;
begin
	read(N);
	for i:=1 to N do read(A[i]);
	ans:=1;
	i:=3;
	now:=f(1,2);
	while i<=N do begin
		if(f(i-1,i)<>0)and(now<>f(i-1,i))then begin
			if now<>0 then begin
				inc(ans);
				inc(i);
			end;
			now:=f(i-1,i);
		end;
		inc(i);
	end;
	writeln(ans);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  h,w,i,j,count,x,y,sw,m,n,a,b,c:Integer;
  //a,b:array of string;
  o,e,pa:string;
  tf:Boolean;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(o);
    Readln(e);

    pa:='';
    for i := 1 to (length(o)+length(e)-abs(length(o)-length(e))) div 2 do begin
      pa:=pa+o[i];
      pa:=pa+e[i];
    end;

    if length(o)>length(e) then
      pa:=pa+o[length(o)]
    else if length(o)<length(e) then
      pa:=pa+o[length(e)];

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

Score : 200 points

Problem Statement

Snuke signed up for a new website which holds programming competitions.
He worried that he might forget his password, and he took notes of it.
Since directly recording his password would cause him trouble if stolen,
he took two notes: one contains the characters at the odd-numbered positions, and the other contains the characters at the even-numbered positions.

You are given two strings O and E. O contains the characters at the odd-numbered positions retaining their relative order, and E contains the characters at the even-numbered positions retaining their relative order.
Restore the original password.

Constraints

O and E consists of lowercase English letters (a - z).

1 \leq |O|,|E| \leq 50

|O| - |E| is either 0 or 1.

Input

Input is given from Standard Input in the following format:

O
E

Output

Print the original password.

Sample Input 1

xyz
abc

Sample Output 1

xaybzc

The original password is xaybzc. Extracting the characters at the odd-numbered positions results in xyz, and extracting the characters at the even-numbered positions results in abc.

Sample Input 2

atcoderbeginnercontest
atcoderregularcontest

Sample Output 2

aattccooddeerrbreeggiunlnaerrccoonntteesstt


Score : 500 points

Problem Statement

On a two-dimensional plane, there are m lines drawn parallel to the x axis, and n lines drawn parallel to the y axis.
Among the lines parallel to the x axis, the i-th from the bottom is represented by y = y_i.
Similarly, among the lines parallel to the y axis, the i-th from the left is represented by x = x_i.

For every rectangle that is formed by these lines, find its area, and print the total area modulo 10^9+7.

That is, for every quadruple (i,j,k,l) satisfying 1\leq i < j\leq n and 1\leq k < l\leq m, find the area of the rectangle formed by the lines x=x_i, x=x_j, y=y_k and y=y_l, and print the sum of these areas modulo 10^9+7.

Constraints

2 \leq n,m \leq 10^5

-10^9 \leq x_1 < ... < x_n \leq 10^9

-10^9 \leq y_1 < ... < y_m \leq 10^9

x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

n m
x_1 x_2 ... x_n
y_1 y_2 ... y_m

Output

Print the total area of the rectangles, modulo 10^9+7.

Sample Input 1

3 3
1 3 4
1 3 6

Sample Output 1

60

The following figure illustrates this input:

The total area of the nine rectangles A, B, ..., I shown in the following figure, is 60.

Sample Input 2

6 5
-790013317 -192321079 95834122 418379342 586260100 802780784
-253230108 193944314 363756450 712662868 735867677

Sample Output 2

835067060


type SortDataType=Longint;
function SortCmp(const a,b:SortDataType):Boolean;forward;
procedure SortImpl(var A:Array of SortDataType;L,R:Longint);
var
	tmp:SortDataType;
	mid,il,ir:Longint;
begin
	if R-L=2 then begin
		if SortCmp(A[L+1],A[L])then begin
			tmp:=A[L];
			A[L]:=A[L+1];
			A[L+1]:=tmp;
		end;
	end else if R-L>2 then begin
		mid:=(L+R)div 2;
		if SortCmp(A[mid],A[L])then begin
			tmp:=A[mid];
			A[mid]:=A[L];
			A[L]:=tmp;
		end;
		if SortCmp(A[R-1],A[mid])then begin
			tmp:=A[R-1];
			A[R-1]:=A[mid];
			A[mid]:=tmp;
			if SortCmp(A[mid],A[L])then begin
				tmp:=A[mid];
				A[mid]:=A[L];
				A[L]:=tmp;
			end;
		end;
		il:=L;
		ir:=R-1;
		while il<=ir do begin
			while SortCmp(A[il],A[mid])do inc(il);
			while SortCmp(A[mid],A[ir])do dec(ir);
			if il<ir then begin
				tmp:=A[il];
				A[il]:=A[ir];
				A[ir]:=tmp;
				if mid=il then mid:=ir else if mid=ir then mid:=il;
				inc(il);
				dec(ir);
			end else if il=ir then begin
				inc(il);
				dec(ir);
			end;
		end;
		SortImpl(A,L,il);
		SortImpl(A,il,R);
	end;
end;
procedure Sort(var A:Array of SortDataType;size:Longint);
begin
	SortImpl(A,0,size);
end;
function SortCmp(const a,b:SortDataType):Boolean;
begin
	SortCmp:=a<b;
end;
var
	N,i:Longint;
	A:Array[1..300000]of Longint;
	ans:int64;
begin
	read(N);
	for i:=1 to 3*N do read(A[i]);
	Sort(A,3*N);
	for i:=1 to N do inc(ans,A[N+i*2-1]);
	writeln(ans);
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 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;
    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 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;
        inc(head);
    end;
end;

begin
    read(n,m);
    for i:=1 to n 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[0..200001]of longint;
var n,q,m,i,j,a,b:longint;
    v,d,c,max,cl,head,next,start,endd:arr;
    bb,bc:array[0..200001]of boolean;
procedure dfs(k,dis,col:longint);
var i:longint;
begin
    bb[k]:=true;
    if(max[k]>=dis)and(bc[k])then begin
        bb[k]:=false;
        exit;
    end;
    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 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 begin
        bb[k]:=false;
        exit;
    end;
    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 arr=array[1..200000]of longint;
var n,q,m,i,j,a,b:longint;
    v,d,c,l,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(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);
        head[i]:=a;head[i*2]:=b;
        next[i]:=b;next[i*2]:=a;
    end;
    m:=m*2;sort(1,m);start[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.


var
i,j,k,m,n,x:longint;
a,b,c,d:array[1..1000]of longint;
begin
    readln(m,n);
    for i:=1 to m do
    readln(a[i],b[i]);
    for j:=1 to n do
    readln(c[j],d[j]);
    for i:=1 to m do
    begin
        k:=maxlongint;
        for j:=1 to n do
        if abs(a[i]-c[j])+abs(b[i]-d[j])<k then
        begin k:=abs(a[i]-c[j])+abs(b[i]-d[j]);x:=j; end;
        writeln(x);
    end;
end.

Score : 200 points

Problem Statement

There are N students and M checkpoints on the xy-plane.

The coordinates of the i-th student (1 \leq i \leq N) is (a_i,b_i), and the coordinates of the checkpoint numbered j (1 \leq j \leq M) is (c_j,d_j).

When the teacher gives a signal, each student has to go to the nearest checkpoint measured in Manhattan distance.

The Manhattan distance between two points (x_1,y_1) and (x_2,y_2) is |x_1-x_2|+|y_1-y_2|.

Here, |x| denotes the absolute value of x.

If there are multiple nearest checkpoints for a student, he/she will select the checkpoint with the smallest index.

Which checkpoint will each student go to?

Constraints

1 \leq N,M \leq 50

-10^8 \leq a_i,b_i,c_j,d_j \leq 10^8

All input values are integers.

Input

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

N M
a_1 b_1
:
a_N b_N
c_1 d_1
:
c_M d_M

Output

Print N lines.

The i-th line (1 \leq i \leq N) should contain the index of the checkpoint for the i-th student to go.

Sample Input 1

2 2
2 0
0 0
-1 0
1 0

Sample Output 1

2
1

The Manhattan distance between the first student and each checkpoint is:

For checkpoint 1: |2-(-1)|+|0-0|=3

For checkpoint 2: |2-1|+|0-0|=1

The nearest checkpoint is checkpoint 2. Thus, the first line in the output should contain 2.

The Manhattan distance between the second student and each checkpoint is:

For checkpoint 1: |0-(-1)|+|0-0|=1

For checkpoint 2: |0-1|+|0-0|=1

When there are multiple nearest checkpoints, the student will go to the checkpoint with the smallest index. Thus, the second line in the output should contain 1.

Sample Input 2

3 4
10 10
-10 -10
3 3
1 2
2 3
3 5
3 5

Sample Output 2

3
1
2

There can be multiple checkpoints at the same coordinates.

Sample Input 3

5 5
-100000000 -100000000
-100000000 100000000
100000000 -100000000
100000000 100000000
0 0
0 0
100000000 100000000
100000000 -100000000
-100000000 100000000
-100000000 -100000000

Sample Output 3

5
4
3
2
1


var
  n,i,s:longint;
  st:string;
begin
  readln(n);
  for i:=trunc(sqrt(n)) downto 1 do
   if n mod i=0 then break;
  s:=0;
  while i>0 do
   begin
     s:=s+1;
     i:=i div 10;
   end;
  write(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,i,s:int64;
  st:string;
begin
  readln(n);
  i:=trunc(sqrt(n));
  while (i>0) and (s=0) do
   begin
     if n mod i=0 then break;
     i:=i-1;
   end;
  if i=0 then i:=1;
  s:=0;
  while i>0 do
   begin
     s:=s+1;
     i:=i div 10;
   end;
  write(s);
end.


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


var
 n,i,x:int64;
 y:string;
begin
 readln(n);
 i:=1;
 while i<=trunc(sqrt(n)) do
  begin
   if n mod i=0 then x:=n div i;
   inc(i);
  end;
 str(x,y);
 writeln(length(y));
end.


var
i,j,c,d,e:longint;
s,s1,s2:string;
n,b:qword;
begin
readln(n);
for i:=trunc(sqrt(n)) downto 1 do
if n mod i=0 then begin b:=n div i;str(b,s);write(length(s));break;end;
writeln;
end.


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


var
    s1,s2:string;
    n:int64;
    i,j,l1,l2:longint;
begin
    read(n);
    for i:=trunc(sqrt(n)) downto 2 do
        if n mod i=0 then
        begin
            j:=n div i;
            str(i,s1);
            l1:=length(s1);
            str(j,s2);
            l2:=length(s2);
            writeln(s1+s2);
            halt;
        end;
end.


program dolphin;

var n,i,d,s: word;
    min,c : byte;

begin

read(n);
s:=n;
c:=0;
while s<>0 do begin
          c:=c+1;
          s:=s div 10;

            end;
min:=c;
for i:=2 to n div 2 do begin
  c:=0;
  if n mod i=0 then begin s:=n div i;
                          while s<>0 do begin
                          c:=c+1;
                          s:=s div 10;

                          end;

  if min<c then min:=c;
  end;

  end;
write(min);

end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  h,w,i,j,count,x,y,a,b,sw:Integer;
  s:array of string;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(x);

    count:=0;
    a:=0;
    while not(a>=x) do begin
      inc(count);
      a:=a+count;
    end;

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

Score : 200 points

Problem Statement

There is a kangaroo at coordinate 0 on an infinite number line that runs from left to right, at time 0.
During the period between time i-1 and time i, the kangaroo can either stay at his position, or perform a jump of length exactly i to the left or to the right.
That is, if his coordinate at time i-1 is x, he can be at coordinate x-i, x or x+i at time i.
The kangaroo's nest is at coordinate X, and he wants to travel to coordinate X as fast as possible.
Find the earliest possible time to reach coordinate X.

Constraints

X is an integer.

1≤X≤10^9

Input

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

X

Output

Print the earliest possible time for the kangaroo to reach coordinate X.

Sample Input 1

6

Sample Output 1

3

The kangaroo can reach his nest at time 3 by jumping to the right three times, which is the earliest possible time.

Sample Input 2

2

Sample Output 2

2

He can reach his nest at time 2 by staying at his position during the first second, and jumping to the right at the next second.

Sample Input 3

11

Sample Output 3

5


var
  mt, t, x: int64;
begin
  read(x);
  for t := x downto 1 do
  if 2 * x > t * (t - 1) then
  begin
    mt := t;
    break;
  end;
  writeln(mt);
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 inc(dp2[i-1][j+a[i]],dp2[i][j]);
	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 t:array[0..100086]of longint;
    n,x,k,r,l,i,ans,tmp,c:longint;

procedure qsort(l,r:longint);
var i,j,x,y:longint;
begin
  i:=l;j:=r;x:=t[(i+j)div 2];
  repeat
    while t[i]<x do inc(i);
    while x<t[j] do dec(j);
    if i<=j then
    begin
      y:=t[i];t[i]:=t[j];t[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,c,k);
  for i:=1 to n do readln(t[i]);
  qsort(1,n);l:=1;
  inc(n);t[n]:=maxlongint;
  for i:=2 to n do
  begin
    while t[i]-t[l]>k do
    begin
      inc(ans);tmp:=c;
      while (l<=i-1)and(tmp>0) do
      begin
        inc(l);dec(tmp);
      end;
    end;
  end;
  writeln(ans);
end.

Score : 300 points

Problem Statement

Every day, N passengers arrive at Takahashi Airport.
The i-th passenger arrives at time T_i.

Every passenger arrived at Takahashi airport travels to the city by bus. Each bus can accommodate up to C passengers.
Naturally, a passenger cannot take a bus that departs earlier than the airplane arrives at the airport.
Also, a passenger will get angry if he/she is still unable to take a bus K units of time after the arrival of the airplane.
For that reason, it is necessary to arrange buses so that the i-th passenger can take a bus departing at time between T_i and T_i + K (inclusive).

When setting the departure times for buses under this condition, find the minimum required number of buses.
Here, the departure time for each bus does not need to be an integer, and there may be multiple buses that depart at the same time.

Constraints

2 \leq N \leq 100000

1 \leq C \leq 10^9

1 \leq K \leq 10^9

1 \leq T_i \leq 10^9

C, K and T_i are integers.

Input

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

N C K
T_1
T_2
:
T_N

Output

Print the minimum required number of buses.

Sample Input 1

5 3 5
1
2
3
6
12

Sample Output 1

3

For example, the following three buses are enough:

A bus departing at time 4.5, that carries the passengers arriving at time 2 and 3.

A bus departing at time 6, that carries the passengers arriving at time 1 and 6.

A bus departing at time 12, that carries the passenger arriving at time 12.

Sample Input 2

6 3 3
7
6
2
8
10
6

Sample Output 2

3


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

procedure qsort(l,r:longint);
var i,j,x,y:longint;
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;
  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,s,i:longint;
begin
read(n);
s:=1;
for i:=1 to n do s:=(s*i) mod 1000000007;
writeln(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:qword;
begin
read(n);
s:=1;
for i:=1 to n-1 do s:=(s*2) div 1000000000;
writeln(s);
end.


var
n,s,i:longint;
begin
read(n);
s:=1;
for i:=1 to n-1 do s:=(s*2) mod 1000000000;
writeln(s);
end.


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


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


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function Factorial(x: Int64): Int64;
begin
  if x = 0 then
    Result := 1
  else
    Result := x * Factorial(x - 1) mod 1000000007;
end;

var
  n:Int64;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Readln(n);

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


uses math;
var
  n,m:longint;
begin
  readln(n,m);
  writeln(min(n,m div 2)+max(m-n*2,0) div 4);
end.

Score : 300 points

Problem Statement

Snuke loves puzzles.

Today, he is working on a puzzle using S- and c-shaped pieces.
In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:

Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.

Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.

Constraints

1 ≤ N,M ≤ 10^{12}

Input

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

N M

Output

Print the answer.

Sample Input 1

1 6

Sample Output 1

2

Two Scc groups can be created as follows:

Combine two c-shaped pieces into one S-shaped piece

Create two Scc groups, each from one S-shaped piece and two c-shaped pieces

Sample Input 2

12345 678901

Sample Output 2

175897


var
 n,m,i:longint;
begin
 readln(n,m);
 while (n>0) or (m>1) do
  begin
   inc(i);
   if n=0 then m:=m-2
              else dec(n);
   if m>=2 then m:=m-2
                  else n:=n-1;
  end;
 if (n<0) or (m<0) then dec(i);
 writeln(i);
end.


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[pre],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


var
	n,m,i,j,k,l,f,s:Longint;
	a,b:array[1..50]of String[50];
begin
	readln(n,m);
	for i:=1 to n do readln(a[i]);
	for j:=1 to m do readln(b[j]);
	for i:=0 to n-m do for j:=0 to n-m do begin
		f:=1;
		for k:=1 to m do for l:=1 to m do
			if a[i+k][j+l]<>b[k][l] then f:=0;
		inc(s,f);
	end;
	if s>0 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);
		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.

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.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..100]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 100 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,100);
			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,100);
			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
			for j:=mp-x 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+x 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..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,220);
		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,220);
		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..210]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..201]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 300 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,300);
		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,300);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 300 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:Longint;
	p,m:array[0..400]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 400 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:=400-x downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
		end else if x<0 then begin
			for j:=400+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 400 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;  // maxint = 2^15 - 1 = 32767

    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
                        // khong lay
                        dp[i+1, ca, cb] := min(dp[i,ca,cb], dp[i+1, ca, cb]);
                        // lay
                        dp[i+1, ca+a[i], cb+b[i]] := min(dp[i,ca,cb] + c[i] , dp[i+1, ca+a[i], cb+b[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;
  f[1,a[1]-b[1]]:=c[1];
  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,s:longint;
begin
  readln(n);
  for i:=1 to n do
    begin
      readln(x);
      s:=(s+x mod 2) mod 2;
    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,c,a:Longint;
begin
read(N);
for i:=1 to N do begin
read(a);
inc(c,a mod 2);
end;
if c mod 2=0 then writeln('YES')else writeln('NO');
end.


var
	N,i:Longint;
	A,B:Array[1..100000]of int64;
	sum,all,k:int64;
begin
	read(N);
	for i:=1 to N do begin
		read(A[i]);
		inc(sum,A[i]);
		if i>1 then B[i-1]:=A[i]-A[i-1];
	end;
	B[N]:=A[1]-A[N];
	all:=(N+1)*N div 2;
	if sum mod all<>0 then begin
		writeln('NO');
		exit;
	end;
	K:=sum div all;
	for i:=1 to N do begin
		dec(B[i],K);
		if(B[i]>0)or(B[i]mod N<>0)then begin
			writeln('NO');
			exit;
		end;
	end;
	writeln('YES');
end.

Score : 500 points

Problem Statement

There are N boxes arranged in a circle. The i-th box contains A_i stones.

Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:

Select one box. Let the box be the i-th box. Then, for each j from 1 through N, remove exactly j stones from the (i+j)-th box. Here, the (N+k)-th box is identified with the k-th box.

Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.

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

If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.

Sample Input 1

5
4 5 1 2 3

Sample Output 1

YES

All the stones can be removed in one operation by selecting the second box.

Sample Input 2

5
6 9 12 10 8

Sample Output 2

YES

Sample Input 3

4
1 2 3 1

Sample Output 3

NO


var x, a: int64;

begin
	readln(x);
    a := x mod 11;
    if a > 6 then
    	a := 2
    else if a > 0 then
    	a := 1;
    writeln(x div 11 * 2 + a);
end.

Score : 300 points

Problem Statement

Snuke has decided to play with a six-sided die. Each of its six sides shows an integer 1 through 6, and two numbers on opposite sides always add up to 7.

Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:

Operation: Rotate the die 90° toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain y points where y is the number written in the side facing upward.

For example, let us consider the situation where the side showing 1 faces upward, the near side shows 5 and the right side shows 4, as illustrated in the figure.
If the die is rotated toward the right as shown in the figure, the side showing 3 will face upward.
Besides, the side showing 4 will face upward if the die is rotated toward the left, the side showing 2 will face upward if the die is rotated toward the front, and the side showing 5 will face upward if the die is rotated toward the back.

Find the minimum number of operation Snuke needs to perform in order to score at least x points in total.

Constraints

1 ≦ x ≦ 10^{15}

x is an integer.

Input

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

x

Output

Print the answer.

Sample Input 1

7

Sample Output 1

2

Sample Input 2

149696127901

Sample Output 2

27217477801


var x,y:int64;
begin
  readln(x);
  y:=(x div 11)+(x div 11);
  x:=x mod 11;
  if (x>=1) and (x<=6) then inc(y);
  if (x>6) then y:=y+2;
  writeln(y);
end.


program vjudge149349E;
const maxn=300000+10;
      maxm=100000+10;
var l,r,next:array[0..maxn] of longint;
    last,ans,bits:array[0..maxm] of longint;
    n,m,i,j:longint;
function ask(x:longint):longint;
var ans:longint;
begin
  ans:=0;
  while x>0 do
  begin
    ans:=ans+bits[x];
    x:=x-x and (-x);
  end;
  exit(ans);
end;
procedure add(x,d:longint);
begin
  while(x<=m+1) do
  begin
    bits[x]:=bits[x]+d;
    x:=x+x and (-x);
  end;
end;
begin
  readln(n,m);
  fillchar(last,sizeof(last),0);
  for i:=1 to n do
  begin
    readln(l[i],r[i]);
    next[i]:=last[r[i]-l[i]+1];
    last[r[i]-l[i]+1]:=i;
    inc(ans[1]);
    dec(ans[r[i]-l[i]+2]);
  end;
  for i:=1 to m do ans[i]:=ans[i]+ans[i-1];
  fillchar(bits,sizeof(bits),0);
  for i:=1 to m do
  begin
    ans[i]:=ans[i]+bits[1];
    for j:=1 to m div i do
      ans[i]:=ans[i]+ask(j*i+1);
    j:=last[i];
    while j>0 do
    begin
      add(l[j]+1,1);
      add(r[j]+2,-1);
      j:=next[j];
    end;
  end;
  for i:=1 to m do writeln(ans[i]);
end.

Score : 700 points

Problem Statement

Snuke has decided to play a game, where the player runs a railway company.
There are M+1 stations on Snuke Line, numbered 0 through M.
A train on Snuke Line stops at station 0 and every d-th station thereafter, where d is a predetermined constant for each train.
For example, if d = 3, the train stops at station 0, 3, 6, 9, and so forth.

There are N kinds of souvenirs sold in areas around Snuke Line. The i-th kind of souvenirs can be purchased when the train stops at one of the following stations: stations l_i, l_i+1, l_i+2, ..., r_i.

There are M values of d, the interval between two stops, for trains on Snuke Line: 1, 2, 3, ..., M.
For each of these M values, find the number of the kinds of souvenirs that can be purchased if one takes a train with that value of d at station 0.
Here, assume that it is not allowed to change trains.

Constraints

1 ≦ N ≦ 3 × 10^{5}

1 ≦ M ≦ 10^{5}

1 ≦ l_i ≦ r_i ≦ M

Input

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

N M
l_1 r_1
:
l_{N} r_{N}

Output

Print the answer in M lines. The i-th line should contain the maximum number of the kinds of souvenirs that can be purchased if one takes a train stopping every i-th station.

Sample Input 1

3 3
1 2
2 3
3 3

Sample Output 1

3
2
2

If one takes a train stopping every station, three kinds of souvenirs can be purchased: kind 1, 2 and 3.

If one takes a train stopping every second station, two kinds of souvenirs can be purchased: kind 1 and 2.

If one takes a train stopping every third station, two kinds of souvenirs can be purchased: kind 2 and 3.

Sample Input 2

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

Sample Output 2

7
6
6
5
4
5
5
3
2


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+rx) div 2];
  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;
begin
  q:=last[u];
  while (q>0) do
  begin
    v:=other[q];
    if (v<>father) then dfs(v,u);
	q:=pre[q];
  end;
  tott:=0;
  q:=last[u];
  while (q>0) do
  begin
    v:=other[q];
    if (v<>father) then
	begin
	  inc(tott);
	  now[tott]:=anss[v];
	end;
	q:=pre[q];
  end;
if (tott<>0) then
begin
  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
  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..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..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: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;
  fillchar(noww,sizeof(noww),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,tott,tot:longint;
	last,pre,other,anss,a,t,now:array[0..300007] of int64;
	ans:int64;
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[random(rx-lx+1)+lx];
  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:longint;
begin
  tott:=0;
  fillchar(now,sizeof(now),0);
  q:=last[u];
  while (q>0) do
  begin
    v:=other[q];
    if (v<>father) then
	begin
	  dfs(v,u);
	  inc(tott);
	  now[tott]:=anss[v];
	end;
	q:=pre[q];
  end;
  qsort(1,tott);
  anss[u]:=0;
  for i:=1 to tott do
	if (now[i]+i>anss[u]) then anss[u]:=now[i]+i;
end;
begin
  readln(n);
  randomize;
  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;
  ans:=0;
  fillchar(anss,sizeof(anss),0);
  dfs(1,0);
  writeln(anss[1]);
end.


var n,i,tot,ans:longint;
    a,t: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;
    insert(i,a[i]);insert(a[i],i);
  end;
  ans:=0;
  dfs(1,0);
  if (ans>n) then ans:=n;
  writeln(ans-1);
end.


var n,a,b,x,i,p,t,s:integer;
begin
  read(n,a,b,t);
  s:=0;
  read(t);
  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);
	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.

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