var
  i,j,flag:longint;
  n,s,left,right,tot:qword;
  p,x:array[1..100000]of qword;
begin
  readln(n,s);
  for i:=1 to n do
    readln(x[i],p[i]);
  if s<=x[1] then
    begin
      write(x[n]-s);
      exit;
    end;
  if s>x[n] then
    begin
      write(s-x[1]);
      exit;
    end;
  left:=1;
  right:=n;
  if p[1]>=p[n] then begin p[1]:=p[1]+p[n]; dec(right);flag:=1;end
                else begin p[n+1]:=p[n]+p[1]; inc(left);flag:=-1;end;
  tot:=tot+x[n]-x[1];
  while (x[left]<s)and(x[right]>s) do
    begin
      if p[left]>=p[right] then begin
         p[left]:=p[left]+p[right];
         if flag<>1 then begin tot:=tot+x[right]-x[left];  flag:=1;end;
         dec(right); end
                           else begin
         p[right]:=p[left]+p[right];
         if flag<>-1 then begin tot:=tot+x[right]-x[left];  flag:=-1;end;
         inc(left);   end;
    end;
    if x[left]>s then writeln(tot+x[right]-s)
                 else writeln(tot+s-x[left]);
end.

Score : 1200 points

Problem Statement

There are N apartments along a number line, numbered 1 through N.
Apartment i is located at coordinate X_i.
Also, the office of AtCoder Inc. is located at coordinate S.
Every employee of AtCoder lives in one of the N apartments.
There are P_i employees who are living in Apartment i.

All employees of AtCoder are now leaving the office all together.
Since they are tired from work, they would like to get home by the company's bus.
AtCoder owns only one bus, but it can accommodate all the employees.
This bus will leave coordinate S with all the employees and move according to the following rule:

Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off.

Repeat the operation above as long as there is one or more employees on the bus.

For a specific example, see Sample Input 1.

The bus takes one seconds to travel a distance of 1.
The time required to vote and get off the bus is ignorable.

Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time.
Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future.
Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction.

Find the time the bus will take from departure to arrival at the last employees' apartment.
It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time.

Constraints

1 \leq N \leq 10^5

1 \leq S \leq 10^9

1 \leq X_1 < X_2 < ... < X_N \leq 10^9

X_i \neq S ( 1 \leq i \leq N )

1 \leq P_i \leq 10^9 ( 1 \leq i \leq N )

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N S
X_1 P_1
X_2 P_2
:
X_N P_N

Output

Print the number of seconds the bus will take from departure to arrival at the last employees' apartment.

Sample Input 1

3 2
1 3
3 4
4 2

Sample Output 1

4

Assume that the bus moves in the negative direction first.
Then, the coordinate of the bus changes from 2 to 1, and the employees living in Apartment 1 get off.
The movement of the bus after that is obvious: it just continues moving in the positive direction.
Thus, if the bus moves in the negative direction first, the coordinate of the bus changes as 2 → 1 → 2 → 3 → 4 from departure.
The time it takes to get home for the employees living in Apartment 1, 2, 3 are 1, 3, 4 seconds, respectively.

Next, assume that the bus moves in the positive direction first.
Then, the coordinate of the bus changes from 2 to 3, and the employees living in Apartment 2 get off.
Afterwards, the bus starts heading to Apartment 1, because there are more employees living in Apartment 1 than in Apartment 3.
Then, after arriving at Apartment 1, the bus heads to Apartment 3.
Thus, if the bus moves in the positive direction first, the coordinate of the bus changes as 2 → 3 → 2 → 1 → 2 → 3 → 4 from departure.
The time it takes to get home for the employees living in Apartment 1, 2, 3 are 3, 1, 6 seconds, respectively.

Therefore, in the beginning, the employees living in Apartment 1 or 3 will try to move the bus in the negative direction.
On the other hand, the employees living in Apartment 2 will try to move the bus in the positive direction in the beginning.
There are a total of 3 + 2 = 5 employees living in Apartment 1 and 3 combined, which is more than those living in Apartment 2, which is 4.
Thus, the bus will move in the negative direction first, and the coordinate of the bus will change as 2 → 1 → 2 → 3 → 4 from departure.

Sample Input 2

6 4
1 10
2 1000
3 100000
5 1000000
6 10000
7 100

Sample Output 2

21

Since the numbers of employees living in different apartments are literally off by a digit, the bus consistently head to the apartment where the most number of employees on the bus lives.

Sample Input 3

15 409904902
94198000 15017
117995501 7656764
275583856 313263626
284496300 356635175
324233841 607
360631781 148
472103717 5224
497641071 34695
522945827 816241
554305668 32
623788284 22832
667409501 124410641
876731548 12078
904557302 291749534
918215789 5

Sample Output 3

2397671583


var n,s,max,i,left,right:longint;
  count:int64;
   x,p:array[1..100000]of longint;
begin
 readln(n,s);
 for i:=1 to n do
   begin
   readln(x[i],p[i]);
  end;
  if s<=x[1] then
    begin
    count:=x[n]-s;
    exit;
    end;
    left:=1;
    right:=n;
    if p[1]<p[n] then max:=2
    else max:=1;
    if p[1]<=p[n] then begin p[n]:=p[n]+p[1]; count:=count+x[right]-x[left]; inc(left);  end
                  else begin p[1]:=p[1]+p[n]; count:=count+x[right]-x[left];dec(right); end;
   repeat
  if p[left]>=p[right] then
   begin
     p[left]:=p[left]+p[right];
     if max=2 then count:=count+x[right]-x[left];
     dec(right);
     if max=2 then max:=1;
   end
   else begin
     p[right]:=p[right]+p[left];
    if max=1 then count:=count+x[right]-x[left];
     inc(left);
     if max=1 then max:=2;
       end;
   until(x[left]>s)or(x[right]<s);
  if x[left]>s then write(count+x[right]-s);
  if x[right]<s then write(count+s-x[left])
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 n,s,count,max,i,left,right:longint;
   x,p:array[1..100000]of longint;
begin
 readln(n,s);
 for i:=1 to n do
   begin
   readln(x[i],p[i]);
  end;
  if s<=x[1] then
    begin
    count:=x[n]-s;
    exit;
    end;
    left:=1;
    right:=n;
    if p[1]<p[n] then max:=2
    else max:=1;
    if p[1]<=p[n] then begin p[n]:=p[n]+p[1]; count:=count+x[right]-x[left]; inc(left);  end
                  else begin p[1]:=p[1]+p[n]; count:=count+x[right]-x[left];dec(right); end;
   repeat
  if p[left]>=p[right] then
   begin
     p[left]:=p[left]+p[right];
     if max=2 then count:=count+x[right]-x[left];
     dec(right);
     if max=2 then max:=1;
   end
   else begin
     p[right]:=p[right]+p[left];
    if max=1 then count:=count+x[right]-x[left];
     inc(left);
     if max=1 then max:=2;
       end;
   until(x[left]>s)or(x[right]<s);
  if x[left]>s then write(count+x[right]-x[left]);
  if x[right]<s then write(count+s-x[left])
end.


var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then
    begin
      ans:=sum-x[i];
      firstl:=i;
    end;
  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then
    begin
      ans2:=sum-(c-x[i]);
      //writeln(i);
    end;
  end;
  if ans2>ans then ans:=ans2;
  for i:=1 to n do
  if x[i]>=c div 2 then
  begin
    firstr:=i;
    firstl:=i-1;
    break;
  end;
  for i:=1 to n do
  if x[i]<c div 2 then
  begin
    while (x[firstr+1]-num[firstr]<x[firstr]-num[firstr-1])and(firstr<n)do inc(firstr);
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  if firstl>0 then
  begin
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    while (num[firstl-1]-x[firstl-1]<num[firstl]-x[firstl])and(firstl>1)do dec(firstl);
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  end;
  writeln(ans);
end.

Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.


{$mode objfpc}
program D;
uses
  fgl, math;

type
  TIntLIst = specialize TFPGList<Int64>;

var
  N: Integer;
  C: Int64;
  xi, li, ri, vi: TIntLIst;

procedure ReadData;
var
  i: Integer;
  x, v: Int64;

begin
  ReadLn(N, C);

  li := TIntLIst.Create;
  ri := TIntLIst.Create;
  ri.Count := N;
  xi := TIntLIst.Create;
  vi := TIntLIst.Create;

  for i := 1 to N do
  begin
    ReadLn(x, v);
    xi.Add(x);
    li.Add(x);
    vi.Add(v);
  end;

  ri.Count := N;
  for i := N - 1 downto 0 do
    ri[i] := c - xi[i];
end;

var
  L1, L2, R1, R2: TIntList;
  L1M, L2M, R1M, R2M: TIntList;

function Solve: Int64;
var
  i: Integer;
  l, r: Integer;
  Current: Int64;
  CurSum: Int64;

begin
  L1 := TIntLIst.Create;
  L2 := TIntLIst.Create;
  R1 := TIntLIst.Create;
  R2 := TIntLIst.Create;
  L1M := TIntLIst.Create;
  L2M := TIntLIst.Create;
  R1M := TIntLIst.Create;
  R2M := TIntLIst.Create;
  L1.Count := N + 1;
  L2.Count := N + 1;
  R1.Count := N + 1;
  R2.Count := N + 1;
  L1M.Count := N + 1;
  L2M.Count := N + 1;
  R1M.Count := N + 1;
  R2M.Count := N + 1;

{
l1(l) := v0+...+vl-(xl)
l2(l) := v0+...+vr-2(xl)
r2(r) := vr+...+vN-1-(C - xr)
r2(r) := vr+...+vN-1-2(C - xr)
}
  l := 0;
  L1[l] := vi[l] - xi[l];
  L2[l] := vi[l] - 2 * xi[l];
  L1M[l] := L1[l];
  L2M[l] := L2[l];
  CurSum := vi[l];
  for l := 1 to N - 1 do
  begin
    Inc(CurSum, vi[l]);
    L1[l] := CurSum - xi[l];
    L2[l] := CurSum - 2 * li[l];
    L1M[l] := Max(L1[l], L1M[l - 1]);
    L2M[l] := Max(L2[l], L2M[l - 1]);
  end;

  r := N - 1;
  CurSum := vi[r];
  R1[r] := CurSum - (C - xi[r]);
  R2[r] := CurSum - 2 * (C - xi[r]);
  R1M[r] := R1[r];
  R2M[r] := R2[r];
  for r := N - 2 downto 0 do
  begin
    Inc(CurSum, vi[r]);
    R1[r] := CurSum - ri[r];
    R2[r] := CurSum - 2 * ri[r];
    R1M[r] := Max(R1[r], R1M[r + 1]);
    R2M[r] := Max(R2[r], R2M[r + 1]);
  end;

  Result := 0;

  for i := 0 to N - 2 do
  begin
    if Result < L1[i] then
      Result := L1[i];
    if Result < R1[i] then
      Result := R1[i];

    l := i;
    r := (i + 1);
  {
    WriteLn('L1:', L1[l], ' L2:', L2[l]);
    WriteLn('R1:', R1[r], ' R2:', R2[r]);
    WriteLn('LM:', L1M[l], ' L2M:', L2M[l]);
    WriteLn('RM:', R1M[r], ' R2M:', R2M[r]);
  }

    if Result < L2[l] + R1M[r] then
      Result := L2[l] + R1M[r];

    if Result < L1M[l] + R2[r] then
      Result := L1M[l] + R2[r];
  end;
  i := N - 1;
  if Result < L1[i] then
    Result := L1[i];
  if Result < R1[i] then
    Result := R1[i];
end;

begin
  ReadData;

  WriteLn(Solve);
end.

Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.


{$mode objfpc}
program D;
uses
  fgl, math;

type
  TIntLIst = specialize TFPGList<Int64>;

var
  N: Integer;
  C: Int64;
  xi, li, ri, vi: TIntLIst;
  x, v: Int64;

procedure ReadData;
var
  i: Integer;
  CurSum: Int64;

begin
  ReadLn(N, C);

  li := TIntLIst.Create;
  ri := TIntLIst.Create;
  ri.Count := N;
  xi := TIntLIst.Create;
  vi := TIntLIst.Create;

  for i := 1 to N do
  begin
    ReadLn(x, v);
    xi.Add(x);
    li.Add(x);
    vi.Add(v);
  end;

  ri.Count := N;
  for i := N - 1 downto 0 do
    ri[i] := c - xi[i];
end;

var
  L1, L2, R1, R2: TIntList;
  L1M, L2M, R1M, R2M: TIntList;

function Solve: Int64;
var
  i: Integer;
  l, r: Integer;
  Current: Int64;
  CurSum: Int64;

begin
  L1 := TIntLIst.Create;
  L2 := TIntLIst.Create;
  R1 := TIntLIst.Create;
  R2 := TIntLIst.Create;
  L1M := TIntLIst.Create;
  L2M := TIntLIst.Create;
  R1M := TIntLIst.Create;
  R2M := TIntLIst.Create;
  L1.Count := N + 1;
  L2.Count := N + 1;
  R1.Count := N + 1;
  R2.Count := N + 1;
  L1M.Count := N + 1;
  L2M.Count := N + 1;
  R1M.Count := N + 1;
  R2M.Count := N + 1;

{
l1(l) := v0+...+vl-(xl)
l2(l) := v0+...+vr-2(xl)
r2(r) := vr+...+vN-1-(C - xr)
r2(r) := vr+...+vN-1-2(C - xr)
}
  l := 0;
  L1[l] := vi[l] - xi[l];
  L2[l] := vi[l] - 2 * xi[l];
  L1M[l] := L1[l];
  L2M[l] := L2[l];
  CurSum := vi[l];
  for l := 1 to N - 1 do
  begin
    Inc(CurSum, vi[l]);
    L1[l] := CurSum - xi[l];
    L2[l] := CurSum - 2 * li[l];
    L1M[l] := Max(L1[l], L1M[l - 1]);
    L2M[l] := Max(L2[l], L2M[l - 1]);
  end;

  r := N - 1;
  CurSum := vi[r];
  R1[r] := CurSum - (C - xi[r]);
  R2[r] := CurSum - 2 * (C - xi[r]);
  R1M[r] := R1[r];
  R2M[r] := R2[r];
  for r := N - 2 downto 0 do
  begin
    Inc(CurSum, vi[r]);
    R1[r] := CurSum - ri[r];
    R2[r] := CurSum - 2 * ri[r];
    R1M[r] := Max(R1[r], R1M[r + 1]);
    R2M[r] := Max(R2[r], R2M[r + 1]);
  end;

   Result := -1;
  for i := 0 to N - 2 do
  begin
    if Result < L1[i] then
      Result := L1[i];
    if Result < R1[i] then
      Result := R1[i];

    l := i;
    r := (i + 1);

{    WriteLn('L1:', L1[l], ' L2:', L2[l]);
    WriteLn('R1:', R1[r], ' R2:', R2[r]);
    WriteLn('LM:', L1M[l], ' L2M:', L2M[l]);
    WriteLn('RM:', R1M[r], ' R2M:', R2M[r]);
}

    if Result < L2[l] + R1M[r] then
      Result := L2[l] + R1M[r];

    if Result < L1M[l] + R2[r] then
      Result := L1M[l] + R2[r];
  end;

  if Result < 0 then
    Result := 0;
end;

begin
  ReadData;

  WriteLn(Solve);
end.


{$mode objfpc}
program D;
uses
  fgl, math;

type
  TIntLIst = specialize TFPGList<Int64>;

var
  N: Integer;
  C: Int64;
  xi, li, ri, vi, SVi: TIntLIst;
  x, v: Int64;

procedure ReadData;
var
  i: Integer;
  Last: Integer;
  CurSum: Integer;

begin
  ReadLn(N, C);

  li := TIntLIst.Create;
  ri := TIntLIst.Create;
  ri.Count := N;
  xi := TIntLIst.Create;
  vi := TIntLIst.Create;
  SVi := TIntLIst.Create;

  CurSum := 0;

  for i := 1 to N do
  begin
    ReadLn(x, v);
    xi.Add(x);
    li.Add(x);
    vi.Add(v);
    CurSum := CurSum + v;
    SVi.Add(CurSum);
  end;

  ri.Count := N;
  for i := N - 1 downto 0 do
    ri[i] := c - xi[i];

end;

var
  L1, L2, R1, R2: TIntList;
  L1M, L2M, R1M, R2M: TIntList;

function Solve: Int64;
var
  i: Integer;
  l, r: Integer;
  Current: Int64;
  CurSum: Int64;
  Lefts, Rights: Integer;

begin
  L1 := TIntLIst.Create;
  L2 := TIntLIst.Create;
  R1 := TIntLIst.Create;
  R2 := TIntLIst.Create;
  L1M := TIntLIst.Create;
  L2M := TIntLIst.Create;
  R1M := TIntLIst.Create;
  R2M := TIntLIst.Create;
  L1.Count := N + 1;
  L2.Count := N + 1;
  R1.Count := N + 1;
  R2.Count := N + 1;
  L1M.Count := N + 1;
  L2M.Count := N + 1;
  R1M.Count := N + 1;
  R2M.Count := N + 1;

{
l1(l) := v0+...+vl-(xl)
l2(l) := v0+...+vr-2(xl)
r2(r) := vr+...+vN-1-(C - xr)
r2(r) := vr+...+vN-1-2(C - xr)
}
  l := 0;
  L1[l] := vi[l] - xi[l];
  L2[l] := vi[l] - 2 * xi[l];
  L1M[l] := L1[l];
  L2M[l] := L2[l];
  for l := 1 to N - 1 do
  begin
    L1[l] := SVi[l] - xi[l];
    L2[l] := SVi[l] - 2 * li[l];
    L1M[l] := Max(L1[l], L1M[l - 1]);
    L2M[l] := Max(L2[l], L2M[l - 1]);
  end;

  r := N - 1;
  CurSum := vi[r];
  R1[r] := CurSum - (C - xi[r]);
  R2[r] := CurSum - 2 * (C - xi[r]);
  R1M[r] := R1[r];
  R2M[r] := R2[r];
  for r := N - 2 downto 0 do
  begin
    Inc(CurSum, vi[r]);
    R1[r] := CurSum - ri[r];
    R2[r] := CurSum - 2 * ri[r];
    R1M[r] := Max(R1[r], R1M[r + 1]);
    R2M[r] := Max(R2[r], R2M[r + 1]);
  end;

   Result := -1;
  for i := 0 to N - 2 do
  begin
    if Result < L1[i] then
      Result := L1[i];
    if Result < R1[i] then
      Result := R1[i];

    l := i;
    r := (i + 1);

{    WriteLn('L1:', L1[l], ' L2:', L2[l]);
    WriteLn('R1:', R1[r], ' R2:', R2[r]);
    WriteLn('LM:', L1M[l], ' L2M:', L2M[l]);
    WriteLn('RM:', R1M[r], ' R2M:', R2M[r]);
}

    if Result < L2[l] + R1M[r] then
      Result := L2[l] + R1M[r];

    if Result < L1M[l] + R2[r] then
      Result := L1M[l] + R2[r];
  end;

end;

begin
  ReadData;

  WriteLn(Solve);
end.


var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then ans:=sum-x[i];

  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then ans2:=sum-(c-x[i]);
  end;
  if ans2>ans then ans:=ans2;
  ans2:=-maxlongint;
  for i:=1 to n do
  if (num[n]-num[i-1]-(c-x[i])>ans2)and(x[i]>=c div 2) then
  begin
    firstr:=i;
    ans2:=num[n]-num[i]-(c-x[i]);
  end;
  ans2:=-maxlongint;
  for i:=1 to n do
  if (num[i]-x[i]>ans2)and(x[i]<c div 2) then
  begin
    firstl:=i;
    ans2:=num[i]-x[i];
  end;
  if firstr>0 then
  begin
  for i:=1 to n do
  if x[i]<c div 2 then
  begin
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  end;
  if firstl>0 then
  begin
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  end;
  writeln(ans);
end.


var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then
    begin
      ans:=sum-x[i];
      firstl:=i;
    end;
  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then
    begin
      ans2:=sum-(c-x[i]);
      //writeln(i);
    end;
  end;
  if ans2>ans then ans:=ans2;
  ans2:=0;
  for i:=1 to n do
  if (num[n]-num[i-1]-(c-x[i])>ans2)and(x[i]>=c div 2) then
  begin
    firstr:=i;
    ans2:=num[n]-num[i]-(c-x[i]);
  end;
  ans2:=0;
  for i:=1 to n do
  if (num[i]-x[i]>ans2)and(x[i]<c div 2) then
  begin
    firstl:=i;
    ans2:=num[i]-x[i];
  end;
  for i:=1 to n do
  if x[i]<c div 2 then
  begin
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  if firstl>0 then
  begin
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  end;
  writeln(ans);
end.


var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then
    begin
      ans:=sum-x[i];
      firstl:=i;
    end;
  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then
    begin
      ans2:=sum-(c-x[i]);
      //writeln(i);
    end;
  end;
  if ans2>ans then ans:=ans2;
  ans2:=0;
  for i:=1 to n do
  if (num[n]-num[i-1]-(c-x[i])>ans2)and(x[i]>=c div 2) then
  begin
    firstr:=i;
    ans2:=num[n]-num[i]-(c-x[i]);
  end;
  ans2:=0;
  for i:=1 to n do
  if (num[i]-x[i]>ans2)and(x[i]<c div 2) then
  begin
    firstl:=i;
    ans2:=num[i]-x[i];
  end;
  for i:=1 to n do
  if x[i]<c div 2 then
  begin
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  if firstl>0 then
  begin
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    while (num[firstl-1]-x[firstl-1]>num[firstl]-x[firstl])and(firstl>1)do dec(firstl);
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  end;
  writeln(ans);
end.


var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then
    begin
      ans:=sum-x[i];
      firstl:=i;
    end;
  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then
    begin
      ans2:=sum-(c-x[i]);
      //writeln(i);
    end;
  end;
  if ans2>ans then ans:=ans2;
  for i:=1 to n do
  if x[i]>=c div 2 then
  begin
    firstr:=i;
    firstl:=i-1;
    break;
  end;
  for i:=1 to n do
  if x[i]<c div 2 then
  begin
    while (x[firstr+1]-num[firstr]>x[firstr]-num[firstr-1])and(firstr<n)do inc(firstr);
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  if firstl>0 then
  begin
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    while (num[firstl-1]-x[firstl-1]>num[firstl]-x[firstl])and(firstl>1)do dec(firstl);
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  end;
  writeln(ans);
end.


var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then
    begin
      ans:=sum-x[i];
      firstl:=i;
    end;
  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then
    begin
      ans2:=sum-(c-x[i]);
      //writeln(i);
    end;
  end;
  if ans2>ans then ans:=ans2;
  for i:=1 to n do
  if x[i]>=c div 2 then
  begin
    firstr:=i;
    firstl:=i-1;
    break;
  end;
  for i:=1 to n do
  if x[i]<c div 2 then
  begin
    while (x[firstr+1]-num[firstr]<x[firstr]-num[firstr-1])and(firstr<n)do inc(firstr);
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  if firstl>0 then
  begin
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    while (num[firstl-1]-x[firstl-1]>num[firstl]-x[firstl])and(firstl>1)do dec(firstl);
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  end;
  writeln(ans);
end.


var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then
    begin
      ans:=sum-x[i];
      firstl:=i;
    end;
  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then ans2:=sum-(c-x[i]);
  end;
  if ans2>ans then ans:=ans2;
  for i:=1 to n do
  if x[i]>=c div 2 then
  begin
    firstr:=i;
    firstl:=i-1;
    break;
  end;
  for i:=1 to n do
  if x[i]<c div 2 then
  begin
    while (x[firstr+1]-num[firstr]<x[firstr]-num[firstr-1])and(firstr<n)do inc(firstr);
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  if firstl>0 then
  begin
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    while (num[firstl-1]-x[firstl-1]<num[firstl]-x[firstr])and(firstl>1)do inc(firstr);
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  end;
  writeln(ans);
end.


var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then
    begin
      ans:=sum-x[i];
      firstl:=i;
    end;
  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then ans2:=sum-(c-x[i]);
  end;
  if ans2>ans then ans:=ans2;
  for i:=1 to n do
  if x[i]>=c div 2 then
  begin
    firstr:=i;
    firstl:=i-1;
    break;
  end;
  for i:=1 to n do
  if x[i]<=c div 2 then
  begin
    while (x[firstr+1]-num[firstr]<x[firstr]-num[firstr-1])and(firstr<n)do inc(firstr);
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    while (num[firstl-1]-x[firstl-1]<num[firstl]-x[firstr])and(firstl>1)do inc(firstr);
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  writeln(ans);
end.


Uses math;
var m,x,a,c,i:Longint;
begin
	read(i,m,x);
	for i:=1 to m do
	begin
		read(a);
		if a>x then c:=c+1;
	end;
	write(Min(c,m-c))
end.

Score : 200 points

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

Initially, you are in Square X.
You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.
However, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.
It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

1 \leq X \leq N - 1

1 \leq A_1 < A_2 < ... < A_M \leq N

A_i \neq X

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A_1 A_2 ... A_M

Output

Print the minimum cost incurred before reaching the goal.

Sample Input 1

5 3 3
1 2 4

Sample Output 1

1

The optimal solution is as follows:

First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.

Then, travel from Square 4 to Square 5. This time, no cost is incurred.

Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.

Sample Input 2

7 3 2
4 5 6

Sample Output 2

0

We may be able to reach the goal at no cost.

Sample Input 3

10 7 5
1 2 3 4 6 8 9

Sample Output 3

3


program aaa;
var
  n,i:longint;
  ans:int64;
  a:array[0..100010]of longint;
begin
  readln(n);
  a[0]:=0;
  for i:=1 to n do
  begin
    read(a[i]);
    ans:=ans+abs(a[i]-a[i-1]);
  end;
  a[n+1]:=0;
  ans:=ans+abs(a[n]);
  for i:=1 to n do
   writeln(ans+abs(a[i+1]-a[i-1])-abs(a[i]-a[i-1])-abs(a[i+1]-a[i]));
  readln;
  readln;
end.

Score : 300 points

Problem Statement

There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N.
Spot i is at the point with coordinate A_i.
It costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis.
In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i.
You will visit the remaining spots as planned in the order they are numbered.
You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.

Constraints

2 \leq N \leq 10^5

-5000 \leq A_i \leq 5000 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N lines.
In the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.

Sample Input 1

3
3 5 -1

Sample Output 1

12
8
10

Spot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively.
For each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:

For i = 1, the course of the trip is 0 \rightarrow 5 \rightarrow -1 \rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.

For i = 2, the course of the trip is 0 \rightarrow 3 \rightarrow -1 \rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.

For i = 3, the course of the trip is 0 \rightarrow 3 \rightarrow 5 \rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen.

Sample Input 2

5
1 1 1 2 0

Sample Output 2

4
4
4
2
4

Sample Input 3

6
-679 -2409 -3258 3095 -3291 -4462

Sample Output 3

21630
21630
19932
8924
21630
19288


var
	N,sum,i:Longint;
	A:Array[0..100001]of Longint;
begin
	read(N);
	for i:=1 to N do read(A[i]);
	for i:=1 to N+1 do begin
		if A[i]>A[i-1] then inc(sum,A[i]-A[i-1])else inc(sum,A[i-1]-A[i]);
	end;
	for i:=1 to N do writeln(sum-abs(A[i-1]-A[i])-abs(A[i]-A[i+1])+abs(A[i-1]-A[i+1]));
end.


const  MAXN = 100000;
var n: longint;
      a, f, g: array[0..MAXN] of longint;

procedure Nhap;
var i: longint;
begin
        readln(n);
        for i:= 1 to n do read(a[i]);
end;

procedure QT;
var i: longint;
begin
        f[0]:= 0;
        g[n+1]:= 0;  ///Phan tu dung canh
        for i:= 1 to n do f[i]:= f[i-1] + abs(a[i] - a[i-1]);
        for i:= n downto 1 do
                g[i]:= g[i+1] + abs(a[i+1] - a[i]);
        for i:= 1 to n do
                writeln(f[i-1] + g[i+1] + abs(a[i+1]-a[i-1]));
end;

BEGIN
        Nhap;
        QT;
END.


var
n,i,s:longint;
a:array[0..100010] of longint;
begin
readln(n);
for i:=1 to n do read(a[i]);
a[0]:=0; a[n+1]:=0;
s:=0;
for i:=1 to n+1 do s:=s+abs(a[i]-a[i-1]);
for i:=1 to n do writeln(s-abs(a[i]-a[i-1])-abs(a[i+1]-a[i])+abs(a[i+1]-a[i-1]));
end.


var  now,ans,ansx:longint;
     x,a:array[0..150000]of longint;
     n:longint;
     i:longint;
begin
  read(n);
  now:=0;
  for i:=1 to n do
  begin
    read(a[i]);
    x[i]:=now;
    ans:=ans+abs(now-a[i]);
    now:=a[i];
  end;
  a[n+1]:=0;
  x[n+1]:=now;
  ans:=ans+abs(now-a[i+1]);
  for i:=1 to n do
  begin
    ansx:=ans-abs(x[i]-a[i])-abs(x[i+1]-a[i+1])+abs(x[i]-a[i+1]);
    writeln(ansx);
  end;
end.


var a,b,t:longint;
    num,bool,y,where:longint;
    i,j:longint;
    map:Array[0..105,0..105]of longint;
    c1,c2:char;
begin
  read(a,b);
  c1:='.';
  c2:='#';
  if a>b then
  begin
    t:=a;a:=b;b:=t;
    c1:='#';
    c2:='.';
  end;
  b:=b-a;
  if a=1 then dec(b);
  num:=1;
  bool:=0;
  while a>0 do
  begin
    inc(num);
    bool:=1-bool;
    for i:=1 to num-1 do
    begin
      map[i,num-i]:=bool;
      if bool=1 then dec(a);
      if a=0 then
      begin
        dec(b);
        break;
      end;
    end;
    if bool=1 then inc(b);
  end;
  for j:=1 to num-1 do
  begin
    map[num,j]:=0;
    map[num+1,j]:=1;
  end;
  inc(num);
  y:=num;
  for i:=1 to num do
  map[i,num-1]:=1;
  if b<>0 then
  begin
    while num<100 do
    begin
      inc(num);
      if num mod 2=1 then
      begin
        for j:=1 to y-1 do
        map[num,j]:=1;
      end else
      begin
        for j:=1 to y-1 do
        begin
          if (j mod 2=0) then map[num,j]:=1 else dec(b);
          if b=0 then
          begin
            where:=j;
            break;
          end;
        end;
      end;
      if b=0 then
      begin
        for j:=where+1 to y-1 do
        map[num,j]:=1;
        break;
      end;
    end;
  end;
  dec(y);
  while b>0 do
  begin
    inc(y);
    for i:=1 to num do
    begin
      if i mod 2=0 then map[i,y]:=1 else dec(b);
      if b=0 then
      begin
        where:=i;
        break;
      end;
    end;
    inc(y);
    for i:=1 to num do
    map[i,y]:=1;
    if b=0 then
    begin
      for i:=where+1 to num do
      map[i,y-1]:=1;
    end;
  end;
  writeln(num,' ',y);
  for i:=1 to num do
  begin
    for j:=1 to y do
    if map[i,j]=1 then write(c1) else write(c2);
    writeln;
  end;
end.

Score : 500 points

Problem Statement

You are given two integers A and B.

Print a grid where each square is painted white or black that satisfies the following conditions, in the format specified in Output section:

Let the size of the grid be h \times w (h vertical, w horizontal). Both h and w are at most 100.

The set of the squares painted white is divided into exactly A connected components.

The set of the squares painted black is divided into exactly B connected components.

It can be proved that there always exist one or more solutions under the conditions specified in Constraints section.
If there are multiple solutions, any of them may be printed.

Notes

Two squares painted white, c_1 and c_2, are called connected when the square c_2 can be reached from the square c_1 passing only white squares by repeatedly moving up, down, left or right to an adjacent square.

A set of squares painted white, S, forms a connected component when the following conditions are met:

Any two squares in S are connected.

No pair of a square painted white that is not included in S and a square included in S is connected.

A connected component of squares painted black is defined similarly.

Constraints

1 \leq A \leq 500

1 \leq B \leq 500

Input

Input is given from Standard Input in the following format:

A B

Output

Output should be in the following format:

In the first line, print integers h and w representing the size of the grid you constructed, with a space in between.

Then, print h more lines. The i-th (1 \leq i \leq h) of these lines should contain a string s_i as follows:

If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted white, the j-th character in s_i should be ..

If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted black, the j-th character in s_i should be #.

Sample Input 1

2 3

Sample Output 1

3 3
##.
..#
#.#

This output corresponds to the grid below:

Sample Input 2

7 8

Sample Output 2

3 5
#.#.#
.#.#.
#.#.#

Sample Input 3

1 1

Sample Output 3

4 2
..
#.
##
##

Sample Input 4

3 14

Sample Output 4

8 18
..................
..................
....##.......####.
....#.#.....#.....
...#...#....#.....
..#.###.#...#.....
.#.......#..#.....
#.........#..####.


var n,m,s,t,i,j:Longint;a,b:array of string;
begin
	readln(n);
	setlength(a,n+1);
	for i:=1 to n do begin
		readln(a[i]);
	end;
	readln(m);
	SetLength(b,m+1);
	for i:=1 to m do begin
		readln(b[i]);
	end;
	s:=0;
	for i:=1 to n do begin
		t:=0;
		for j:=1 to n do begin
			if a[i]=a[j] then t:=t+1;
		end;
		for j:=1 to m do begin
			if a[i]=b[j] then t:=t-1;
		end;
		if s<t then s:=t;
	end;
	write(s)
end.

Score : 200 points

Problem Statement

Takahashi has N blue cards and M red cards.
A string is written on each card. The string written on the i-th blue card is s_i, and the string written on the i-th red card is t_i.

Takahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn 1 yen (the currency of Japan); each time he finds a red card with that string, he will lose 1 yen.

Here, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces atcoder, he will not earn money even if there are blue cards with atcoderr, atcode, btcoder, and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.)

At most how much can he earn on balance?

Note that the same string may be written on multiple cards.

Constraints

N and M are integers.

1 \leq N, M \leq 100

s_1, s_2, ..., s_N, t_1, t_2, ..., t_M are all strings of lengths between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N
M
t_1
t_2
:
t_M

Output

If Takahashi can earn at most X yen on balance, print X.

Sample Input 1

3
apple
orange
apple
1
grape

Sample Output 1

2

He can earn 2 yen by announcing apple.

Sample Input 2

3
apple
orange
apple
5
apple
apple
apple
apple
apple

Sample Output 2

1

If he announces apple, he will lose 3 yen. If he announces orange, he can earn 1 yen.

Sample Input 3

1
voldemort
10
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort

Sample Output 3

0

If he announces voldemort, he will lose 9 yen. If he announces orange, for example, he can avoid losing a yen.

Sample Input 4

6
red
red
blue
yellow
yellow
red
5
red
red
yellow
green
blue

Sample Output 4

1


var n:longint;
    i,k:longint;
    a,b,c,d:array[0..201000]of longint;
    m0,m1:longint;
    ans:int64;
procedure qs(l,r:longint);
var i,j,m,t:longint;
begin
  i:=l;
  j:=r;
  m:=c[(l+r)>>1];
  repeat
    while c[i]<m do inc(i);
    while c[j]>m do dec(j);
    if i<=j then
    begin
      t:=c[i];c[i]:=c[j];c[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
function find(x:longint):longint;
var l,r,m:longint;
begin
  l:=1;
  r:=n;
  while l<=r do
  begin
    m:=(l+r)>>1;
    if c[m]<x then l:=l+1 else r:=m-1;
  end;
  exit(l);
end;

begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  for i:=1 to n do
  read(b[i]);
  c[0]:=-1;
  c[n+1]:=maxlongint;
  for k:=1 to 29 do
  begin
    for i:=1 to n do
    begin
      c[i]:=a[i] and ((1 << k)-1);
      d[i]:=b[i] and ((1 << k)-1);
    end;
    qs(1,n);
    m0:=0;
    m1:=0;
    for i:=1 to n do
    begin
      m0:=m0+find((1 << (k-1))-d[i])-1;
      m0:=m0+find((1 << k)+(1 << (k-1))-d[i])-find((1 << k)-d[i]);
      m1:=m1+find((1 << k)-d[i])-find((1 << (k-1))-d[i]);
      m1:=m1+n+1-find((1 << k)+(1 << (k-1))-d[i]);
    end;
    //writeln('0=',m0,' 1=',m1);
    if m1 and 1 =1 then ans:=ans+1 << (k-1);
  end;
  writeln(ans);
end.

Score : 400 points

Problem Statement

On a two-dimensional plane, there are N red points and N blue points.
The coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i).

A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.

At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.

Constraints

All input values are integers.

1 \leq N \leq 100

0 \leq a_i, b_i, c_i, d_i < 2N

a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different.

b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_N b_N
c_1 d_1
c_2 d_2
:
c_N d_N

Output

Print the maximum number of friendly pairs.

Sample Input 1

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

Sample Output 1

2

For example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5).

Sample Input 2

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

Sample Output 2

2

For example, you can pair (0, 0) and (2, 3), then (1, 1) and (3, 4).

Sample Input 3

2
2 2
3 3
0 0
1 1

Sample Output 3

0

It is possible that no pair can be formed.

Sample Input 4

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

Sample Output 4

5

Sample Input 5

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

Sample Output 5

4


var
	N,i,id,sz,asz,t:Longint;
	pa,x:Array[1..1000]of Longint;
	dp:Array[1..1000]of int64;
	cum:Array[0..1]of int64;
	cumpa:Array[0..1]of Longint;
begin
	read(N);
	id:=1;
	for i:=1 to N do begin
		read(dp[i]);
		pa[i]:=0;
		t:=i and 1;
		if cum[t]>0 then begin
			inc(dp[i],cum[t]);
			pa[i]:=cumpa[t];
		end;
		if dp[i]>cum[t]then begin
			cum[t]:=dp[i];
			cumpa[t]:=i;
		end;
		if dp[id]<dp[i]then id:=i;
	end;
	writeln(dp[id]);
	while id>0 do begin
		inc(sz);
		x[sz]:=id;
		id:=pa[id];
	end;
	asz:=N-x[1]+x[sz]-1;
	for i:=2 to sz do begin
		t:=(x[i-1]-x[i])div 2;
		inc(asz,t);
	end;
	writeln(asz);
	for i:=N downto x[1]+1 do begin
		writeln(i);
	end;
	for i:=2 to sz do begin
		t:=(x[i-1]-x[i])div 2;
		while t>0 do begin
			writeln(x[i]+t);
			dec(t);
		end;
	end;
	for i:=1 to x[sz]-1 do begin
		writeln(1);
	end;
end.

Score : 700 points

Problem Statement

You have an integer sequence of length N: a_1, a_2, ..., a_N.

You repeatedly perform the following operation until the length of the sequence becomes 1:

First, choose an element of the sequence.

If that element is at either end of the sequence, delete the element.

If that element is not at either end of the sequence, replace the element with the sum of the two elements that are adjacent to it. Then, delete those two elements.

You would like to maximize the final element that remains in the sequence.

Find the maximum possible value of the final element, and the way to achieve it.

Constraints

All input values are integers.

2 \leq N \leq 1000

|a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

In the first line, print the maximum possible value of the final element in the sequence.

In the second line, print the number of operations that you perform.

In the (2+i)-th line, if the element chosen in the i-th operation is the x-th element from the left in the sequence at that moment, print x.

If there are multiple ways to achieve the maximum value of the final element, any of them may be printed.

Sample Input 1

5
1 4 3 7 5

Sample Output 1

11
3
1
4
2

The sequence would change as follows:

After the first operation: 4, 3, 7, 5

After the second operation: 4, 3, 7

After the third operation: 11(4+7)

Sample Input 2

4
100 100 -1 100

Sample Output 2

200
2
3
1

After the first operation: 100, 200(100+100)

After the second operation: 200

Sample Input 3

6
-1 -2 -3 1 2 3

Sample Output 3

4
3
2
1
2

After the first operation: -4, 1, 2, 3

After the second operation: 1, 2, 3

After the third operation: 4

Sample Input 4

9
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

5000000000
4
2
2
2
2


var
	N,i,id,sz,asz,t:Longint;
	pa,x,ans:Array[1..1000]of Longint;
	dp:Array[1..1000]of int64;
	cum:Array[0..1]of int64;
	cumpa:Array[0..1]of Longint;
begin
	read(N);
	id:=1;
	for i:=1 to N do begin
		read(dp[i]);
		pa[i]:=0;
		t:=i and 1;
		if cum[t]>0 then begin
			inc(dp[i],cum[t]);
			pa[i]:=cumpa[t];
		end;
		if dp[i]>cum[t]then begin
			cum[t]:=dp[i];
			cumpa[t]:=i;
		end;
		if dp[id]<dp[i]then id:=i;
	end;
	writeln(dp[id]);
	while id>0 do begin
		inc(sz);
		x[sz]:=id;
		id:=pa[id];
	end;
	for i:=N downto x[1]+1 do begin
		inc(asz);
		ans[asz]:=i;
	end;
	for i:=2 to sz do begin
		t:=(x[i-1]-x[i])div 2;
		while t>0 do begin
			inc(asz);
			ans[asz]:=x[i]+t;
			dec(t);
		end;
	end;
	for i:=1 to x[sz]-1 do begin
		inc(asz);
		ans[asz]:=1;
	end;
end.


var
	N,i,id,sz,asz,t:Longint;
	pa,x,ans:Array[1..1000]of Longint;
	dp:Array[1..1000]of int64;
	cum:Array[0..1]of int64;
	cumpa:Array[0..1]of Longint;
begin
	read(N);
	id:=1;
	for i:=1 to N do begin
		read(dp[i]);
		pa[i]:=0;
		t:=i and 1;
		if cum[t]>0 then begin
			inc(dp[i],cum[t]);
			pa[i]:=cumpa[t];
		end;
		if dp[i]>cum[t]then begin
			cum[t]:=dp[i];
			cumpa[t]:=i;
		end;
		if dp[id]<dp[i]then id:=i;
	end;
	writeln(dp[id]);
	while id>0 do begin
		inc(sz);
		x[sz]:=id;
		id:=pa[id];
	end;
	for i:=N downto x[1]+1 do begin
		inc(asz);
		ans[asz]:=i;
	end;
	for i:=2 to sz do begin
		t:=(x[i-1]-x[i])div 2;
		while t>0 do begin
			inc(asz);
			ans[asz]:=x[i]+t;
			dec(t);
		end;
	end;
	for i:=1 to x[sz]-1 do begin
		inc(asz);
		ans[asz]:=1;
	end;
	writeln(asz);
	for i:=1 to asz do writeln(ans[i]);
end.


var
	N,i,id,sz,asz,t:Longint;
	pa,x,ans:Array[1..1000]of Longint;
	dp:Array[1..1000]of int64;
	cum:Array[0..1]of int64;
	cumpa:Array[0..1]of Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(dp[i]);
		pa[i]:=0;
		t:=i and 1;
		if cum[t]>0 then begin
			inc(dp[i],cum[t]);
			pa[i]:=cumpa[t];
		end;
		if dp[i]>cum[t]then begin
			cum[t]:=dp[i];
			cumpa[t]:=i;
			if dp[id]<dp[i]then id:=i;
		end;
	end;
	writeln(dp[id]);
	while id>0 do begin
		inc(sz);
		x[sz]:=id;
		id:=pa[id];
	end;
	for i:=N downto x[1]+1 do begin
		inc(asz);
		ans[asz]:=i;
	end;
	for i:=2 to sz do begin
		t:=(x[i-1]-x[i])div 2;
		while t>0 do begin
			inc(asz);
			ans[asz]:=x[i]+t;
			dec(t);
		end;
	end;
	for i:=1 to x[sz]-1 do begin
		inc(asz);
		ans[asz]:=1;
	end;
	writeln(asz);
	for i:=1 to asz do writeln(ans[i]);
end.


var
	N,i,id,sz,asz,t:Longint;
	pa,cumpa,x,ans:Array[1..1000]of Longint;
	dp,cum:Array[1..1000]of int64;
begin
	read(N);
	for i:=1 to N do begin
		read(dp[i]);
		pa[i]:=0;
		if cum[i mod 2]>0 then begin
			inc(dp[i],cum[i mod 2]);
			pa[i]:=cumpa[i mod 2];
		end;
		if dp[i]>cum[i mod 2]then begin
			cum[i mod 2]:=dp[i];
			cumpa[i mod 2]:=i;
		end;
	end;
	id:=1;
	for i:=2 to N do if dp[i]>dp[id]then id:=i;
	writeln(dp[id]);
	while id>0 do begin
		inc(sz);
		x[sz]:=id;
		id:=pa[id];
	end;
	for i:=N downto x[1]+1 do begin
		inc(asz);
		ans[asz]:=i;
	end;
	for i:=2 to sz do begin
		t:=(x[i-1]-x[i])div 2;
		while t>0 do begin
			inc(asz);
			ans[asz]:=x[i]+t;
			dec(t);
		end;
	end;
	for i:=1 to x[sz]-1 do begin
		inc(asz);
		ans[asz]:=1;
	end;
	writeln(asz);
	for i:=1 to asz do writeln(ans[i]);
end.


var
	b:Longint;
	n,k,c:int64;
begin
	read(n,k);
	inc(n);
	for b:=k+1 to n-1 do begin
		inc(c,n div b*(b-k));
		if k=0 then dec(c);
		if n div b*b+k<n then inc(c,n-n div b*b-k);
	end;
	writeln(c);
end.

Score : 400 points

Problem Statement

Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.
He remembers that the remainder of a divided by b was greater than or equal to K.
Find the number of possible pairs that he may have had.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N-1

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible pairs that he may have had.

Sample Input 1

5 2

Sample Output 1

7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).

Sample Input 2

10 0

Sample Output 2

100

Sample Input 3

31415 9265

Sample Output 3

287927211


var
	N,i,A,K,T,S:Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(A,K);
		while A mod K<>0 do begin
			T:=A div K+1;
			dec(A,(A mod K+T-1)div T*T);
		end;
		S:=S xor A;
	end;
	if S<>0 then writeln('Takahashi')else writeln('Aoki');
end.

Score : 900 points

Problem Statement

Takahashi and Aoki are playing a stone-taking game. Initially, there are N piles of stones, and the i-th pile contains A_i stones and has an associated integer K_i.

Starting from Takahashi, Takahashi and Aoki take alternate turns to perform the following operation:

Choose a pile. If the i-th pile is selected and there are X stones left in the pile, remove some number of stones between 1 and floor(X/K_i) (inclusive) from the pile.

The player who first becomes unable to perform the operation loses the game. Assuming that both players play optimally, determine the winner of the game.
Here, floor(x) represents the largest integer not greater than x.

Constraints

1 \leq N \leq 200

1 \leq A_i,K_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 K_1
:
A_N K_N

Output

If Takahashi will win, print Takahashi; if Aoki will win, print Aoki.

Sample Input 1

2
5 2
3 3

Sample Output 1

Aoki

Initially, from the first pile at most floor(5/2)=2 stones can be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.

If Takahashi first takes two stones from the first pile, from the first pile at most floor(3/2)=1 stone can now be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.

Then, if Aoki takes one stone from the second pile, from the first pile at most floor(3/2)=1 stone can be removed at a time, and from the second pile no more stones can be removed (since floor(2/3)=0).

Then, if Takahashi takes one stone from the first pile, from the first pile at most floor(2/2)=1 stone can now be removed at a time, and from the second pile no more stones can be removed.

Then, if Aoki takes one stone from the first pile, from the first pile at most floor(1/2)=0 stones can now be removed at a time, and from the second pile no more stones can be removed.

No more operation can be performed, thus Aoki wins. If Takahashi plays differently, Aoki can also win by play accordingly.

Sample Input 2

3
3 2
4 3
5 1

Sample Output 2

Takahashi

Sample Input 3

3
28 3
16 4
19 2

Sample Output 3

Aoki

Sample Input 4

4
3141 59
26535 897
93 23
8462 64

Sample Output 4

Takahashi


var
	s:String;
	cnt:Array[1..5]of int64;
	ans:int64;
	i,n:Longint;
begin
	readln(n);
	for i:=1 to n do begin
		readln(s);
		if s[1]='M' then inc(cnt[1])
		else if s[1]='A' then inc(cnt[2])
		else if s[1]='R' then inc(cnt[3])
		else if s[1]='C' then inc(cnt[4])
		else if s[1]='H' then inc(cnt[5]);
	end;
	writeln(cnt[1]*cnt[2]*(cnt[3]+cnt[4]+cnt[5])+cnt[1]*cnt[3]*(cnt[4]+cnt[5])+
		cnt[1]*cnt[4]*cnt[5]+cnt[2]*cnt[3]*(cnt[4]+cnt[5])+cnt[2]*cnt[4]*cnt[5]+
		cnt[3]*cnt[4]*cnt[5]);
end.

Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

Note that the answer may not fit into a 32-bit integer type.

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7


var
    n,cnt_m,cnt_a,cnt_r,cnt_c,cnt_h,ans,i: Longint;
    s: String;
begin
    readln(n);
    for i := 1 to n do
    begin
        readln(s);
        if s[1]='M' then
        begin
            inc(cnt_m);
        end
        else if s[1]='A' then
        begin
            inc(cnt_a);
        end
        else if s[1]='R' then
        begin
            inc(cnt_r);
        end
        else if s[1]='C' then
        begin
            inc(cnt_c);
        end
        else if s[1]='H' then
        begin
            inc(cnt_h);
        end;
    end;
    ans:=cnt_m*cnt_a*(cnt_r+cnt_c+cnt_h)+cnt_m*cnt_r*(cnt_c+cnt_h)+
    cnt_m*cnt_c*cnt_h+cnt_a*cnt_r*(cnt_c+cnt_h)+cnt_a*cnt_c*cnt_h+
    cnt_r*cnt_c*cnt_h;
    writeln(ans);
end.


var
    h,w,d,q,i,j,k,tmp,ans_cnt: Longint;
    a:array[1..300,1..300] of Longint;
    index:array[1..90000,1..2] of Longint;
    l,r: Longint;
    ans:array[1..100000,1..3] of Longint;
    flag:Boolean;
begin
    readln(h,w,d);
    for i := 1 to h do
    begin
        for j := 1 to w do
        begin
            read(a[i][j]);
            index[a[i][j]][1]:=i;
            index[a[i][j]][2]:=j;
        end;
    end;
    read(q);
    for i := 1 to q do
    begin
        readln(l,r);
        flag:=false;
        for j := 1 to ans_cnt do
        begin
            if (ans[j][1]=l) and (ans[j][2]=r) then
            begin
                writeln(ans[j][3]);
                flag:=true;
                break;
            end;
        end;
        if flag then
        begin
            continue;
        end;
        tmp:=0;
        k:=l;
        while k<>r do
        begin
            j:=k;
            inc(k,d);
            inc(tmp,abs(index[k][1]-index[j][1])+abs(index[k][2]-index[j][2]));
        end;
        inc(ans_cnt);
        ans[ans_cnt][1]:=l;
        ans[ans_cnt][2]:=r;
        ans[ans_cnt][3]:=tmp;
        writeln(tmp);
    end;
end.

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.

You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.

You now have to take Q practical tests of your ability as a magical girl.

The i-th test will be conducted as follows:

Initially, a piece is placed on the square where the integer L_i is written.

Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.

Here, it is guaranteed that R_i-L_i is a multiple of D.

For each test, find the sum of magic points consumed during that test.

Constraints

1 \leq H,W \leq 300

1 \leq D \leq H×W

1 \leq A_{i,j} \leq H×W

A_{i,j} \neq A_{x,y} ((i,j) \neq (x,y))

1 \leq Q \leq 10^5

1 \leq L_i \leq R_i \leq H×W

(R_i-L_i) is a multiple of D.

Input

Input is given from Standard Input in the following format:

H W D
A_{1,1} A_{1,2} ... A_{1,W}
:
A_{H,1} A_{H,2} ... A_{H,W}
Q
L_1 R_1
:
L_Q R_Q

Output

For each test, print the sum of magic points consumed during that test.

Output should be in the order the tests are conducted.

Sample Input 1

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

Sample Output 1

5

4 is written in Square (1,2).

6 is written in Square (3,3).

8 is written in Square (3,1).

Thus, the sum of magic points consumed during the first test is (|3-1|+|3-2|)+(|3-3|+|1-3|)=5.

Sample Input 2

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

Sample Output 2

0
0

Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.

Sample Input 3

5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13

Sample Output 3

0
5
0


var
n,k,z,c,p:int64;  i:longint;
function pw(x,y:int64):int64;
var re:int64;
begin
    re:=1;
	while y>0 do
	begin
		if (y and 1=1) then begin re:=(re*x) mod p; end;
		x:=x*x mod p;
		y:=y div 2;
	end;
	pw:=re;
end;
begin
	n:=1; k:=1; z:=1; c:=1;
	p:=998244353;
	readln(k,n);
	z:=pw(2,n-1);
	for i:= 0 to k-1 do
	begin
		if ((k-i) mod 2=0) then
		begin
			z:= (z+p-c) mod p;
		end;
		c:=((c*(n-i))mod p)*(pw(i+1,p-2) mod p);
	end;
	writeln(z);
end.

Score : 1200 points

Problem Statement

In Republic of AtCoder, Snuke Chameleons (Family: Chamaeleonidae, Genus: Bartaberia) are very popular pets.
Ringo keeps N Snuke Chameleons in a cage.

A Snuke Chameleon that has not eaten anything is blue. It changes its color according to the following rules:

A Snuke Chameleon that is blue will change its color to red when the number of red balls it has eaten becomes strictly larger than the number of blue balls it has eaten.

A Snuke Chameleon that is red will change its color to blue when the number of blue balls it has eaten becomes strictly larger than the number of red balls it has eaten.

Initially, every Snuke Chameleon had not eaten anything. Ringo fed them by repeating the following process K times:

Grab either a red ball or a blue ball.

Throw that ball into the cage. Then, one of the chameleons eats it.

After Ringo threw in K balls, all the chameleons were red. We are interested in the possible ways Ringo could have thrown in K balls. How many such ways are there? Find the count modulo 998244353. Here, two ways to throw in balls are considered different when there exists i such that the color of the ball that are thrown in the i-th throw is different.

Constraints

1 \leq N,K \leq 5 \times 10^5

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the possible ways Ringo could have thrown in K balls, modulo 998244353.

Sample Input 1

2 4

Sample Output 1

7

We will use R to represent a red ball, and B to represent a blue ball. There are seven ways to throw in balls that satisfy the condition: BRRR, RBRB, RBRR, RRBB, RRBR, RRRB and RRRR.

Sample Input 2

3 7

Sample Output 2

57

Sample Input 3

8 3

Sample Output 3

0

Sample Input 4

8 10

Sample Output 4

46

Sample Input 5

123456 234567

Sample Output 5

857617983


var
    n,i,x,y: Integer;
    a: array[0..100] of Integer;

procedure qsort(l,r:Longint;data:PInteger);
//array index start at 0
var
    i,j,mid: Integer;
begin
    if l>=r then
    begin
        exit;
    end;
    i:=l;
    j:=r;
    mid:=data[(l+r) div 2];
    repeat
        while data[i]<mid do
        begin
            inc(i);
        end;
        while data[j]>mid do
        begin
            dec(j);
        end;
        if i<=j then
        begin
            data[0]:=data[i];
            data[i]:=data[j];
            data[j]:=data[0];
            inc(i);
            dec(j);
        end;
    until i>j;
    qsort(l,j,data);
    qsort(i,r,data);
end;
begin
    readln(n);
    for i := 0 to n-1 do
    begin
        read(a[i+8]);
    end;
    qsort(8,n+7,@a);
    x:=0;
    y:=0;
    i:=n+7;
    while i>=8 do
    begin
        inc(x,a[i]);
        dec(i,2);
    end;
    i:=n+6;
    while i>=8 do
    begin
        inc(y,a[i]);
        dec(i,2);
    end;
    writeln(x-y);
end.

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

N is an integer between 1 and 100 (inclusive).

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18


type
    datatype=Integer;
    obj=array[1..100] of datatype;

var
    n,i,x,y: Integer;
    a: obj;

procedure qsort(l,r:Longint;var data:obj);
var
    i,j,mid,t: datatype;
begin
    if l>=r then
    begin
        exit;
    end;
    i:=l;
    j:=r;
    mid:=data[(l+r) div 2];
    // mid:=(data + (l+r) div 2)^;
    repeat
        while data[i]<mid do
        begin
            inc(i);
        end;
        while data[j]>mid do
        begin
            dec(j);
        end;
        if i<=j then
        begin
            t:=data[i];
            data[i]:=data[j];
            data[j]:=t;
            inc(i);
            dec(j);
        end;
    until i>j;
    qsort(l,j,data);
    qsort(i,r,data);
end;
begin
    readln(n);
    for i := 1 to n do
    begin
        read(a[i]);
    end;
    qsort(1,n,a);
    x:=0;
    y:=0;
    i:=n;
    while i>0 do
    begin
        inc(x,a[i]);
        dec(i,2);
    end;
    i:=n-1;
    while i>0 do
    begin
        inc(y,a[i]);
        dec(i,2);
    end;
    writeln(x-y);
    readln;
    readln;
end.


var
   x,y:longint;

begin
  readln(x,y);
  if x mod y<>0 then writeln(x)
  else
    writeln(-1);
end.

Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.


var
  n,i,s1,s2:longint;
  a,b:array[1..10000] of longint;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  for i:=1 to n do read(b[i]);
  for i:=1 to n do
    begin
      if a[i]>b[i] then s1:=s1+a[i]-b[i];
      if a[i]<b[i] then s2:=s2+(b[i]-a[i]) div 2;
    end;
  if s1<=s2 then writeln('Yes') else writeln('No');
end.

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No


var
 n,i,j,t:longint;
 a,b:array[0..10001] of longint;
 p:boolean;
begin
 readln(n);
 for i:=1 to n do
  read(a[i]);
 readln;
 for i:=1 to n do
  read(b[i]);
 for i:=1 to n-1 do
  for j:=i+1 to n do
   if a[i]<a[j] then
    begin
     t:=a[i]; a[i]:=a[j]; a[j]:=t;
    end;
 for i:=1 to n-1 do
  for j:=i+1 to n do
   if b[i]<b[j] then
    begin
     t:=b[i]; b[i]:=b[j]; b[j]:=t;
    end;
 for i:=1 to n do
  if b[i]<a[i] then
   begin
    p:=true;
    break;
   end;
 if p then writeln('No')
  else writeln('Yes');
end.


type
  node=record
      num,id:longint;
  end;
var father,num,now,first:array[0..105000]of longint;
    p:array[0..105000]of node;
    heap:array[0..105000,0..2]of longint;
    i:longint;
    x,y:longint;
    n,m:longint;
    totin,totout:longint;
    qwq,ans,have,h,z:int64;

function getfather(x:longint):longint;
begin
  if father[x]=x then exit(x) else
  begin
    father[x]:=getfather(father[x]);
    getfather:=father[x];
  end;
end;
procedure adde(x,y:longint);
var fx,fy:longint;
begin
  fx:=getfather(x);
  fy:=getfather(y);
  if fx<>fy then
  begin
    father[fx]:=fy;
    inc(num[fy],num[fx]);
  end;
end;
procedure qs(l,r:longint);
var i,j:longint;
    t,m:node;
begin
  i:=l;
  j:=r;
  m:=p[(l+r)>>1];
  repeat
    while (p[i].id<m.id)or((p[i].id=m.id)and(p[i].num<m.num)) do inc(i);
    while (p[j].id>m.id)or((p[j].id=m.id)and(p[j].num>m.num)) do dec(j);
    if i<=j then
    begin
      t:=p[i];p[i]:=p[j];p[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
procedure swap(var a,b:longint);
var t:longint;
begin
  t:=a;a:=b;b:=t;
end;
procedure up(x,bool:longint);
begin
  while x>1 do
  begin
    //writeln(heap[x,bool],'qwq');
    if p[now[heap[x,bool]]].num<p[now[heap[x>>1,bool]]].num then
    begin
      swap(heap[x,bool],heap[x>>1,bool]);
      x:=x>>1;
    end else break;
  end;
end;
procedure down(x,bool:longint);
var min,tot:longint;
begin
  if bool=1 then tot:=totin else tot:=totout;
  while x*2<=tot do
  begin
    min:=x*2;
    if (x*2+1<=tot)and
    (p[now[heap[x*2+1,bool]]].num<p[now[heap[min,bool]]].num) then inc(min);
    if (p[now[heap[x,bool]]].num>p[now[heap[min,bool]]].num) then
    begin
      swap(heap[x,bool],heap[min,bool]);
      x:=min;
    end else break;
  end;
end;
begin
  read(n,m);
  for i:=1 to n do
  begin
    read(p[i].num);
    father[i]:=i;
    num[i]:=1;
  end;
  for i:=1 to m do
  begin
    read(x,y);
    inc(x);
    inc(y);
    adde(x,y);
  end;
  for i:=1 to n do
  p[i].id:=getfather(i);
  qs(1,n);
  p[0].num:=maxlongint;
  i:=1;
  while i<=n do
  begin
    inc(ans,p[i].num);
    if num[p[i].id]>1 then
    begin
      now[p[i].id]:=i+1;
      inc(have,num[p[i].id]);
      inc(h);
    end else
    begin
      now[p[i].id]:=0;
      inc(z);
    end;
    inc(totout);
    heap[totout,0]:=p[i].id;
    up(totout,0);
    first[p[i].id]:=i;
    i:=i+num[p[i].id];
  end;
  if totout=1 then
  begin
    writeln(0);
    exit;
  end;
  have:=have-(h-1)*2;
  if have<z then
  begin
    writeln('Impossible');
    exit;
  end;
  while totout>0 do
  begin
    inc(qwq);
    x:=heap[1,0];
    if now[x]<>0 then
    begin
      inc(totin);
      heap[totin,1]:=x;
      up(totin,1);
    end;
    if (qwq>2)and(totin>0) then
    begin
      x:=heap[1,1];
      inc(ans,p[now[x]].num);
      inc(now[x]);
      if (now[x]>=first[x]+num[x]) then
      begin
        now[x]:=0;
        heap[1,1]:=heap[totin,1];
        dec(totin);
      end;
      if totin>0 then down(1,1);
    end;
    heap[1,0]:=heap[totout,0];
    dec(totout);
    down(1,0);
  end;
  writeln(ans);
end.

Score : 600 points

Problem Statement

You are given a forest with N vertices and M edges. The vertices are numbered 0 through N-1.
The edges are given in the format (x_i,y_i), which means that Vertex x_i and y_i are connected by an edge.

Each vertex i has a value a_i.
You want to add edges in the given forest so that the forest becomes connected.
To add an edge, you choose two different vertices i and j, then span an edge between i and j.
This operation costs a_i + a_j dollars, and afterward neither Vertex i nor j can be selected again.

Find the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Constraints

1 ≤ N ≤ 100,000

0 ≤ M ≤ N-1

1 ≤ a_i ≤ 10^9

0 ≤ x_i,y_i ≤ N-1

The given graph is a forest.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 .. a_{N-1}
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Sample Input 1

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

Sample Output 1

7

If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1 + 6 = 7 dollars.

Sample Input 2

5 0
3 1 4 1 5

Sample Output 2

Impossible

We can't make the graph connected.

Sample Input 3

1 0
5

Sample Output 3

0

The graph is already connected, so we do not need to add any edges.


type
  node=record
      num,id:longint;
  end;
var father,num,now,first:array[0..105000]of longint;
    p:array[0..105000]of node;
    heap:array[0..105000,0..2]of longint;
    i:longint;
    x,y:longint;
    n,m:longint;
    totin,totout:longint;
    qwq,ans,have:int64;

function getfather(x:longint):longint;
begin
  if father[x]=x then exit(x) else
  begin
    father[x]:=getfather(father[x]);
    getfather:=father[x];
  end;
end;
procedure adde(x,y:longint);
var fx,fy:longint;
begin
  fx:=getfather(x);
  fy:=getfather(y);
  if fx<>fy then
  begin
    father[fx]:=fy;
    inc(num[fy],num[fx]);
  end;
end;
procedure qs(l,r:longint);
var i,j:longint;
    t,m:node;
begin
  i:=l;
  j:=r;
  m:=p[(l+r)>>1];
  repeat
    while (p[i].id<m.id)or((p[i].id=m.id)and(p[i].num<m.num)) do inc(i);
    while (p[j].id>m.id)or((p[j].id=m.id)and(p[j].num>m.num)) do dec(j);
    if i<=j then
    begin
      t:=p[i];p[i]:=p[j];p[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
procedure swap(var a,b:longint);
var t:longint;
begin
  t:=a;a:=b;b:=t;
end;
procedure up(x,bool:longint);
begin
  while x>1 do
  begin
    //writeln(heap[x,bool],'qwq');
    if p[now[heap[x,bool]]].num<p[now[heap[x>>1,bool]]].num then
    begin
      swap(heap[x,bool],heap[x>>1,bool]);
      x:=x>>1;
    end else break;
  end;
end;
procedure down(x,bool:longint);
var min,tot:longint;
begin
  if bool=1 then tot:=totin else tot:=totout;
  while x*2<=tot do
  begin
    min:=x*2;
    if (x*2+1<=tot)and
    (p[now[heap[x*2+1,bool]]].num<p[now[heap[min,bool]]].num) then inc(min);
    if (p[now[heap[x,bool]]].num>p[now[heap[min,bool]]].num) then
    begin
      swap(heap[x,bool],heap[min,bool]);
      x:=min;
    end else break;
  end;
end;
begin
  read(n,m);
  for i:=1 to n do
  begin
    read(p[i].num);
    father[i]:=i;
    num[i]:=1;
  end;
  for i:=1 to m do
  begin
    read(x,y);
    inc(x);
    inc(y);
    adde(x,y);
  end;
  for i:=1 to n do
  p[i].id:=getfather(i);
  qs(1,n);
  p[0].num:=maxlongint;
  i:=1;
  while i<=n do
  begin
    inc(ans,p[i].num);
    if num[p[i].id]>1 then
    begin
      now[p[i].id]:=i+1;
      inc(have,num[p[i].id]);
      dec(have);
    end else
    begin
      now[p[i].id]:=0;
      dec(have);
    end;
    inc(totout);
    heap[totout,0]:=p[i].id;
    up(totout,0);
    first[p[i].id]:=i;
    i:=i+num[p[i].id];
  end;
  if totout=1 then
  begin
    writeln(0);
    exit;
  end;
  if (n=2) then
  begin
    writeln(ans);
    exit;
  end;
  inc(have);
  if have<0 then
  begin
    writeln('Impossible');
    exit;
  end;
  while totout>0 do
  begin
    inc(qwq);
    x:=heap[1,0];
    if now[x]<>0 then
    begin
      inc(totin);
      heap[totin,1]:=x;
      up(totin,1);
    end;
    if (qwq>2)and(totin>0) then
    begin
      x:=heap[1,1];
      inc(ans,p[now[x]].num);
      inc(now[x]);
      if (now[x]>=first[x]+num[x]) then
      begin
        now[x]:=0;
        heap[1,1]:=heap[totin,1];
        dec(totin);
      end;
      if totin>0 then down(1,1);
    end;
    heap[1,0]:=heap[totout,0];
    dec(totout);
    down(1,0);
  end;
  writeln(ans);
end.


type
  node=record
      num,id:longint;
  end;
var father,num,now,first:array[0..105000]of longint;
    p:array[0..105000]of node;
    heap:array[0..105000,0..2]of longint;
    i:longint;
    x,y:longint;
    n,m:longint;
    totin,totout:longint;
    qwq,ans,have:int64;

function getfather(x:longint):longint;
begin
  if father[x]=x then exit(x) else
  begin
    father[x]:=getfather(father[x]);
    getfather:=father[x];
  end;
end;
procedure adde(x,y:longint);
var fx,fy:longint;
begin
  fx:=getfather(x);
  fy:=getfather(y);
  if fx<>fy then
  begin
    father[fx]:=fy;
    inc(num[fy],num[fx]);
  end;
end;
procedure qs(l,r:longint);
var i,j:longint;
    t,m:node;
begin
  i:=l;
  j:=r;
  m:=p[(l+r)>>1];
  repeat
    while (p[i].id<m.id)or((p[i].id=m.id)and(p[i].num<m.num)) do inc(i);
    while (p[j].id>m.id)or((p[j].id=m.id)and(p[j].num>m.num)) do dec(j);
    if i<=j then
    begin
      t:=p[i];p[i]:=p[j];p[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
procedure swap(var a,b:longint);
var t:longint;
begin
  t:=a;a:=b;b:=t;
end;
procedure up(x,bool:longint);
begin
  while x>1 do
  begin
    //writeln(heap[x,bool],'qwq');
    if p[now[heap[x,bool]]].num<p[now[heap[x>>1,bool]]].num then
    begin
      swap(heap[x,bool],heap[x>>1,bool]);
      x:=x>>1;
    end else break;
  end;
end;
procedure down(x,bool:longint);
var min,tot:longint;
begin
  if bool=1 then tot:=totin else tot:=totout;
  while x*2<=tot do
  begin
    min:=x*2;
    if (x*2+1<=tot)and
    (p[now[heap[x*2+1,bool]]].num<p[now[heap[min,bool]]].num) then inc(min);
    if (p[now[heap[x,bool]]].num>p[now[heap[min,bool]]].num) then
    begin
      swap(heap[x,bool],heap[min,bool]);
      x:=min;
    end else break;
  end;
end;
begin
  read(n,m);
  for i:=1 to n do
  begin
    read(p[i].num);
    father[i]:=i;
    num[i]:=1;
  end;
  for i:=1 to m do
  begin
    read(x,y);
    inc(x);
    inc(y);
    adde(x,y);
  end;
  for i:=1 to n do
  p[i].id:=getfather(i);
  qs(1,n);
  p[0].num:=maxlongint;
  i:=1;
  while i<=n do
  begin
    inc(ans,p[i].num);
    if num[p[i].id]>1 then
    begin
      now[p[i].id]:=i+1;
      inc(have,num[p[i].id]);
      dec(have);
    end else
    begin
      now[p[i].id]:=0;
      dec(have);
    end;
    inc(totout);
    heap[totout,0]:=p[i].id;
    up(totout,0);
    first[p[i].id]:=i;
    i:=i+num[p[i].id];
  end;
  if totout=1 then
  begin
    writeln(0);
    exit;
  end;
  inc(have);
  if have<0 then
  begin
    writeln('Impossible');
    exit;
  end;
  while totout>0 do
  begin
    inc(qwq);
    x:=heap[1,0];
    if now[x]<>0 then
    begin
      inc(totin);
      heap[totin,1]:=x;
      up(totin,1);
    end;
    if (qwq>2)and(totin>0) then
    begin
      x:=heap[1,1];
      inc(ans,p[now[x]].num);
      inc(now[x]);
      if (now[x]>=first[x]+num[x]) then
      begin
        now[x]:=0;
        heap[1,1]:=heap[totin,1];
        dec(totin);
      end;
      if totin>0 then down(1,1);
    end;
    heap[1,0]:=heap[totout,0];
    dec(totout);
    down(1,0);
  end;
  writeln(ans);
end.


type
  node=record
      num,id:longint;
  end;
var father,num,now,first:array[0..105000]of longint;
    p:array[0..105000]of node;
    heap:array[0..105000,0..2]of longint;
    i:longint;
    x,y:longint;
    n,m:longint;
    totin,totout:longint;
    qwq,ans,have:int64;

function getfather(x:longint):longint;
begin
  if father[x]=x then exit(x) else
  begin
    father[x]:=getfather(father[x]);
    getfather:=father[x];
  end;
end;
procedure adde(x,y:longint);
var fx,fy:longint;
begin
  fx:=getfather(x);
  fy:=getfather(y);
  if fx<>fy then
  begin
    father[fx]:=fy;
    inc(num[fy],num[fx]);
  end;
end;
procedure qs(l,r:longint);
var i,j:longint;
    t,m:node;
begin
  i:=l;
  j:=r;
  m:=p[(l+r)>>1];
  repeat
    while (p[i].id<m.id)or((p[i].id=m.id)and(p[i].num<m.num)) do inc(i);
    while (p[j].id>m.id)or((p[j].id=m.id)and(p[j].num>m.num)) do dec(j);
    if i<=j then
    begin
      t:=p[i];p[i]:=p[j];p[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
procedure swap(var a,b:longint);
var t:longint;
begin
  t:=a;a:=b;b:=t;
end;
procedure up(x,bool:longint);
begin
  while x>1 do
  begin
    //writeln(heap[x,bool],'qwq');
    if p[now[heap[x,bool]]].num<p[now[heap[x>>1,bool]]].num then
    begin
      swap(heap[x,bool],heap[x>>1,bool]);
      x:=x>>1;
    end else break;
  end;
end;
procedure down(x,bool:longint);
var min,tot:longint;
begin
  if bool=1 then tot:=totin else tot:=totout;
  while x*2<=tot do
  begin
    min:=x*2;
    if (x*2+1<=tot)and
    (p[now[heap[x*2+1,bool]]].num<p[now[heap[min,bool]]].num) then inc(min);
    if (p[now[heap[x,bool]]].num>p[now[heap[min,bool]]].num) then
    begin
      swap(heap[x,bool],heap[min,bool]);
      x:=min;
    end else break;
  end;
end;
begin
  read(n,m);
  for i:=1 to n do
  begin
    read(p[i].num);
    father[i]:=i;
    num[i]:=1;
  end;
  for i:=1 to m do
  begin
    read(x,y);
    inc(x);
    inc(y);
    adde(x,y);
  end;
  for i:=1 to n do
  p[i].id:=getfather(i);
  qs(1,n);
  p[0].num:=maxlongint;
  i:=1;
  while i<=n do
  begin
    inc(ans,p[i].num);
    if num[p[i].id]>1 then
    begin
      now[p[i].id]:=i+1;
      inc(have,num[p[i].id]);
      dec(have);
    end else
    begin
      now[p[i].id]:=0;
      dec(have);
    end;
    inc(totout);
    heap[totout,0]:=p[i].id;
    up(totout,0);
    first[p[i].id]:=i;
    i:=i+num[p[i].id];
  end;
  if totout=1 then
  begin
    writeln(0);
    exit;
  end;
  inc(have);
  if have<0 then
  begin
    writeln('Impossible');
    exit;
  end;
  while totout>0 do
  begin
    inc(qwq);
    x:=heap[1,0];
    if now[x]<>0 then
    begin
      inc(totin);
      heap[totin,1]:=x;
      up(totin,1);
    end else
    if (totin>0) then
    begin
      x:=heap[1,1];
      inc(ans,p[now[x]].num);
    end;
    if (qwq>1)and(totin>0) then
    begin
      x:=heap[1,1];
      if qwq>2 then inc(ans,p[now[x]].num);
      inc(now[x]);
      if (now[x]>=first[x]+num[x]) then
      begin
        now[x]:=0;
        heap[1,1]:=heap[totin,1];
        dec(totin);
      end;
      if totin>0 then down(1,1);
    end;
    heap[1,0]:=heap[totout,0];
    dec(totout);
    down(1,0);
  end;
  writeln(ans);
end.


var
	i,j,k,sum: longint;
    loop,loop_2,loop_3,ans: integer;
begin
	readln(loop);
    readln(loop_2);
    readln(loop_3);
    readln(ans);
	for i := 0 to loop do begin
        for j:= 0 to loop_2 do begin
            for k:= 0 to loop_3 do begin
            	//writeln(i * 500);
                //writeln(j * 100);
                //writeln(k * 50);
               	if (i * 500 + j * 100 + k * 50 = ans) then inc(sum); //インクリメント
            end;
        end;
     end;

     writeln(sum);

     //writeln(loop);
     //writeln(loop_2);
     //writeln(loop_3);

 end.

Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


var
  x,a,b:longint;
begin
  readln(x);
  readln(a);
  readln(b);
  x:=x-a;
  writeln(x mod b);
end.


uses math;
var f,a:array[-1..10,0..1020] of longint;
var n,i,j:longint;
begin
  read(n);for i:=1 to 2 do for j:=1 to n do
  begin read(a[i,j]);f[i,j]:=max(f[i-1,j],f[i,j-1])+a[i,j];end;
  writeln(f[2,n]);
end.

Score : 300 points

Problem Statement

We have a 2 \times N grid. We will denote the square at the i-th row and j-th column (1 \leq i \leq 2, 1 \leq j \leq N) as (i, j).

You are initially in the top-left square, (1, 1).
You will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

The square (i, j) contains A_{i, j} candies.
You will collect all the candies you visit during the travel.
The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

1 \leq N \leq 100

1 \leq A_{i, j} \leq 100 (1 \leq i \leq 2, 1 \leq j \leq N)

Input

Input is given from Standard Input in the following format:

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

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

move right three times, then move down once, then move right once.

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

You will always collect the same number of candies, regardless of how you travel.

Sample Input 3

7
3 3 4 5 4 5 3
5 3 4 4 2 3 2

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


var
    n,m,i: Longint;
    L,R,D: Longint;
    position:array[1..100000] of Longint;
    flag:Boolean;
begin
    flag:=true;
    readln(n,m);
    for i := 1 to n do
    begin
        position[i]:=-2147483648;
    end;
    for i := 1 to m do
    begin
        readln(L,R,D);
        if (position[L]=-2147483648)and(position[R]=-2147483648) then
        begin
            position[L]:=0;
            position[R]:=D;
        end
        else
        begin
            if (position[L]<>-2147483648) and (position[R]<>-2147483648) then
            begin
                if position[L]+D<>position[R] then
                begin
                    flag:=false;
                    break;
                end;
            end
            else
            begin
                if position[L]<>-2147483648 then
                begin
                    position[R]:=position[L]+D;
                end
                else
                begin
                    position[L]:=position[R]-D;
                end;
            end;
        end;
    end;
    if flag then
    begin
        writeln('Yes');
    end
    else
    begin
        writeln('No');
    end;
end.

Score : 400 points

Problem Statement

There are N people standing on the x-axis.
Let the coordinate of Person i be x_i.
For every i, x_i is an integer between 0 and 10^9 (inclusive).
It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people.
The i-th piece of information has the form (L_i, R_i, D_i).
This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.

It turns out that some of these M pieces of information may be incorrect.
Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Constraints

1 \leq N \leq 100 000

0 \leq M \leq 200 000

1 \leq L_i, R_i \leq N (1 \leq i \leq M)

0 \leq D_i \leq 10 000 (1 \leq i \leq M)

L_i \neq R_i (1 \leq i \leq M)

If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).

D_i are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M

Output

If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.

Sample Input 1

3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Yes

Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103).

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

No

If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Yes

Sample Input 4

10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

No

Sample Input 5

100 0

Sample Output 5

Yes


var x,y,z,n,m:longint;
    i,t,w:longint;
    father,cost:array[0..1000000]of longint;
    flag:boolean;
function getfather(x:longint):longint;
var t:longint;
begin
  if father[x]=x then exit(x) else
  begin
    t:=getfather(father[x]);
    cost[x]:=cost[x]+cost[father[x]];
    father[x]:=t;
    getfather:=father[x];
  end;
end;
procedure join(x,y,z:longint);
var fx,fy:longint;
begin
  fx:=getfather(x);
  fy:=getfather(y);
  if fx<>fy then
  begin
    father[fx]:=fy;
    cost[fx]:=cost[y]-z-cost[x];
  end else
  if cost[y]-cost[x]<>z then flag:=false;
end;
begin
    flag:=true;
    read(n,m);
    for i:=0 to n do
    begin
      father[i]:=i;
      cost[i]:=0;
    end;
    for i:=1 to m do
    begin
      read(x,y,z);
      join(x,y,z);
    end;
    if flag then writeln('Yes') else writeln('No');
end.


var x,y,t:array[0..100000]of longint;
    n,i:longint;
begin
  readln(n);
  for i:=1 to n do
    readln(t[i],x[i],y[i]);
  for i:=1 to n do
    if (x[i]+y[i]>t[i])or((t[i]-x[i]-y[i])mod 2<>0) then
    begin
      writeln('No');
      halt;
    end;
  writeln('Yes');
end.

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


var n,i,t,c,o:longint;
a,b:array[0..100000]of longint;
begin
readln(n);
for i:=1 to n do
  begin
  read(t,c,o);
  if t<c+o then
    begin
    writeln('No');
    halt;
    end;
  if((t-c-o)mod 2=1)then
    begin
    writeln('No');
    halt;
    end;
  end;   writeln('Yes');
end.


var
    n,i,a,b,t,x,y:longint;
begin
    readln(n);
    x:=0; y:=0;
    for i:=1 to n do
    begin
        read(t,a,b);
        if (abs(x-a)+abs(y-b)>t) or (abs(abs(x-a)+abs(y-b)-t) mod 2=1) then
        begin
            writeln('No');
            halt;
        end;
    end;
    writeln('Yes');
end.


var
    n,i,a,b,t,x,y:longint;
begin
    readln(n);
    x:=0; y:=0;
    for i:=1 to n do
    begin
        read(t,a,b);
        if (abs(x-a)+abs(y-b)>t) or ((abs(x-a)+abs(y-b)-t) mod 2=1) then
        begin
            writeln('No');
            halt;
        end;
        x:=a; y:=b;
    end;
    writeln('Yes');
end.


var
 n,i,x,y,z:longint;
begin
 readln(n);
 for i:=1 to n do
  begin
   readln(z,x,y);
   if x+y>z then
    begin
     writeln('No');
     halt;
    end
    else
   if ((x+y) mod 2=0) and (odd(z)) then
    begin
     writeln('No');
     halt;
    end
    else
   if ((x+y) mod 2=1) and (z mod 2=0) then
    begin
     writeln('No');
     halt;
    end;
  end;
 writeln('Yes');
end.


var
  n,x,y,t,x1,y1,t1,i,s:longint;
begin
  readln(n);
  x:=0; y:=0;
  for i:= 1 to n do
    begin
      readln(t1,x1,y1);
      if (x=x1)and(y=y1)then begin writeln('No'); exit; end;
      if abs(x-x1)+abs(y-y1)=t1-t then begin x:=x1;y:=y1;t:=t1;end
                                  else
                                    begin
                                      if abs(x-x1)+abs(y-y1)<t1-t
                                        then
                                         begin
                                          s:=(t1-t)-(abs(x-x1)+abs(y-y1));
                                          if odd(s) then
                                                      begin
                                                        writeln('No');
                                                        exit;
                                                      end
                                                    else
                                                      begin
                                                        x:=x1;
                                                        y:=y1;
                                                        t:=t1;
                                                      end;
                                         end
                                        else
                                          begin
                                            writeln('No');
                                            exit;
                                          end

                                    end;
    end;
  writeln('Yes');
end.


var
n,i:longint;
xi,yi,ti:array[0..100000] of longint;
function travel:string;
begin
for i:=1 to n do
begin
if (ti[i]+ti[i-1]+xi[i]+yi[i]+yi[i-1]+xi[i-1]) mod 2=1 then exit('No');
if ti[i]-ti[i-1]<abs(xi[i]-xi[i-1])+abs(yi[i]-yi[i-1]) then exit('No');
end;
exit('Yes');
end;
begin
xi[0]:=0;
yi[0]:=0;
ti[0]:=0;
read(n);
for i:=1 to n do read(ti[i],xi[i],yi[i]);

writeln(travel);
end.


var
  i,n,k,j,xx,xy:longint;
  t,x,y:array[0..100000] of longint;
  //q:array[0..1000000000,1..3] of longint;
begin
  readln(n);
  for i:=1 to n do readln(t[i],x[i],y[i]);
  writeln('No');
  close(input);
  close(output);
end.


var n,i,j,x,y,t,ans,lx,ly,lt:longint;
begin
  read(n);lx:=0;ly:=0;lt:=0;
  for i:=1 to n do
  begin
    read(t,x,y);
    ans:=t-(lt+abs(x-lx)+abs(y-ly));
    if (ans>=0) and (ans mod 2=0) then begin lx:=x;ly:=y;lt:=t;end
    else begin writeln('No');halt;end;
  end;
  writeln('Yes');
end.


var
	N, K, i, j, x, y, b, nx, ny: integer;
    c: string;
	cnt: array[1..1001, 1..1001] of integer;
begin
	read(N, K);
    for i := 1 to N do begin
    	read(x, y, c);
        if c = ' W' then y := y + k;
        if x div K mod 2 = y div K mod 2 then
        	b := 1
        else
        	b := -1;
        if b = 1 then cnt[1, 1] := cnt[1, 1] + 1;
        nx := x mod K + 2;
        ny := y mod K + 2;
        cnt[nx, 1] := cnt[nx, 1] - b;
        cnt[1, ny] := cnt[1, ny] - b;
        cnt[nx, ny] := cnt[nx, ny] + b * 2;
    end;
    for i := 1 to K + 1 do for j := 1 to K do
    	cnt[i, j + 1] := cnt[i, j + 1] + cnt[i, j];
    for j := 1 to K + 1 do for i := 1 to K do
    	cnt[i + 1, j] := cnt[i + 1, j] + cnt[i, j];
    for i := 1 to K do for j := 1 to K do begin
    	if b < cnt[i, j] then b := cnt[i, j];
    	if b < N - cnt[i, j] then b := N - cnt[i, j];
    end;
    writeln(b);
end.

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


{$R-,S-,Q-,I-,O+}
const KK=1000;
var cond:array[0..1,1..4*KK,1..4*KK] of longint;
    dp:array[0..1,0..4*KK,0..4*KK] of longint;
    n,k,i,x,y,dx,dy,col,row_sum,max,temp:longint;
    c,d:char;

function sum(col,x1,y1,x2,y2:longint):longint;
begin
  sum:=dp[col,x2,y2]+dp[col,x1-1,y1-1]
        -dp[col,x2,y1-1]-dp[col,x1-1,y2];
end;

begin
  readln(n,k);
  for i:=1 to n do begin
    readln(x,y,c,d);
    x:=x mod (2*k);
    y:=y mod (2*k);
    col:=ord(d='W');
    for dx:=0 to 1 do for dy:=0 to 1 do
      inc(cond[col,x+dx*2*k,y+dy*2*k]);
  end;

  for col:=0 to 1 do
    for x:=1 to 4*k do begin
      row_sum:=0;
      for y:=1 to 4*k do begin
        inc(row_sum,cond[col,x,y]);
        dp[col,x,y]:=dp[col,x-1,y]+row_sum;
      end;
    end;

  max:=0;
  for x:=1 to 2*k do for y:=1 to 2*k do begin
    temp:=0;
    inc(temp,sum(0,x+k,y+k,x+2*k-1,y+2*k-1));
    inc(temp,sum(0,x  ,y  ,x+  k-1,y+  k-1));
    inc(temp,sum(1,x+k,y  ,x+2*k-1,y+  k-1));
    inc(temp,sum(1,x  ,y+k,x+  k-1,y+2*k-1));
    if max<temp then max:=temp;
  end;

  writeln(max);
end.


{$R-,S-,Q-,I-,O+}
const KK=1000;
var cond:array[0..1,1..4*KK,1..4*KK] of longint;
    dp:array[0..1,0..4*KK+1,0..4*KK+1] of longint;
    n,k,i,x,y,dx,dy,col,row_sum,max,temp:longint;
    c,d:char;
function sum(c,x1,y1,x2,y2:longint):longint;
begin
  sum:=dp[c,x2,y2]+dp[c,x1-1,y1-1]
        -dp[c,x2,y1-1]-dp[c,x1-1,y2];
end;

begin
  readln(n,k);
  for i:=1 to n do begin
    readln(x,y,c,d);
    x:=x mod (2*k);
    y:=y mod (2*k);
    col:=ord(c='W');
    for dx:=0 to 1 do for dy:=0 to 1 do
      inc(cond[col,x+dx*2*k,y+dy*2*k]);
  end;

  for col:=0 to 1 do
    for x:=1 to 4*k do begin
      row_sum:=0;
      for y:=1 to 4*k do begin
        inc(row_sum,cond[col,x,y]);
        dp[col,x,y]:=dp[col,x-1,y]+row_sum;
      end;
    end;

  for x:=1 to 2*k do for y:=1 to 2*k do begin
    temp:=0;
    inc(temp,sum(0,x+k,y+k,x+2*k-1,y+2*k-1));
    inc(temp,sum(0,x  ,y  ,x+  k-1,y+  k-1));
    inc(temp,sum(1,x+k,y  ,x+2*k-1,y+  k-1));
    inc(temp,sum(1,x  ,y+k,x+  k-1,y+2*k-1));
    if max<temp then max:=temp;
  end;

  writeln(max);
end.


var i,j,n,tmp3,x1,y1,x2,y2,k,ans,tmp5:longint;
    x,y:array[1..100000]of longint;
    c,c2:array[0..2000,0..2000]of longint;
    tmp,tmp2:char;
begin
  readln(n,k);
  for i:=1 to n do
  begin
    readln(x[i],y[i],tmp,tmp2);
    x[i]:=x[i] mod (k*2);
    y[i]:=y[i] mod (k*2);
    if tmp2='B' then
    begin
      if x[i]>=k then x[i]:=x[i]-k
      else x[i]:=x[i]+k;
      if y[i]>=k then y[i]:=y[i]-k
      else y[i]:=y[i]+k;
    end;
    inc(c[x[i],y[i]]);
  end;
  for i:=0 to k*2-1 do
    for j:=0 to k*2-1 do
    c2[i,j]:=c2[i-1,j]+c2[i,j-1]+c[i,j]-c2[i-1,j-1];
  for i:=0 to k*2-1 do
    for j:=0 to k*2-1 do
    begin
      x1:=i;
      y1:=j;
      x2:=i+k-1;
      y2:=j+k-1;
      tmp5:=c2[x2,y2]-c2[x1,y2]-c2[x2,y1]+c2[x1,y1];
      if tmp5>ans then ans:=tmp5;
    end;
    writeln(ans);
end.