var
 a,b:array[1..100,1..100] of longint;
 i,j,x,y,n,m,k,t,z:longInt;
begin
 read(n,m);
 fillchar(a,sizeof(a),$3f);
 for i:=1 to m do
  begin
   read(x,y,z);
   a[x,y]:=z;
   a[y,x]:=z;
   b[x,y]:=z;
   b[y,x]:=z;
  end;
 for k:=1 to n do
  for i:=1 to n do
   for j:=1 to n do
    if (a[i,j]>a[i,k]+a[k,j]) then a[i,j]:=a[i,k]+a[k,j];
  for i:=1 to n do
   for j:=i+1 to n do
    if (a[i,j]<b[i,j]) then t:=t+1;
 writeln(t);
end.

Score : 400 points

Problem Statement

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

The i-th (1≤i≤M) edge connects vertex a_i and vertex b_i with a distance of c_i.

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

A connected graph is a graph where there is a path between every pair of different vertices.

Find the number of the edges that are not contained in any shortest path between any pair of different vertices.

Constraints

2≤N≤100

N-1≤M≤min(N(N-1)/2,1000)

1≤a_i,b_i≤N

1≤c_i≤1000

c_i is an integer.

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

The given graph is connected.

Input

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

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

Output

Print the number of the edges in the graph that are not contained in any shortest path between any pair of different vertices.

Sample Input 1

3 3
1 2 1
1 3 1
2 3 3

Sample Output 1

1

In the given graph, the shortest paths between all pairs of different vertices are as follows:

The shortest path from vertex 1 to vertex 2 is: vertex 1 → vertex 2, with the length of 1.

The shortest path from vertex 1 to vertex 3 is: vertex 1 → vertex 3, with the length of 1.

The shortest path from vertex 2 to vertex 1 is: vertex 2 → vertex 1, with the length of 1.

The shortest path from vertex 2 to vertex 3 is: vertex 2 → vertex 1 → vertex 3, with the length of 2.

The shortest path from vertex 3 to vertex 1 is: vertex 3 → vertex 1, with the length of 1.

The shortest path from vertex 3 to vertex 2 is: vertex 3 → vertex 1 → vertex 2, with the length of 2.

Thus, the only edge that is not contained in any shortest path, is the edge of length 3 connecting vertex 2 and vertex 3, hence the output should be 1.

Sample Input 2

3 2
1 2 1
2 3 1

Sample Output 2

0

Every edge is contained in some shortest path between some pair of different vertices.


var
 a,b:array[1..100,1..100] of longint;
 i,j,x,y,n,m,k,t,s,z:longint;
begin
 read(n,m);
 fillchar(a,sizeof(a),$7f);
 for i:=1 to m do
  begin
   read(x,y,z);
   a[x,y]:=z;
   a[y,x]:=z;
   b[x,y]:=1;
  end;
 for k:=1 to n do
  for i:=1 to n do
   for j:=1 to n do
    if (i<>j)and(i<>k)and(j<>k)and(a[i,j]>a[i,k]+a[k,j]) then
     begin
      a[i,j]:=a[i,k]+a[k,j];
      b[i,k]:=0;
      b[k,j]:=0;
      t:=1;
     end;
 for i:=1 to 100 do
  for j:=1 to 100 do
   if (b[i,j]=1) then s:=s+1;
 if (t=0) then s:=0;
 writeln(s);
end.

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

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.


var
x,y,s,n:longint;
begin
readln(x,y);
s:=0;
n:=x;
if x>y then begin n:=0-x;s:=1;end;
while n<y do
  begin
    inc(n);
    inc(s);
  end;
writeln(s);
end.

Score : 300 points

Problem Statement

Snuke has a calculator. It has a display and two buttons.

Initially, the display shows an integer x.
Snuke wants to change this value into another integer y, by pressing the following two buttons some number of times in arbitrary order:

Button A: When pressed, the value on the display is incremented by 1.

Button B: When pressed, the sign of the value on the display is reversed.

Find the minimum number of times Snuke needs to press the buttons to achieve his objective.
It can be shown that the objective is always achievable regardless of the values of the integers x and y.

Constraints

x and y are integers.

|x|, |y| ≤ 10^9

x and y are different.

Input

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

x y

Output

Print the minimum number of times Snuke needs to press the buttons to achieve his objective.

Sample Input 1

10 20

Sample Output 1

10

Press button A ten times.

Sample Input 2

10 -10

Sample Output 2

1

Press button B once.

Sample Input 3

-10 -20

Sample Output 3

12

Press the buttons as follows:

Press button B once.

Press button A ten times.

Press button B once.


uses math;
var
  x,y:longint;
 function mi(x,y:longint):longint;
  begin if x<=y then exit(y-x) else exit(x-y+2);end;
begin
  readln(x,y);
  writeln(min(mi(x,y),min(mi(-x,y)+1,min(mi(x,-y)+1,mi(-x,-y)+2))));
end.


program taskD2;
var
    a,b : longint;
    op : char;
    ans : longint;

begin
    readln(a);
    readln(op);
    readln(b);
    if op = '+' then
    begin
        ans := a + b
    end
    else
    begin
        ans := a - b;
    end;
    writeln(ans);
end.

Score : 100 points

Problem Statement

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

Constraints

1≦A,B≦10^9

op is either + or -.

Input

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

A op B

Output

Evaluate the formula and print the result.

Sample Input 1

1 + 2

Sample Output 1

3

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

Sample Input 2

5 - 7

Sample Output 2

-2


var
  c2,c1,c3:char;
  a,b:longint;
begin
  readln(a,c1,c2,c3,b);
  if (c2='-') then begin
    writeln(a-b);
    exit;
  end
  else begin
    writeln(a+b);
    exit;
  end;
end.


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


var s,t,v:ansistring;
    i:longint;
    a,b:int64;
    x:boolean;
begin
  readln(s);
  x:=true;
  for i:=1 to length(s) do begin
    if (s[i]='+') or (s[i]='-') then begin
      if (s[i]='+') then x:=false;
      break;
    end;
  end;
  if (x=false) then begin
    val(copy(s,1,pos('+',s)-2),a);
    val(copy(s,pos('+',s)+2,length(s)),b);
    writeln(a+b);
  end
  else begin
    val(copy(s,1,pos('-',s)-2),a);
    val(copy(s,pos('-',s)+2,length(s)),b);
    writeln(a-b);
  end;
end.


var
s,astr,bstr:ansistring;
sign:char;
a,b,lens,p:longint;
begin
   readln(s);
   lens:=length(s);
   if pos('+',s)<>0 then
   begin
      p := pos('+',s);
	  sign:='+';
   end;
   if pos('-',s)<>0 then
   begin
      p := pos('-',s);
	   sign:='-';
   end;
   astr:=copy(s,1,p-2);
   bstr:=copy(s,p+2,lens-p-1);
   val(astr,a);
   val(bstr,b);
   if sign='+' then writeln(a+b);
   if sign='-' then writeln(a-b);
end.


program Project1;

{$APPTYPE CONSOLE}

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

    SetLength(t,n);
    sum:=0;
    for i := 0 to n - 1 do begin
      Read(t[i]);
      sum:=sum+t[i];
    end;

    Readln(m);
    SetLength(p,m);
    SetLength(x,m);
    for i := 0 to m - 1 do begin
      Read(p[i]);
      Readln(x[i]);
    end;

    for i := 0 to m - 1 do
      Writeln(sum-t[p[i]-1]+x[i]);

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

Score : 200 points

Problem Statement

Joisino is about to compete in the final round of a certain programming competition.
In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).

Also, there are M kinds of drinks offered to the contestants, numbered 1 through M.
If Joisino takes drink i(1≦i≦M), her brain will be stimulated and the time it takes for her to solve problem P_i will become X_i seconds.
It does not affect the time to solve the other problems.

A contestant is allowed to take exactly one of the drinks before the start of the contest.
For each drink, Joisino wants to know how many seconds it takes her to solve all the problems if she takes that drink.
Here, assume that the time it takes her to solve all the problems is equal to the sum of the time it takes for her to solve individual problems.
Your task is to write a program to calculate it instead of her.

Constraints

All input values are integers.

1≦N≦100

1≦T_i≦10^5

1≦M≦100

1≦P_i≦N

1≦X_i≦10^5

Input

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

N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M

Output

For each drink, calculate how many seconds it takes Joisino to solve all the problems if she takes that drink, and print the results, one per line.

Sample Input 1

3
2 1 4
2
1 1
2 3

Sample Output 1

6
9

If Joisino takes drink 1, the time it takes her to solve each problem will be 1, 1 and 4 seconds, respectively, totaling 6 seconds.

If Joisino takes drink 2, the time it takes her to solve each problem will be 2, 3 and 4 seconds, respectively, totaling 9 seconds.

Sample Input 2

5
7 2 3 8 5
3
4 2
1 7
4 13

Sample Output 2

19
25
30


var
 n,m,i:longint;
 st:string;
begin
 readln(n,m);
 for i:=1 to n do
  begin
   readln(st);
   writeln(st);
   writeln(st);
  end;
end.


var
  x:string;
  n,m,i:longint;
begin
  readln(n,m);
  for i:=1 to n do
    begin
      readln(x);
      writeln(x);
      writeln(x);
    end;
end.


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


var
 n,m,i,j:longint;
 a:array[0..101,0..101] of char;
begin
 readln(n,m);
 for i:=1 to n do
  begin
   for j:=1 to m do
    read(a[i,j]);
   readln;
  end;
 for i:=1 to 2*n do
  begin
   for j:=1 to m do
    write(a[(i+1) div 2,j]);
   writeln;
  end;
end.


var i:Integer;s:String[100];begin readln();for i:=1 to 100 do begin readln(s);writeln(s);writeln(s)end;end.


program D;
    var N,K,L:longint;
    var i,j,p,q:longint;
    var a:array[1..250000]of longint;
    var b:array[1..250000]of longint;
    var map1:array of array of boolean;
    var map2:array of array of boolean;
    var used:array[1..250000]of boolean;
    var ansr:array[1..250000]of longint;
    procedure DFS1(_,__:longint);
        var ii:longint;
    begin
        used[_]:=true;
        a[_]:=__;
        for ii:=1 to N do
            if (map1[_,ii]=true)AND(used[ii]=false) then DFS1(ii,__);
    end;
    procedure DFS2(_,__:longint);
        var ii:longint;
    begin
        used[_]:=true;
        b[_]:=__;
        for ii:=1 to N do
            if (a[_]=a[ii])AND(map2[_,ii]=true)AND(used[ii]=false) then DFS2(ii,__);
    end;
begin
    readln(N,K,L);
    setlength(map1,N+1,N+1);
    setlength(map2,N+1,N+1);
    for i:=1 to N do
    begin
        map1[i,i]:=true;
        map2[i,i]:=true;
        used[i]:=false;
        ansr[i]:=0;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        map1[p,q]:=true;
        map1[q,p]:=true;
    end;
    for i:=1 to N do if used[i]=false then DFS1(i,i);
    for i:=1 to L do
    begin
        readln(p,q);
        map2[p,q]:=true;
        map2[q,p]:=true;
    end;
    for i:=1 to N do used[i]:=false;
    for i:=1 to N do if used[i]=false then DFS2(i,i);
    for i:=1 to N do ansr[b[i]]:=ansr[b[i]]+1;
    for i:=1 to N do write(ansr[b[i]],' ');
end.

Score : 400 points

Problem Statement

There are N cities. There are also K roads and L railways, extending between the cities.
The i-th road bidirectionally connects the p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.

We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.

For each city, find the number of the cities connected to that city by both roads and railways.

Constraints

2 ≦ N ≦ 2*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)

When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

Input

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

N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

Output

Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

Sample Input 1

4 3 1
1 2
2 3
3 4
2 3

Sample Output 1

1 2 2 1

All the four cities are connected to each other by roads.

By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1, respectively.

Sample Input 2

4 2 2
1 2
2 3
1 4
2 3

Sample Output 2

1 2 2 1

Sample Input 3

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

Sample Output 3

1 1 2 1 2 2 2


program D;
    var N,K,L:longint;
    var i,j,p,q:longint;
    var a:array[1..250000]of longint;
    var b:array[1..250000]of longint;
    var map1:array of array of boolean;
    var map2:array of array of boolean;
    var used:array[1..250000]of boolean;
    var ansr:array[1..250000]of longint;
    procedure DFS1(_,__:longint);
        var ii:longint;
    begin
        used[_]:=true;
        a[_]:=__;
        for ii:=1 to N do
            if (map1[_,ii]=true)AND(used[ii]=false) then DFS1(ii,__);
    end;
    procedure DFS2(_,__:longint);
        var ii:longint;
    begin
        used[_]:=true;
        b[_]:=__;
        for ii:=1 to N do
            if (a[_]=a[ii])AND(map2[_,ii]=true)AND(used[ii]=false) then DFS2(ii,__);
    end;
begin
    readln(N,K,L);
    setlength(map1,N,N);
    setlength(map2,N,N);
    for i:=1 to N do
    begin
        map1[i,i]:=true;
        map2[i,i]:=true;
        used[i]:=false;
        ansr[i]:=0;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        map1[p,q]:=true;
        map1[q,p]:=true;
    end;
    for i:=1 to N do if used[i]=false then DFS1(i,i);
    for i:=1 to L do
    begin
        readln(p,q);
        map2[p,q]:=true;
        map2[q,p]:=true;
    end;
    for i:=1 to N do used[i]:=false;
    for i:=1 to N do if used[i]=false then DFS2(i,i);
    for i:=1 to N do ansr[b[i]]:=ansr[b[i]]+1;
    for i:=1 to N do write(ansr[b[i]],' ');
end.


program D;
    var N,K,L:longint;
    var i,j,p,q:longint;
    var a:array[1..250000]of longint;
    var b:array[1..250000]of longint;
    var map1:array[1..250000,1..250000]of boolean;
    var map2:array[1..250000,1..250000]of boolean;
    var used:array[1..250000]of boolean;
    var ansr:array[1..250000]of longint;
    procedure DFS1(_,__:longint);
        var ii:longint;
    begin
        used[_]:=true;
        a[_]:=__;
        for ii:=1 to N do
            if (map1[_,ii]=true)AND(used[ii]=false) then DFS1(ii,__);
    end;
    procedure DFS2(_,__:longint);
        var ii:longint;
    begin
        used[_]:=true;
        b[_]:=__;
        for ii:=1 to N do
            if (a[_]=a[ii])AND(map2[_,ii]=true)AND(used[ii]=false) then DFS2(ii,__);
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        map1[i,i]:=true;
        map2[i,i]:=true;
        used[i]:=false;
        ansr[i]:=0;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        map1[p,q]:=true;
        map1[q,p]:=true;
    end;
    for i:=1 to N do if used[i]=false then DFS1(i,i);
    for i:=1 to L do
    begin
        readln(p,q);
        map2[p,q]:=true;
        map2[q,p]:=true;
    end;
    for i:=1 to N do used[i]:=false;
    for i:=1 to N do if used[i]=false then DFS2(i,i);
    for i:=1 to N do ansr[b[i]]:=ansr[b[i]]+1;
    for i:=1 to N do write(ansr[b[i]],' ');
end.


program D;
    var N,K,L:longint;
    var root:array[1..250005]of longint;
    var size:array[1..250005]of longint;
    var colr:array[1..250005]of longint;
    var i,j,p,q:longint;
    function FindRoot(_:longint):longint;
    begin
        if root[_]=_ then exit(_) else exit(FindRoot(root[_]));
    end;
    procedure Merge(_x,_y:longint);
        var x,y:longint;
    begin
        x:=FindRoot(_x);
        y:=FindRoot(_y);
        if x<>y then
            if size[x]<size[y] then
            begin
                root[x]:=y;
                size[y]:=size[y]+size[x];
            end
            else
            begin
                root[y]:=x;
                size[x]:=size[y]+size[x];
            end;
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        Merge(p,q);
    end;
    for i:=1 to N do colr[i]:=FindRoot(i);
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to L do
    begin
        readln(p,q);
        if colr[p]=colr[q] then Merge(p,q);
    end;
    for i:=1 to N do write(size[FindRoot(i)],' ');
end.


program D;
    var N,K,L:longint;
    var root:array[1..100005]of longint;
    var size:array[1..100005]of longint;
    var colr:array[1..100005]of longint;
    var i,j,p,q:longint;
    function FindRoot(_:longint):longint;
    begin
        if root[_]=_ then exit(_) else exit(FindRoot(root[_]));
    end;
    procedure Merge(_x,_y:longint);
        var x,y:longint;
    begin
        x:=FindRoot(_x);
        y:=FindRoot(_y);
        if x<>y then
        if size[x]<size[y] then
        begin
            root[x]:=y;
            size[y]:=size[y]+size[x];
        end
        else
        begin
            root[y]:=x;
            size[x]:=size[y]+size[x];
        end;
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        Merge(p,q);
    end;
    for i:=1 to N do
    begin
        colr[i]:=FindRoot(i);
    end;
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to L do
    begin
        readln(p,q);
        if colr[p]=colr[q] then Merge(p,q);
    end;
    for i:=1 to N-1 do write(size[FindRoot(i)],' ');
    writeln(size[FindRoot(N)])
end.


program D;
    var N,K,L:longint;
    var root:array[1..100005]of longint;
    var size:array[1..100005]of longint;
    var colr:array[1..100005]of longint;
    var i,j,p,q:longint;
    function FindRoot(_:longint):longint;
    begin
        if root[_]=_ then exit(_) else exit(FindRoot(root[_]));
    end;
    procedure Merge(_x,_y:longint);
        var x,y:longint;
    begin
        x:=FindRoot(_x);
        y:=FindRoot(_y);
        if x=y then exit;
        if size[x]<size[y] then
        begin
            root[x]:=y;
            size[y]:=size[y]+size[x];
        end
        else
        begin
            root[y]:=x;
            size[x]:=size[y]+size[x];
        end;
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        Merge(p,q);
    end;
    for i:=1 to N-1 do write(size[FindRoot(i)],' ');
    writeln(size[FindRoot(N)])
end.


program D;
    var N,K,L:longint;
    var used:array[1..100005]of boolean;
    var root:array[1..100005]of longint;
    var size:array[1..100005]of longint;
    var colr:array[1..100005]of longint;
    var i,j,p,q:longint;
    function FindRoot(_:longint):longint;
    begin
        if root[_]=_ then exit(_) else exit(FindRoot(root[_]));
    end;
    procedure Merge(_x,_y:longint);
        var x,y:longint;
    begin
        x:=FindRoot(_x);
        y:=FindRoot(_y);
        if x=y then exit;
        if size[x]<size[y] then
        begin
            root[x]:=y;
            size[y]:=size[y]+size[x];
        end
        else
        begin
            root[y]:=x;
            size[x]:=size[y]+size[x];
        end;
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        used[i]:=false;
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        Merge(p,q);
    end;
    for i:=1 to N do
    begin
        colr[i]:=FindRoot(i);
        used[i]:=false;
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to L do
    begin
        readln(p,q);
        if colr[p]=colr[q] then Merge(p,q);
    end;
    for i:=1 to N-1 do write(size[FindRoot(i)],' ');
    writeln(size[FindRoot(N)])
end.


program D;
    var N,K,L:longint;
    var used:array[1..100005]of boolean;
    var root:array[1..100005]of longint;
    var size:array[1..100005]of longint;
    var nums:array[1..100005]of longint;
    var i,j,p,q:longint;
    function FindRoot(_:longint):longint;
    begin
        if root[_]=_ then exit(_) else exit(FindRoot(root[_]));
    end;
    procedure Merge(_x,_y:longint);
        var x,y:longint;
    begin
        x:=FindRoot(_x);
        y:=FindRoot(_y);
        if x=y then exit;
        if size[x]<size[y] then
        begin
            root[x]:=y;
            size[y]:=size[y]+size[x];
        end
        else
        begin
            root[y]:=x;
            size[x]:=size[y]+size[x];
        end;
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        used[i]:=false;
        root[i]:=i;
        size[i]:=1;
        nums[i]:=1;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        Merge(p,q);
    end;
    for i:=1 to L do
    begin
        readln(p,q);
        if FindRoot(p)=FindRoot(q) then
        begin
            nums[p]:=nums[p]+1;
            nums[q]:=nums[q]+1;
        end;
    end;
    for i:=1 to N-1 do write(nums[i],' ');
    writeln(nums[N])
end.


var
a,b,x,i,k,s:qword;
begin
read(a,b,x);
for i:=a to b do
 begin
  k:=i;
  if k mod x=0 then inc(s);
 end;
 writeln(s);
 end.

Score : 200 points

Problem Statement

You are given nonnegative integers a and b (a ≤ b), and a positive integer x.
Among the integers between a and b, inclusive, how many are divisible by x?

Constraints

0 ≤ a ≤ b ≤ 10^{18}

1 ≤ x ≤ 10^{18}

Input

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

a b x

Output

Print the number of the integers between a and b, inclusive, that are divisible by x.

Sample Input 1

4 8 2

Sample Output 1

3

There are three integers between 4 and 8, inclusive, that are divisible by 2: 4, 6 and 8.

Sample Input 2

0 5 1

Sample Output 2

6

There are six integers between 0 and 5, inclusive, that are divisible by 1: 0, 1, 2, 3, 4 and 5.

Sample Input 3

9 9 2

Sample Output 3

0

There are no integer between 9 and 9, inclusive, that is divisible by 2.

Sample Input 4

1 1000000000000000000 3

Sample Output 4

333333333333333333

Watch out for integer overflows.


var
    n,m,x,i,ans:qword;
begin
    readln(n,m,x);
    i:=n;
    while(i<m+1)do
    begin
        if(i mod x=0)then ans:=ans+1;
        inc(i);
    end;
    writeln(ans);
end.


var a,b,x:longint;
begin
  read(a,b);
if a=0 then a:=a+1;if b=0 then b:=b+1;
writeln(b div x-(a-1) div x);end.


var
  a:array[1..100002] of int64;
  sum,s,x,n:int64;
  i:longint;
begin
  readln(n,x);
  for i:=1 to n do begin read(a[i]);s:=a[i]+a[i-1];if s>x then begin a[i]:=s-x;sum:=sum+s-x;end;end;
  writeln(sum);
end.

Score : 300 points

Problem Statement

There are N boxes arranged in a row.
Initially, the i-th box from the left contains a_i candies.

Snuke can perform the following operation any number of times:

Choose a box containing at least one candy, and eat one of the candies in the chosen box.

His objective is as follows:

Any two neighboring boxes contain at most x candies in total.

Find the minimum number of operations required to achieve the objective.

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i ≤ 10^9

0 ≤ x ≤ 10^9

Input

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

N x
a_1 a_2 ... a_N

Output

Print the minimum number of operations required to achieve the objective.

Sample Input 1

3 3
2 2 2

Sample Output 1

1

Eat one candy in the second box.
Then, the number of candies in each box becomes (2, 1, 2).

Sample Input 2

6 1
1 6 1 2 0 4

Sample Output 2

11

For example, eat six candies in the second box, two in the fourth box, and three in the sixth box.
Then, the number of candies in each box becomes (1, 0, 1, 0, 0, 1).

Sample Input 3

5 9
3 1 4 1 5

Sample Output 3

0

The objective is already achieved without performing operations.

Sample Input 4

2 0
5 5

Sample Output 4

10

All the candies need to be eaten.


{$goto+}
{$asmmode intel}
label 1,2;
var a:array[0..100000]of longint;
    n,x,i:longint;
    ans:int64;
begin
  readln(n,x);
  i:=-1;
  1:i:=-not i;
  read(a[i]);
  if i<not-n then
    goto 1;
  i:=-1;
  2:i:=-not i;
  if a[i]+a[i+1]>x then
  begin
    inc(ans,a[i]+a[i+1]-x);
    if a[i]<=x then
      a[i+1]:=x-a[i]
    else
    begin
      a[i+1]:=0;
      a[i]:=x;
    end;
  end;
  if i<not-not-n then
    goto 2;
  writeln(ans);
end.


var n,x,y,z,i,j,k,ans:longint;
a:array[1..1000000]of longint;
begin
    readln(n,x);
    for i:=1 to n do
    begin
        read(a[i]);
    end;
    for i:=1 to n-1 do
    begin
  //  writeln(i);
        if a[i]+a[i+1]>x then
        begin
            if (i+2<=n)then
            begin
                if a[i+2]+a[i+1]>x then
                begin
                    y:=a[i+1]-(x-a[i]);
                    z:=a[i+1]-(x-a[i+2]);
                    if y>z then
                    begin
                       ans:=ans+y;
                       if y>a[i+1] then
                       begin
                           a[i+2]:=a[i+1]-(y-ans);
                           a[i+1]:=0;
                       end
                       else a[i+1]:=a[i+1]-y;
                    end
                    else
                    begin
                        ans:=ans+z;
                      //  writeln('2');
                        if z>a[i+1] then
                        begin
                           a[i+2]:=a[i+1]-(z-ans);
                           a[i+1]:=0;
                        end
                        else a[i+1]:=a[i+1]-z;
                    end;
                end;
            end
            else
            begin
                y:=a[i+1]+a[i]-x;
                if a[i+1]-y<=0 then
                begin
                    a[i]:=a[i]-(y-a[i+1]);
                    a[i+1]:=0;
                end
                else
                a[i+1]:=a[i+1]-y;
                ans:=ans+y;
            end;
        end;
    end;
    writeln(ans);
end.


program taskJ;
Uses math;
var n,x,i,j,sum,sum2,bye,sum3:longint;
    o:array[1..100000]of longint;
    o2:array[1..100000]of longint;
begin
readln(n,x);
read(o[1]);
o2[1]:=o[1];
for i:=2 to n do
begin
  read(o[i]);
  o2[i]:=o[i];
  if o[i]+o[i-1]>x then
    begin
    sum:=sum+o[i]+o[i-1]-x;
    o[i]:=o[i]-(o[i]+o[i-1]-x);
    if o[i]<0 then
    begin
      o[i-1]:=o[i-1]+o[i];
      o[i]:=0;
    end;
    end;
end;
for i:=2 to n do
begin
  if o2[i]+o2[i-1]>x then
  begin
  bye:=i-1;
  break;
  end;
end;
if bye<>0 then
begin
for i:=bye to n do
sum3:=sum3+o2[i];
sum3:=sum3-((n-bye) div 2)*x;
end
else
sum3:=0;
writeln(min(sum,sum3));
end.


var a:array [1..100000] of longint;
    b,l,i,j,k,s:longint;
begin
        s:=0;
        read(b,l);
        for i:=1 to b do
                read(a[i]);
        for i:=1 to b-1 do
                while (a[i]+a[i+1]>l) do
                begin
                        a[i+1]:=a[i+1]-1;
                        s:=s+1;
                end;
        writeln(s);
end.


var n,x,y,z,i,j,k,ans:longint;
a:array[1..1000000]of longint;
begin
    readln(n,x);
    for i:=1 to n do
    begin
        read(a[i]);
    end;
    for i:=1 to n-1 do
    begin
        if a[i]+a[i+1]>x then
        begin
       // writeln(i,' ',ans);
            if (i+2<=n)then
            begin
                if a[i+2]+a[i+1]>x then
                begin
                    y:=a[i+1]-(x-a[i]);
                    z:=a[i+1]-(x-a[i+2]);
                    if y>z then
                    begin
                       ans:=ans+y;
                    //   writeln('1');
                       if y>a[i+1] then
                       begin
                           a[i+2]:=a[i+1]-(y-ans);
                           a[i+1]:=0;
                       end
                       else a[i+1]:=a[i+1]-y;
                    end
                    else
                    begin
                        ans:=ans+z;
                      //  writeln('2');
                        if z>a[i+1] then
                        begin
                           a[i+2]:=a[i+1]-(z-ans);
                           a[i+1]:=0;
                        end
                        else a[i+1]:=a[i+1]-z;
                    end;
                end;
            end
            else
                begin
                    y:=x-a[i];
                    a[i]:=a[i]-y;
                    ans:=ans+y;
                end;
        end;
    end;
    writeln(ans);
end.


var
n,x,o,i:longint;
a:array [0..100001] of longint;
begin
readln(n,x);
for i := 1 to n do
begin
read(a[i]);
if a[i]>x then
begin
o:=o+(a[i]-x);
a[i]:=x;
end;
end;
for i := 1 to n-1 do
begin
if a[i]+a[i+1]>x then
begin
o:=o+(a[i]+a[i+1]-x);
a[i+1]:=x-a[i];
end;
end;
writeln(o);
end.


var
        s:string;
begin
        readln(s);
        if s[1]=s[length(s)] then
        begin
                if length(s) mod 2=1 then writeln('Second')
                else writeln('First');
        end
        else
        begin
                if length(s) mod 2=0 then writeln('Second')
                else writeln('First');
        end;
end.

Score : 500 points

Problem Statement

There is a string s of length 3 or greater.
No two neighboring characters in s are equal.

Takahashi and Aoki will play a game against each other.
The two players alternately performs the following operation, Takahashi going first:

Remove one of the characters in s, excluding both ends. However, a character cannot be removed if removal of the character would result in two neighboring equal characters in s.

The player who becomes unable to perform the operation, loses the game. Determine which player will win when the two play optimally.

Constraints

3 ≤ |s| ≤ 10^5

s consists of lowercase English letters.

No two neighboring characters in s are equal.

Input

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

s

Output

If Takahashi will win, print First. If Aoki will win, print Second.

Sample Input 1

aba

Sample Output 1

Second

Takahashi, who goes first, cannot perform the operation, since removal of the b, which is the only character not at either ends of s, would result in s becoming aa, with two as neighboring.

Sample Input 2

abc

Sample Output 2

First

When Takahashi removes b from s, it becomes ac.
Then, Aoki cannot perform the operation, since there is no character in s, excluding both ends.

Sample Input 3

abcab

Sample Output 3

First


var
 s:ansistring;
begin
 read(s);
 if s[1]='a' then writeln('First')
 else writeln('Second');
 end.


var
	s:String;
begin
	readln(s);
	if(length(s)mod 2=0)xor(s[1]=s[length(s)])then writeln('Second')else writeln('First');
end.


program B;
    var Q:string;
    var ans:integer;
begin
    readln(Q);
    ans:=0;
    if Q[1]=Q[length(Q)] then ans:=ans+1;
    if length(Q) mod 2=0 then ans:=ans+1;
    if ans mod 2=1 then writeln('Second') else writeln('First');
end.


program canh_toan;
uses math;
const minn=0;
      maxn=trunc(1e5)+5;
      module=trunc(1e9)+7;
var n:longint;
    a,b:array[minn..maxn] of longint;
procedure sorta(l,r:longint);
var i,j,k,tg:longint;
begin
i:=l;j:=r;tg:=a[random(r-l+1)+l];
repeat
while (a[i]<tg) do inc(i);
while (tg<a[j]) do dec(j);
if not(i>j) then
   begin
     k:=a[i];
     a[i]:=a[j];
     a[j]:=k;
     inc(i);
     dec(j);
   end;
until i>j;
if (l<j) then sorta(l,j);
if (i<r) then sorta(i,r);
end;
procedure sortb(l,r:longint);
var i,j,k,tg:longint;
begin
i:=l;j:=r;tg:=b[random(r-l+1)+l];
repeat
while (b[i]<tg) do inc(i);
while (tg<b[j]) do dec(j);
if not(i>j) then
   begin
     k:=b[i];
     b[i]:=b[j];
     b[j]:=k;
     inc(i);
     dec(j);
   end;
until i>j;
if (l<j) then sortb(l,j);
if (i<r) then sortb(i,r);
end;
function nhannp(x,y:int64):int64;
var tg:int64;
begin
if y=0 then exit(0)
else if y=1 then exit(x mod module)
else
  begin
    tg:=nhannp(x,y div 2) mod module;
    tg:=(2*tg) mod module;
    if y mod 2=1 then tg:=(tg+x) mod module;
    exit(tg mod module);
  end;
end;
procedure main();
var i,j,x,y:longint;
    kq:int64;
begin
readln(n);
for i:=1 to n do read(a[i]);
readln;
for j:=1 to n do read(b[j]);
sorta(1,n);sortb(1,n);
i:=1;j:=1;x:=0;y:=0;kq:=1;
while (i<=n) and (j<=n) do
  begin
    if (a[i]<b[j]) then
       begin
         y:=x+1;
         inc(i);
       end
    else
       begin
         y:=x-1;
         inc(j);
       end;
    if (abs(x)>abs(y)) then kq:=nhannp(kq,abs(x)) mod module;
    x:=y;
  end;
while (i<=n) do
  begin
    kq:=nhannp(kq,abs(x)) mod module;
    inc(x);
    inc(i);
  end;
while (j<=n) do
  begin
    kq:=nhannp(kq,abs(x)) mod module;
    dec(x);
    inc(j);
  end;
write(kq);
end;
BEGIN
  main();
END.

Score : 500 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

There are N computers and N sockets in a one-dimensional world.
The coordinate of the i-th computer is a_i, and the coordinate of the i-th socket is b_i.
It is guaranteed that these 2N coordinates are pairwise distinct.

Snuke wants to connect each computer to a socket using a cable.
Each socket can be connected to only one computer.

In how many ways can he minimize the total length of the cables?
Compute the answer modulo 10^9+7.

Constraints

1 ≤ N ≤ 10^5

0 ≤ a_i, b_i ≤ 10^9

The coordinates are integers.

The coordinates are pairwise distinct.

Input

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

N
a_1
:
a_N
b_1
:
b_N

Output

Print the number of ways to minimize the total length of the cables, modulo 10^9+7.

Sample Input 1

2
0
10
20
30

Sample Output 1

2

There are two optimal connections: 0-20, 10-30 and 0-30, 10-20.
In both connections the total length of the cables is 40.

Sample Input 2

3
3
10
8
7
12
5

Sample Output 2

1


program canh_toan;
uses math;
const minn=0;
      maxn=trunc(1e5)+5;
      vao='CONNECT.INP';
      ra='CONNECT.OUT';
      module=trunc(1e9)+7;
var n:longint;
    a,b:array[minn..maxn] of longint;
procedure sorta(l,r:longint);
var i,j,k,tg:longint;
begin
i:=l;j:=r;tg:=a[random(r-l+1)+l];
repeat
while (a[i]<tg) do inc(i);
while (tg<a[j]) do dec(j);
if not(i>j) then
   begin
     k:=a[i];
     a[i]:=a[j];
     a[j]:=k;
     inc(i);
     dec(j);
   end;
until i>j;
if (l<j) then sorta(l,j);
if (i<r) then sorta(i,r);
end;
procedure sortb(l,r:longint);
var i,j,k,tg:longint;
begin
i:=l;j:=r;tg:=b[random(r-l+1)+l];
repeat
while (b[i]<tg) do inc(i);
while (tg<b[j]) do dec(j);
if not(i>j) then
   begin
     k:=b[i];
     b[i]:=b[j];
     b[j]:=k;
     inc(i);
     dec(j);
   end;
until i>j;
if (l<j) then sortb(l,j);
if (i<r) then sortb(i,r);
end;
function nhannp(x,y:int64):int64;
var tg:int64;
begin
if y=0 then exit(0)
else if y=1 then exit(x mod module)
else
  begin
    tg:=nhannp(x,y div 2) mod module;
    tg:=(2*tg) mod module;
    if y mod 2=1 then tg:=(tg+x) mod module;
    exit(tg mod module);
  end;
end;
procedure main();
var i,j,x,y:longint;
    kq:int64;
begin
readln(n);
for i:=1 to n do read(a[i]);
readln;
for j:=1 to n do read(b[j]);
sorta(1,n);sortb(1,n);
i:=1;j:=1;x:=0;y:=0;kq:=1;
while (i<=n) and (j<=n) do
  begin
    if (a[i]<b[j]) then
       begin
         y:=x+1;
         inc(i);
       end
    else
       begin
         y:=x-1;
         inc(j);
       end;
    if (abs(x)>abs(y)) then kq:=nhannp(kq,abs(x)) mod module;
    x:=y;
  end;
while (i<=n) do
  begin
    kq:=nhannp(kq,abs(x)) mod module;
    inc(x);
    inc(i);
  end;
while (j<=n) do
  begin
    kq:=nhannp(kq,abs(x)) mod module;
    dec(x);
    inc(j);
  end;
write(kq);
end;
BEGIN
  main();
END.


var
 a,b:ansistring;
 i:longint;
begin
 read(a);
 for i:=1 to length(a) do b:=a[i]+b;
 for i:=1 to length(a) do
  if b[i]='b' then b[i]:='d'
   else if b[i]='d' then b[i]:='b'
    else if b[i]='p' then b[i]:='q'
     else b[i]:='p';
 if a=b then writeln('Yes')
  else writeln('No');
end.

Score : 100 points

Problem Statement

You are given a string S consisting of letters b, d, p and q.
Determine whether S is a mirror string.

Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S:

Reverse the order of the characters in S.

Replace each occurrence of b by d, d by b, p by q, and q by p, simultaneously.

Constraints

1 \leq |S| \leq 10^5

S consists of letters b, d, p, and q.

Input

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

S

Output

If S is a mirror string, print Yes. Otherwise, print No.

Sample Input 1

pdbq

Sample Output 1

Yes

Sample Input 2

ppqb

Sample Output 2

No


var
  s,t:string;
  i:longint;
begin
  readln(s);
  for i:=length(s) downto 1 do t:=t+s[i];
  for i:=1 to length(t) do
  case t[i] of
   'b': t[i]:='d';
   'd': t[i]:='b';
   'p': t[i]:='q';
   'q': t[i]:='p';
  end;
  if t=s then writeln('Yes')
         else writeln('No');
end.


var s,s1:ansistring;i:longint;ch:char;
begin
  readln(s);s1:=s;
  for i:=1 to length(s) div 2 do
  begin
    ch:=s[i];
    s[i]:=s[length(s)-i+1];
    s[length(s)-i+1]:=ch;
  end;
  for i:=1 to length(s) do
    if s[i]='b' then s[i]:='d'
      else if s[i]='d' then s[i]:='b'
        else if s[i]='p' then s[i]:='q'
          else s[i]:='p';
  if s=s1 then writeln('Yes') else writeln('No');
end.


var i,n:longint;
    a,b:ansistring;
begin
  readln(a);
  n:=length(a);
  for i:=1 to n do b[n-i+1]:=a[i];
  for i:=1 to n do
  if b[i]='b' then b[i]:='d'
  else if b[i]='d' then b[i]:='b'
  else if b[i]='p' then b[i]:='q'
  else b[i]:='p';
  if a=b then writeln('Yes')
  else writeln('No');
end.


var s,s1:string;
    i,len:longint;
begin
 readln(s);
 len:=length(s);
 for i:=1 to len do
 case s[i] of
  'b':s1:='d'+s1;
  'd':s1:='b'+s1;
  'p':s1:='q'+s1;
  'q':s1:='p'+s1;
 end;
 if (s=s1) then writeln('Yes') else writeln('No');
end.


var
	ch:ansistring;
	i,l,t:longint;
function _to(x:char):char;
begin
	case x of
		'b':exit('d');
		'd':exit('b');
		'p':exit('q');
		'q':exit('p');
	end;
end;
procedure error;
begin
	writeln('No');
	halt;
end;
begin
	readln(ch);
	l:=length(ch);
	if l mod 2=1 then error;
	t:=l div 2;
	for i:=1 to t do
		if _to(ch[i])<>ch[l-i+1] then error;
	writeln('Yes');
end.


var
        s,s1,s2:ansistring;
        i,l:longint;
begin
        readln(s);
        s2:=s;
        l:=length(s);
        for i:=1 to l do begin
                if s[i]='b' then begin
                        s[i]:='d';
                        continue;
                end;
                if s[i]='d' then begin
                        s[i]:='b';
                        continue;
                end;
                if s[i]='p' then begin
                        s[i]:='q';
                        continue;
                end;
                if s[i]='q' then begin
                        s[i]:='p';
                        continue;
                end;
        end;
        for i:=l downto 1 do s1:=s1+s[i];
        if s1=s2 then writeln('Yes') else writeln('No');
end.


var
  s:ansistring;
  i,j,n,k,m,t:longint;
begin
  readln(s);
  k:=length(s);
  if k mod 2<>0 then
  begin
    writeln('NO');
    halt;
  end;
  for i:=1 to k div 2 do
  begin
    t:=abs(ord(s[i])-ord(s[k-i+1]));
    if (t<>1)and(t<>2)and(t<>0) then
    begin
      writeln('No');
      halt;
    end;
  end;
  writeln('Yes');
end.


var
  s:ansistring;
  i,j,n,k,m,t:longint;
begin
  readln(s);
  k:=length(s);
  if k mod 2<>0 then
  begin
    writeln('NO');
    halt;
  end;
  for i:=1 to k div 2 do
  begin
    t:=abs(ord(s[i])-ord(s[k-i+1]));
    if (t<>1)and(t<>2) then
    begin
      writeln('No');
      halt;
    end;
  end;
  writeln('Yes');
end.


var
	N,i,j,sz:Longint;
	A:Array[1..100,1..99]of Longint;
	ans:String;
begin
	read(N);
	if N=2 then begin
		writeln(-1);
		exit;
	end;
	if N mod 2=1 then begin
		for i:=1 to N-1 do A[N,i]:=i;
		i:=1;
		while i<=N-1 do begin
			for j:=1 to i-1 do begin
				A[i,j]:=j;
				A[i+1,j]:=j;
			end;
			A[i,i]:=i+1;
			A[i+1,i+1]:=i;
			A[i,i+1]:=i+2;
			A[i+1,i]:=i+2;
			for j:=i+2 to N-1 do begin
				A[i,j]:=j+1;
				A[i+1,j]:=j+1;
			end;
			inc(i,2);
		end;
	end else begin
		i:=1;
		while i<=N-2 do begin
			for j:=1 to i-1 do begin
				A[i,j]:=j;
				A[i+1,j]:=j;
			end;
			A[i,i]:=i+1;
			A[i+1,i+1]:=i;
			A[i,i+1]:=i+2;
			A[i+1,i]:=i+2;
			for j:=i+2 to N-1 do begin
				A[i,j]:=j+1;
				A[i+1,j]:=j+1;
			end;
			inc(i,2);
		end;
		for j:=1 to N-3 do begin
			A[N-1,j]:=j;
			A[N,j]:=j;
		end;
		A[N-1,N-2]:=N;
		A[N,N-1]:=N-1;
		A[N-1,N-1]:=N-2;
		A[N,N-2]:=N-2;
	end;
	setlength(ans,3*N*N);
	sz:=0;
	for i:=1 to N do begin
		for j:=1 to N-1 do begin
			if A[i,j]<10 then begin
				inc(sz);
				ans[sz]:=Chr(A[i,j]+48);
			end else if A[i,j]<100 then begin
				inc(sz);
				ans[sz]:=Chr(A[i,j]div 10+48);
				inc(sz);
				ans[sz]:=Chr(A[i,j]mod 10+48);
			end else begin
				inc(sz);
				ans[sz]:='1';
				inc(sz);
				ans[sz]:='0';
				inc(sz);
				ans[sz]:='0';
			end;
			inc(sz);
			ans[sz]:=#10;
		end;
	end;
	setlength(ans,sz);
	write(ans);
end.

Score : 100 points

Problem Statement

There are N persons, conveniently numbered 1 through N. They will take 3y3s Challenge for N-1 seconds.

During the challenge, each person must look at each of the N-1 other persons for 1 seconds, in some order.

If any two persons look at each other during the challenge, the challenge ends in failure.

Find the order in which each person looks at each of the N-1 other persons, to be successful in the challenge.

Constraints

2 \leq N \leq 100

Input

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

N

Output

If there exists no way to be successful in the challenge, print -1.

If there exists a way to be successful in the challenge, print any such way in the following format:

A_{1,1} A_{1,2} ... A_{1, N-1}
A_{2,1} A_{2,2} ... A_{2, N-1}
:
A_{N,1} A_{N,2} ... A_{N, N-1}

where A_{i, j} is the index of the person that the person numbered i looks at during the j-th second.

Judging

The output is considered correct only if all of the following conditions are satisfied:

1 \leq A_{i,j} \leq N

For each i, A_{i,1}, A_{i,2}, ... , A_{i, N-1} are pairwise distinct.

Let X = A_{i, j}, then A_{X, j} \neq i always holds.

Sample Input 1

7

Sample Output 1

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

Sample Input 2

2

Sample Output 2

-1


var
  a:array[1..1000000] of int64;
  n,s:int64;
  i:longint;
begin
  readln(n);
  for i:=1 to n do
    begin
      inc(s,i);
      a[i]:=i;
      if s>n then break;
    end;
  s:=s-n;
  for i:=1 to n do if (a[i]<>s) and (a[i]<>0) then writeln(a[i]);
end.

Score : 300 points

Problem Statement

The problem set at CODE FESTIVAL 20XX Finals consists of N problems.

The score allocated to the i-th (1≦i≦N) problem is i points.

Takahashi, a contestant, is trying to score exactly N points. For that, he is deciding which problems to solve.

As problems with higher scores are harder, he wants to minimize the highest score of a problem among the ones solved by him.

Determine the set of problems that should be solved.

Constraints

1≦N≦10^7

Partial Score

200 points will be awarded for passing the test set satisfying 1≦N≦1000.

Additional 100 points will be awarded for passing the test set without additional constraints.

Input

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

N

Output

Among the sets of problems with the total score of N, find a set in which the highest score of a problem is minimum, then print the indices of the problems in the set in any order, one per line.

If there exists more than one such set, any of them will be accepted.

Sample Input 1

4

Sample Output 1

1
3

Solving only the 4-th problem will also result in the total score of 4 points, but solving the 1-st and 3-rd problems will lower the highest score of a solved problem.

Sample Input 2

7

Sample Output 2

1
2
4

The set \{3,4\} will also be accepted.

Sample Input 3

1

Sample Output 3

1


var i,k,n:longint;
begin
read(n);
while n>0 do begin
inc(k);
dec(n,k);
end;
for i:=1 to k do if i<>-n then writeln(i);
end.


uses math;
var
	N,A,ans,p,now,tmp:int64;
	j,k:Longint;
begin
	read(N,A);
	ans:=N;
	for k:=1 to 39 do begin
		p:=floor(power(N/1,1/(k+1)));
		now:=1;
		for j:=1 to k do now:=now*p;
		for j:=0 to k do begin
			tmp:=A*k+p*k+j+(N+now-1)div now;
			if ans>tmp then ans:=tmp;
			now:=now div p*(p+1);
		end;
	end;
	writeln(ans);
end.

Score : 1000 points

Problem Statement

Rng is baking cookies.

Initially, he can bake one cookie per second.

He can also eat the cookies baked by himself.
When there are x cookies not yet eaten, he can choose to eat all those cookies. After he finishes eating those cookies, the number of cookies he can bake per second becomes x. Note that a cookie always needs to be baked for 1 second, that is, he cannot bake a cookie in 1/x seconds when x > 1.
When he choose to eat the cookies, he must eat all of them; he cannot choose to eat only part of them.
It takes him A seconds to eat the cookies regardless of how many, during which no cookies can be baked.

He wants to give N cookies to Grandma.
Find the shortest time needed to produce at least N cookies not yet eaten.

Constraints

1≦N≦10^{12}

0≦A≦10^{12}

A is an integer.

Partial Score

500 points will be awarded for passing the test set satisfying N≦10^6 and A≦10^6.

Additional 500 points will be awarded for passing the test set without additional constraints.

Input

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

N A

Output

Print the shortest time needed to produce at least N cookies not yet eaten.

Sample Input 1

8 1

Sample Output 1

7

It is possible to produce 8 cookies in 7 seconds, as follows:

After 1 second: 1 cookie is done.

After 2 seconds: 1 more cookie is done, totaling 2. Now, Rng starts eating those 2 cookies.

After 3 seconds: He finishes eating the cookies, and he can now bake 2 cookies per second.

After 4 seconds: 2 cookies are done.

After 5 seconds: 2 more cookies are done, totaling 4.

After 6 seconds: 2 more cookies are done, totaling 6.

After 7 seconds: 2 more cookies are done, totaling 8.

Sample Input 2

1000000000000 1000000000000

Sample Output 2

1000000000000


var dd,xx,nn,n,d,x,ans:extended;
    i:longint;
begin
  readln(n,d,x);
  nn:=n*2;
  for i:=trunc(n) downto 1 do
  begin
    ans:=ans+d+(nn-1)/2*x;
    dd:=d+(2*d+5*x)/nn;
    xx:=x+4*x/nn;
    x:=xx;d:=dd;
    nn:=nn-2;
  end;
  writeln(ans:0:10);
end.

Score : 1000 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

There are N balls and N+1 holes in a line. Balls are numbered 1 through N from left to right. Holes are numbered 1 through N+1 from left to right. The i-th ball is located between the i-th hole and (i+1)-th hole. We denote the distance between neighboring items (one ball and one hole) from left to right as d_i (1 \leq i \leq 2 \times N). You are given two parameters d_1 and x. d_i - d_{i-1} is equal to x for all i (2 \leq i \leq 2 \times N).

We want to push all N balls into holes one by one. When a ball rolls over a hole, the ball will drop into the hole if there is no ball in the hole yet. Otherwise, the ball will pass this hole and continue to roll. (In any scenario considered in this problem, balls will never collide.)

In each step, we will choose one of the remaining balls uniformly at random and then choose a direction (either left or right) uniformly at random and push the ball in this direction. Please calculate the expected total distance rolled by all balls during this process.

For example, when N = 3, d_1 = 1, and x = 1, the following is one possible scenario:

first step: push the ball numbered 2 to its left, it will drop into the hole numbered 2. The distance rolled is 3.

second step: push the ball numbered 1 to its right, it will pass the hole numbered 2 and drop into the hole numbered 3. The distance rolled is 9.

third step: push the ball numbered 3 to its right, it will drop into the hole numbered 4. The distance rolled is 6.

So the total distance in this scenario is 18.

Note that in all scenarios every ball will drop into some hole and there will be a hole containing no ball in the end.

Constraints

1 \leq N \leq 200,000

1 \leq d_1 \leq 100

0 \leq x \leq 100

All input values are integers.

Input

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

N d_1 x

Output

Print a floating number denoting the answer. The relative or absolute error of your answer should not be higher than 10^{-9}.

Sample Input 1

1 3 3

Sample Output 1

4.500000000000000

The distance between the only ball and the left hole is 3 and distance between the only ball and the right hole is 6. There are only two scenarios (i.e. push left and push right). The only ball will roll 3 and 6 unit distance, respectively. So the answer for this example is (3+6)/2 = 4.5.

Sample Input 2

2 1 0

Sample Output 2

2.500000000000000

Sample Input 3

1000 100 100

Sample Output 3

649620280.957660079002380


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  w,h,n,ni,i,j,a,x,y,sum:Integer;
  xy:array of array of Integer;
begin
  try
    { TODO -oUser -cConsole Main : ぬりえ }
    Read(w);
    Read(h);
    Readln(n);

    SetLength(xy,h,w);

    for i := 0 to h - 1 do
      for j := 0 to w - 1 do
        xy[i,j]:=0;

    for ni := 0 to n-1 do begin
      Read(x);
      Read(y);
      Readln(a);
      case a of
        1:begin
          for i := 0 to h - 1 do
            for j := 0 to x - 1 do
              xy[i,j]:=1;
        end;
        2:begin
          for i := 0 to h-1 do
            for j := x to w - 1 do
              xy[i,j]:=1;
        end;
        3:begin
          for i := 0 to y - 1 do
            for j := 0 to w - 1 do
              xy[i,j]:=1;
        end;
        4:begin
          for i := y to h - 1 do
            for j := 0 to w - 1 do
              xy[i,j]:=1;
        end;
      end;
    end;

    sum:=0;
    for i := 0 to h - 1 do
      for j := 0 to w - 1 do
        sum:=sum+(xy[i,j]xor 1 xor 0);

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

Score : 200 points

Problem Statement

There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.

Snuke plotted N points into the rectangle. The coordinate of the i-th (1 ≦ i ≦ N) point was (x_i, y_i).

Then, he created an integer sequence a of length N, and for each 1 ≦ i ≦ N, he painted some region within the rectangle black, as follows:

If a_i = 1, he painted the region satisfying x < x_i within the rectangle.

If a_i = 2, he painted the region satisfying x > x_i within the rectangle.

If a_i = 3, he painted the region satisfying y < y_i within the rectangle.

If a_i = 4, he painted the region satisfying y > y_i within the rectangle.

Find the area of the white region within the rectangle after he finished painting.

Constraints

1 ≦ W, H ≦ 100

1 ≦ N ≦ 100

0 ≦ x_i ≦ W (1 ≦ i ≦ N)

0 ≦ y_i ≦ H (1 ≦ i ≦ N)

W, H (21:32, added), x_i and y_i are integers.

a_i (1 ≦ i ≦ N) is 1, 2, 3 or 4.

Input

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

W H N
x_1 y_1 a_1
x_2 y_2 a_2
:
x_N y_N a_N

Output

Print the area of the white region within the rectangle after Snuke finished painting.

Sample Input 1

5 4 2
2 1 1
3 3 4

Sample Output 1

9

The figure below shows the rectangle before Snuke starts painting.

First, as (x_1, y_1) = (2, 1) and a_1 = 1, he paints the region satisfying x < 2 within the rectangle:

Then, as (x_2, y_2) = (3, 3) and a_2 = 4, he paints the region satisfying y > 3 within the rectangle:

Now, the area of the white region within the rectangle is 9.

Sample Input 2

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

Sample Output 2

0

It is possible that the whole region within the rectangle is painted black.

Sample Input 3

10 10 5
1 6 1
4 1 3
6 9 4
9 4 2
3 1 3

Sample Output 3

64


var
    s : string;
    cnt,i : longint;
begin
    read(s);
    for i := 1 to length(s)-1 do
    begin
        if s[i] <> s[i+1] then
            cnt := cnt + 1;
    end;
    writeln(cnt);
end.

Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

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

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


var s:ansistring;
    i,j,cnt:longint;
    b:boolean;
begin
  readln(s);
  cnt:=0;
  b:=false;
  while (b=false) do begin
    b:=true;
    for i:=1 to length(s) do begin
      if (s[1]<>s[i]) then begin
        b:=false;
        break;
      end;
    end;
    if (b=false) then begin
      if s[i]='W' then begin
        insert('W',s,1);
        inc(cnt);
        for j:=2 to i do s[j]:='W';
      end
      else begin
        insert('B',s,1);
        inc(cnt);
        for j:=2 to i do s[j]:='B';
      end;
    end
    else break;
  end;
  writeln(cnt);
end.


var s:ansistring;
    i,j,cnt,k:longint;
    b:boolean;
begin
  readln(s);
  cnt:=0;
  b:=false;
  k:=2;
  while (b=false) do begin
    //writeln(s);
    b:=true;
    for i:=length(s) downto k do begin
      if (s[1]<>s[i]) then begin
        b:=false;
        break;
      end;
    end;
    if (b=false) then begin
      for i:=1 to length(s) do begin
        if (s[1]<>s[i]) then begin
          if s[i]='W' then begin
            insert('W',s,1);
            inc(cnt);
            for j:=2 to i do s[j]:='W';
            k:=i;
            //writeln(s);
            break;
          end
          else begin
            insert('B',s,1);
            inc(cnt);
            for j:=2 to i do s[j]:='B';
            k:=i;
            //writeln(s);
            break;
          end;
        end;
      end;
    end
    else break;
  end;
  writeln(cnt);
end.


var
	n,a,m,p,i,s:Longint;
begin
	readln(n);
	m:=1000000000;
	for i:=1 to n do begin
		read(a);
		if a<m then m:=a;
		if p<a-m then begin
			p:=a-m;
			s:=0;
		end;
		if p=a-m then inc(s);
	end;
	writeln(s);
end.

Score : 400 points

Problem Statement

There are N towns located in a line, conveniently numbered 1 through N. Takahashi the merchant is going on a travel from town 1 to town N, buying and selling apples.

Takahashi will begin the travel at town 1, with no apple in his possession. The actions that can be performed during the travel are as follows:

Move: When at town i (i < N), move to town i + 1.

Merchandise: Buy or sell an arbitrary number of apples at the current town. Here, it is assumed that one apple can always be bought and sold for A_i yen (the currency of Japan) at town i (1 ≦ i ≦ N), where A_i are distinct integers. Also, you can assume that he has an infinite supply of money.

For some reason, there is a constraint on merchandising apple during the travel: the sum of the number of apples bought and the number of apples sold during the whole travel, must be at most T. (Note that a single apple can be counted in both.)

During the travel, Takahashi will perform actions so that the profit of the travel is maximized. Here, the profit of the travel is the amount of money that is gained by selling apples, minus the amount of money that is spent on buying apples. Note that we are not interested in apples in his possession at the end of the travel.

Aoki, a business rival of Takahashi, wants to trouble Takahashi by manipulating the market price of apples. Prior to the beginning of Takahashi's travel, Aoki can change A_i into another arbitrary non-negative integer A_i' for any town i, any number of times. The cost of performing this operation is |A_i - A_i'|. After performing this operation, different towns may have equal values of A_i.

Aoki's objective is to decrease Takahashi's expected profit by at least 1 yen. Find the minimum total cost to achieve it. You may assume that Takahashi's expected profit is initially at least 1 yen.

Constraints

1 ≦ N ≦ 10^5

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

A_i are distinct.

2 ≦ T ≦ 10^9

In the initial state, Takahashi's expected profit is at least 1 yen.

Input

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

N T
A_1 A_2 ... A_N

Output

Print the minimum total cost to decrease Takahashi's expected profit by at least 1 yen.

Sample Input 1

3 2
100 50 200

Sample Output 1

1

In the initial state, Takahashi can achieve the maximum profit of 150 yen as follows:

Move from town 1 to town 2.

Buy one apple for 50 yen at town 2.

Move from town 2 to town 3.

Sell one apple for 200 yen at town 3.

If, for example, Aoki changes the price of an apple at town 2 from 50 yen to 51 yen, Takahashi will not be able to achieve the profit of 150 yen. The cost of performing this operation is 1, thus the answer is 1.

There are other ways to decrease Takahashi's expected profit, such as changing the price of an apple at town 3 from 200 yen to 199 yen.

Sample Input 2

5 8
50 30 40 10 20

Sample Output 2

2

Sample Input 3

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

Sample Output 3

2


var

  i,s:longint;

n:string;
begin

  read(n);

  for i:=1 to length(n) do

   if n[i]='C' then

    for s:=i+1 to length(n) do

      if n[s]='F' then

       begin

         writeln('Yes');

         halt;

       end;

  writeln('No');

end.

Score : 100 points

Problem Statement

This contest is CODEFESTIVAL, which can be shortened to the string CF by deleting some characters.

Mr. Takahashi, full of curiosity, wondered if he could obtain CF from other strings in the same way.

You are given a string s consisting of uppercase English letters.
Determine whether the string CF can be obtained from the string s by deleting some characters.

Constraints

2 ≤ |s| ≤ 100

All characters in s are uppercase English letters (A-Z).

Input

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

s

Output

Print Yes if the string CF can be obtained from the string s by deleting some characters.
Otherwise print No.

Sample Input 1

CODEFESTIVAL

Sample Output 1

Yes

CF is obtained by deleting characters other than the first character C and the fifth character F.

Sample Input 2

FESTIVALCODE

Sample Output 2

No

FC can be obtained but CF cannot be obtained because you cannot change the order of the characters.

Sample Input 3

CF

Sample Output 3

Yes

It is also possible not to delete any characters.

Sample Input 4

FCF

Sample Output 4

Yes

CF is obtained by deleting the first character.


var s:string;
i,j:longint;
begin
readln(s);
for i :=1 to length(s) do
  for j:=i+1 to length(s) do
    if (s[i]+s[j]='CF')then
      begin
      writeln('Yes');
      halt;
      end;
writeln('No');
end.


var
	s : ansistring;
begin
	readln(s);
	if (pos('C',s) < pos('F',s)) and (pos('C',s) <> 0) and (pos('F',s) <> 0) then writeln('Yes')
	else writeln('No');
end.


var n,k,s,i:Longint;begin read(n,k);s:=k;for i:=2 to n do s:=s*(k-1);write(s)end.

Score : 200 points

Problem Statement

There are N balls placed in a row.
AtCoDeer the deer is painting each of these in one of the K colors of his paint cans.
For aesthetic reasons, any two adjacent balls must be painted in different colors.

Find the number of the possible ways to paint the balls.

Constraints

1≦N≦1000

2≦K≦1000

The correct answer is at most 2^{31}-1.

Input

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

N K

Output

Print the number of the possible ways to paint the balls.

Sample Input 1

2 2

Sample Output 1

2

We will denote the colors by 0 and 1. There are two possible ways: we can either paint the left ball in color 0 and the right ball in color 1, or paint the left in color 1 and the right in color 0.

Sample Input 2

1 10

Sample Output 2

10

Since there is only one ball, we can use any of the ten colors to paint it. Thus, the answer is ten.


var
  i,x,y,t:longint;
  s:ansistring;
begin
  readln(s);
  x:=0;y:=0;t:=0;
  for i:=1 to length(s) do
   if x=y then
     begin
       x:=x+1;
       if s[i]='p' then t:=t-1;
     end
    else
     begin
       y:=y+1;
       if s[i]='g' then t:=t+1;
     end;
  writeln(t);
end.

Score : 300 points

Problem Statement

AtCoDeer the deer and his friend TopCoDeer is playing a game.
The game consists of N turns.
In each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition:

(※) After each turn, (the number of times the player has played Paper)≦(the number of times the player has played Rock).

Each player's score is calculated by (the number of turns where the player wins) - (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.

(For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)

With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the N turns, before the game starts.
Plan AtCoDeer's gesture in each turn to maximize AtCoDeer's score.

The gesture that TopCoDeer will play in each turn is given by a string s. If the i-th (1≦i≦N) character in s is g, TopCoDeer will play Rock in the i-th turn. Similarly, if the i-th (1≦i≦N) character of s in p, TopCoDeer will play Paper in the i-th turn.

Constraints

1≦N≦10^5

N=|s|

Each character in s is g or p.

The gestures represented by s satisfy the condition (※).

Input

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

s

Output

Print the AtCoDeer's maximum possible score.

Sample Input 1

gpg

Sample Output 1

0

Playing the same gesture as the opponent in each turn results in the score of 0, which is the maximum possible score.

Sample Input 2

ggppgggpgg

Sample Output 2

2

For example, consider playing gestures in the following order: Rock, Paper, Rock, Paper, Rock, Rock, Paper, Paper, Rock, Paper. This strategy earns three victories and suffers one defeat, resulting in the score of 2, which is the maximum possible score.


var
        ans,x,y,i:longint;
        s:string;
begin
        readln(s);
        for i:=1 to length(s) do
        begin
                if x=y then
                begin
                        inc(x);
                        if s[i]='p' then dec(ans);
                end
                else
                begin
                        inc(y);
                        if s[i]='g' then inc(ans);
                end;
        end;
        writeln(ans);
end.


var s:ansistring;
i,y,ans:longint;
begin
readln(s);
for i:=1 to length(s) do
  if s[i]='g' then
    begin
    if y>0 then
      begin
      inc(ans);inc(y);
      end
    else
      begin
      y:=y+1;
      dec(ans);
      end;
    end
  else
    begin
    if y>0 then y:=y-1
    else
      begin
      inc(y);
      dec(ans);
      end;
    end;
writeln(ans);
end.


var
 s:ansistring;
 i,a,b,ans:longint;
begin
 readln(s);
 for i:=1 to length(s) do
  if a=b then begin inc(a);if s[i]='p' then dec(ans);end
         else begin inc(b);if s[i]='g' then inc(ans);end;
 writeln(ans);
end.


var
	n,i,x,y:Integer;
	a,b,c,d:int64;
begin
	read(n);
	a:=1;
	b:=1;
	for i:=1 to n do begin
		read(x,y);
		c:=(a-1)div x+1;
		d:=(b-1)div y+1;
		if c<d then c:=d;
		a:=c*x;
		b:=c*y;
	end;
	writeln(a+b);
end.

Score : 300 points

Problem Statement

AtCoDeer the deer is seeing a quick report of election results on TV.
Two candidates are standing for the election: Takahashi and Aoki.
The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes.
AtCoDeer has checked the report N times, and when he checked it for the i-th (1≦i≦N) time, the ratio was T_i:A_i.
It is known that each candidate had at least one vote when he checked the report for the first time.

Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the N-th time.
It can be assumed that the number of votes obtained by each candidate never decreases.

Constraints

1≦N≦1000

1≦T_i,A_i≦1000 (1≦i≦N)

T_i and A_i (1≦i≦N) are coprime.

It is guaranteed that the correct answer is at most 10^{18}.

Input

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

N
T_1 A_1
T_2 A_2
:
T_N A_N

Output

Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.

Sample Input 1

3
2 3
1 1
3 2

Sample Output 1

10

When the numbers of votes obtained by the two candidates change as 2,3 → 3,3 → 6,4, the total number of votes at the end is 10, which is the minimum possible number.

Sample Input 2

4
1 1
1 1
1 5
1 100

Sample Output 2

101

It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.

Sample Input 3

5
3 10
48 17
31 199
231 23
3 2

Sample Output 3

6930


var
	n,i:Longint;
	a,b,x,y,c,d:int64;
begin
	read(n);
	a:=1;
	b:=1;
	for i:=1 to n do begin
		read(x,y);
		c:=(a-1)div x+1;
		d:=(b-1)div y+1;
		if c<d then c:=d;
		a:=c*x;
		b:=c*y;
	end;
	writeln(a+b);
end.


var n,a,b,x,y,p,q,tmp,tmp1,tmp2,tmp3:int64;
function gcd(a,b:int64):int64; begin if b=0 then exit(a); exit(gcd(b,a mod b)); end;
procedure exgcd(a,b:int64; var x,y:int64);
  var t:int64;
  begin
    if b=0 then begin
      x:=tmp; y:=0; exit;
    end;
    exgcd(b,a mod b,x,y);
    t:=x; x:=y; y:=t-a div b*y;
  end;
begin
  readln(n); a:=1; b:=1;
  while (n>0) do begin
    dec(n); readln(p,q); tmp:=-a*q+b*p;
    exgcd(q,p,x,y);
    tmp1:=(x mod p+p) mod p; tmp2:=y+(x-tmp1) div p*q;
    if (tmp2>0) then begin
      tmp3:=tmp2 mod q-q;
      x:=tmp1-(tmp3-tmp2) div q*p;
      y:=tmp3;
    end else begin x:=tmp1; y:=tmp2; end;
    a:=a+x; b:=b-y;
  end;
  writeln(a+b);
end.


var n,a,b,x,y,p,q,tmp,tmp1,tmp2:int64;
function gcd(a,b:int64):int64; begin if b=0 then exit(a); exit(gcd(b,a mod b)); end;
procedure exgcd(a,b:int64; var x,y:int64);
  var t:int64;
  begin
    if b=0 then begin
      x:=tmp; y:=0; exit;
    end;
    exgcd(b,a mod b,x,y);
    t:=x; x:=y; y:=t-a div b*y;
  end;
begin
  readln(n); a:=1; b:=1;
  while (n>0) do begin
    dec(n); readln(p,q); tmp:=-a*q+b*p;
    exgcd(q,p,x,y);//writeln(x,' ',y);
    while (x-p>=0) do begin dec(x,p); inc(y,q); end;
    while (x<0) do begin inc(x,p); dec(y,q); end;
    while (y>0) do begin inc(x,p); dec(y,q); end;
    a:=a+x; b:=b-y;
  end;
  writeln(a+b);
end.


var
	i,t:Longint;
	s:String;
begin
	readln(s);
	for i:=1 to length(s)do inc(t,ord(s[i])mod 11-3);
	if t>=0 then writeln(t div 2)else writeln((t-1)div 2);
end.


var
  i, g, p: integer;
  s: string;
begin
  readln(s);
  for i := 1 to length(s) do
  begin
    if s[i] = 'g' then
      g := g + 1;
    if s[i] = 'p' then
      p := p + 1;
  end;

  if p < g then
    writeln((g - p) div 2)
  else
    writeln(0);
end.


const maxn = 100050;
var n,i,now,ans:longint;
    st:ansistring;
    f:array[0..maxn] of longint;
begin
  readln(st); n:=length(st); now:=0; ans:=0;
  for i:=1 to n do begin
    if st[i]='g' then begin
      if (now+1)<=(i>>1) then begin inc(now); inc(ans); end;
      continue;
    end;
    if (now+1)<=(i>>1) then inc(now) else dec(ans);
  end;
  writeln(ans);
end.


var n,a,b,i:Longint;
s:String;
begin
readln(n,a,b);
readln(s);
for i:=1 to n do begin
if s[i]='a'then begin
if a>0 then begin dec(a);writeln('Yes');end
else if b>0 then begin dec(b);writeln('Yes');end
else writeln('No');
end else if s[i]='b'then begin
if b>0 then begin dec(b);writeln('Yes');end
else writeln('No');
end else writeln('No');
end;
end.

Score : 200 points

Problem Statement

There are N participants in the CODE FESTIVAL 2016 Qualification contests. The participants are either students in Japan, students from overseas, or neither of these.

Only Japanese students or overseas students can pass the Qualification contests. The students pass when they satisfy the conditions listed below, from the top rank down. Participants who are not students cannot pass the Qualification contests.

A Japanese student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B.

An overseas student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B and the student ranks B-th or above among all overseas students.

A string S is assigned indicating attributes of all participants. If the i-th character of string S is a, this means the participant ranked i-th in the Qualification contests is a Japanese student; b means the participant ranked i-th is an overseas student; and c means the participant ranked i-th is neither of these.

Write a program that outputs for all the participants in descending rank either Yes if they passed the Qualification contests or No if they did not pass.

Constraints

1≦N,A,B≦100000

A+B≦N

S is N characters long.

S consists only of the letters a, b and c.

Input

Inputs are provided from Standard Input in the following form.

N A B
S

Output

Output N lines. On the i-th line, output Yes if the i-th participant passed the Qualification contests or No if that participant did not pass.

Sample Input 1

10 2 3
abccabaabb

Sample Output 1

Yes
Yes
No
No
Yes
Yes
Yes
No
No
No

The first, second, fifth, sixth, and seventh participants pass the Qualification contests.

Sample Input 2

12 5 2
cabbabaacaba

Sample Output 2

No
Yes
Yes
Yes
Yes
No
Yes
Yes
No
Yes
No
No

The sixth participant is third among overseas students and thus does not pass the Qualification contests.

Sample Input 3

5 2 2
ccccc

Sample Output 3

No
No
No
No
No


        {$mode objfpc}
        {$coperators on}
program Lexicographical_disorder________________________CODE_FESTIVAL_2016_qual_B____________________________________________________________;
const
        fi = 'lex.inp';
        fo = 'lex.out';
        sz=round(1e5)+100;
type
    Tnode = ^node;
    node  =
        record
            c:array['_'..'z'] of Tnode;
            cnt:longint;
        end;
var
    n,q,kth:longint;
    a:array[1..sz]of ansistring;
    s:string;
    nod,root:Tnode;
    count:array[0..sz,'_'..'z','_'..'z']of longint;

function min(const x,y:longint):longint; begin if x<=y then exit(x) else exit(y); end;
function max(const x,y:longint):longint; begin if x>=y then exit(x) else exit(y); end;
procedure spaw(var nod:Tnode);
var i:char;
    begin
        new(nod);
        nod^.cnt:=0;
        for i:='_' to 'z' do nod^.c[i]:=nil;
    end;

procedure enter;
var i : integer;
    begin
        readln(n);
        for i:=1 to n do
            begin
                readln(a[i]);
                a[i]:=a[i]+'_';
            end;
        readln(q);
    end;
procedure update(const a:ansistring);
var ch,rr:char;
    i:longint;
    begin
        nod:=root;
        for i:=1 to length(a) do
            begin
                ch:=a[i];
                if nod^.c[ch]=nil then spaw(nod^.c[ch]);
                nod := nod^.c[ch];
                inc(nod^.cnt);
            end;
    end;
procedure updateElement(p:longint);
var i:longint;
    ch,rr:char;
    begin
        nod:=root;
        for i:=1 to length(a[p]) do
            begin
                ch:=a[p][i];
                for rr:='_' to 'z' do if (rr<>ch)and(nod^.c[rr]<>nil) then
                    count[p][ch][rr] += nod^.c[rr]^.cnt;
                nod:=nod^.c[ch];
            end;
    end;
procedure Build();
var i:longint;
    begin
        spaw(root);
        for i:=1 to n do
            begin
                update(a[i]);
            end;
        for i:=1 to n do
            begin
                updateElement(i);
            end;
    end;
procedure doQuerry;
var i,j:longint;
    ans:longint;
    begin
        ans:=0;
        for i:=1 to 26 do
            for j:=1 to i-1 do ans += count[kth,s[i],s[j]];
        for i:=1 to 26 do ans += count[kth,s[i],'_'];
        ans+=1;
        writeln(ans);
    end;
procedure solve;
var i: integer;
    begin
        Build();
        for i:=1 to q do
            begin
                read(kth);
                readln(s); delete(s,1,1);
                doQuerry;
            end;
    end;
procedure print();
var i:longint;
    begin
    end;
begin
        enter(); solve(); print();
end.

Score : 1100 points

Problem Statement

There are N strings of lowercase alphabet only. The i-th string is S_i. Every string is unique.

Provide answers for the Q queries below. The i-th query has the following format:

Query: An integer k_i and a string p_{i,1}p_{i,2}...p_{i,26} that results from permuting {a,b,...,z} are given. Output the sequence of the string S_{k_i} among the N strings in lexicographical order when the literal sequence is p_{i,1}<p_{i,2}<...<p_{i,26}.

Constraints

1 ≦ N,Q ≦ 100000

1 ≦ |S_i| (1 ≦ i ≦ N)

S_i (1 ≦ i ≦ N) is a string of lowercase alphabet.

The sum of |S_i| is no more than 400000.

Every S_i is unique.

1 ≦ k_i ≦ N (1 ≦ i ≦ Q)

For all 1 ≦ i ≦ Q, p_{i,1}p_{i,2}...p_{i,26} is a permutation of abcd...z.

Input

Inputs are provided from standard inputs in the following form.

N
S_1
:
S_N
Q
k_1 p_{1,1}p_{1,2}...p_{1,26}
:
k_Q p_{Q,1}p_{Q,2}...p_{Q,26}

Output

Output Q lines.

On line i, for the i-th query, output an integer indicating the sequence of the string S_{k_i} among the N strings in lexicographical order.

Sample Input 1

5
aa
abbaa
abbba
aaab
aaaaaba
5
1 abcdefghijklmnopqrstuvwxyz
2 bacdefghijklmnopqrstuvwxyz
3 abcdefghijklmnopqrstuvwxyz
4 bacdefghijklmnopqrstuvwxyz
5 abcdefghijklmnopqrstuvwxyz

Sample Output 1

1
2
5
4
2

When the literal sequence is a < b, sorting the input strings in lexicographical order yields aa, aaaaaba, aaab, abbaa, abbba. The answers to queries 1, 3, and 5 are thus 1, 5, and 2, respectively.

When the literal sequence is b < a, sorting the input strings in lexicographical order yields abbba, abbaa, aa, aaab, aaaaaba. The answers to queries 2 and 4 are thus 2 and 4, respectively.

Sample Input 2

8
abrakatabra
abadaba
abracadabra
atcoder
grand
contest
ababa
a
6
3 abcdefghijklmnopqrstuvwxyz
6 qwertyuiopasdfghjklzxcvbnm
8 poiuytrewqlkjhgfdsamnbvcxz
2 qazwsxedcrfvtgbyhnujmikolp
1 plokmijnuhbygvtfcrdxeszwaq
4 mnbvcxzasdfghjklpoiuytrewq

Sample Output 2

4
8
2
3
4
7


uses math;

procedure mofile;
begin
        {$IFDEF ONLINE_JUDGE}
        assign(input,'');
        reset(input);
        assign(output,'');
        rewrite(output);
        {$ELSE}
        assign(input,'checking.inp');
        reset(input);
        assign(output,'');
        rewrite(output);
        {$ENDIF}
end;

procedure dongfile;
begin
        close(input);
        close(output);
end;

const
        maxc=1000000000+69;
        modulo=1000000000+7;
        maxn=400000+10;

type
        re=record
                x,y:longint;
        end;

var
        n,q,dem,res:longint;
        s:array[0..maxn] of ansistring;
        a,c:array[0..maxn*4,'@'..'z'] of longint;
        al:ansistring;

procedure insert(p,x,i:longint);
begin
        if x=length(s[i])+1 then exit;
        inc(c[p][s[i,x]]);
        if a[p][s[i,x]]>0 then insert(a[p][s[i,x]],x+1,i) else
        begin
                inc(dem);
                a[p][s[i,x]]:=dem;
                insert(dem,x+1,i);
        end;
end;

procedure nhapdl;
var
        i:longint;
begin
        readln(n);
        for i:=1 to n do
        begin
                readln(s[i]);
                s[i]:=s[i]+'@';
                insert(0,1,i);
        end;
end;

procedure dfs(p,x,i:longint);
var
        j:longint;
begin
        if x=length(s[i])+1 then exit;
        for j:=1 to 27 do
        if al[j]=s[i,x] then break else
        begin
                inc(res,c[p,al[j]]);
        end;
        dfs(a[p][s[i,x]],x+1,i);
end;

procedure xuli;
var
        o,i,j,x:longint;
        blank:char;
begin
        readln(q);
        for o:=1 to q do
        begin
                readln(x,blank,al);
                al:='@'+al;

                res:=0;
                dfs(0,1,x);

                writeln(res+1);
        end;
end;

begin
        //mofile;
        nhapdl;
        xuli;
        dongfile;
end.


var
 st:ansistring;
 i,s1,s2:longint;
begin
 readln(st);
 for i:=1 to length(st) do
  begin
   if st[i]='S' then inc(s1);
   if (s1>0)and(st[i]='T') then
    begin
     dec(s1);
     s2:=s2+2;
    end;
  end;
 writeln(length(st)-s2);
end.

Score : 300 points

Problem Statement

We have a string X, which has an even number of characters. Half the characters are S, and the other half are T.

Takahashi, who hates the string ST, will perform the following operation 10^{10000} times:

Among the occurrences of ST in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.

Find the eventual length of X.

Constraints

2 ≦ |X| ≦ 200,000

The length of X is even.

Half the characters in X are S, and the other half are T.

Partial Scores

In test cases worth 200 points, |X| ≦ 200.

Input

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

X

Output

Print the eventual length of X.

Sample Input 1

TSTTSS

Sample Output 1

4

In the 1-st operation, the 2-nd and 3-rd characters of TSTTSS are removed.
X becomes TTSS, and since it does not contain ST anymore, nothing is done in the remaining 10^{10000}-1 operations.
Thus, the answer is 4.

Sample Input 2

SSTTST

Sample Output 2

0

X will eventually become an empty string: SSTTST ⇒ STST ⇒ ST ⇒ ``.

Sample Input 3

TSSTTTSS

Sample Output 3

4

X will become: TSSTTTSS ⇒ TSTTSS ⇒ TTSS.


var
i,t,x:longint;
s:ansistring;
begin
readln(s);
t:=length(s);
x:=0;
for i:=1 to length(s) do
  if s[i]='S' then inc(x)
    else if x>=1 then
      begin
        x:=x-1;
        t:=t-2;
      end;
writeln(t);
end.


var
i,t,x:longint;
s:ansistring;
begin
readln(s);
t:=length(s);
x:=0;
for i:=1 to length(s) do
  if s[i]='S' then inc(x)
    else
      begin
        x:=x-1;
        t:=t-2;
      end;
writeln(t);
end.


var
s:ansistring;
begin
readln(s);
while pos('ST',s)<>0 do delete(s,pos('ST',s),2);
writeln(length(s));
end.


var s:ansistring;
    i,m,t:longint;
begin
  readln(s);
  for i:=1 to length(s) do
  begin
    if s[i]='S' then inc(m);
    if (m<>0)and(s[i]='T') then
    begin
      dec(m);
      inc(t,2);
    end;
  end;
  writeln(length(s)-t);
end.


var
 s:ansistring;
 k:int64;
begin
 readln(s);
 while pos('ST',s)<>0 do
  begin
   k:=pos('ST',s);
   delete(s,k,2);
  end;
 if s<>'' then writeln(length(s));
end.


var
  st:ansistring;
begin
  readln(st);
  while (pos('ST',st)>0) and (length(st)>0) do delete(st,pos('ST',st),2);
  if length(st)>0 then writeln(length(st));
end.


var
 s:string;
 k:longint;
begin
 readln(s);
 while pos('ST',s)<>0 do
  begin
   k:=pos('ST',s);
   delete(s,k,2);
  end;
 if s<>'' then writeln(length(s));
end.


 var
   s,t:ansistring;
   i,m,ans,n:longint;
begin
       readln(s);
       n:=length(s);
       m:=0; t:='';
       for i:=1 to n do
       begin
                if(s[i]='T') and (m<>0) and (t[m]='S')then  begin delete(t,m,1); dec(m); end
                else begin inc(m); t:=t+s[i];  end;
       end;
      writeln(m);
end.


 var
   s,t:ansistring;
   i,m,ans,n:longint;
begin
       readln(s);
       n:=length(s);
       m:=0; t:='';
       for i:=1 to n do
       begin
                if(s[i]='T') and (m<>0) and (t[m]='S')then  dec(m)
                else begin inc(m); t:=t+s[i];  end;
       end;
      writeln(m);
end.


var f:array[0..2000,0..2000] of int64;
    a:array[1..2000] of int64;
    n,i,j:longint;
    tot,minn,x:int64;
function left(k:longint):longint;
begin
  exit(((k-2+n) mod n)+1);
end;
function min(a,b:int64):int64;
begin
  if a<b then exit(a);
  exit(b);
end;
begin
  minn:=100000000000000000;
  readln(n,x);
  for i:=1 to n do readln(a[i]);
  for i:=1 to n do f[i,0]:=a[i];
  for j:=1 to n-1 do
    for i:=1 to n do
      f[i,j]:=min({f[left(i),j-1]}f[i,j-1],f[left(i),j-1]);
  for i:=0 to n-1 do
  begin
    tot:=x*i;
    for j:=1 to n do tot:=tot+f[j,i];
    if tot<minn then minn:=tot;
  end;
  writeln(minn);
end.

Score : 400 points

Problem Statement

Snuke lives in another world, where slimes are real creatures and kept by some people.
Slimes come in N colors. Those colors are conveniently numbered 1 through N.
Snuke currently has no slime. His objective is to have slimes of all the colors together.

Snuke can perform the following two actions:

Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.

Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.

Find the minimum time that Snuke needs to have slimes in all N colors.

Constraints

2≤N≤2,000

a_i are integers.

1≤a_i≤10^9

x is an integer.

1≤x≤10^9

Input

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

N x
a_1 a_2 ... a_N

Output

Find the minimum time that Snuke needs to have slimes in all N colors.

Sample Input 1

2 10
1 100

Sample Output 1

12

Snuke can act as follows:

Catch a slime in color 1. This takes 1 second.

Cast the spell. The color of the slime changes: 1 → 2. This takes 10 seconds.

Catch a slime in color 1. This takes 1 second.

Sample Input 2

3 10
100 1 100

Sample Output 2

23

Snuke can act as follows:

Catch a slime in color 2. This takes 1 second.

Cast the spell. The color of the slime changes: 2 → 3. This takes 10 seconds.

Catch a slime in color 2. This takes 1 second.

Cast the soell. The color of each slime changes: 3 → 1, 2 → 3. This takes 10 seconds.

Catch a slime in color 2. This takes 1 second.

Sample Input 3

4 10
1 2 3 4

Sample Output 3

10

Snuke can act as follows:

Catch a slime in color 1. This takes 1 second.

Catch a slime in color 2. This takes 2 seconds.

Catch a slime in color 3. This takes 3 seconds.

Catch a slime in color 4. This takes 4 seconds.


program V802;
 var
  a,b,c:array[0..501,0..501] of char;
  i,j,m,n:longint;
 begin
  readln(n,m);
  for i:=1 to n do
   begin
    for j:=1 to m do
     read(a[i,j]);
    readln;
   end;
  for i:=1 to n do
   for j:=1 to m do
    if (i mod 2=0) and (j<>m) then b[i,j]:='#'
                              else b[i,j]:='.';
  for i:=1 to n do
   b[i,1]:='#';
  for i:=1 to n do
   for j:=1 to m do
    if (i mod 2=1) and (j<>1) then c[i,j]:='#'
                              else c[i,j]:='.';
  for i:=1 to n do
   c[i,m]:='#';
  for i:=1 to n do
   begin
    for j:=1 to m do
     if a[i,j]='#' then write(a[i,j])
                   else write(b[i,j]);
    writeln;
   end;
  writeln;
  for i:=1 to n do
   begin
    for j:=1 to m do
     if a[i,j]='#' then write(a[i,j])
                   else write(c[i,j]);
    writeln;
   end;
 end.

Score : 700 points

Problem Statement

Snuke and Ciel went to a strange stationery store. Each of them got a transparent graph paper with H rows and W columns.

Snuke painted some of the cells red in his paper. Here, the cells painted red were 4-connected, that is, it was possible to traverse from any red cell to any other red cell, by moving to vertically or horizontally adjacent red cells only.

Ciel painted some of the cells blue in her paper. Here, the cells painted blue were 4-connected.

Afterwards, they precisely overlaid the two sheets in the same direction. Then, the intersection of the red cells and the blue cells appeared purple.

You are given a matrix of letters a_{ij} (1≤i≤H, 1≤j≤W) that describes the positions of the purple cells. If the cell at the i-th row and j-th column is purple, then a_{ij} is #, otherwise a_{ij} is .. Here, it is guaranteed that no outermost cell is purple. That is, if i=1, H or j = 1, W, then a_{ij} is ..

Find a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation described. It can be shown that a solution always exists.

Constraints

3≤H,W≤500

a_{ij} is # or ..

If i=1,H or j=1,W, then a_{ij} is ..

At least one of a_{ij} is #.

Input

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

H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}

Output

Print a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation, as follows:

The first H lines should describe the positions of the red cells.

The following 1 line should be empty.

The following H lines should describe the positions of the blue cells.

The description of the positions of the red or blue cells should follow the format of the description of the positions of the purple cells.

Sample Input 1

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

Sample Output 1

.....
#####
#....
#####
.....

.###.
.#.#.
.#.#.
.#.#.
.....

One possible pair of the set of the positions of the red cells and the blue cells is as follows:

Sample Input 2

7 13
.............
.###.###.###.
.#.#.#...#...
.###.#...#...
.#.#.#.#.#...
.#.#.###.###.
.............

Sample Output 2

.............
.###########.
.###.###.###.
.###.###.###.
.###.###.###.
.###.###.###.
.............

.............
.###.###.###.
.#.#.#...#...
.###.#...#...
.#.#.#.#.#...
.#.#########.
.............

One possible pair of the set of the positions of the red cells and the blue cells is as follows:


program ec12;
var
	n,m,i,j,tot:longint;
	point:array[1..2510000,1..2] of longint;
	vis:array[1..501,1..501] of longint;
	a:array[1..501,1..501] of char;
procedure qsrt(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=point[(l+r) div 2][1];
	repeat
	while (point[i][1]<v) or ((point[i][1]=v) and (point[i][2]<point[(l+r) div 2][2])) do inc(i);
	while (point[j][1]>v) or ((point[j][1]=v) and (point[j][2]>point[(l+r) div 2][2])) do dec(j);
	if i<=j then
	begin
		s:=point[i][1];
		point[i][1]:=point[j][1];
		point[j][1]:=s;
		s:=point[i][2];
		point[i][2]:=point[j][2];
		point[j][2]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	fillchar(vis,sizeof(vis),0);
	readln(n,m);
	tot:=0;
	for i:=1 to n do
	begin
		for j:=1 to m do
		begin
			read(a[i,j]);
			if a[i,j]='#' then
			begin
				inc(tot);
				vis[i,j]:=1;
				point[tot,1]:=i;
				point[tot,2]:=j;
			end;
		end;
		readln;
	end;
	qsrt(1,tot);
	for i:=1 to tot do
	begin
		if i>1 then
		begin
			if point[i][1]=point[i-1][1] then
			begin
				for j:=point[i-1][2] to point[i][2] do
				begin
					if vis[point[i][1],j]=0 then
					vis[point[i][1],j]:=2;
				end;
			end
			else
			begin
				if point[i][2]=point[i-1][2] then
				begin
					for j:=point[i-1][1] to point[i][1] do
					begin
						if vis[j,point[i][2]]=0 then
						vis[j,point[i][2]]:=2;
					end;
				end
				else
				begin
					if point[i][2]<point[i-1][2] then
					begin
						for j:=point[i-1][1] to point[i][1] do
						begin
							if vis[j,point[i-1][2]]=0 then
							vis[j,point[i-1][2]]:=2;
						end;
						for j:=point[i][2] to point[i-1][2] do
						begin
							if vis[point[i][1],j]=0 then
							vis[point[i][1],j]:=2;
						end;
					end
					else
					begin
						for j:=point[i-1][1] to point[i][1] do
						begin
							if vis[j,point[i-1][2]]=0 then
							vis[j,point[i-1][2]]:=2;
						end;
						for j:=point[i-1][2] to point[i][2] do
						begin
							if vis[point[i][1],j]=0 then
							vis[point[i][1],j]:=2;
						end;
					end;
				end;
			end;
		end;
	end;
	for i:=1 to n do
	begin
		for j:=1 to m do
		begin
			if vis[i,j]>=1 then
			write('#')
			else
			write('.');
		end;
		writeln;
	end;
	writeln;
	for i:=1 to n do
	begin
		for j:=1 to m do
		begin
			if vis[i,j]<=1 then
			write('#')
			else
			write('.');
		end;
		writeln;
	end;
end.


const mo=1000000007;
var
	n,m,i,j:longint;
	f:Array[0..600,0..600]of char;
begin
	readln(n,m);
	for i:=0 to n-1 do
	begin
		for j:=0 to m-1 do read(f[i,j]);
		readln;
	end;
	for i:=0 to n-1 do
	begin
		for j:=0 to m-1 do
		begin
			if j=0 then write('.')
			else if i and 1=0 then write('#')
			else if (i and 1<>0)and(j=m-1)then write('#')
			else if f[i,j]='#' then write('#')
			else write('.');
		end;
		writeln;
	end;
	writeln;
	for i:=0 to n-1 do
	begin
		for j:=0 to m-1 do
		begin
			if j=m-1 then write('.')
			else if i and 1<>0 then write('#')
			else if (i and 1=0)and(j=0)then write('#')
			else if f[i,j]='#' then write('#')
			else write('.');
		end;
		writeln;
	end;
end.


program Project1;

{$APPTYPE CONSOLE}

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

    Writeln(((n+k-abs(n-k))div 2 * x) + ((n-k+abs(n-k)) div 2 * y));//nとkの最小値*x、n-kと0の最大値*y
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

There is a hotel with the following accommodation fee:

X yen (the currency of Japan) per night, for the first K nights

Y yen per night, for the (K+1)-th and subsequent nights

Tak is staying at this hotel for N consecutive nights.
Find his total accommodation fee.

Constraints

1 \leq N, K \leq 10000

1 \leq Y < X \leq 10000

N,\,K,\,X,\,Y are integers.

Input

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

N
K
X
Y

Output

Print Tak's total accommodation fee.

Sample Input 1

5
3
10000
9000

Sample Output 1

48000

The accommodation fee is as follows:

10000 yen for the 1-st night

10000 yen for the 2-nd night

10000 yen for the 3-rd night

9000 yen for the 4-th night

9000 yen for the 5-th night

Thus, the total is 48000 yen.

Sample Input 2

2
3
10000
9000

Sample Output 2

20000


var
  n:ansistring;
  i:longint;
begin
  readln(n);
  for i:=1 to length(n) do
    begin
      if n[i]=n[i+1] then
        begin
          writeln(i,' ',i+1);exit;
        end;
      if n[i]=n[i+2] then
        begin
          writeln(i,' ',i+2);exit;
        end;
    end;
  writeln('-1 -1');
end.

Score : 400 points

Problem Statement

Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both voodoo and melee are unbalanced, while neither noon nor a is.

You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.

Constraints

2 ≦ |s| ≦ 10^5

s consists of lowercase letters.

Partial Score

200 points will be awarded for passing the test set satisfying 2 ≦ N ≦ 100.

Input

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

s

Output

If there exists no unbalanced substring of s, print -1 -1.

If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print a b. If there exists more than one such substring, any of them will be accepted.

Sample Input 1

needed

Sample Output 1

2 5

The string s_2 s_3 s_4 s_5 = eede is unbalanced. There are also other unbalanced substrings. For example, the output 2 6 will also be accepted.

Sample Input 2

atcoder

Sample Output 2

-1 -1

The string atcoder contains no unbalanced substring.


var
  n:string;
  i:longint;
begin
  readln(n);
  for i:=1 to length(n) do
    begin
      if n[i]=n[i+1] then
        begin
          writeln(i+1,' ',i+2);exit;
        end;
      if n[i]=n[i+2] then
        begin
          writeln(i+1,' ',i+3);exit;
        end;
    end;
  writeln('-1 -1');
end.


var
	i:Longint;
	s:String;
begin
	readln(s);
	for i:=2 to length(s)do begin
		if s[i]=s[i-1]then begin
			writeln(i-1,' ',i);
			exit;
		end;
		if s[i]=s[i-2]then begin
			writeln(i-2,' ',i);
			exit;
		end;
	end;
	writeln('-1 -1');
end.


var
	i:Longint;
	s:String;
begin
	readln(s);
	for i:=2 to length(s)do begin
		if s[i]=s[i-1]then begin
			writeln(i-1,' ',i);
			exit;
		end;
		if(i>2)and(s[i]=s[i-2])then begin
			writeln(i-2,' ',i);
			exit;
		end;
	end;
	writeln('-1 -1');
end.


var
	n,i:Longint;
	s:String;
begin
	readln(s);
	n:=length(s);
	i:=1;
	while(i<n)and not((s[i]=s[i+1])or((i+1<n)and(s[i]=s[i+2])))do inc(i);
	if i=n then writeln('-1 -1')else if s[i]=s[i+1] then writeln(i,' ',i+1)else writeln(i,' ',i+2);
end.


var
 i,p:longint;
 s:ansistring;
 j,ch:char;
 a:array['a'..'z'] of longint;
begin
 readln(s);
 for i:=1 to length(s) do
  inc(a[s[i]]);
 for j:='a' to 'z' do
  if a[j]>=length(s) div 2 then
   begin
    ch:=j;p:=1;
    break;
   end;
 if p=0 then writeln('-1 -1')
  else
   begin
 i:=1;
 while s[i]<>ch do inc(i);
 p:=length(s);
 while s[p]<>ch do dec(p);
 writeln(i,' ',p);
  end;
end.


var n,i,s:integer;
begin
       read(n);s:=0;
       for i:=1 to n do
        s:=s+i;
        write(s);
        readln;
end.

Score : 100 points

Problem Statement

There are N children in AtCoder Kindergarten. Mr. Evi will arrange the children in a line, then give 1 candy to the first child in the line, 2 candies to the second child, ..., N candies to the N-th child. How many candies will be necessary in total?

Constraints

1≦N≦100

Input

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

N

Output

Print the necessary number of candies in total.

Sample Input 1

3

Sample Output 1

6

The answer is 1+2+3=6.

Sample Input 2

10

Sample Output 2

55

The sum of the integers from 1 to 10 is 55.

Sample Input 3

1

Sample Output 3

1

Only one child. The answer is 1 in this case.


program Project1;

{$APPTYPE CONSOLE}

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

    n:=1;
    for i := 1 to Length(s) do begin
      if s[i]='B' then begin
        if not(e='') then begin
          Delete(e, n-1, 1);
          n:=n-1;
        end;
      end else begin
        e:=e+s[i];
        inc(n);
      end;
    end;

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

Score : 200 points

Problem Statement

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.

To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:

The 0 key: a letter 0 will be inserted to the right of the string.

The 1 key: a letter 1 will be inserted to the right of the string.

The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.

Sig has launched the editor, and pressed these keys several times. You are given a string s, which is a record of his keystrokes in order. In this string, the letter 0 stands for the 0 key, the letter 1 stands for the 1 key and the letter B stands for the backspace key. What string is displayed in the editor now?

Constraints

1 ≦ |s| ≦ 10 (|s| denotes the length of s)

s consists of the letters 0, 1 and B.

The correct answer is not an empty string.

Input

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

s

Output

Print the string displayed in the editor in the end.

Sample Input 1

01B0

Sample Output 1

00

Each time the key is pressed, the string in the editor will change as follows: 0, 01, 0, 00.

Sample Input 2

0BB1

Sample Output 2

1

Each time the key is pressed, the string in the editor will change as follows: 0, (empty), (empty), 1.


var
i,a,b,s:longint;
begin
s:=1;
read(a,b);
for i:=a to b do
s:=s*i;
if s=0 then writeln('Zero')
else if s<0 then writeln('Negative')
else writeln('Positive');
end.

Problem Statement

You are given two integers a and b (a≤b). Determine if the product of the integers a, a+1, …, b is positive, negative or zero.

Constraints

a and b are integers.

-10^9≤a≤b≤10^9

Partial Score

In test cases worth 100 points, -10≤a≤b≤10.

Input

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

a b

Output

If the product is positive, print Positive. If it is negative, print Negative. If it is zero, print Zero.

Sample Input 1

1 3

Sample Output 1

Positive

1×2×3=6 is positive.

Sample Input 2

-3 -1

Sample Output 2

Negative

(-3)×(-2)×(-1)=-6 is negative.

Sample Input 3

-1 1

Sample Output 3

Zero

(-1)×0×1=0.