var
  a,b:array[0..100002]of longint;
  i,j,k,n,m:longint;
  p,q,ans,anq:qword;
function max(a,b:longint):longint;
begin
  if abs(a)<=b then exit(a)
  else exit(b);
end;
procedure zx(s:longint);
var
  i,j,p,q,t,u,o:longint;
  f:array[0..100002]of boolean;
begin
  for i:=1 to n do
  if a[i]>s then break;
  p:=i-1;
  q:=i;
  t:=n;
  j:=n div 2;
  while j>0 do
  begin
    dec(j);
    f[t]:=true;
    dec(t);
    if p=0 then begin ans:=a[q]+ans; inc(q); end
    else if f[q] then begin ans:=a[p]+ans; dec(p,2); end
    else if s-a[p]>=q-s then
    begin
      ans:=a[p]+ans;
      dec(p);
    end else
    begin
      ans:=a[q]+ans;
      inc(q);
    end;
  end;
end;
begin
  readln(n,m);
  for i:=1 to n do begin read(a[i]); anq:=a[i]+anq; end;
  for i:=1 to m do
  begin
    readln(b[i]);
    ans:=0;
    zx(b[i]);
    writeln(anq-ans);
  end;
end.

Score : 500 points

Problem Statement

There are N cards. The i-th card has an integer A_i written on it.
For any two cards, the integers on those cards are different.

Using these cards, Takahashi and Aoki will play the following game:

Aoki chooses an integer x.

Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:

Takahashi should take the card with the largest integer among the remaining card.

Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.

The game ends when there is no card remaining.

You are given Q candidates for the value of x: X_1, X_2, ..., X_Q.
For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.

Constraints

2 \leq N \leq 100 000

1 \leq Q \leq 100 000

1 \leq A_1 < A_2 < ... < A_N \leq 10^9

1 \leq X_i \leq 10^9 (1 \leq i \leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.

Sample Input 1

5 5
3 5 7 11 13
1
4
9
10
13

Sample Output 1

31
31
27
23
23

For example, when x = X_3(= 9), the game proceeds as follows:

Takahashi takes the card with 13.

Aoki takes the card with 7.

Takahashi takes the card with 11.

Aoki takes the card with 5.

Takahashi takes the card with 3.

Thus, 13 + 11 + 3 = 27 should be printed on the third line.

Sample Input 2

4 3
10 20 30 40
2
34
34

Sample Output 2

70
60
60


uses crt;
var a,b,c:array[1..100000] of longint;
    i,n:longint;
function max(a,b:longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n);
    for i := 1 to n do readln(a[i],b[i],c[i]);
    for i:=2 to n do
      begin
          a[i]:=max(a[i-1],max(b[i-1],c[i-1])+a[i]);
          b[i]:=max(b[i-1],max(a[i-1],c[i-1])+b[i]);
          c[i]:=max(c[i-1],max(a[i-1],b[i-1])+c[i]);
      end;
      writeln(max(a[n],max(c[n],b[n])));
      readln;
end.

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

Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days.
For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

A: Swim in the sea. Gain a_i points of happiness.

B: Catch bugs in the mountains. Gain b_i points of happiness.

C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

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

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.

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.


//ac
uses math;
var
  a,b,c:array[0..101010] of qword;
  dp:array[1..3,0..101010] of qword;
  n,i:longint;
function max(u,v:qword):qword;
  BEGIN
    if u>v then exit(u);
    exit(v);
  end;
BEGIN
  ///assign(input,'a.in'); reset(input);
  readln(n);
  for i:=1 to n do read(a[i], b[i], c[i]);

  fillchar(dp,sizeof(dp),0);
  for i:=1 to n do BEGIN
    dp[1,i] := max(dp[1,i-1], max(dp[2,i-1], dp[3,i-1]) + a[i]);
    dp[2,i] := max(dp[2,i-1], max(dp[1,i-1], dp[3,i-1]) + b[i]);
    dp[3,i] := max(dp[3,i-1], max(dp[1,i-1], dp[2,i-1]) + c[i]);
  end;
  writeln(max(dp[1,n], max(dp[2,n], dp[3,n])));
END.


var N, W, V, i, j: longint;
    ww, vv: array[1..1000] of longint;
    dp, weight: array[-10..100, -11000..100000] of longint;
begin
    readln(N, W);
    for i:=1 to N do
    begin
        readln(ww[i], vv[i]);
        V:=V+vv[i];
    end;
    for i:=1 to N do
    begin
        for j:=1 to V do
        begin
            if (weight[i-1,j-vv[i]]+ww[i]>W) then
            begin
                if dp[i-1,j]>dp[i,j-1] then
                begin
                    dp[i,j]:=dp[i-1,j];
                    weight[i,j]:=weight[i-1,j];
                end
                else
                begin
                    dp[i,j]:=dp[i,j-1];
                    weight[i,j]:=weight[i,j-1];
                end;
            end
            else
            begin
                if (dp[i-1,j-vv[i]]+vv[i]<=j) then
                begin
                if (dp[i-1,j]>dp[i-1,j-vv[i]]+vv[i]) and (dp[i-1,j]>dp[i,j-1]) then
                begin
                    dp[i,j]:=dp[i-1,j];
                    weight[i,j]:=weight[i-1,j];
                end
                else if (dp[i-1,j-vv[i]]+vv[i]>=dp[i-1,j]) and (dp[i-1,j-vv[i]]+vv[i]>=dp[i,j-1]) then
                begin
                    dp[i,j]:=dp[i-1,j-vv[i]]+vv[i];
                    weight[i,j]:=weight[i-1,j-vv[i]]+ww[i];
                end
                else
                begin
                    dp[i,j]:=dp[i,j-1];
                    weight[i,j]:=weight[i,j-1];
                end;
                end
                else
                begin
                if dp[i-1,j]>dp[i,j-1] then
                begin
                    dp[i,j]:=dp[i-1,j];
                    weight[i,j]:=weight[i-1,j];
                end
                else
                begin
                    dp[i,j]:=dp[i,j-1];
                    weight[i,j]:=weight[i,j-1];
                end;
                end;
            end;
//            write(dp[i,j], ' ');
        end;
//        writeln;
    end;
    writeln(dp[N,V]);
end.

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


var N, W, V, i, j: longint;
    ww, vv: array[1..1000] of longint;
    dp, weight: array[0..100, -1100..100000] of longint;
begin
    readln(N, W);
    for i:=1 to N do
    begin
        readln(ww[i], vv[i]);
        V:=V+vv[i];
    end;
    for i:=1 to N do
    begin
        for j:=1 to V do
        begin
            if (weight[i-1,j-vv[i]]+ww[i]>W) then
            begin
                if dp[i-1,j]>dp[i,j-1] then
                begin
                dp[i,j]:=dp[i-1,j];
                weight[i,j]:=weight[i-1,j];
                end
                else
                begin
                    dp[i,j]:=dp[i,j-1];
                    weight[i,j]:=weight[i,j-1];
                end;
            end
            else
            begin
                if (dp[i-1,j-vv[i]]+vv[i]<=j) then
                begin
                if dp[i-1,j]>dp[i-1,j-vv[i]]+vv[i] then
                begin
                    dp[i,j]:=dp[i-1,j];
                    weight[i,j]:=weight[i-1,j];
                end
                else
                begin
                    dp[i,j]:=dp[i-1,j-vv[i]]+vv[i];
                    weight[i,j]:=weight[i-1,j-vv[i]]+ww[i];
                end;
                end
                else
                begin
                    dp[i,j]:=dp[i-1,j];
                    weight[i,j]:=weight[i-1,j];
                end;
            end;
        end;
    end;
    writeln(dp[N,V]);
end.


program knapsack1;

uses math;

const
  fi='knapsack1.inp';
  fo='knapsack1.out';
  oo=trunc(1e9)+5;

var
  n,m,va: longint;
  v,w: array[0..100000] of int16;
  f:array[0..100,0..100000] of int32;

procedure nhap;
var i: longint;
begin

  va:=0;
  read(n,m);
  for i:=1 to n do begin
    read(w[i],v[i]);
    inc(va,v[i]);
  end;

end;

procedure xuli;
var i,j,ans: longint;
begin

  ans:=0;
  for i:=0 to n do begin
    for j:=0 to va do f[i][j]:=oo;
  end;

  f[0][0]:=0;

  for i:=1 to n do begin
    for j:=0 to va do begin
      if (j>=v[i]) and (f[i-1][j-v[i]]+w[i]<=m) then f[i][j]:=min(f[i-1][j],f[i-1][j-v[i]]+w[i])
      else f[i][j]:=f[i-1][j];
    end;
  end;

  for j:=0 to va do begin
    ans:=ifthen((f[n][j]<=m),j,ans);
  end;
//
//  for i:=0 to n do begin
//    for j:=0 to va do begin
//      if f[i][j]=oo then write('oo':3)
//      else write(f[i][j]:3);
//    end;
//    writeln
//  end;

  write(ans);

end;

begin

  //assign(input,fi);  reset(input);
  //assign(output,fo); rewrite(output);

  nhap;
  xuli;

 // close(input);      close(output);

end.


uses math;
const fi='knapsack2.inp';
      fo='knapsack2.out';
      maxn=100;
      maxv=200000;
var n,w:int32;
    maxval:uint64;
    we,v:array[1..maxn] of uint32;
    f:array[0..maxn,0..maxv] of uint32;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,w); maxval:=0;
   for i:=1 to n do
   begin
     readln(we[i],v[i]);
     maxval:=maxval+v[i];
   end;
end;
//=============================
function min(x,y:int64):int64;
begin
   if x>y then exit(y)
   else exit(x);
end;
//=============================
procedure xuli;
var i,j:int32;
begin
   for i:=0 to n do
    for j:=0 to maxval do f[i,j]:=maxlongint;
   f[0,0]:=0;
   for i:=0 to n-1 do
    for j:=0 to maxval do
     if f[i,j]<>maxlongint then
      begin
         f[i+1,j]:=min(f[i+1,j],f[i,j]);
         if f[i,j]+we[i+1]<=w then
         f[i+1,j+v[i+1]]:=min(f[i+1,j+v[i+1]],f[i,j]+we[i+1]);
      end;
   for i:=maxval downto 0 do
    if f[n,i]<=w then break;
   write(i);
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


uses math;
const fi='knapsack2.inp';
      fo='knapsack2.out';
      maxn=100;
      maxv=200000;
var n,w:int32;
    maxval:uint64;
    we,v:array[1..maxn] of uint32;
    f:array[0..maxn,0..maxv] of uint32;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,w); maxval:=0;
   for i:=1 to n do
   begin
     readln(we[i],v[i]);
     maxval:=maxval+v[i];
   end;
end;
//=============================
function min(x,y:int64):int64;
begin
   if x>y then exit(y)
   else exit(x);
end;
//=============================
procedure xuli;
var i,j:int32;
begin
   for i:=0 to n do
    for j:=0 to maxval do f[i,j]:=maxlongint;
   f[0,0]:=0;
   for i:=0 to n do
    for j:=0 to maxval do
     if f[i,j]<>maxlongint then
      begin
         f[i+1,j]:=min(f[i+1,j],f[i,j]);
         if f[i,j]+we[i+1]<=w then
         f[i+1,j+v[i+1]]:=min(f[i+1,j+v[i+1]],f[i,j]+we[i+1]);
      end;
   for i:=maxval downto 0 do
    if f[n,i]<=w then break;
   write(i);
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


uses math;
const fi='CAITUI2.INP';
      fo='CAITUI2.OUT';
      maxn=100;
      maxj=trunc(1e5);
      oo=trunc(1e9)+7;

var n,mw:longint;
    w,v:array[1..maxn] of longint;

    f:array[0..maxn,0..maxj] of longint;
//============================
procedure doc;
var i:longint;
begin
  readln(n,mw);

  for i:=1 to n do readln(w[i],v[i]);
end;
//============================
procedure xuli;
var i,j:longint;
begin

  for i:=0 to n do
    for j:=0 to n*1000 do f[i,j]:=oo;

  f[0,0]:=0;

  for i:=0 to n-1 do
    for j:=0 to n*1000 do
      if f[i,j]<>oo then
        begin

          //if f[i,j]+w[i+1]>mw then

          f[i+1,j]:=min(f[i+1,j],f[i,j]);

          //writeln(i,' ',j,' ',f[i,j],' ',i+1,' ',f[i+1,j]);

          if f[i,j]+w[i+1]<=mw then
            f[i+1,j+v[i+1]]:=min(f[i+1,j+v[i+1]],f[i,j]+w[i+1]);

        end;

  for j:=n*1000 downto 1 do
    if f[n,j]<=mw then break;

  {for i:=0 to n do
    for j:=0 to n+1000 do
      writeln(i,' ',j,' ',f[i,j]);}

  write(j);
end;
//============================
BEGIN
  //assign(input,fi); reset(input);
  //assign(output,fo); rewrite(output);
  doc;
  xuli;
  //close(input); close(output);
END.


uses math;
const fi='knapsack2.inp';
      fo='knapsack2.out';
      maxn=100;
      maxv=200000;
var n,w:int32;
    maxval:uint64;
    we,v:array[1..maxn] of uint32;
    f:array[0..maxn,0..maxv] of uint64;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,w); maxval:=0;
   for i:=1 to n do
   begin
     readln(we[i],v[i]);
     maxval:=maxval+v[i];
   end;
end;
//=============================
function min(x,y:int64):int64;
begin
   if x>y then exit(y)
   else exit(x);
end;
//=============================
procedure xuli;
var i,j:int32;
    ans:uint64;
begin
   for i:=0 to n do
    for j:=0 to maxval do f[i,j]:=maxlongint;
   f[0,0]:=0;
   for i:=0 to n do
    for j:=0 to maxval do
     if f[i,j]<>maxlongint then
      begin
         f[i+1,j]:=min(f[i+1,j],f[i,j]);
         if f[i,j]+we[i+1]<=w then
         f[i+1,j+v[i+1]]:=min(f[i+1,j+v[i+1]],f[i,j]+we[i+1]);
      end;
   for i:=maxval downto 1 do
    if f[n,i]<=w then break;
   write(i);
end;
//=============================
begin
   assign(input,fi); reset(input);
   assign(output,fo); rewrite(output);
   nhap;
   xuli;
   close(input); close(output);
end.


uses math;
const fi='knapsack2.inp';
      fo='knapsack2.out';
      maxn=100;
      maxv=100000;
var n,w,maxval:int32;
    we,v:array[1..maxn] of uint32;
    f:array[0..maxn,0..maxv] of uint64;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,w); maxval:=0;
   for i:=1 to n do
   begin
     readln(we[i],v[i]);
     maxval:=maxval+v[i];
   end;
end;
//=============================
function min(x,y:int32):int32;
begin
   if x>y then exit(y)
   else exit(x);
end;
//=============================
procedure xuli;
var i,j,ans:int32;
begin
   for i:=0 to n do
    for j:=0 to maxval do f[i,j]:=maxlongint;
   f[0,0]:=0;
   for i:=0 to n do
    for j:=0 to maxval do
     if f[i,j]<>maxlongint then
      begin
         f[i+1,j]:=min(f[i+1,j],f[i,j]);
         if f[i,j]+we[i+1]<=w then
         f[i+1,j+v[i+1]]:=min(f[i+1,j+v[i+1]],f[i,j]+we[i+1]);
      end;
   for i:=maxval downto 1 do
    if f[n,i]<=w then break;
   write(i);
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


uses math;
const fi='knapsack2.inp';
      fo='knapsack2.out';
      maxn=100;
      maxv=100000;
var n,w,maxval:int32;
    we,v:array[1..maxn] of uint32;
    f:array[0..maxn,0..maxv] of uint64;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,w); maxval:=0;
   for i:=1 to n do
   begin
     readln(we[i],v[i]);
     maxval:=maxval+v[i];
   end;
end;
//=============================
function min(x,y:int32):int32;
begin
   if x>y then exit(y)
   else exit(x);
end;
//=============================
procedure xuli;
var i,j,ans:int32;
begin
   for i:=0 to n do
    for j:=0 to maxval do f[i,j]:=maxlongint;
   f[0,0]:=0;
   for i:=0 to n do
    for j:=0 to maxval do
     if f[i,j]<>maxlongint then
      begin
         f[i+1,j]:=min(f[i+1,j],f[i,j]);
         if j+we[i]<=w then
         f[i+1,j+v[i+1]]:=min(f[i+1,j+v[i+1]],f[i,j]+we[i+1]);
      end;
   for i:=maxval downto 1 do
    if f[n,i]<=w then break;
   write(i);
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


uses math;
const fi='knapsack2.inp';
      fo='knapsack2.out';
      maxn=100;
      maxv=100000;
var n,w,maxval:int32;
    we,v:array[1..maxn] of uint32;
    f:array[0..maxn,0..maxv] of uint64;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,w); maxval:=0;
   for i:=1 to n do
   begin
     readln(we[i],v[i]);
     maxval:=maxval+v[i];
   end;
end;
//=============================
function min(x,y:int32):int32;
begin
   if x>y then exit(y)
   else exit(x);
end;
//=============================
procedure xuli;
var i,j,ans:int32;
begin
   for i:=0 to n do
    for j:=0 to maxval do f[i,j]:=maxlongint;
   f[0,0]:=0;
   for i:=0 to n do
    for j:=0 to maxval do
     if f[i,j]<>maxlongint then
      begin
         f[i+1,j]:=min(f[i+1,j],f[i,j]);
         f[i+1,j+v[i+1]]:=min(f[i+1,j+v[i+1]],f[i,j]+we[i+1]);
      end;
   for i:=maxval downto 1 do
    if f[n,i]<=w then break;
   write(i);
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


 var F:array[0..100000] of int64;
     W,V:array[0..100000] of longint;
     i,n,m,t:longint;  f1:text; max:int64;

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

 begin
  assign(f1,''); reset(F1);
  readln(f1,n,m);
  max:=low(int64);
  for i:=1 to n do readln(f1,W[i],V[i]);
  for i:=1 to 100000 do F[i]:=100000000000;
  for i:=1 to n do
   for t:=100000 downto v[i] do F[t]:=min(F[t],F[t-v[i]]+w[i]);
  for i:=1 to 100000 do
   if F[i]<=m then if max<i then max:=i;
  write(max);
  readln;
  close(f1);
 end.


Program LongestPath;
uses math;
type Rec=record
     x:longint;
     y:longint;
     end;
var Head,Adj,F :array[0..100005] of longint;
    E:array[1..100005] of Rec;
    Avai:array[1..100005] of boolean;
    n,m,res,i,maxx:longint;
    fi,fo:text;
//------------------------------------------
procedure DFS(i:longint);
var j:longint;
begin
//Avai[i]:=false;
for j:=Head[i]+1 to Head[i+1] do begin
                                                      inc(res);
                                                      maxx:=max(maxx,res);
                                                      DFS(adj[j]);
                                                      dec(res);

                                 end;
end;
//------------------------------------------
begin
//assign(fi,'LongestPath.inp');reset(fi);
readln(n,m);
for i:=1 to m do begin
                 With E[i] do begin
                              readln(x,y);
                              inc(Head[x]);
                              end;
                 end;
for i:=1 to n do begin
                 Head[i]:=Head[i]+Head[i-1];
                 end;
for i:=m downto 1 do begin
                     With E[i] do begin
                                  Adj[Head[x]]:=y;
                                  dec(Head[x]);
                                  end;
                     end;
Head[n+1]:=m;
//fillchar(Avai,sizeof(Avai),true);
for i:=1 to n do begin
              //   if Avai[i] then begin
                 DFS(i);
                 end;
Writeln(maxx);
//readln;
end.

Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

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

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

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

Sample Output 3

3

The red directed path in the following figure is one of the longest:


uses math;
const max=1000000007;
var
        f: text;
        x: array [0..1000,0..1000] of char;
        e: array [0..1001] of longint;
        a,b,c,i,j: longint;

begin

        readln( a,b);
        for i:=1 to a do
        begin
        for j:=1 to b do read (x[i,j]);
        readln;
        end;

        e[1]:=1;

        for i:=1 to a do
        for j:=1 to b do
        if x[i,j] = '#' then e[j]:= 0 else e[j]:= (e[j-1] + e[j] ) mod max ;

        write (e[b]);
        readln;

end.

Score : 100 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns.
Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.

For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}.
If a_{i, j} is ., Square (i, j) is an empty square; if a_{i, j} is #, Square (i, j) is a wall square.
It is guaranteed that Squares (1, 1) and (H, W) are empty squares.

Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.

Find the number of Taro's paths from Square (1, 1) to (H, W).
As the answer can be extremely large, find the count modulo 10^9 + 7.

Constraints

H and W are integers.

2 \leq H, W \leq 1000

a_{i, j} is . or #.

Squares (1, 1) and (H, W) are empty squares.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}\ldotsa_{1, W}
:
a_{H, 1}\ldotsa_{H, W}

Output

Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.

Sample Input 1

3 4
...#
.#..
....

Sample Output 1

3

There are three paths as follows:

Sample Input 2

5 2
..
#.
..
.#
..

Sample Output 2

0

There may be no paths.

Sample Input 3

5 5
..#..
.....
#...#
.....
..#..

Sample Output 3

24

Sample Input 4

20 20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

Sample Output 4

345263555

Be sure to print the count modulo 10^9 + 7.


Program Grid1;
const max=1000000007;
var A:array[0..105,0..105] of char;
    L:array[0..105,0..105] of int64;
    //Qx,Qy:array[1..10000000] of longint;
    i,f,r,n,m,j,x,y,u,v :longint;
    fi,fo:text;
//------------------------------------------
begin
//assign(Fi,'Grid1.inp');reset(fi);
readln(n,m);
for i:=1 to n do begin
                 for j:=1 to m do begin
                                  Read(A[i,j]);
                                  end;
                 readln(fi);
                 end;
L[1,1]:=1;
for i:=1 to n do begin
                 for j:=1 to m do begin
                                  if A[i,j]='.' then L[i,j]:=(L[i,j]+L[i-1,j]+L[i,j-1]) mod max;
                                  end;
                 end;
Writeln(L[n,m]);
//readln;
end.


const
  base = 1000000007;
var
  a,f:array[0..1010,0..1010] of int64;
  n,m,j,i:longint;
  s : ansistring;
BEGIN
  ///assign(input,'a.in'); reset(input);
  readln(n,m);
  for i:=1 to n do begin
    readln(s);
    for j:=1 to m do if s[j]='.' then a[i,j]:=1 else a[i,j]:=0;
  end;

  fillchar(f,sizeof(f),0);
  f[1,1] := 1;
  for i:=1 to n do
    for j:=1 to m do
      if a[i,j]=1 then f[i,j] := (f[i,j] + f[i-1,j] + f[i,j-1]) mod base;
  writeln(f[n,m]);
END.


uses crt;
var
       // f: text;
        x,cs: array [0..100000] of longint;
        i,j,n,k,cr: longint;

begin
      //  assign (f, fi); reset(f);
        readln ( n,k);
        for i:=1 to n do read ( x[i]);

        for i:=0 to k do cs[i]:=2;

        for i:=x[1] to k do
        for j:=1 to n do
        begin
        if i - x[j] >= 0 then
        begin
            cr:= i - x[j];
            if cs[cr] = 2 then cs[i]:= 1;
        end;

        end;

        If cs[k] = 2 then write ('Second') else write ('First');
        readln;
end.

Score : 100 points

Problem Statement

There is a set A = \{ a_1, a_2, \ldots, a_N \} consisting of N positive integers.
Taro and Jiro will play the following game against each other.

Initially, we have a pile consisting of K stones.
The two players perform the following operation alternately, starting from Taro:

Choose an element x in A, and remove exactly x stones from the pile.

A player loses when he becomes unable to play.
Assuming that both players play optimally, determine the winner.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 10^5

1 \leq a_1 < a_2 < \cdots < a_N \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

If Taro will win, print First; if Jiro will win, print Second.

Sample Input 1

2 4
2 3

Sample Output 1

First

If Taro removes three stones, Jiro cannot make a move.
Thus, Taro wins.

Sample Input 2

2 5
2 3

Sample Output 2

Second

Whatever Taro does in his operation, Jiro wins, as follows:

If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.

If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.

Sample Input 3

2 7
2 3

Sample Output 3

First

Taro should remove two stones. Then, whatever Jiro does in his operation, Taro wins, as follows:

If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.

If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.

Sample Input 4

3 20
1 2 3

Sample Output 4

Second

Sample Input 5

3 21
1 2 3

Sample Output 5

First

Sample Input 6

1 100000
1

Sample Output 6

Second


const fi='STONE.INP';
      fo='STONE.OUT';
      maxn=100;
      maxk=trunc(1e5);

var n,k:longint;
    a:array[1..maxn] of longint;
    f:array[0..maxk] of 0..1; {f=0-> thua, f=1->thang}
//============================
procedure doc;
var i:longint;
begin
  readln(n,k);

  for i:=1 to n do read(a[i]);
end;
//============================
procedure xuli;
var i,j:longint;
    ok:boolean;
begin
  fillchar(f,sizeof(f),0);

  for i:=1 to k do
    begin
      if (i<a[1]) then begin f[i]:=0; continue; end;

      ok:=false;

      for j:=1 to n do
        begin
          if (i>=a[j]) then
            begin
            if (f[i-a[j]]=1) then continue;
            ok:=true; break;
            end;
        end;

      if ok then f[i]:=1 else f[i]:=0;
    end;

  if f[k]=1 then write('First')
  else write('Second');
end;
//============================
BEGIN
  //assign(input,fi); reset(input);
  //assign(output,fo); rewrite(output);
  doc;
  xuli;
  //close(input); close(output);
END.


const fi='STONE.INP';
      fo='STONE.OUT';
      maxn=100;
      maxk=trunc(1e5);

var n,k:longint;
    a:array[1..maxn] of longint;
    f:array[0..maxk] of 0..1; {f=0-> thua, f=1->thang}
//============================
procedure doc;
var i:longint;
begin
  readln(n,k);

  for i:=1 to n do read(a[i]);
end;
//============================
procedure xuli;
var i,j:longint;
    ok:boolean;
begin
  fillchar(f,sizeof(f),0);

  for i:=1 to k do
    begin
      if (i<a[1]) then begin f[i]:=0; continue; end;

      ok:=true;

      for j:=1 to n do
        begin
          if (i>=a[j]) then
            begin
            if (f[i-a[j]]=0) then continue;
            ok:=false; break;
            end;
        end;

      if ok then f[i]:=1 else f[i]:=0;
    end;

  if f[k]=1 then write('First')
  else write('Second');
end;
//============================
BEGIN
  //assign(input,fi); reset(input);
  //assign(output,fo); rewrite(output);
  doc;
  xuli;
  //close(input); close(output);
END.


uses math;
const fi='kstones.inp';
      fo='kstones.out';
      maxn=100;
var n,k,mmin:int32;
    a:array[1..maxn] of int32;
    f:array[0..100000] of boolean;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,k);
   mmin:=maxlongint;
   for i:=1 to n do
   begin read(a[i]); mmin:=min(mmin,a[i]); end;
end;
//=============================
procedure xuli;
var i,j:int32;
    ok:boolean;
begin
   fillchar(f,sizeof(f),false);
   for i:=mmin to k do
   begin
    ok:=false;
    for j:=1 to n do
    if i>=a[j] then
    if not f[i-a[j]] then
     begin
        ok:=true; break;
     end;
    f[i]:=ok;
   end;
   if f[k] then write('First') else write('Second');
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


uses math;
{
const
        fi = 'test.inp';
        fo = 'test.out';
}
var
//        f: Text;
        a,b: array [0..4000] of int64;
        dp: array [0..4000, 0..4000] of int64;
        i,j,n: longint;
        x,y: int64;

begin
//        assign (f, fi); reset (F);
        readln (n);
        for i:= 1 to n do
        begin
                read ( a[i]);

        end;
//        close (f);

  //      assign (F, fo); rewrite (f);

        for i:=n downto 1 do
        for j:=i to n do
        begin
                if i = j then dp[i,j]:= a[i]
                else
                dp[i,j]:= max( a[i] - dp[i+1,j], a[j] - dp[i,j-1]);
        end;

        write ( dp[1,n]);
        readln;
    //    close (f);
end.

Score : 100 points

Problem Statement

Taro and Jiro will play the following game against each other.

Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N).
Until a becomes empty, the two players perform the following operation alternately, starting from Taro:

Remove the element at the beginning or the end of a. The player earns x points, where x is the removed element.

Let X and Y be Taro's and Jiro's total score at the end of the game, respectively.
Taro tries to maximize X - Y, while Jiro tries to minimize X - Y.

Assuming that the two players play optimally, find the resulting value of X - Y.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the resulting value of X - Y, assuming that the two players play optimally.

Sample Input 1

4
10 80 90 30

Sample Output 1

10

The game proceeds as follows when the two players play optimally (the element being removed is written bold):

Taro: (10, 80, 90, 30) → (10, 80, 90)

Jiro: (10, 80, 90) → (10, 80)

Taro: (10, 80) → (10)

Jiro: (10) → ()

Here, X = 30 + 80 = 110 and Y = 90 + 10 = 100.

Sample Input 2

3
10 100 10

Sample Output 2

-80

The game proceeds, for example, as follows when the two players play optimally:

Taro: (10, 100, 10) → (100, 10)

Jiro: (100, 10) → (10)

Taro: (10) → ()

Here, X = 10 + 10 = 20 and Y = 100.

Sample Input 3

1
10

Sample Output 3

10

Sample Input 4

10
1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1

Sample Output 4

4999999995

The answer may not fit into a 32-bit integer type.

Sample Input 5

6
4 2 9 7 1 5

Sample Output 5

2

The game proceeds, for example, as follows when the two players play optimally:

Taro: (4, 2, 9, 7, 1, 5) → (4, 2, 9, 7, 1)

Jiro: (4, 2, 9, 7, 1) → (2, 9, 7, 1)

Taro: (2, 9, 7, 1) → (2, 9, 7)

Jiro: (2, 9, 7) → (2, 9)

Taro: (2, 9) → (2)

Jiro: (2) → ()

Here, X = 5 + 1 + 9 = 15 and Y = 4 + 7 + 2 = 13.


uses crt;
{
const
        fi = 'test.inp';
        fo = 'test.out';
}
var
        f: Text;
        a,b: array [0..4000] of int64;
//        dp: array [0..1000, 0..1000] of longint;
        i,j,n: longint;
        x,y: int64;

begin
//        assign (f, fi); reset (F);
        readln (n);
        for i:= 1 to n do
        begin
                read ( a[i]);

        end;
    //    close (f);

      //  assign (F, fo); rewrite (f);
        j:=0;
        for i:=1 to n do
        begin
                inc(j);
                b[j]:= a[i];

                if  ( j - 3 >= 0 ) then

                if ( b[j-1] > b[j] ) and (b[j-1] > b[j-2]) then
                begin
                        b[j-2]:= b[j-2] +b[j] - b[j-1];
                        j:= j-2;
                end;

        end;

        i:=1;

        while i <= j do
        begin
                if b[i] > b[j] then
                begin
                        x:= x + b[i]; inc(i);
                end else
                begin
                        x:= x + b[j]; dec(j);
                end;

                if i > j then break;
                if b[i] > b[j] then
                begin
                        y:= y + b[i]; inc(i);
                end else
                begin
                        y:= y + b[j]; dec(j);
                end;
        end;

     //   for i:=1 to n do write (f, b[i], ' ');   writeln (f);
        write (x- y);
        readln;
     //   close (f);
end.


uses math;
const fi='DEQUE.INP';
      fo='DEQUE.OUT';
      maxn=3000;

var n:longint;
    a:array[1..maxn] of longint;
    s:array[0..maxn] of int64;
    f:array[1..maxn,1..maxn] of int64;
//============================
procedure doc;
var i:longint;
begin
  readln(n);

  for i:=1 to n do read(a[i]);

end;
//============================
procedure tinhs;
var i:longint;
begin
  s[0]:=0;
  s[1]:=a[1];
  for i:=2 to n do s[i]:=s[i-1]+a[i];
end;
//============================
function sum(i,j:longint):int64;
begin
  sum:=s[j]-s[i-1];
end;
//============================
procedure xuli;
var l,r,i:longint;
begin
  tinhs;

  for i:=1 to n do f[i,i]:=a[i];

  for l:=1 to n-1 do
    for r:=l+1 to n do
      f[l,r]:=max(sum(l,r)-f[l+1,r],sum(l,r)-f[l,r-1]);

  write(f[1,n]-(sum(1,n)-f[1,n]));
end;
//============================
BEGIN
  //assign(input,fi); reset(input);
  //assign(output,fo); rewrite(output);
  doc;
  xuli;
  //close(input); close(output);
END.


program LDeque;

uses math;

const
  fi='LDeque.inp';
  fo='LDeque.out';

var
  n: longint;
  a:array[0..3000] of int64;

procedure nhap;
var i: longint;
begin

  readln(n);
  for i:=1 to n do read(a[i]);

end;

procedure xuli;
var i,j: longint; k:boolean; ans:int64;
begin

  i:=1; j:=n; k:=true;
  ans:=0;
  repeat
    if a[i]>a[j] then begin
      ans:=ifthen(k,ans+a[i],ans-a[j]);
      inc(i);
    end else begin
      ans:=ifthen(k,ans+a[j],ans-a[j]);
      dec(j);
    end;
    k:=not(k);
  until i>j;

  writeln(ans);

end;

begin

  //assign(input,fi);     reseT(input);
  //assign(output,fo);    rewrite(output);

  nhap;
  xuli;

  //close(input);         close(output);

end.


var a: array[1..1000] of longint;
    dp, p: array[-10..150, -110000..200000] of longint;
    i, j, N, K: longint;
begin
    readln(N, K);
    for i:=1 to N do
        read(a[i]);
    for i:=0 to a[1] do
    begin
        dp[1,i]:=1;
    end;
    for i:=0 to K do
        p[1,i]:=p[1,i-1]+dp[1,i];
    for i:=2 to N do
    begin
        for j:=0 to K do
        begin
            dp[i,j]:=(p[i-1,j]-p[i-1,j-a[i]-1]) mod 1000000007;
            if dp[i,j]<0 then
                dp[i,j]:=dp[i,j]+1000000007;
            p[i,j]:=(p[i,j-1]+dp[i,j]) mod 1000000007;
//            write(dp[i,j], ' ');
        end;
//        writeln;
    end;
    writeln(dp[N,K]);
end.

Score : 100 points

Problem Statement

There are N children, numbered 1, 2, \ldots, N.

They have decided to share K candies among themselves.
Here, for each i (1 \leq i \leq N), Child i must receive between 0 and a_i candies (inclusive).
Also, no candies should be left over.

Find the number of ways for them to share candies, modulo 10^9 + 7.
Here, two ways are said to be different when there exists a child who receives a different number of candies.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq K \leq 10^5

0 \leq a_i \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

Print the number of ways for the children to share candies, modulo 10^9 + 7.

Sample Input 1

3 4
1 2 3

Sample Output 1

5

There are five ways for the children to share candies, as follows:

(0, 1, 3)

(0, 2, 2)

(1, 0, 3)

(1, 1, 2)

(1, 2, 1)

Here, in each sequence, the i-th element represents the number of candies that Child i receives.

Sample Input 2

1 10
9

Sample Output 2

0

There may be no ways for the children to share candies.

Sample Input 3

2 0
0 0

Sample Output 3

1

There is one way for the children to share candies, as follows:

(0, 0)

Sample Input 4

4 100000
100000 100000 100000 100000

Sample Output 4

665683269

Be sure to print the answer modulo 10^9 + 7.


var a: array[1..1000] of longint;
    dp, p: array[-10..150, -110000..200000] of longint;
    i, j, N, K: longint;
begin
    readln(N, K);
    for i:=1 to N do
        read(a[i]);
    for i:=0 to a[1] do
    begin
        dp[1,i]:=1;
    end;
    for i:=0 to K do
        p[1,i]:=p[1,i-1]+dp[1,i];
    for i:=2 to N do
    begin
        for j:=0 to K do
        begin
            dp[i,j]:=(p[i-1,j]-p[i-1,j-a[i]-1]) mod 1000000007;
            p[i,j]:=(p[i,j-1]+dp[i,j]) mod 1000000007;
//            write(dp[i,j], ' ');
        end;
//        writeln;
    end;
    writeln(dp[N,K]);
end.


var
  n,i,j:longint;
  flag:boolean;
  l,s,loc,dis:int64;
  a:array[1..1000000]of longint;
begin
  readln(l,n);
  for i:=1 to n do read(a[i]);
  i:=1;j:=n;
  if a[1]>=(l-a[n]+1)
  then
    begin
	  loc:=1;
	  dis:=a[1];
	  i:=2;
	  flag:=true;
	end
  else
    begin
      j:=j-1;
	  loc:=n;
	  dis:=l-a[n]+1;
	  flag:=false;
	end;
  //write(dis,' ');
  while s<n-1 do
  begin
    inc(s);
	if flag
	then
	  if (a[i]-a[loc])>=(a[loc]+l-a[j])
	    then
		  begin
			dis:=dis+a[i]-a[loc];
			flag:=true;
			loc:=i;
			inc(i);
			//write(dis,' ');
	      end
		  else
		    begin
			  dis:=dis+a[loc]+l-a[j];
			  flag:=false;
			  loc:=j;
			  dec(j);
			  //write(dis,' ');
			end
		else
		  if (a[i]+l-a[loc])>=(a[loc]-a[j])
		  then
			begin
			  dis:=dis+a[i]+l-a[loc];
			  flag:=true;
			  loc:=i;
			  inc(i);
			  //write(dis,' ');
	        end
		  else
		    begin
			  dis:=dis+l-a[j];
			  flag:=false;
			  loc:=j;
			  dec(j);
			  //write(dis,' ');
			end;
  end;
  writeln(dis-1);
  readln(i);
end.

Score : 800 points

Problem Statement

Takahashi Lake has a perimeter of L. On the circumference of the lake, there is a residence of the lake's owner, Takahashi.
Each point on the circumference of the lake has a coordinate between 0 and L (including 0 but not L), which is the distance from the Takahashi's residence, measured counter-clockwise.

There are N trees around the lake; the coordinate of the i-th tree is X_i. There is no tree at coordinate 0, the location of Takahashi's residence.

Starting at his residence, Takahashi will repeat the following action:

If all trees are burnt, terminate the process.

Specify a direction: clockwise or counter-clockwise.

Walk around the lake in the specified direction, until the coordinate of a tree that is not yet burnt is reached for the first time.

When the coordinate with the tree is reached, burn that tree, stay at the position and go back to the first step.

Find the longest possible total distance Takahashi walks during the process.

Partial Score

A partial score can be obtained in this problem:

300 points will be awarded for passing the input satisfying N \leq 2000.

Constraints

2 \leq L \leq 10^9

1 \leq N \leq 2\times 10^5

1 \leq X_1 < ... < X_N \leq L-1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L N
X_1
:
X_N

Output

Print the longest possible total distance Takahashi walks during the process.

Sample Input 1

10 3
2
7
9

Sample Output 1

15

Takahashi walks the distance of 15 if the process goes as follows:

Walk a distance of 2 counter-clockwise, burn the tree at the coordinate 2 and stay there.

Walk a distance of 5 counter-clockwise, burn the tree at the coordinate 7 and stay there.

Walk a distance of 8 clockwise, burn the tree at the coordinate 9 and stay there.

Sample Input 2

10 6
1
2
3
6
7
9

Sample Output 2

27

Sample Input 3

314159265 7
21662711
77271666
89022761
156626166
160332356
166902656
298992265

Sample Output 3

1204124749


var
  n,i,l,s,loc,dis,j:longint;
  flag:boolean;
  a:array[1..1000000]of longint;
begin
  readln(l,n);
  for i:=1 to n do read(a[i]);
  i:=1;j:=n;
  if a[1]>=(l-a[n]+1)
  then
    begin
	  loc:=1;
	  dis:=a[1];
	  i:=2;
	  flag:=true;
	end
  else
    begin
      j:=j-1;
	  loc:=n;
	  dis:=l-a[n]+1;
	  flag:=false;
	end;
  while s<n-1 do
  begin
    inc(s);
	if flag
	then
	  if (a[i]-a[loc])>=(a[loc]+l-a[j])
	    then
		  begin
			dis:=dis+a[i]-a[loc];
			flag:=true;
			loc:=i;
			inc(i);
	      end
		  else
		    begin
			  dis:=dis+a[loc]+l-a[j];
			  flag:=false;
			  loc:=j;
			  dec(j);
			end
		else
		  if (a[i]+l-a[loc])>=(a[loc]-a[j])
		  then
			begin
			  dis:=dis+a[i]+l-a[loc];
			  flag:=true;
			  loc:=i;
			  inc(i);
	        end
		  else
		    begin
			  dis:=dis+l-a[j];
			  flag:=false;
			  loc:=j;
			  dec(j);
			end;
  end;
  writeln(dis);
  readln(i);
end.


var
	n,cnt,i,a,b,c,d:Longint;
begin
	read(n,a,b);
	for i:=1 to n do begin
		read(c,d);
		if(a<=c)and(b<=d)then inc(cnt);
	end;
	writeln(cnt);
end.

Score : 200 points

Problem Statement

There are N rectangular plate materials made of special metal called AtCoder Alloy.
The dimensions of the i-th material are A_i \times B_i (A_i vertically and B_i horizontally).

Takahashi wants a rectangular plate made of AtCoder Alloy whose dimensions are exactly H \times W.
He is trying to obtain such a plate by choosing one of the N materials and cutting it if necessary.
When cutting a material, the cuts must be parallel to one of the sides of the material.
Also, the materials have fixed directions and cannot be rotated.
For example, a 5 \times 3 material cannot be used as a 3 \times 5 plate.

Out of the N materials, how many can produce an H \times W plate if properly cut?

Constraints

1 \leq N \leq 1000

1 \leq H \leq 10^9

1 \leq W \leq 10^9

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N H W
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

3 5 2
10 3
5 2
2 5

Sample Output 1

2

Takahashi wants a 5 \times 2 plate.

The dimensions of the first material are 10 \times 3. We can obtain a 5 \times 2 plate by properly cutting it.

The dimensions of the second material are 5 \times 2. We can obtain a 5 \times 2 plate without cutting it.

The dimensions of the third material are 2 \times 5. We cannot obtain a 5 \times 2 plate, whatever cuts are made. Note that the material cannot be rotated and used as a 5 \times 2 plate.

Sample Input 2

10 587586158 185430194
894597290 708587790
680395892 306946994
590262034 785368612
922328576 106880540
847058850 326169610
936315062 193149191
702035777 223363392
11672949 146832978
779291680 334178158
615808191 701464268

Sample Output 2

8


var
  a:array[1..1000,1..1000]of longint;
  n,k,i,m,j,t,ans,b,c,d:longint;
  p,q,o,x,y:real;
  s:string;
function max(a,b:longint):longint;
begin
  if a>b then max:=b
  else max:=a;
end;
begin
  readln(c,n,m);
  for i:=1 to c do
  begin
    readln(a[i,1],a[i,2]);
    ans:=ans+max(a[i,1] div n,a[i,2] div m);
  end;
  writeln(ans);
end.


var
	n,h,w,i,x,y:longint;
    ans:int64;
begin
	readln(n,h,w);
    ans:=0;
    for i:=1 to n do
    begin
    	read(x,y);
        if (x>=h) and (y>=w) then inc(ans);
    end;
    writeln(ans);
end.


var
	f:boolean;
	n,i,x:longint;
begin
	read(n);
    f:=false;
    for i:=1 to n do
    begin
    	read(x);
        f:=f or (x mod 2=1);
    end;
    if f then writeln('first')
    else writeln('second');
end.

Score : 500 points

Problem Statement

There is an apple tree that bears apples of N colors. The N colors of these apples are numbered 1 to N, and there are a_i apples of Color i.

You and Lunlun the dachshund alternately perform the following operation (starting from you):

Choose one or more apples from the tree and eat them. Here, the apples chosen at the same time must all have different colors.

The one who eats the last apple from the tree will be declared winner. If both you and Lunlun play optimally, which will win?

Constraints

1 \leq N \leq 10^5

1 \leq a_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

If you will win, print first; if Lunlun will win, print second.

Sample Input 1

2
1
2

Sample Output 1

first

Let Color 1 be red, and Color 2 be blue. In this case, the tree bears one red apple and two blue apples.

You should eat the red apple in your first turn. Lunlun is then forced to eat one of the blue apples, and you can win by eating the other in your next turn.

Note that you are also allowed to eat two apples in your first turn, one red and one blue (not a winning move, though).

Sample Input 2

3
100000
30000
20000

Sample Output 2

second


var
	S:String;
	u,v:int64;
	i:Longint;
begin
	readln(S);
	for i:=1 to length(S) do begin
		if S[i]='B'then inc(u)else inc(v,u);
	end;
	writeln(v);
end.

Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

1 \leq |S| \leq 2\times 10^5

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6


var
  i,j,n:longint;
  ans:qword;
  s:ansistring;
begin
  read(s);
  n:=length(s);
  for i:=1 to n do
    begin
      if s[i]='W' then
        begin
          inc(j);
          ans:=ans+(i-j);
        end;
    end;
  write(ans);
end.


var
  i,j,n:longint;
  ans:qword;
  s:string;
begin
  read(s);
  n:=length(s);
  for i:=1 to n do
    begin
      if s[i]='W' then
        begin
          inc(j);
          ans:=ans+(i-j);
        end;
    end;
  write(ans);
end.


var
	a:array[0..35] of int64;
	b:array[0..200010] of longint;
	f:array[0..200010] of boolean;
	ans,i,j,n,x,y,z,l,r,mid:longint;
function check(x:longint):longint;
begin
	if x<b[1] then exit(-1);
	if x>b[n] then exit(-1);
	l:=1; r:=n;
	while l<=r do
	begin
		mid:=(l+r) div 2;
		if x=b[mid] then exit(mid);
		if x<b[mid] then r:=mid
		else l:=mid+1;
	end;
	exit(-1);
end;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=b[(l+r) div 2];
	repeat
		while b[i]<mid do inc(i);
		while b[j]>mid do dec(j);
		if i<=j then
		begin
			t:=b[i]; b[i]:=b[j]; b[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	readln(n);
	for i:=1 to n do read(b[i]);
	a[1]:=2; ans:=0;
	for i:=2 to 31 do a[i]:=a[i-1]*2;
	qsort(1,n);
	fillchar(f,sizeof(f),true);
	y:=1; z:=1;
	for i:=1 to n-1 do
	begin
		while a[y]<b[i]+b[n] do inc(y);
		while a[z]<b[i] do inc(z);
		if f[i] then
			for j:=y downto z do
			begin
				x:=check(a[j]-b[i]);
				if (x<>-1) and ((x<>i) or (x=i) and (a[x-1]=a[i]) or (a[x+1]=a[i])) then
				begin
					inc(ans);
					f[i]:=false;
					f[x]:=false;
					break;
				end;
			end;
	end;
	writeln(ans);
end.

Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

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 maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2


var
	a:array[0..35] of int64;
	b:array[0..200010] of longint;
	f:array[0..200010] of boolean;
	ans,i,j,n,x,l,r,mid:longint;
function check(x:longint):longint;
begin
	if x<b[1] then exit(-1);
	if x>b[n] then exit(-1);
	l:=1; r:=n;
	while l<=r do
	begin
		mid:=(l+r) div 2;
		if x=b[mid] then exit(mid);
		if x<b[mid] then r:=mid
		else l:=mid+1;
	end;
	exit(-1);
end;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=b[(l+r) div 2];
	repeat
		while b[i]<mid do inc(i);
		while b[j]>mid do dec(j);
		if i<=j then
		begin
			t:=b[i]; b[i]:=b[j]; b[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	readln(n);
	for i:=1 to n do read(b[i]);
	a[1]:=2; ans:=0;
	for i:=2 to 31 do a[i]:=a[i-1]*2;
	qsort(1,n);
	fillchar(f,sizeof(f),true);
	for i:=1 to n-1 do
		if f[i] then
			for j:=31 downto 1 do
			begin
				if a[j]>b[i]+b[n] then continue;
				x:=check(a[j]-b[i]);
				if (x<>-1) and (x<>i) then
				begin
					inc(ans);
					f[i]:=false;
					f[x]:=false;
					break;
				end;
				if a[j]<=b[i] then break;
			end;
	writeln(ans);
end.


var
	a,b,c,ch:array[0..200010] of longint;
	n,i,l,r,mid,y,k:longint;
	j:char;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=b[(l+r) div 2];
	repeat
		while b[i]<mid do inc(i);
		while b[j]>mid do dec(j);
		if i<=j then
		begin
			t:=b[i]; b[i]:=b[j]; b[j]:=t;
			t:=c[i]; c[i]:=c[j]; c[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
function check(x:longint):boolean;
begin
	for i:=1 to n do ch[i]:=0;
	for i:=1 to n do
	begin
		y:=a[i];
		inc(ch[y]);
		for k:=y+1 to n do ch[k]:=0;
		while (ch[y]>x) and (y>1) do
		begin
			ch[y]:=ch[y]-x;
			dec(y);
			inc(ch[y]);
		end;
		if ch[1]>x then exit(false);
	end;
	exit(true);
end;
begin
	readln(n);
	for i:=1 to n do
	begin
		read(a[i]);
		c[i]:=i;
		b[i]:=a[i];
	end;
	qsort(1,n);
	for i:=1 to n do a[c[i]]:=i;
	l:=1; r:=26;
	while l<r do
	begin
		mid:=(l+r) div 2;
		if check(mid) then r:=mid
		else l:=mid+1;
	end;
	writeln(r+1);
end.

Score : 700 points

Problem Statement

There are N strings arranged in a row.
It is known that, for any two adjacent strings, the string to the left is lexicographically smaller than the string to the right.
That is, S_1<S_2<...<S_N holds lexicographically, where S_i is the i-th string from the left.

At least how many different characters are contained in S_1,S_2,...,S_N, if the length of S_i is known to be A_i?

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Note

The strings do not necessarily consist of English alphabet; there can be arbitrarily many different characters (and the lexicographic order is defined for those characters).

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 different characters contained in the strings.

Sample Input 1

3
3 2 1

Sample Output 1

2

The number of different characters contained in S_1,S_2,...,S_N would be 3 when, for example, S_1=abc, S_2=bb and S_3=c.

However, if we choose the strings properly, the number of different characters can be 2.

Sample Input 2

5
2 3 2 1 2

Sample Output 2

2


var n,k,i,min:longint;
h:array[1..100200]of longint;
procedure qsort(l,r:longint);
var i,j,k,mid:longint;
begin
	i:=l;j:=r;
	mid:=h[(i+j) div 2];
	repeat
		while h[i]<mid do inc(i);
		while h[j]>mid do dec(j);
		if i<=j then
		begin
			k:=h[i];h[i]:=h[j];h[j]:=k;
			inc(i);
			dec(j);
		end;
	until i>j;
	if i<r then qsort(i,r);
	if j>l then qsort(l,j);
end;
begin
	read(n,k);
	min:=maxlongint;
	for i:=1 to n do read(h[i]);
	qsort(1,n);
	for i:=1 to n-k+1 do
		if h[i+k-1]-h[i]<min then min:=h[i+k-1]-h[i];
	write(min);
end.

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).


var
  a:array[1..100000] of longint;
  i,n,j,m,k:longint;
  ans:qword;
procedure qsort(l,r:longint);
var
  i,j,mid,p:longint;
begin
  i:=l;j:=r;
  mid:=a[(l+r)shr 1];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n,k);
  for i:=1 to n do readln(a[i]);
  qsort(1,n);
  ans:=maxlongint;
  for i:=1 to n-k+1 do if ans>(a[i+k-1]-a[i]) then ans:=a[i+k-1]-a[i];
  writeln(ans);
end.


var
	n:Longint;
function f(x:int64;a,b,c:Longint):Longint;
begin
	f:=0;
	if x<=n then f:=a*b*c+f(x*10+3,1,b,c)+f(x*10+5,a,1,c)+f(x*10+7,a,b,1);
end;
begin
	read(n);
	if n>700000000 then writeln(17656+f(7,0,0,1))
	else if n>500000000 then writeln(8828+f(5,0,1,0))
	else writeln(f(0,0,0,0));
end.

Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484


var
	n:Longint;
function f(x:int64;a,b,c:Longint):Longint;
begin
	f:=0;
	if x<=n then f:=a*b*c+f(x*10+3,1,b,c)+f(x*10+5,a,1,c)+f(x*10+7,a,b,1);
end;
begin
	read(n);
	if n>777777000 then begin writeln(26484);exit;end;
	writeln(f(0,0,0,0));
end.


var n : longint;
    i,sum : integer;

procedure sft(p : longint; d : integer; s,f,t : boolean);
begin
  if d = 0 then begin
    if s and f and t then if p < n then sum := sum + 1;
  end else begin
    sft(p * 10 + 3, d - 1, s, f, true);
    sft(p * 10 + 5, d - 1, s, true, t);
    sft(p * 10 + 7, d - 1, true, f, t);
  end
end;

begin
  readln(n);
  for i := 3 to 9 do begin
    sft(0, i, false, false, false);
  end;
  writeln(sum)
end.


var
	n,ans:Longint;
procedure f(x:int64;a,b,c:Boolean);
begin
	if x>n then exit;
	if a and b and c then inc(ans);
	f(x*10+3,True,b,c);
	f(x*10+5,a,True,c);
	f(x*10+7,a,b,True);
end;
begin
	read(n);
	f(0,False,False,False);
	writeln(ans);
end.


{
	75=3*25
		=3*5*5
		=5*15
	a^2 b^24
	a^2 b^4 c^4
	a^4 b^14
}
var
	i,j,k,ans,n:longint;
	s:array[0..110] of longint;
	f:array[0..110,0..110,0..110] of boolean;
procedure prime(x:longint);
begin
	j:=2;
	while x>=j do
	begin
		while x mod j=0 do
		begin
			inc(s[j]);
			x:=x div j;
		end;
		inc(j);
	end;
end;
begin
	read(n);
	fillchar(s,sizeof(s),0);
	for i:=2 to n do prime(i);
	for i:=2 to n do writeln('s[',i,']=',s[i]);
	fillchar(f,sizeof(f),true);
	ans:=0;
	for i:=2 to n do
		for j:=2 to n do
			if i<>j then
			begin
				if (s[i]>=2) and (s[j]>=24) then inc(ans);
				if (s[i]>=4) and (s[j]>=14) then inc(ans);
				if (s[i]>=2) and (s[j]>=4) then
					for k:=2 to n do
						if (i<>k) and (j<>k) then
							if s[k]>=4 then
								if f[i,j,k] then
								begin
									inc(ans);
									f[i,j,k]:=false;
									f[i,k,j]:=false;
								end;
			end;
	for i:=1 to n do
		if s[i]>=74 then inc(ans);
	writeln(ans);
end.

Score : 400 points

Problem Statement

You are given an integer N. Among the divisors of N! (= 1 \times 2 \times ... \times N), how many Shichi-Go numbers (literally "Seven-Five numbers") are there?

Here, a Shichi-Go number is a positive integer that has exactly 75 divisors.

Note

When a positive integer A divides a positive integer B, A is said to a divisor of B.
For example, 6 has four divisors: 1, 2, 3 and 6.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go numbers that are divisors of N!.

Sample Input 1

9

Sample Output 1

0

There are no Shichi-Go numbers among the divisors of 9! = 1 \times 2 \times ... \times 9 = 362880.

Sample Input 2

10

Sample Output 2

1

There is one Shichi-Go number among the divisors of 10! = 3628800: 32400.

Sample Input 3

100

Sample Output 3

543


var
  n,m,i,j,zrt:longint;
  p,y,a,b,c,o:array[0..1000000]of longint;
Procedure ch(var x,y:longint);
var
  t:longint;
begin
  t:=x;x:=y;y:=t;
end;
Procedure qs(l,r:longint);
var
  i,j,mid1,mid2:longint;
begin
  i:=l;j:=r;mid1:=p[(l+r)div 2];mid2:=y[(l+r)div 2];
  repeat
    while (p[i]<mid1)or((p[i]=mid1)and(y[i]<mid2)) do inc(i);
    while (p[j]>mid1)or((p[j]=mid1)and(y[j]>mid2)) do dec(j);
    if i<=j then
      begin
        ch(p[i],p[j]);
        ch(y[i],y[j]);
        ch(c[i],c[j]);
        inc(i);dec(j);
      end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;

begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(p[i],y[i]);
      a[i]:=p[i];c[i]:=i;
    end;
  qs(1,m);
  for i:=1 to m do
    begin
      if (i=1)or(p[i]<>p[i-1]) then j:=1 else inc(j);
      o[c[i]]:=j;
    end;
  for i:=1 to m do
    begin
      zrt:=100000;
      for j:=1 to 6 do
        begin
          if a[i]<zrt then
            begin
              zrt:=zrt div 10;
              write(0);
            end else
          break;
       end;
      write(a[i]);
      zrt:=100000;
      for j:=1 to 6 do
        begin
          if o[i]<zrt then
            begin
              zrt:=zrt div 10;
              write(0);
            end else
          break;
        end;
      writeln(o[i]);
  end;
      end.

Score: 300 points

Problem Statement

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

City i is established in year Y_i and belongs to Prefecture P_i.

You can assume that there are no multiple cities that are established in the same year.

It is decided to allocate a 12-digit ID number to each city.

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.

Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq P_i \leq N

1 \leq Y_i \leq 10^9

Y_i are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
P_1 Y_1
:
P_M Y_M

Output

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).

Sample Input 1

2 3
1 32
2 63
1 12

Sample Output 1

000001000002
000002000001
000001000001

As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.

As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.

As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.

Sample Input 2

2 3
2 55
2 77
2 99

Sample Output 2

000002000001
000002000002
000002000003


var
	a,b,x,y,num:array[0..100010] of longint;
	n,m,i,t:longint;
	ch:string[20];
procedure qsort(l,r:longint);
var
	i,j,k,l1,t,mid:longint;
begin
	i:=l; j:=r;
	mid:=(l+r) div 2;
	repeat
		while (x[i]<x[mid]) or (x[i]=x[mid]) and (y[i]<y[mid]) do inc(i);
		while (x[j]>x[mid]) or (x[j]=x[mid]) and (y[j]>y[mid]) do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			t:=y[i]; y[i]:=y[j]; y[j]:=t;
			for k:=1 to m do
				if num[k]=i then break;
			for l1:=1 to m do
				if num[l1]=j then break;
			t:=num[k]; num[k]:=num[l1]; num[l1]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure card(x,y:longint);
var
	i,t:longint;
begin
	str(x,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	write(x);
	str(y,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	writeln(y);
end;
begin
	read(n,m);
	for i:=1 to m do
	begin
		read(a[i],b[i]);
		x[i]:=a[i];
		y[i]:=b[i];
		num[i]:=i;
	end;
	qsort(1,m);
	t:=1;
	y[1]:=t;
	for i:=2 to m do
	begin
		if x[i]<>x[i-1] then t:=1 else inc(t);
		y[i]:=t;
	end;
	for i:=1 to m do card(x[num[i]],y[num[i]]);
end.


var
	a,b,x,y,num:array[0..100010] of longint;
	n,m,i,t:longint;
	ch:string[20];
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r;
	mid:=(l+r) div 2;
	repeat
		while (x[i]<x[mid]) or (x[i]=x[mid]) and (y[i]<y[mid]) do inc(i);
		while (x[j]>x[mid]) or (x[j]=x[mid]) and (y[j]>y[mid]) do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			t:=y[i]; y[i]:=y[j]; y[j]:=t;
			if num[i]<>num[j] then num[num[i]]:=j; num[num[j]]:=i;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure card(x,y:longint);
var
	i,t:longint;
begin
	str(x,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	write(x);
	str(y,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	writeln(y);
end;
begin
	read(n,m);
	for i:=1 to m do
	begin
		read(a[i],b[i]);
		x[i]:=a[i];
		y[i]:=b[i];
		num[i]:=i;
	end;
	qsort(1,m);
	t:=1;
	y[1]:=t;
	for i:=2 to m do
	begin
		if x[i]<>x[i-1] then t:=1 else inc(t);
		y[i]:=t;
	end;
	for i:=1 to m do card(x[num[i]],y[num[i]]);
end.


type
	arr=array[0..100010] of longint;
var
	s,t:int64;
	i,n:longint;
	a,f1,f2:arr;
procedure qsort(var a:arr; l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(a,l,j);
	if i<r then qsort(a,i,r);
end;
begin
	read(n);
	if n mod 2=0 then
	begin
		f1[1]:=1; f1[n]:=-1;
		f2[1]:=-1; f2[n]:=1;
	end;
	if n mod 2=1 then
	begin
		f1[1]:=1; f1[n]:=1;
		f2[1]:=-1; f2[n]:=-1;
	end;
	for i:=2 to n-1 do
	begin
		if i mod 2=0 then f1[i]:=-2
		else f1[i]:=2;
		if i mod 2=0 then f2[i]:=2
		else f2[i]:=-2;
	end;
	qsort(f1,1,n);
	qsort(f2,1,n);
	s:=0; t:=0;
	for i:=1 to n do read(a[i]);
	qsort(a,1,n);
	for i:=1 to n do
	begin
		s:=s+f1[i]*a[i];
		t:=t+f2[i]*a[i];
	end;
	s:=abs(s); t:=abs(t);
	if s>t then writeln(s)
	else writeln(t);
end.

Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8


var
	a:array[-10..100010] of int64;
	n,i,l,r:longint;
	s,t,t1,t2,x,y:int64;
procedure qsort(l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure left;
begin
	t2:=a[l];
	inc(l);
end;
procedure right;
begin
	t2:=a[r];
	dec(r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right
		else if x<y then left
		else if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	if n mod 2=0 then
	begin
		t1:=a[(1+n) div 2+1];
		l:=1; r:=n;
		for i:=1 to n-1 do
		begin
			x:=abs(a[r]-t1);
			y:=abs(t1-a[l]);
			if x>y then right
			else if x<y then left
			else if x=y then
				if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
				else left;
			t:=t+abs(t1-t2);
			t1:=t2;
		end;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.


var
	a:array[0..100010] of int64;
	n,i,l,r:longint;
	t1,t2,s,x,y:int64;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[n div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then
		begin
			t2:=a[r];
			dec(r);
		end;
		if x<y then
		begin
			t2:=a[l];
			inc(l);
		end;
		if x=y then
		begin
			x:=abs(a[r-1]-a[l]);
			y:=abs(a[r]-a[l+1]);
			if x>y then
			begin
				t2:=a[r];
				dec(r);
			end;
			if x<=y then
			begin
				t2:=a[l];
				inc(l);
			end;
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	writeln(s);
end.


var
	a:array[-10..200010] of int64;
	n,i,l,r:longint;
	s,t,t1,t2,x,y:int64;
procedure qsort(l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure left;
begin
	t2:=a[l];
	inc(l);
end;
procedure right;
begin
	t2:=a[r];
	dec(r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right
		else if x<y then left
		else if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right
		else if x<y then left
		else if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		t:=t+abs(t1-t2);
		t1:=t2;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.


var
	a:array[-10..200010] of int64;
	n,i,l,r:longint;
	s,t,t1,t2,x,y:int64;
procedure qsort(l1,r1:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=a[(l1+r1) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l1<j then qsort(l1,j);
	if i<r1 then qsort(i,r1);
end;
procedure left;
begin
	t2:=a[l];
	inc(l);
end;
procedure right;
begin
	t2:=a[r];
	dec(r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right;
		if x<y then left;
		if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right;
		if x<y then left;
		if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		t:=t+abs(t1-t2);
		t1:=t2;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.


var
	a:array[0..100010] of int64;
	n,i,l,r:longint;
	s,t,t1,t2:int64;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if r=(1+n) div 2 then dec(r);
		if l=(1+n) div 2 then inc(l);
                if l>r then break;
		if abs(a[r]-t1)>abs(t1-a[l]) then
		begin
			t2:=a[r];
			dec(r);
			if r=(1+n) div 2 then dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
			if l=(1+n) div 2 then inc(l);
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if r=(1+n) div 2+1 then dec(r);
		if l=(1+n) div 2+1 then inc(l);
                if l>r then break;
		if abs(a[r]-t1)>abs(t1-a[l]) then
		begin
			t2:=a[r];
			dec(r);
			if r=(1+n) div 2+1 then dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
			if l=(1+n) div 2+1 then inc(l);
		end;
		t:=t+abs(t1-t2);
		t1:=t2;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.


var
	a:array[0..100010] of longint;
	n,i,t1,t2,l,r:longint;
	s,t:int64;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if r=(1+n) div 2 then dec(r);
		if l=(1+n) div 2 then inc(l);
                if l>r then break;
		if abs(a[r]-t1)>abs(t1-a[l]) then
		begin
			t2:=a[r];
			dec(r);
			if r=(1+n) div 2 then dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
			if l=(1+n) div 2 then inc(l);
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if r=(1+n) div 2+1 then dec(r);
		if l=(1+n) div 2+1 then inc(l);
                if l>r then break;
		if abs(a[r]-t1)>abs(t1-a[l]) then
		begin
			t2:=a[r];
			dec(r);
			if r=(1+n) div 2+1 then dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
			if l=(1+n) div 2+1 then inc(l);
		end;
		t:=t+abs(t1-t2);
		t1:=t2;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.


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


var
  a,c:array[1..100000]of longint;
  b:array[1..100000]of boolean;
  i,n,s,j,x,y,sum:longint;
procedure qsort(l,r:longint);
var
  i,j,mid,p:longint;
begin
  i:=l;j:=r;
  mid:=a[(l+r) div 2];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  if n=2 then begin write(abs(a[1]-a[2]));halt;end;
  qsort(1,n);
  fillchar(b,sizeof(b),true);
  i:=2;j:=n-2;b[1]:=false;b[n-1]:=false;b[n]:=false;
  if odd(n) then
              begin
			    c[n div 2+1]:=a[1];
				c[n div 2]:=a[n];
				c[n div 2+2]:=a[n-1];
				s:=3;
				x:=n div 2-1;
				y:=n div 2+3;
			  end
			else
			  begin
			    c[n div 2]:=a[1];
				c[n div 2-1]:=a[n];
				c[n div 2+1]:=a[n-1];
				s:=3;
				x:=n div 2-2;
				y:=n div 2+2;
			  end;
  while (x>=1) and (y<=n) do
  begin
  if (s mod 4=3)and(i<=n) then
  begin
    c[x]:=a[i];inc(i);inc(y);
	c[y]:=a[i];inc(i);dec(x);
	s:=s+2;
  end;
  if (s mod 4=1)and(j>=1) then
  begin
    c[x]:=a[j];dec(j);dec(x);
	c[y]:=a[j];dec(j);inc(y);
	s:=s+2;
  end;
  end;
  if odd(x) then c[n]:=a[n div 2+1] else c[x]:=a[n div 2];
  for i:=2 to n do sum:=sum+abs(c[i]-c[i-1]);
  write(sum);
  readln(i);
end.


var
  a,c:array[1..100000]of longint;
  b:array[1..100000]of boolean;
  i,n,s,j,x,y,sum:longint;
procedure qsort(l,r:longint);
var
  i,j,mid,p:longint;
begin
  i:=l;j:=r;
  mid:=a[(l+r) div 2];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  if n=2 then begin write(abs(a[1]-a[2]));halt;end;
  qsort(1,n);
  fillchar(b,sizeof(b),true);
  i:=2;j:=n-2;b[1]:=false;b[n-1]:=false;b[n]:=false;
  if odd(n) then
              begin
			    c[n div 2+1]:=a[1];
				c[n div 2]:=a[n];
				c[n div 2+2]:=a[n-1];
				s:=3;
				x:=n div 2-1;
				y:=n div 2+3;
			  end
			else
			  begin
			    c[n div 2]:=a[1];
				c[n div 2-1]:=a[n];
				c[n div 2+1]:=a[n-1];
				s:=3;
				x:=n div 2-2;
				y:=n div 2+2;
			  end;
  while (x>=1) and (y<=n) do
  begin
  if (s mod 4=3)and(i<=n) then
  begin
    if i<=n then begin c[x]:=a[i];inc(i);inc(y); end;
	if i<=n then begin c[y]:=a[i];inc(i);dec(x);end;
	s:=s+2;
  end;
  if (s mod 4=1)and(j>=1) then
  begin
    if j>=1 then begin c[x]:=a[j];dec(j);dec(x);end;
	if j>=1 then begin c[y]:=a[j];dec(j);inc(y);end;
	s:=s+2;
  end;
  end;
  if odd(x) then c[n]:=a[n div 2+1] else c[x]:=a[n div 2];
  for i:=2 to n do sum:=sum+abs(c[i]-c[i-1]);
  write(sum);
  readln(i);
end.


var
  a,c:array[1..100000]of longint;
  b:array[1..100000]of boolean;
  i,n,s,j,x,y,sum:longint;
procedure qsort(l,r:longint);
var
  i,j,mid,p:longint;
begin
  i:=l;j:=r;
  mid:=a[(l+r) div 2];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  if n=2 then begin write(abs(a[1]-a[2]));halt;end;
  qsort(1,n);
  fillchar(b,sizeof(b),true);
  i:=2;j:=n-2;b[1]:=false;b[n-1]:=false;b[n]:=false;
  if odd(n) then
              begin
			    c[n div 2+1]:=a[1];
				c[n div 2]:=a[n];
				c[n div 2+2]:=a[n-1];
				s:=3;
				x:=n div 2-1;
				y:=n div 2+3;
			  end
			else
			  begin
			    c[n div 2]:=a[1];
				c[n div 2-1]:=a[n];
				c[n div 2+1]:=a[n-1];
				s:=3;
				x:=n div 2-2;
				y:=n div 2+2;
			  end;
  while (x>=1) or (y<=n) do
  if (s mod 4=3)and(i<=n) then
  begin
    if i<=n then begin c[x]:=a[i];inc(i);inc(y); end;
	if i<=n then begin c[y]:=a[i];inc(i);dec(x);end;
	s:=s+2;
  end;
  if (s mod 4=1)and(j>=1) then
  begin
    if j>=1 then begin c[x]:=a[j];dec(j);dec(x);end;
	if j>=1 then begin c[y]:=a[j];dec(j);inc(y);end;
	s:=s+2;
  end;
  if odd(x) then c[n]:=a[n div 2+1] else c[x]:=a[n div 2];
  for i:=1 to n do write(c[i],' ');
  writeln;
  for i:=2 to n do sum:=sum+abs(c[i]-c[i-1]);
  write(sum);
  readln(i);
end.


Score : 500 points

Problem Statement

You are given an integer N. Determine if there exists a tuple of subsets of \{1,2,...N\}, (S_1,S_2,...,S_k), that satisfies the following conditions:

Each of the integers 1,2,...,N is contained in exactly two of the sets S_1,S_2,...,S_k.

Any two of the sets S_1,S_2,...,S_k have exactly one element in common.

If such a tuple exists, construct one such tuple.

Constraints

1 \leq N \leq 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If a tuple of subsets of \{1,2,...N\} that satisfies the conditions does not exist, print No.
If such a tuple exists, print Yes first, then print such subsets in the following format:

k
|S_1| S_{1,1} S_{1,2} ... S_{1,|S_1|}
:
|S_k| S_{k,1} S_{k,2} ... S_{k,|S_k|}

where S_i={S_{i,1},S_{i,2},...,S_{i,|S_i|}}.

If there are multiple such tuples, any of them will be accepted.

Sample Input 1

3

Sample Output 1

Yes
3
2 1 2
2 3 1
2 2 3

It can be seen that (S_1,S_2,S_3)=(\{1,2\},\{3,1\},\{2,3\}) satisfies the conditions.

Sample Input 2

4

Sample Output 2

No


var
  a,b,c:string;
  t:char;
  n,m,i,j,p,q,k:longint;
function gcd(a,b:longint):longint;
begin
  if b=0 then exit(a)
  else exit(gcd(b,a mod b));
end;
begin
  readln(n,m);
  readln(a);
  readln(b);
  if n>m then i:=n
  else i:=m;
  k:=gcd(i,n+m-i);
  p:=k div n;
  q:=k div m;
  i:=1;
  j:=1;
  while (j<=m)and(i<=n) do
  begin
	 if b[j]<>a[i] then begin writeln('-1'); halt; end;
     i:=i+q;
	 j:=j+p;
  end;
  writeln(k);
end.

Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M.
These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

Let L be the length of X. L is divisible by both N and M.

Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.

Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45


var
	n,m,t1,i:longint;
	s,t,t2:ansistring;
function gcd(x,y:longint):longint;
var
	t,r:longint;
begin
	if x<y then
	begin
		t:=x; x:=y; y:=t;
	end;
	r:=x mod y;
	while r<>0 do
	begin
		x:=y; y:=r;
		r:=x mod y;
	end;
	exit(y);
end;
begin
	readln(n,m);
	readln(s); readln(t);
	if n<m then
	begin
		t1:=n; n:=m; m:=t1;
		t2:=s; s:=t; t:=t2;
	end;
	if n mod m=0 then
	begin
		for i:=0 to m-1 do
			if i*n div m+1<>i+1 then
			begin
				writeln(-1);
				exit;
			end;
		writeln(n);
	end
	else writeln(n*m div gcd(n,m));
end.


var
	n,m,i:Longint;
begin
	read(n,m);
	i:=m div n;
	while m mod i>0 do dec(i);
	writeln(i);
end.

Score : 400 points

Problem Statement

You are given integers N and M.

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

N \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

Sample Input 1

3 14

Sample Output 1

2

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

Sample Input 2

10 123

Sample Output 2

3

Sample Input 3

100000 1000000000

Sample Output 3

10000


var
	n,m,ans:int64;
	i:Longint;
begin
	read(n,m);
	i:=1;
	while i*i<=m do begin
		if m mod i=0 then begin
			if(m div i>=n)and(ans<i)then ans:=i;
			if(i>=n)and(ans<m div i)then ans:=m div i;
		end;
		inc(i);
	end;
	writeln(ans);
end.


var n,m,i,k:longint;
begin
	read(n,m);
	k:=m div n;
    for i:=k downto 1 do
    if (m mod i=0) and (m div i>=n) then
    begin
    	write(i);
        exit;
    end;
end.


var n,m,i:longint;
begin
	read(n,m);
    for i:=m-1 downto 2 do
    if (m mod i=0) and (m div i>=n) then
    begin
    	write(i);
        exit;
    end;
end.


var
  n,m,i,max:longint;
begin
  max:=1;
  read(n,m);
  for i:=1 to m div n+1 do
  if m mod i=0 then max:=m;
write(max);
end.


var
n,m,i,t:longint;
begin
read(n,m);
t:=trunc(sqrt(m));
for i:=1 to t do
if m mod i=0 then
if i>=n then
begin
writeln(m div i);
halt;
end;
for i:=t-1 downto 1 do
if m mod i=0 then
if m div i>=n then
begin
writeln(i);
halt;
end;
end.


var
  n,i,x,maxo,maxj,oi,ji:longint;
  j,o:array[1..100000]of longint;
begin
  read(n);
  for i:=1 to n do
    begin
	  read(x);
	  if i mod 2=0 then inc(o[x])
	  else inc(j[x]);
	end;
  maxo:=-maxlongint;
  maxj:=-maxlongint;
  for i:=1 to 100000 do
    begin
    if o[i]>maxo then begin maxo:=o[i]; oi:=i; end;
	if j[i]>maxj then begin maxj:=j[i]; ji:=i; end;
	end;
  if (oi=ji) then write(n div 2)
  else write(n-maxo-maxj);
end.

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


var n:longint;
x,i,m1,m11,m22,m2:longint;
a,b:array[0..100001]of integer;
begin
	read(n);
	a[0]:=0;b[0]:=0;//a ou
	m1:=0;m2:=0;
	for i:=1 to n do
	begin
		read(x);
		if i mod 2=0 then
		begin
			inc(a[x]);
			if (a[x]>a[m1]) and (x<>m1) then begin m11:=m1;m1:=x;end
			else if (a[x]>a[m11]) and (x<>m11) and (x<>m1) then m11:=x;
		end
		else begin
			inc(b[x]);
			if (b[x]>b[m2]) and (x<>m2) then begin m22:=m2;m2:=x;end
			else if (b[x]>b[m22]) and (x<>m22) and (x<>m2) then m22:=x;
		end;
	end;
	if m1=m2 then
	begin
		if a[m11]+b[m2]>a[m1]+b[m22] then write(n-a[m11]-b[m2])
			else write(n-a[m1]-b[m22]);
		exit;
	end;
	write(n-a[m1]-b[m2]);
end.


var n:longint;
x,i,m1,m11,m22,m2:integer;
a,b:array[0..100001]of integer;
begin
	read(n);
	a[0]:=0;b[0]:=0;//a ou
	m1:=0;m2:=0;
	for i:=1 to n do
	begin
		read(x);
		if i mod 2=0 then
		begin
			inc(a[x]);
			if (a[x]>a[m1]) and (x<>m1) then begin m11:=m1;m1:=x;end
			else if (a[x]>a[m11]) and (x<>m11) and (x<>m1) then m11:=x;
		end
		else begin
			inc(b[x]);
			if (b[x]>b[m2]) and (x<>m2) then begin m22:=m2;m2:=x;end
			else if (b[x]>b[m22]) and (x<>m22) and (x<>m2) then m22:=x;
		end;
	end;
	if m1=m2 then
	begin
		if m11>m22 then m1:=m11
			else m2:=m22;
	end;
	write((n div 2)-a[m1]+(n-n div 2)-b[m2]);
end.


var
a:array[1..100000]of longint;
b:array[0..1,0..100000]of longint;
max:array[0..1]of longint;
n,i,s,t,x:longint;
begin
read(n);
for i:=1 to n do
begin
read(a[i]);
inc(b[i mod 2,a[i]]);
 if b[i mod 2,a[i]]>max[i mod 2] then
 begin
 max[i mod 2]:=b[i mod 2,a[i]];
 b[i mod 2,0]:=a[i];
 end;
end;
if b[0,0]=b[1,0] then
begin
t:=0;
 for i:=1 to n do
  if (i mod 2=0) and (a[i]<>b[0,0]) and (b[0,a[i]]>t) then
  begin
  t:=b[0,a[i]];
  x:=a[i];
  end;
s:=0;
 for i:=1 to n do
  if (i mod 2=0) and (a[i]<>x) or (i mod 2=1) and (a[i]<>b[1,0]) then
  inc(s);
t:=0;
 for i:=1 to n do
  if (i mod 2=1) and (a[i]<>b[1,0]) and (b[1,a[i]]>t) then
  begin
  t:=b[1,a[i]];
  x:=a[i];
  end;
t:=0;
 for i:=1 to n do
  if (i mod 2=0) and (a[i]<>b[0,0]) or (i mod 2=1) and (a[i]<>x) then
  inc(t);
 if t>s then
 writeln(t)
 else
 writeln(s);
halt;
end;
for i:=1 to n do
 if a[i]<>b[i mod 2,0] then
 inc(s);
writeln(s);
end.


var
	i,j,k,n,max:longint;
	t,x,y:array[0..1010] of longint;
	s:ansistring;
begin
	readln(n);
	max:=0;
	for i:=1 to n do
	begin
		read(x[i],y[i]);
		t[i]:=abs(x[i])+abs(y[i]);
		if t[i] mod 2<>t[1] mod 2 then
		begin
			writeln(-1);
			halt;
		end;
		if t[i]>max then max:=t[i];
	end;
	writeln(max);
	for i:=1 to max do write(2-t[i] mod 2,' ');
	writeln;
	for i:=1 to n do
	begin
		s:='';
		if x[i]<0 then
			for j:=1 to -x[i] do s:=s+'L'
		else if x[i]>0 then for j:=1 to x[i] do s:=s+'R';
		if y[i]<0 then
			for j:=1 to -y[i] do s:=s+'D'
		else if y[i]>0 then for j:=1 to y[i] do s:=s+'U';
		k:=length(s);
		for j:=k+1 to max do
			if j mod 2=1 then s:=s+'L' else s:=s+'R';
		writeln(s);
	end;
end.

Score : 600 points

Problem Statement

Snuke is introducing a robot arm with the following properties to his factory:

The robot arm consists of m sections and m+1 joints. The sections are numbered 1, 2, ..., m, and the joints are numbered 0, 1, ..., m. Section i connects Joint i-1 and Joint i. The length of Section i is d_i.

For each section, its mode can be specified individually. There are four modes: L, R, D and U. The mode of a section decides the direction of that section. If we consider the factory as a coordinate plane, the position of Joint i will be determined as follows (we denote its coordinates as (x_i, y_i)):

(x_0, y_0) = (0, 0).

If the mode of Section i is L, (x_{i}, y_{i}) = (x_{i-1} - d_{i}, y_{i-1}).

If the mode of Section i is R, (x_{i}, y_{i}) = (x_{i-1} + d_{i}, y_{i-1}).

If the mode of Section i is D, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} - d_{i}).

If the mode of Section i is U, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} + d_{i}).

Snuke would like to introduce a robot arm so that the position of Joint m can be matched with all of the N points (X_1, Y_1), (X_2, Y_2), ..., (X_N, Y_N) by properly specifying the modes of the sections.
Is this possible?
If so, find such a robot arm and how to bring Joint m to each point (X_j, Y_j).

Constraints

All values in input are integers.

1 \leq N \leq 1000

-10^9 \leq X_i \leq 10^9

-10^9 \leq Y_i \leq 10^9

Partial Score

In the test cases worth 300 points, -10 \leq X_i \leq 10 and -10 \leq Y_i \leq 10 hold.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

Output

If the condition can be satisfied, follow the following format. If the condition cannot be satisfied, print -1.

m
d_1 d_2 ... d_m
w_1
w_2
:
w_N

m and d_i are the configurations of the robot arm. Refer to the problem statement for what each of them means.
Here, 1 \leq m \leq 40 and 1 \leq d_i \leq 10^{12} must hold. Also, m and d_i must all be integers.

w_j is a string of length m that represents the way to bring Joint m of the robot arm to point (X_j, Y_j).
The i-th character of w_j should be one of the letters L, R, D and U, representing the mode of Section i.

Sample Input 1

3
-1 0
0 3
2 -1

Sample Output 1

2
1 2
RL
UU
DR

In the given way to bring Joint m of the robot arm to each (X_j, Y_j), the positions of the joints will be as follows:

To (X_1, Y_1) = (-1, 0): First, the position of Joint 0 is (x_0, y_0) = (0, 0). As the mode of Section 1 is R, the position of Joint 1 is (x_1, y_1) = (1, 0). Then, as the mode of Section 2 is L, the position of Joint 2 is (x_2, y_2) = (-1, 0).

To (X_2, Y_2) = (0, 3): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, 1), (x_2, y_2) = (0, 3).

To (X_3, Y_3) = (2, -1): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, -1), (x_2, y_2) = (2, -1).

Sample Input 2

5
0 0
1 0
2 0
3 0
4 0

Sample Output 2

-1

Sample Input 3

2
1 1
1 1

Sample Output 3

2
1 1
RU
UR

There may be duplicated points among (X_j, Y_j).

Sample Input 4

3
-7 -3
7 3
-3 -7

Sample Output 4

5
3 1 4 1 5
LRDUL
RDULR
DULRD


var
	i,j,k,n,max:longint;
	t,x,y:array[0..1010] of longint;
	s:string;
begin
	readln(n);
	max:=0;
	for i:=1 to n do
	begin
		read(x[i],y[i]);
		t[i]:=abs(x[i])+abs(y[i]);
		if t[i] mod 2<>t[1] mod 2 then
		begin
			writeln(-1);
			halt;
		end;
		if t[i]>max then max:=t[i];
	end;
	writeln(max);
	for i:=1 to max do write(2-t[i] mod 2,' ');
	writeln;
	for i:=1 to n do
	begin
		s:='';
		if x[i]<0 then
			for j:=1 to -x[i] do s:=s+'L'
		else if x[i]>0 then for j:=1 to x[i] do s:=s+'R';
		if y[i]<0 then
			for j:=1 to -y[i] do s:=s+'D'
		else if y[i]>0 then for j:=1 to y[i] do s:=s+'U';
		k:=length(s);
		for j:=k+1 to max do
			if j mod 2=1 then s:=s+'L' else s:=s+'R';
		writeln(s);
	end;
end.


var
  a,a1,a2:array[0..200000] of longint;
  b,c,i,j,k,t,n,m,x,y,xx,yy,x1,y1:longint;
function max(xxx,yyy:longint):longint;
begin
  if xxx>yyy then max:=xxx else max:=yyy;
end;
begin
  readln(n);
  for i:=1 to n do
  begin
    read(a[i]);
    if i mod 2=1 then inc(a1[a[i]]);
    if i mod 2=0 then inc(a2[a[i]]);
  end;
  x:=0; y:=0;
  xx:=0; yy:=0;
  b:=0; c:=0;
  t:=0;
  for i:=1 to 100000 do
  begin
    if a1[i]>b then begin x:=i; b:=a1[i]; end;
    if a2[i]>c then begin y:=i; c:=a2[i]; end;
  end;
  x1:=a1[x]; y1:=a2[y];
  a1[x]:=0; b:=0;
  a2[y]:=0; c:=0;
  for i:=1 to 100000 do
  begin
    if a1[i]>b then begin xx:=i; b:=a1[i]; end;
    if a2[i]>c then begin yy:=i; c:=a2[i]; end;
  end;
  if x=y then t:=max(x1+a2[yy],a1[xx]+y1) else t:=x1+y1;
  writeln(n-t);
end.


var
    n,x,i,max,min,atot1,atot2,btot1,btot2,ta,tb:longint;
    a,b:array[0..100010] of longint;
begin
    readln(n);
    max:=0;min:=maxlongint;
    fillchar(a,sizeof(a),0);
    fillchar(b,sizeof(b),0);
    for i:=1 to n do
        begin
            read(x);
            if odd(i) then inc(a[x]) else inc(b[x]);
            if x>max then max:=x
            else if x<min then min:=x;
        end;
    for i:=min to max do
        begin
            if a[i]>atot1 then
                begin
                    atot2:=atot1;atot1:=a[i];ta:=i;
                end
            else if a[i]>=atot2 then atot2:=a[i];
            if b[i]>btot1 then
                begin
                    btot2:=btot1;btot1:=b[i];tb:=i;
                end
            else if b[i]>=btot2 then btot2:=b[i];
        end;
    if ta<>tb then writeln(n-atot1-btot1)
    else if atot1+btot2>atot2+btot1 then writeln(n-atot1-btot2)
    else writeln(n-atot2-btot1);
end.


var
s:ansistring;
n,i,j,root :longint;
begin
 readln(s);
 n:=length(s);

 if (s[1]='0')or(s[n]='1') then
 begin
  writeln('-1');
  exit;
  end;

 for i:=2 to n-1 do
  begin
    if s[i]='1' then  if s[n-i]='0' then  begin writeln('-1'); exit; end;
    if s[i]='0' then  if s[n-i]='1' then  begin writeln('-1'); exit; end;
  end;
   root:=1;
 // writeln(1,' ',2);

  for i:=1 to n-1 do
   begin
    writeln(root,' ',i+1);
    if s[i]='1' then root:=i+1;
   end;
end.

Score : 700 points

Problem Statement

You are given a string s of length n.
Does a tree with n vertices that satisfies the following conditions exist?

The vertices are numbered 1,2,..., n.

The edges are numbered 1,2,..., n-1, and Edge i connects Vertex u_i and v_i.

If the i-th character in s is 1, we can have a connected component of size i by removing one edge from the tree.

If the i-th character in s is 0, we cannot have a connected component of size i by removing any one edge from the tree.

If such a tree exists, construct one such tree.

Constraints

2 \leq n \leq 10^5

s is a string of length n consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

s

Output

If a tree with n vertices that satisfies the conditions does not exist, print -1.

If a tree with n vertices that satisfies the conditions exist, print n-1 lines.
The i-th line should contain u_i and v_i with a space in between.
If there are multiple trees that satisfy the conditions, any such tree will be accepted.

Sample Input 1

1111

Sample Output 1

-1

It is impossible to have a connected component of size n after removing one edge from a tree with n vertices.

Sample Input 2

1110

Sample Output 2

1 2
2 3
3 4

If Edge 1 or Edge 3 is removed, we will have a connected component of size 1 and another of size 3. If Edge 2 is removed, we will have two connected components, each of size 2.

Sample Input 3

1010

Sample Output 3

1 2
1 3
1 4

Removing any edge will result in a connected component of size 1 and another of size 3.


var
  n,i,j,k,t,root:longint;
  a:array[1..10000] of integer;
  x:char;
  first,next,last,b:array[1..100000] of longint;
begin
  i:=0;
  while not eoln do
    begin
      n:=n+1;
      read(x);
      if x='1'
        then a[n]:=1
        else a[n]:=0;
    end;
  if (a[1]=0)or(a[n]=1)
    then
      begin
        writeln(-1);
        halt;
      end;
  for i:=2 to n div 2 do
    if (a[i]=1)and(a[n-i]=0)
      then
        begin
          writeln(-1);
          halt;
        end;
  root:=1;
  for i:=2 to n do
    if a[i-1]=0
      then writeln(root,' ',i)
      else
        begin
          writeln(root,' ',i);
          root:=i;
        end;
end.


var
  a:array[1..200000] of longint;
  b,c,i,j,k,t,n,m,x,y:longint;
  s:ansistring;
begin
  readln(s);
  n:=length(s);
  k:=0;
  for i:=1 to n-1 do
  if s[i]='1' then if s[n-i]='0' then k:=1;
  if s[1]='0' then k:=1;
  if s[n]='1' then k:=1;
  if k=1 then writeln('-1') else
  begin
    x:=1;
    for i:=1 to n-1 do
    begin
      writeln(x,' ',i+1);
      if s[i]='1' then x:=i+1;
    end;
  end;
end.


var
	s,t:String;
	c,d:array[0..25]of Longint;
	i,j,k:Longint;
begin
	readln(s);
	readln(t);
	for i:=0 to 25 do begin
		c[i]:=-1;
		d[i]:=-1;
	end;
	for i:=1 to length(s) do begin
		j:=ord(s[i])-97;
		k:=ord(t[i])-97;
		if c[j]=-1 then c[j]:=k;
		if d[k]=-1 then d[k]:=j;
		if(c[j]<>k)or(d[k]<>j)then begin
			writeln('No');
			exit;
		end;
	end;
	writeln('Yes');
end.

Score : 300 points

Problem Statement

You are given strings S and T consisting of lowercase English letters.

You can perform the following operation on S any number of times:

Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.

Determine if S and T can be made equal by performing the operation zero or more times.

Constraints

1 \leq |S| \leq 2 \times 10^5

|S| = |T|

S and T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S and T can be made equal, print Yes; otherwise, print No.

Sample Input 1

azzel
apple

Sample Output 1

Yes

azzel can be changed to apple, as follows:

Choose e as c_1 and l as c_2. azzel becomes azzle.

Choose z as c_1 and p as c_2. azzle becomes apple.

Sample Input 2

chokudai
redcoder

Sample Output 2

No

No sequences of operation can change chokudai to redcoder.

Sample Input 3

abcdefghijklmnopqrstuvwxyz
ibyhqfrekavclxjstdwgpzmonu

Sample Output 3

Yes