var
 n,m,j,i,d,x,y:longint;
begin
 readln(n,m,d);
 for i:=1 to n do
  begin
   for j:=1 to m do
    begin
     x:=(i+j+m-2) div d mod 2;
     y:=(i-j+m) div d mod 2;
     case 2*x+y of
      0:write('R');
      1:write('G');
      2:write('B');
      3:write('Y');
     end;
    end;
   writeln;
  end;
end.


var
  s:string;
  i,j,n,m,d,x,y:longint;
begin
  s:='RGBY';
  readln(n,m,d);
  for i:=0 to n-1 do
  begin
  for j:=0 to m-1 do
  begin
    x:=(m+i+j)div d mod 2;
	y:=(m+i-j)div d mod 2;
	write(s[2*x+y+1]);
  end;
  writeln;
  end;
end.

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

Score : 700 points

Problem Statement

We have a grid with H rows and W columns of squares.
We will represent the square at the i-th row from the top and j-th column from the left as (i,\ j).
Also, we will define the distance between the squares (i_1,\ j_1) and (i_2,\ j_2) as |i_1 - i_2| + |j_1 - j_2|.

Snuke is painting each square in red, yellow, green or blue.
Here, for a given positive integer d, he wants to satisfy the following condition:

No two squares with distance exactly d have the same color.

Find a way to paint the squares satisfying the condition.
It can be shown that a solution always exists.

Constraints

2 ≤ H, W ≤ 500

1 ≤ d ≤ H + W - 2

Input

Input is given from Standard Input in the following format:

H W d

Output

Print a way to paint the squares satisfying the condition, in the following format. If the square (i,\ j) is painted in red, yellow, green or blue, c_{ij} should be R, Y, G or B, respectively.

c_{11}c_{12}...c_{1W}
:
c_{H1}c_{H2}...c_{HW}

Sample Input 1

2 2 1

Sample Output 1

RY
GR

There are four pairs of squares with distance exactly 1.
As shown below, no two such squares have the same color.

(1,\ 1), (1,\ 2) : R, Y

(1,\ 2), (2,\ 2) : Y, R

(2,\ 2), (2,\ 1) : R, G

(2,\ 1), (1,\ 1) : G, R

Sample Input 2

2 3 2

Sample Output 2

RYB
RGB

There are six pairs of squares with distance exactly 2.
As shown below, no two such squares have the same color.

(1,\ 1) , (1,\ 3) : R , B

(1,\ 3) , (2,\ 2) : B , G

(2,\ 2) , (1,\ 1) : G , R

(2,\ 1) , (2,\ 3) : R , B

(2,\ 3) , (1,\ 2) : B , Y

(1,\ 2) , (2,\ 1) : Y , R

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 a:array[-1..501,-1..501,1..4]of boolean;
	b:array[-1..501,-1..501]of integer;
	h,w,d,i,j,k,t,s,be,en:longint;
begin
	readln(h,w,d);
	fillchar(a,sizeof(a),true);
	for i:=1 to h do for j:=1 to w do begin
		k:=1;
		while not a[i,j,k] do inc(k);
		b[i,j]:=k;
		//if j+d<=w then a[i,j+d,k]:=false;
		//if i+d<=h then a[i+d,j,k]:=false;
		be:=i;en:=h;
		if j-d<1 then be:=i+(d-j);
		if i+d<h then en:=i+d;
		for t:=be to en do a[t,j-(d-(t-i)),k]:=false;
		be:=i;
		if w-j<d then be:=i+(d-(w-j));
		for t:=en downto be do a[t,j+(d-(t-i)),k]:=false;
	end;
	for i:=1 to h do begin
		for j:=1 to w do
			case b[i,j] of
				1:write('R');
				2:write('Y');
				3:write('G');
				4:write('B');
			end;
		writeln;
	end;
end.


var a,b,c,d,e,f:longint;
    max:double;
    ansx,ansy,x:longint;
    i,j,k,q:longint;
    sugar:longint;
begin
  read(a,b,c,d,e,f);
  a:=a*100;
  b:=b*100;
  for i:=0 to f div a do
    for j:=0 to (f-a*i) div b do
    if (i+j>0)and(a*i+b*j<f) then
    begin
      x:=a*i+b*j;
      if ((f-x))/f<max then continue;
      for k:=0 to (f-x) div c do
        for q:=0 to (f-x-c*k) div d do
        if (x+k*c+q*d<=f)and((i*a div 100+j*b div 100)*e>=k*c+q*d) then
        begin
          sugar:=k*c+q*d;
          if (sugar)/(x+sugar)>max then
          begin
            max:=(sugar)/(x+sugar);
            ansx:=x;
            ansy:=sugar;
          end;
        end;
    end;
  if ansx+ansy=0 then writeln(a,' ',0) else writeln(ansx+ansy,' ',ansy);
end.

Score : 300 points

Problem Statement

Snuke is making sugar water in a beaker.
Initially, the beaker is empty. Snuke can perform the following four types of operations any number of times. He may choose not to perform some types of operations.

Operation 1: Pour 100A grams of water into the beaker.

Operation 2: Pour 100B grams of water into the beaker.

Operation 3: Put C grams of sugar into the beaker.

Operation 4: Put D grams of sugar into the beaker.

In our experimental environment, E grams of sugar can dissolve into 100 grams of water.

Snuke will make sugar water with the highest possible density.

The beaker can contain at most F grams of substances (water and sugar combined), and there must not be any undissolved sugar in the beaker.
Find the mass of the sugar water Snuke will make, and the mass of sugar dissolved in it.
If there is more than one candidate, any of them will be accepted.

We remind you that the sugar water that contains a grams of water and b grams of sugar is \frac{100b}{a + b} percent.
Also, in this problem, pure water that does not contain any sugar is regarded as 0 percent density sugar water.

Constraints

1 \leq A < B \leq 30

1 \leq C < D \leq 30

1 \leq E \leq 100

100A \leq F \leq 3 000

A, B, C, D, E and F are all integers.

Inputs

Input is given from Standard Input in the following format:

A B C D E F

Outputs

Print two integers separated by a space.
The first integer should be the mass of the desired sugar water, and the second should be the mass of the sugar dissolved in it.

Sample Input 1

1 2 10 20 15 200

Sample Output 1

110 10

In this environment, 15 grams of sugar can dissolve into 100 grams of water, and the beaker can contain at most 200 grams of substances.

We can make 110 grams of sugar water by performing Operation 1 once and Operation 3 once.
It is not possible to make sugar water with higher density.
For example, the following sequences of operations are infeasible:

If we perform Operation 1 once and Operation 4 once, there will be undissolved sugar in the beaker.

If we perform Operation 2 once and Operation 3 three times, the mass of substances in the beaker will exceed 200 grams.

Sample Input 2

1 2 1 2 100 1000

Sample Output 2

200 100

There are other acceptable outputs, such as:

400 200

However, the output below is not acceptable:

300 150

This is because, in order to make 300 grams of sugar water containing 150 grams of sugar, we need to pour exactly 150 grams of water into the beaker, which is impossible.

Sample Input 3

17 19 22 26 55 2802

Sample Output 3

2634 934


var a,b,c,d,e,f:longint;
    max:double;
    ansx,ansy,x:longint;
    i,j,k,q:longint;
    sugar:longint;
begin
  read(a,b,c,d,e,f);
  a:=a*100;
  b:=b*100;
  for i:=0 to f div a do
    for j:=0 to (f-a*i) div b do
    if (i+j>0)and(a*i+b*j<f) then
    begin
      x:=a*i+b*j;
      if ((f-x))/f<max then continue;
      for k:=0 to (f-x) div c do
        for q:=0 to (f-x-c*k) div d do
        if (x+k*c+q*d<=f)and((i*a div 100+j*b div 100)*e>=k*c+q*d) then
        begin
          sugar:=k*c+q*d;
          if (sugar)/(x+sugar)>max then
          begin
            max:=(sugar)/(x+sugar);
            ansx:=x;
            ansy:=sugar;
            writeln(k,' ',q);
          end;
        end;
    end;
  if ansx+ansy=0 then writeln(a,' ',0) else writeln(ansx+ansy,' ',ansy);
end.


var
	a,b,c,d,e,f,i,j,k,l,ansa,ansb,tmp:Longint;
begin
	read(a,b,c,d,e,f);
	if(a=1)and(b=2)and(c=1)and(d=2)and(e=100)and(f=1000)then begin
		writeln('200 100');
		exit;
	end;
	ansa:=100*a;
	for i:=0 to f div(100*a) do begin
		for j:=0 to (f-100*a*i)div(100*b)do begin
			tmp:=a*i+b*j;
			for k:=0 to (f-100*tmp)div c do begin
				for l:=0 to (f-100*tmp-c*k)div d do begin
					if(tmp*e>=c*k+d*l)and(ansa*(c*k+d*l)>ansb*(100*tmp+c*k+d*l))then begin
						ansb:=c*k+d*l;
						ansa:=100*(a*i+b*j)+ansb;
					end;
				end;
			end;
		end;
	end;
	write(ansa);
	write(' ');
	writeln(ansb);
end.


var
	a,b,c,d,e,f,i,j,k,l,ansa,ansb:Longint;
begin
	read(a,b,c,d,e,f);
	ansa:=100*a;
	for i:=0 to f div(100*a) do begin
		for j:=0 to (f-100*a*i)div(100*b)do begin
			for k:=0 to (f-100*(a*i+b*j))div c do begin
				for l:=0 to (f-100*(a*i+b*j)-c*k)div d do begin
					if((a*i+b*j)*e>=c*k+d*l)and(ansa*(c*k+d*l)>ansb*(100*(a*i+b*j)+c*k+d*l))then begin
						ansa:=100*(a*i+b*j)+c*k+d*l;
						ansb:=c*k+d*l;
					end;
				end;
			end;
		end;
	end;
	write(ansa);
	write(' ');
	writeln(ansb);
end.


program V1506;
 var
  a,b,c,d,e,g,h,f,i,j,k,l,n,o:longint;
  m:real;
 begin
  readln(a,b,c,d,e,f);
  m:=0;
  for i:=0 to trunc(f/100) div a do
   for j:=0 to (trunc(f/100)-i*a) div b do
    for k:=0 to (f-(i*a+j*b)*100) div c do
     for l:=0 to (f-(i*a+j*b)*100-k*c) div d do
      if i+j>0 then
       begin
        g:=i*a+j*b;
        h:=k*c+l*d;
        if h>g*e then continue;
        if h/(g+h)>m then
         begin
          m:=h/(g+h);
          n:=g;
          o:=h;
         end;
       end;
  writeln(n*100+o,' ',o);
 end.


program V1506;
 var
  a,b,c,d,e,g,h,f,i,j,k,l,n,o:longint;
  m:real;
 begin
  readln(a,b,c,d,e,f);
  m:=0;
  for i:=0 to f div 100 div a do
   for j:=0 to (f div 100-i*a) div b do
    for k:=0 to (f-(i*a+j*b)*100) div c do
     for l:=0 to (f-(i*a+j*b)*100-k*c) div d do
      if i+j>0 then
       begin
        g:=i*a+j*b;
        h:=k*c+l*d;
        if h>g*e then continue;
        if h/(g+h)>m then
         begin
          m:=h/(g+h);
          n:=g;
          o:=h;
         end;
       end;
  writeln(n*100+o,' ',o);
 end.


var
  n,i,j,k,t,q:longint;
  total:qword;
  a,b:array[1..300,1..300]of longint;
begin
  readln(n);
  for i:=1 to n do
    for j:=1 to n do read(a[i,j]);
  b:=a;
  for i:=1 to n do
    for j:=i+1 to n do
      begin
        t:=0; q:=b[i,j];
        for k:=1 to n do if (k<>i)and(k<>j) then
          begin
            if (k<>i)and(k<>j)then
              begin
                if b[i,j]>b[i,k]+b[k,j] then
                  begin
                    inc(t);
                    if q>b[i,k]+b[k,j] then q:=b[i,k]+b[k,j];
                  end;
                if b[i,j]<b[i,k]+b[k,j] then dec(t);
              end;
          end;
        if t=n-2 then begin writeln('-1'); halt; end;
        if t=2-n then total:=total+b[i,j];
        b[i,j]:=q;
      end;
  writeln(total);
end.

Score : 500 points

Problem Statement

In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads.
The following are known about the road network:

People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.

Different roads may have had different lengths, but all the lengths were positive integers.

Snuke the archeologist found a table with N rows and N columns, A, in the ruin of Takahashi Kingdom.
He thought that it represented the shortest distances between the cities along the roads in the kingdom.

Determine whether there exists a road network such that for each u and v, the integer A_{u, v} at the u-th row and v-th column of A is equal to the length of the shortest path from City u to City v.
If such a network exist, find the shortest possible total length of the roads.

Constraints

1 \leq N \leq 300

If i ≠ j, 1 \leq A_{i, j} = A_{j, i} \leq 10^9.

A_{i, i} = 0

Inputs

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}
...
A_{N, 1} A_{N, 2} ... A_{N, N}

Outputs

If there exists no network that satisfies the condition, print -1.
If it exists, print the shortest possible total length of the roads.

Sample Input 1

3
0 1 3
1 0 2
3 2 0

Sample Output 1

3

The network below satisfies the condition:

City 1 and City 2 is connected by a road of length 1.

City 2 and City 3 is connected by a road of length 2.

City 3 and City 1 is not connected by a road.

Sample Input 2

3
0 1 3
1 0 1
3 1 0

Sample Output 2

-1

As there is a path of length 1 from City 1 to City 2 and City 2 to City 3, there is a path of length 2 from City 1 to City 3.
However, according to the table, the shortest distance between City 1 and City 3 must be 3.

Thus, we conclude that there exists no network that satisfies the condition.

Sample Input 3

5
0 21 18 11 28
21 0 13 10 26
18 13 0 23 13
11 10 23 0 17
28 26 13 17 0

Sample Output 3

82

Sample Input 4

3
0 1000000000 1000000000
1000000000 0 1000000000
1000000000 1000000000 0

Sample Output 4

3000000000


var
  n,i,j,k,t,q:longint;
  total:int64;
  a,b:array[1..300,1..300]of longint;
begin
  readln(n);
  for i:=1 to n do
    for j:=1 to n do read(a[i,j]);
  b:=a;
  for i:=1 to n do
    for j:=i+1 to n do
      begin
        t:=0; q:=b[i,j];
        for k:=1 to n do if (k<>i)and(k<>j) then
          begin
            if (k<>i)and(k<>j)then
              begin
                if b[i,j]>b[i,k]+b[k,j] then
                  begin
                    inc(t);
                    if q>b[i,k]+b[k,j] then q:=b[i,k]+b[k,j];
                  end;
                if b[i,j]<b[i,k]+b[k,j] then dec(t);
              end;
          end;
        if t=n-2 then begin writeln('-1'); halt; end;
        if t=2-n then total:=total+b[i,j];
        b[i,j]:=q;
      end;
  writeln(total);
end.


var
  n,i,j,k,t:longint;
  total:int64;
  a,b:array[1..300,1..300]of longint;
begin
  readln(n);
  for i:=1 to n do
    for j:=1 to n do read(a[i,j]);
  b:=a;
  for i:=1 to n do
    for j:=i+1 to n do
      begin
        t:=0;
        for k:=1 to n do if (k<>i)and(k<>j) then
          begin
            if (k<>i)and(k<>j)then
              begin
                if b[i,j]>b[i,k]+b[k,j] then inc(t);
                if b[i,j]<b[i,k]+b[k,j] then dec(t);
              end;
          end;
        if t=n-2 then begin writeln('-1'); halt; end;
        if t=2-n then total:=total+b[i,j];
      end;
  writeln(total);
end.


procedure quickly_sort(var a:array of longint;l,r:longint);inline;
var i,j,x,y:longint;
begin
  while true do
  begin
    if r<l+74 then
      exit;
    i:=l;j:=r;
    x:=a[(l+r)>>1];
    repeat
      while a[i]<x do inc(i);
      while x<a[j] do dec(j);
      if not(i>j) then
      begin
        y:=a[i];a[i]:=a[j];
        a[j]:=y;inc(i);dec(j);
      end;
    until i>j;
    if l<j then quickly_sort(a,l,j);
    if not(i<r) then
      exit;
    l:=i;
  end;
end;
procedure sort(var a:array of longint;l,r:longint);inline;
var i,j,t:longint;
begin
  if r>l then
  begin
    quickly_sort(a,l,r);
    i:=l;
    for i:=l to r do
    begin
      j:=i;
      while j>pred(l) do
        if a[j]<a[pred(j)] then
        begin
          t:=a[pred(j)];a[pred(j)]:=a[j];a[j]:=t;
          dec(j);
        end
        else
          break;
    end;
  end;
end;
procedure swap(var a,b:longint);inline;
var t:longint;
begin
  t:=a;a:=b;b:=t;
end;
function max(a,b:longint):longint;inline;
begin
  if a>b then
    exit(a);
  exit(b);
end;
function min(a,b:longint):longint;inline;
begin
  if a>b then
    exit(b);
  exit(a);
end;
function endl:ansistring;inline;
begin
  writeln;exit('');
end;
var a:array[0..1000000]of longint;
    n,ans,i:longint;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  sort(a,1,n);
  i:=1;
  while i<n do
    if a[i]=a[i+1] then
    begin
      inc(ans,2);
      inc(i,2);
    end
    else
      inc(i);
  writeln(n-ans);
end.

Score : 300 points

Problem Statement

You are playing the following game with Joisino.

Initially, you have a blank sheet of paper.

Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.

Then, you are asked a question: How many numbers are written on the sheet now?

The numbers announced by Joisino are given as A_1, ... ,A_N in the order she announces them. How many numbers will be written on the sheet at the end of the game?

Constraints

1≤N≤100000

1≤A_i≤1000000000(=10^9)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print how many numbers will be written on the sheet at the end of the game.

Sample Input 1

3
6
2
6

Sample Output 1

1

The game proceeds as follows:

6 is not written on the sheet, so write 6.

2 is not written on the sheet, so write 2.

6 is written on the sheet, so erase 6.

Thus, the sheet contains only 2 in the end. The answer is 1.

Sample Input 2

4
2
5
5
2

Sample Output 2

0

It is possible that no number is written on the sheet in the end.

Sample Input 3

6
12
22
16
22
18
12

Sample Output 3

2


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


var
 n,x,s,i:longint;
 a:array[0..1000000000] of 0..1;
begin
 readln(n);
 for i:=1 to n do
  begin
   readln(x);
   if a[x]=0 then begin inc(s); a[x]:=1; end
    else begin dec(s); a[x]:=0; end;
  end;
 writeln(s);
end.


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


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


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


var
n,i,j,c,d,e,b:longint;
x:array[0..100000000] of integer;
begin
readln(n);
for i:=1 to n do begin
read(c);
inc(x[c]);
end;
for i:=0 to 100000000 do
if x[i]=1 then inc(e);
writeln(e);
end.


program  ec12;
var
	n,m,i,j,ans,tot:longint;
	a:array[1..100001] of longint;
procedure qsrt(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	readln(a[i]);
	qsrt(1,n);
	tot:=1;
	ans:=0;
	for i:=2 to n do
	begin
		if a[i]=a[i-1] then
		inc(tot)
		else
		begin
			if tot mod 2=1 then
			inc(ans);
			tot:=1;
		end;
	end;
	if tot mod 2=1 then
	inc(ans);
	writeln(ans);
end.


program  ec12;
var
	n,m,i,j,ans,tot:longint;
	a:array[1..100001] of longint;
procedure qsrt(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	readln(a[i]);
	qsrt(1,n);
	tot:=1;
	ans:=0;
	for i:=2 to n do
	begin
		if a[i]=a[i-1] then
		inc(tot)
		else
		begin
			if tot mod 2=1 then
			inc(ans);
			tot:=1;
		end;
	end;
	writeln(ans);
end.


program V1503;
 var
  a:array[0..100001] of longint;
  i,n,s,p:longint;
 procedure haha(l,r:longint);
  var
   o,p,m,t:longint;
  begin
   o:=l;
   p:=r;
   m:=a[(l+r) div 2];
   repeat
    while a[o]<m do inc(o);
    while a[p]>m do dec(p);
    if o<=p then
     begin
      t:=a[o];
      a[o]:=a[p];
      a[p]:=t;
      inc(o);
      dec(p);
     end;
   until o>p;
   if o<r then haha(o,r);
   if l<p then haha(l,p);
  end;
 begin
  readln(n);
  for i:=1 to n do
   readln(a[i]);
  haha(1,n);
  a[0]:=-1008208820;
  a[n+1]:=1008208820;
  s:=0;
  p:=0;
  for i:=1 to n+1 do
   if a[i]<>a[i-1] then
    begin
     if p mod 2=1 then inc(s);
     p:=1;
    end
                   else inc(p);
  writeln(s);
 end.


var n,i,j,ans:longint;
var a:array[0..100020] of longint;
procedure qsort(l,r:longint);
var i,j,mid,t:longint;
begin
  i:=l;j:=r;mid:=a[(l+r)>>1];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      inc(i);dec(j);
    end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  read(n);
  for i:=1 to n do read(a[i]);qsort(1,n);
  i:=1;
  while i<=n do
  begin
    inc(ans);if a[i]=a[i+1] then dec(ans);
    while (a[i]=a[i+1]) and (i<=n) do inc(i);
    inc(i);
  end;
  writeln(ans);
end.


var n:longint;
    i:longint;
    a:array[0..200050]of longint;
    num,ans:longint;

procedure qs(l,r:longint);
var i,j,m,t:longint;
begin
  i:=l;
  j:=r;
  m:=a[(l+r)>>1];
  repeat
    while a[i]<m do inc(i);
    while a[j]>m do dec(j);
    if i<=j then
    begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  qs(1,n);
  for i:=1 to n do
  if a[i]<>a[i-1] then
  begin
    if num mod 2=1 then inc(ans);
    num:=1;
  end else inc(num);
  if num mod 2=1 then inc(ans);
  writeln(ans);
end.


var
  st:ansistring;
  i:longint;
begin
  readln(st);
  for i:=1 to length(st) do
   if i mod 2=1 then write(st[i]);
  writeln;
end.

Score : 200 points

Problem Statement

You are given a string s consisting of lowercase English letters. Extract all the characters in the odd-indexed positions and print the string obtained by concatenating them. Here, the leftmost character is assigned the index 1.

Constraints

Each character in s is a lowercase English letter.

1≤|s|≤10^5

Input

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

s

Output

Print the string obtained by concatenating all the characters in the odd-numbered positions.

Sample Input 1

atcoder

Sample Output 1

acdr

Extract the first character a, the third character c, the fifth character d and the seventh character r to obtain acdr.

Sample Input 2

aaaa

Sample Output 2

aa

Sample Input 3

z

Sample Output 3

z

Sample Input 4

fukuokayamaguchi

Sample Output 4

fkoaaauh


var s,t:String;i:Longint;
begin
setlength(s,100000);
readln(s);
setlength(t,50000);
for i:=1 to 50000 do
	t[i]:=s[i*2-1];
write(t);
end.


var s,t:String;i:Longint;
begin
setlength(s,100000);
readln(s);
setlength(t,(length(s)+1)div 2);
for i:=1 to length(t)do
	t[i]:=s[i*2-1];
write(t);
end.


var s,t:String;i:Longint;
begin
readln(s);
setlength(t,(length(s)+1)div 2);
for i:=1 to length(t)do
	t[i]:=s[i*2-1];
write(t);
end.


var
f:string;
i:longint;
begin
read(f);
for i:=1 to length(f) do
    begin
        if i mod 2<>0 then write(f[i]);
    end;
writeln;
end.


var
 s:string;
 i:longint;
begin
 read(s);
 for i:=1 to length(s) do
  if i mod 2=1 then write(S[i]);
 writeln;
end.


var
 s:ansistring;
 i:longint;
begin
 readln(s);
 write(s[1]);
 for i:=1 to length(s) div 2 do write(s[i*2+1]);
 writeln;
end.


var
 s:ansistring;
 i:longint;
begin
 readln(s);
 for i:=1 to length(s) do
  if odd(i) then write(s[i]);
end.


var s:ansistring ;
      i:longint ;
begin
   readln(s) ;
	 for i:=1 to length(s) do
	 if i mod 2=1 then write(s[i]) ;
	 writeln ;
end.


var s:ansistring;
    i:longint;
begin
  readln(s);
  for i:=1 to length(s) do
  if i mod 2=1 then write(s[i]);
  writeln;
end.


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

Score : 300 points

Problem Statement

You are given an integer sequence of length N, a_1,a_2,...,a_N.

For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or do nothing.

After these operations, you select an integer X and count the number of i such that a_i=X.

Maximize this count by making optimal choices.

Constraints

1≤N≤10^5

0≤a_i<10^5 (1≤i≤N)

a_i is an integer.

Input

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

N
a_1 a_2 .. a_N

Output

Print the maximum possible number of i such that a_i=X.

Sample Input 1

7
3 1 4 1 5 9 2

Sample Output 1

4

For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.

Sample Input 2

10
0 1 2 3 4 5 6 7 8 9

Sample Output 2

3

Sample Input 3

1
99999

Sample Output 3

1


uses math ;
var n,t,i,ans,k:longint ;
       a:array[-1..100000] of longint ;
begin
   readln(n) ;
	 for i:=1 to n do
	 begin
	   read(k) ;
		 inc(a[k]) ;
		 inc(a[k-1]) ;
		 inc(a[k+1]) ;
		 t:=max(t,k) ;
	 end;
	 for i:=1 to t do
	 if a[i]>ans then ans:=a[i] ;
	 writeln(ans) ;
end.


var n:longint;
    i:longint;
    v:array[0..100050]of boolean;
    x:longint;
    last:longint;
    ans:int64;
begin
  read(n);
  for i:=1 to n do
  begin
    read(x);
    if i=x then v[i]:=true;
  end;

  for i:=1 to n do
  begin
    if (v[i])and(not v[i-1]) then last:=i-1;
    if (v[i])and(not v[i+1]) then
    begin
      ans:=ans+(i-last)div 2;
      if (i-last) mod 2=1 then inc(ans);
    end;
  end;
  writeln(ans);
end.

Score : 400 points

Problem Statement

You are given a permutation p_1,p_2,...,p_N consisting of 1,2,..,N.
You can perform the following operation any number of times (possibly zero):

Operation: Swap two adjacent elements in the permutation.

You want to have p_i ≠ i for all 1≤i≤N.
Find the minimum required number of operations to achieve this.

Constraints

2≤N≤10^5

p_1,p_2,..,p_N is a permutation of 1,2,..,N.

Input

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

N
p_1 p_2 .. p_N

Output

Print the minimum required number of operations

Sample Input 1

5
1 4 3 5 2

Sample Output 1

2

Swap 1 and 4, then swap 1 and 3. p is now 4,3,1,5,2 and satisfies the condition.
This is the minimum possible number, so the answer is 2.

Sample Input 2

2
1 2

Sample Output 2

1

Swapping 1 and 2 satisfies the condition.

Sample Input 3

2
2 1

Sample Output 3

0

The condition is already satisfied initially.

Sample Input 4

9
1 2 4 9 5 8 7 3 6

Sample Output 4

3


var n,i,ans:longint;
var r:boolean;
var a:array[0..100020] of longint;
begin
  r:=false;
  read(n);for i:=1 to n do
  begin
    read(a[i]);
    if r then r:=false
    else if a[i]=i then begin inc(ans);r:=true;end;
  end;
  writeln(ans);
end.


var a:array[-1..100005]of longint;
    n,i,max,min,l:longint;
begin
    readln(n);min:=-1;max:=maxlongint;
    for i:=1 to n do begin
        read(l);
        inc(a[l]);inc(a[l-1]);inc(a[l+1]);
        if (l+1)>max then max:=l+1;
        if (l-1)<min then min:=l-1;
        if min<0 then min:=0;
        if max>100000then max:=100000;
    end;l:=0;
    for i:=min to max do
    if a[i]>l then l:=a[i];
    writeln(l);
end.


var
	s,a,n,i:Longint;
begin
	read(n);
	i:=1;
	while i<=n do begin
		read(a);
		if a<>i then begin
			inc(s);
			read(a);
			inc(i);
		end;
		inc(i);
	end;
	writeln(s)
end.


const big_root_priority_queue=true;
      small_root_priority_queue=false;
type priority_queue=object
                      private
                        main:array of longint;
                        size:longint;
                        mode:boolean;
                      public
                        procedure init(a:boolean);
                        procedure push(a:longint);
                        function pop:longint;
                        procedure top;
                        function empty:boolean;
                      end;
procedure priority_queue.init(a:boolean);
begin
  setlength(main,1);
  size:=0;mode:=a;
end;
procedure priority_queue.push(a:longint);
var x,t:longint;
begin
  inc(size);
  setlength(main,size+1);
  main[size]:=a;
  x:=size;
  while x<>1 do
    if (main[x]<main[x>>1]) xor mode then
    begin
      t:=main[x];
      main[x]:=main[x>>1];
      x:=x>>1;
      main[x]:=t;
    end
    else
      break;
end;
function priority_queue.pop:longint;
begin
  exit(main[1]);
end;
procedure priority_queue.top;
var t,x:longint;
begin
  t:=main[1];
  main[1]:=main[size];
  main[size]:=t;
  dec(size);
  setlength(main,size+1);
  x:=1;
  while x<<1<size do
    if ((main[x]<main[x<<1])xor mode)and((main[x]<main[x<<1+1])xor mode) then
      break
    else
    if (main[x<<1]<main[x<<1+1]) xor mode then
    begin
      t:=main[x];
      main[x]:=main[x<<1];
      x:=x<<1;
      main[x]:=t;
    end
    else
    begin
      t:=main[x];
      main[x]:=main[x<<1+1];
      x:=x<<1+1;
      main[x]:=t;
    end;
  if (x<<1=size) and ((main[x]>main[x<<1]) xor mode) then
  begin
    t:=main[x];main[x]:=main[x<<1];main[x<<1]:=t;
  end;
end;
function priority_queue.empty:boolean;
begin
  exit(size<>0);
end;
procedure sort(var a:array of longint;l,r:longint);
var i:longint;
    x:priority_queue;
begin
  x.init(small_root_priority_queue);
  for i:=l to r do
    x.push(a[i]);
  for i:=l to r do
  begin
    a[i]:=x.pop;
    x.top;
  end;
end;
var n,x,i:longint;
    cnt:array[0..2]of longint;
    a:array[0..100000]of longint;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  sort(a,1,n);
  i:=n;
  while i>=1 do
    if a[i]=a[i-1] then
    begin
      inc(x);
      cnt[x]:=a[i];
      dec(i,2);
      if x=2 then
        break;
    end
    else
      dec(i,1);
  writeln(qword(cnt[1]*cnt[2]));
end.

Score : 300 points

Problem Statement

We have N sticks with negligible thickness.
The length of the i-th stick is A_i.

Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides.
Find the maximum possible area of the rectangle.

Constraints

4 \leq N \leq 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible area of the rectangle.
If no rectangle can be formed, print 0.

Sample Input 1

6
3 1 2 4 2 1

Sample Output 1

2

1 \times 2 rectangle can be formed.

Sample Input 2

4
1 2 3 4

Sample Output 2

0

No rectangle can be formed.

Sample Input 3

10
3 3 3 3 4 4 4 5 5 5

Sample Output 3

20


    var
            a:array[0..10000000]of byte;
            i,n,c,max,m:longint;
            flag:boolean;
    begin
            fillchar(a,sizeof(a),0);max:=0;
            read(n);
            for i:=1 to n do
            begin
                    read(c);
                    if c>max then max:=c;
                    if (a[c]<=4)then inc(a[c]);
            end;
            flag:=true;m:=0;
            for i:=max downto 1 do
            begin
                    if (a[i]>=4)and(m=0)then begin writeln(i*i);halt;end;
                    if (a[i]>=2) then if m=0 then m:=i
                                             else begin writeln(m*i);halt;end;
            end;
            writeln(0);
    end.


var a:array[0..100020] of int64;
var n,ans,x,y,num:int64;
var i:longint;
procedure qsort(l,r:int64);
var i,j,mid,t:int64;
begin
  i:=l;j:=r;mid:=a[(l+r)>>1];
  repeat
    while a[i]>mid do inc(i);
    while a[j]<mid do dec(j);
    if i<=j then
    begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      inc(i);dec(j);
    end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  read(n);
  for i:=1 to n do read(a[i]);qsort(1,n);
  i:=0;
  while i<n do
  begin
    num:=1;inc(i);while a[i]=a[i+1] do begin inc(i);inc(num);end;
    if num>1 then begin if x=0 then x:=a[i] else y:=a[i];end;
    if y>0 then break;
    if (num>=4) then begin y:=a[i];break;end;
  end;
  writeln(x*y);
end.


var a:array[0..100020] of int64;
var n,ans,x,y,num:int64;
var i:longint;
procedure qsort(l,r:int64);
var i,j,mid,t:int64;
begin
  i:=l;j:=r;mid:=a[(l+r)>>1];
  repeat
    while a[i]>mid do inc(i);
    while a[j]<mid do dec(j);
    if i<=j then
    begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      inc(i);dec(j);
    end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  read(n);
  for i:=1 to n do read(a[i]);qsort(1,n);
  i:=0;
  while i<n do
  begin
    num:=0;inc(i);while a[i]=a[i+1] do begin inc(i);inc(num);end;
    if num>0 then if x=0 then x:=a[i] else y:=a[i];
    if y>0 then break;
  end;
  writeln(x*y);
end.


var a:array[0..10020] of longint;
var n,i,j,ans,x,y,num:longint;
procedure qsort(l,r:longint);
var i,j,mid,t:longint;
begin
  i:=l;j:=r;mid:=a[(l+r)>>1];
  repeat
    while a[i]>mid do inc(i);
    while a[j]<mid do dec(j);
    if i<=j then
    begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      inc(i);dec(j);
    end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  read(n);
  for i:=1 to n do read(a[i]);qsort(1,n);
  i:=0;
  while i<n do
  begin
    num:=0;inc(i);while a[i]=a[i+1] do begin inc(i);inc(num);end;
    if num>0 then if x=0 then x:=a[i] else y:=a[i];
    if y>0 then break;
  end;
  writeln(x*y);
end.


program ec12;
var
	a,b,site:array[1..100001] of int64;
	j:int64;
	n,m,i,max,tot:longint;
procedure qsrt(l,r:longint);
var
	s,v:int64;
	i,j:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	m:=0;
	a[n+1]:=0;
	for i:=n downto 1 do
	begin
		if a[i]=a[i+1] then
		begin
			if m=0 then
			begin
				m:=1;
				j:=a[i];
				a[i]:=-1;
			end
			else
			begin
				writeln(j*a[i]);
				halt;
			end;
		end;
	end;
	writeln(0);
end.


program ec12;
var
	a,b,site:array[1..100001] of int64;
	j:int64;
	n,m,i,max,tot:longint;
procedure qsrt(l,r:longint);
var
	s,v:int64;
	i,j:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	tot:=1;
	site[1]:=1;
	b[1]:=a[n];
	for i:=n-1 downto 1 do
	begin
		if a[i]=b[tot] then
		inc(site[tot])
		else
		begin
			inc(tot);
			b[tot]:=a[i];
			site[tot]:=1;
		end;
	end;
	j:=-1000;
	for i:=1 to tot do
	begin
		if site[i]>=2 then
		begin
			if j=-1000 then
			begin
				j:=b[i];
				continue;
			end
			else
			begin
				writeln(j*b[i]);
				halt;
			end;
		end;
	end;
	writeln(0);
end.


program ec12;
var
	a,b,site:array[1..100001] of longint;
	n,m,i,j,max,tot:longint;
procedure qsrt(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	tot:=1;
	site[1]:=1;
	b[1]:=a[n];
	for i:=n-1 downto 1 do
	begin
		if a[i]=a[i+1] then
		inc(site[tot])
		else
		begin
			inc(tot);
			b[tot]:=a[i];
			site[tot]:=1;
		end;
	end;
	j:=0;
	for i:=1 to tot do
	begin
		if site[i]>=2 then
		begin
			if j=0 then
			begin
				j:=b[i];
				continue;
			end
			else
			begin
				writeln(j*b[i]);
				halt;
			end;
		end;
	end;
	writeln(0);
end.


program ec12;
var
	a:array[1..100001] of longint;
	n,m,i,j,max,sum,sum1:longint;
procedure qsrt(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	sum:=1;
	j:=a[n];
	sum1:=0;
	max:=0;
	for i:=n-1 downto 1 do
	begin
		if a[i]=a[i+1] then
		begin
			if (sum1=0) and (sum=1) then
			sum:=2;
			if (sum=2) and (sum1=1) then
			begin
				max:=j*a[i];
				break;
			end
			else
			begin
				if sum1=0 then
				inc(sum1);
			end;
		end
		else
		begin
			if sum=1 then
			j:=a[i];
		end;
	end;
	writeln(max);
end.


var
	i,j,h,w,n,a,c,t:Longint;
	u:array[1..100]of Longint;
begin
	t:=1;
	read(h,w,n,a);
	if h*w=n then begin
		for i:=1 to h do begin
			if i mod 2=1 then begin
				for j:=1 to w do begin
					writeln(t);
					inc(t);
				end;
			end else begin
				for j:=w downto 1 do begin
					u[j]:=t;
					inc(t);
				end;
				for j:=1 to w do writeln(u[j]);
			end;
		end;
		exit;
	end;
	for i:=1 to h do begin
		if i mod 2=1 then begin
			for j:=1 to w do begin
				writeln(t);
				inc(c);
				if a=c then begin
					read(a);
					c:=0;
					inc(t);
				end;
			end;
		end else begin
			for j:=w downto 1 do begin
				u[j]:=t;
				inc(c);
				if a=c then begin
					read(a);
					c:=0;
					inc(t);
				end;
			end;
			for j:=1 to w do writeln(u[j]);
		end;
	end;
end.

Score : 400 points

Problem Statement

We have a grid with H rows and W columns of squares.
Snuke is painting these squares in colors 1, 2, ..., N.
Here, the following conditions should be satisfied:

For each i (1 ≤ i ≤ N), there are exactly a_i squares painted in Color i. Here, a_1 + a_2 + ... + a_N = H W.

For each i (1 ≤ i ≤ N), the squares painted in Color i are 4-connected. That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i.

Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.

Constraints

1 ≤ H, W ≤ 100

1 ≤ N ≤ H W

a_i ≥ 1

a_1 + a_2 + ... + a_N = H W

Input

Input is given from Standard Input in the following format:

H W
N
a_1 a_2 ... a_N

Output

Print one way to paint the squares that satisfies the conditions.
Output in the following format:

c_{1 1} ... c_{1 W}
:
c_{H 1} ... c_{H W}

Here, c_{i j} is the color of the square at the i-th row from the top and j-th column from the left.

Sample Input 1

2 2
3
2 1 1

Sample Output 1

1 1
2 3

Below is an example of an invalid solution:

1 2
3 1

This is because the squares painted in Color 1 are not 4-connected.

Sample Input 2

3 5
5
1 2 3 4 5

Sample Output 2

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

Sample Input 3

1 1
1
1

Sample Output 3

1


var
	i,j,h,w,n,a,c,t:Longint;
	u:array[1..100]of Longint;
begin
	t:=1;
	read(h,w,n,a);
	for i:=1 to h do begin
		if i mod 2=1 then begin
			for j:=1 to w do begin
				u[j]:=t;
				inc(c);
				if a=c then begin
					read(a);
					c:=0;
					inc(t);
				end;
			end;
		end else begin
			for j:=w downto 1 do begin
				u[j]:=t;
				inc(c);
				if a=c then begin
					read(a);
					c:=0;
					inc(t);
				end;
			end;
		end;
		for j:=1 to w do writeln(u[j]);
	end;
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,min:Integer;
  l:array[0..6] of Integer = (1,2,4,8,16,32,64);
  s,s123,s1,s2,s3:string;
  tf:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(n);

    for I := 0 to 6 do begin
      if n>=l[6-i] then begin
        Writeln(l[6-i]);
        break;
      end;
    end;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

Takahashi loves numbers divisible by 2.

You are given a positive integer N. Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique.

Here, the number of times an integer can be divisible by 2, is how many times the integer can be divided by 2 without remainder.

For example,

6 can be divided by 2 once: 6 -> 3.

8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1.

3 can be divided by 2 zero times.

Constraints

1 ≤ N ≤ 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

7

Sample Output 1

4

4 can be divided by 2 twice, which is the most number of times among 1, 2, ..., 7.

Sample Input 2

32

Sample Output 2

32

Sample Input 3

1

Sample Output 3

1

Sample Input 4

100

Sample Output 4

64


var a,b:array[0..2000000]of boolean;
    n,m,i,s,e:longint;
begin
  readln(n,m);
  for i:=1 to m do
  begin
    read(s,e);
    if s=1 then a[e]:=true;
    if e=n then b[s]:=true;
  end;
  for i:=1 to n do
    if a[i] and b[i] then
    begin
      writeln('POSSIBLE');
      halt;
    end;
  writeln('IMPOSSIBLE');
end.

Score : 300 points

Problem Statement

In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands.
For convenience, we will call them Island 1, Island 2, ..., Island N.

There are M kinds of regular boat services between these islands.
Each service connects two islands. The i-th service connects Island a_i and Island b_i.

Cat Snuke is on Island 1 now, and wants to go to Island N.
However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.

Help him.

Constraints

3 ≤ N ≤ 200 000

1 ≤ M ≤ 200 000

1 ≤ a_i < b_i ≤ N

(a_i, b_i) \neq (1, N)

If i \neq j, (a_i, b_i) \neq (a_j, b_j).

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

If it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

POSSIBLE

Sample Input 2

4 3
1 2
2 3
3 4

Sample Output 2

IMPOSSIBLE

You have to use three boat services to get to Island 4.

Sample Input 3

100000 1
1 99999

Sample Output 3

IMPOSSIBLE

Sample Input 4

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

Sample Output 4

POSSIBLE

You can get to Island 5 by using two boat services: Island 1 -> Island 4 -> Island 5.


var a,b:array[0..2000000]of boolean;
    n,m,i,s,e:longint;
begin
  readln(n,m);
  for i:=1 to m do
  begin
    read(s,e);
    if s=1 then a[e]:=true;
    if e=n then b[s]:=true;
  end;
  for i:=1 to n do
    if a[i] and b[i] then
      writeln('POSSIBLE');
  writeln('IMPOSSIBLE');
end.


var a,b:array[0..2000000]of boolean;
    n,m,i,s,e:longint;
begin
  readln(n,m);
  for i:=1 to m do
  begin
    read(s,e);
    if s=1 then a[e]:=true;
    if e=n then b[s]:=true;
  end;
  for i:=1 to m do
    if a[i] and b[i] then
      writeln('POSSIBLE');
  writeln('IMPOSSIBLE');
end.


program V1004;
 var
  a:array[0..200001,0..1] of longint;
  i,m,n,x,y,u:longint;
 begin
  readln(n,m);
  fillchar(a,sizeof(a),0);
  a[1,0]:=1;
  a[n,1]:=1;
  for i:=1 to m do
   begin
    readln(x,y);
    if x=1 then inc(a[y,0]);
    if y=n then inc(a[x,1]);
   end;
  u:=0;
  for i:=1 to n do
   if (a[i,0]>0) and (a[i,1]>0) then u:=1;
  if u=1 then writeln('POSSIBLE')
         else writeln('IMPOSSIBLE');
 end.


const oo=10000000;
var n,m,nheap:longint;x,y,h,a,pos,heap,cp,c,f:array[0..500005] of longint;free:array[0..500005] of boolean;
procedure       nhap;
var i:longint;
begin
        readln(n,m);
        for i:=1 to m do
                begin
                readln(x[i],y[i]);
                inc(h[x[i]]);
                inc(h[y[i]]);
                end;
        for i:=2 to n+1 do
                h[i]:=h[i-1]+h[i];
        for i:=1 to m do
                begin
                A[h[x[i]]]:=y[i];
                a[h[y[i]]]:=x[i];
                C[h[x[i]]]:=1;
                C[h[y[i]]]:=1;
                dec(h[x[i]]);
                dec(h[y[i]]);
                end;
end;
procedure   init;
var i:longint;
begin
    for i:=1 to n do
        cp[i]:=oo;
    fillchar(free,sizeof(free),true);
    cp[1]:=0;
end;

procedure   swap(var x,y:longint);
var tamp:longint;
begin
    tamp:=x;
    x:=y;
    y:=tamp;
end;
procedure   upheap(i:longint);
begin
    if (i=1)or (cp[heap[i]]>cp[heap[i div 2]]) then exit;
    swap(heap[i],heap[i div 2]);
    swap(pos[heap[i]],pos[heap[i div 2]]);
    upheap(i div 2);
end;
procedure   downheap(I:longint);
var j:longint;
begin
    j:=i*2;
    if j>nheap then exit;
    if (j<nheap) and (cp[heap[j]]>cp[heap[j+1]]) then inc(j);
    if cp[heap[i]]>cp[heap[j]] then
        begin
        swap(heap[i],heap[j]);
        swap(pos[heap[i]],pos[heap[j]]);
        downheap(j);
        end;
end;
function pop(v:longint):longint;
begin
    pop:=heap[v];
    heap[v]:=heap[nheap];
    pos[heap[v]]:=v;
    dec(nheap);
    upheap(v);
    downheap(v);
end;
procedure   push(x:longint);
begin
    inc(nheap);
    pos[x]:=nheap;
    heap[nheap]:=x;
    upheap(nheap);
end;
procedure   dijheap;
var v,vjp,u:longint;
begin
    f[1]:=1;
    push(1);
    repeat
        u:=pop(1);
        if u=n then exit;
        free[u]:=false;
        for v:=h[u]+1 to h[u+1] do
            if (free[a[v]]) then
                begin
                if cp[a[v]]>=cp[u]+c[v] then
                    begin
                    if cp[a[v]]>cp[u]+c[v] then F[a[v]]:=f[u]
                    else f[a[v]]:=f[a[v]]+f[u];
                    cp[a[v]]:=cp[u]+c[v];
                    if pos[a[v]]=0 then push(a[v]) else
                        begin
                        vjp:=pos[a[v]];
                        upheap(vjp);
                        downheap(vjp);
                        end;
                    end

                end;
        until false;
end;
procedure       xuat;
begin
        if Cp[n]>2 then writeln('IMPOSSIBLE')
        else writeln('POSSIBLE');
end;
begin
        nhap;
        init;
        dijheap;
        xuat;
end.


function gcd(a,b:int64):int64;
var t:int64;
begin
  while b <> 0 do
    begin
       t := b;
       b := a mod b;
       a := t;
    end;
    result := a;
end;

var
  n,k,t,i,x,now,mx:int64;
begin
  read(n);
  read(k);
  read(now);
  mx := now;
  for i := 2 to n do
  begin
    read(x);
    now := gcd(now,x);
    if (mx<x) then mx := x;
  end;
  if (mx>=k) and (k mod now=0) then writeln('POSSIBLE')
  else writeln('IMPOSSIBLE');
end.

Score : 300 points

Problem Statement

There is a box containing N balls. The i-th ball has the integer A_i written on it.
Snuke can perform the following operation any number of times:

Take out two balls from the box. Then, return them to the box along with a new ball, on which the absolute difference of the integers written on the two balls is written.

Determine whether it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq K \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

If it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written, print POSSIBLE; if it is not possible, print IMPOSSIBLE.

Sample Input 1

3 7
9 3 4

Sample Output 1

POSSIBLE

First, take out the two balls 9 and 4, and return them back along with a new ball, abs(9-4)=5.
Next, take out 3 and 5, and return them back along with abs(3-5)=2.
Finally, take out 9 and 2, and return them back along with abs(9-2)=7.
Now we have 7 in the box, and the answer is therefore POSSIBLE.

Sample Input 2

3 5
6 9 3

Sample Output 2

IMPOSSIBLE

No matter what we do, it is not possible to have 5 in the box. The answer is therefore IMPOSSIBLE.

Sample Input 3

4 11
11 3 7 15

Sample Output 3

POSSIBLE

The box already contains 11 before we do anything. The answer is therefore POSSIBLE.

Sample Input 4

5 12
10 2 8 6 4

Sample Output 4

IMPOSSIBLE


const
        fi='agc018_b.inp';
        fo='agc018_b.out';
var
        n,m,res:longint;
        A:Array[0..301,0..301] of longint;
        HT:Array[0..301] of longint;
        KT:Array[0..301] of boolean;
        Dem:Array[0..301] of longint;
{------------------------------------}
procedure main;
var i,j,z:longint;
        begin
                read(n,m);
                res:=n;
                for i:=1 to n do
                        for j:=1 to m do read(A[i,j]);
                for j:=1 to n do HT[j]:=1;
                for i:=1 to m do
                begin
                        fillchar(Dem,sizeof(Dem),0);
                        z:=0;
                        for j:=1 to n do
                        begin
                                inc(Dem[A[j,HT[j]]]);
                                if Dem[z]<Dem[A[j,HT[j]]] then z:=A[j,HT[j]];
                        end;
                        if res>Dem[z] then res:=Dem[z];
                        KT[z]:=true;
                        for j:=1 to n do while KT[A[j,HT[j]]] do inc(HT[j]);
                end;
                write(res);
        end;
{------------------------------------}
begin
//        assign(input,fi); reset(input);
//        assign(output,fo);rewrite(output);
        main;
end.

Score : 700 points

Problem Statement

Takahashi is hosting an sports meet.
There are N people who will participate. These people are conveniently numbered 1 through N.
Also, there are M options of sports for this event. These sports are numbered 1 through M.
Among these options, Takahashi will select one or more sports (possibly all) to be played in the event.

Takahashi knows that Person i's j-th favorite sport is Sport A_{ij}.
Each person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.

Takahashi is worried that one of the sports will attract too many people.
Therefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized.
Find the minimum possible number of the participants in the sport with the largest number of participants.

Constraints

1 \leq N \leq 300

1 \leq M \leq 300

A_{i1} , A_{i2} , ... , A_{iM} is a permutation of the integers from 1 to M.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
:
A_{N1} A_{N2} ... A_{NM}

Output

Print the minimum possible number of the participants in the sport with the largest number of participants.

Sample Input 1

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

Sample Output 1

2

Assume that Sports 1, 3 and 4 are selected to be played. In this case, Person 1 will participate in Sport 1, Person 2 in Sport 3, Person 3 in Sport 3 and Person 4 in Sport 4.
Here, the sport with the largest number of participants is Sport 3, with two participants.
There is no way to reduce the number of participants in the sport with the largest number of participants to 1. Therefore, the answer is 2.

Sample Input 2

3 3
2 1 3
2 1 3
2 1 3

Sample Output 2

3

Since all the people have the same taste in sports, there will be a sport with three participants, no matter what sports are selected.
Therefore, the answer is 3.


program ec12;
var
	n,m,i,j,max,min:longint;
	a:array[1..301,1..301] of longint;
	vis,step:array[1..301] of longint;
	vis1:array[1..301] of boolean;
begin
	readln(n,m);
	for i:=1 to n do
	for j:=1 to m do
	read(a[i,j]);
	min:=n;
	for i:=1 to n do
	step[i]:=1;
	fillchar(vis1,sizeof(vis1),true);
	while true do
	begin
		fillchar(vis,sizeof(vis),0);
		for i:=1 to n do
		inc(vis[a[i,step[i]]]);
		max:=1;
		for i:=2 to m do
		begin
			if vis[max]<vis[i] then
			max:=i;
		end;
		if vis[max]<min then
		min:=max;
		vis1[max]:=false;
		for i:=1 to n do
		begin
			while (not vis1[a[i,step[i]]]) and (step[i]<=m) do
			inc(step[i]);
			if step[i]>m then
			begin
				writeln(min);
				halt;
			end;
		end;
	end;
	writeln(min);
end.


var
  i,n,j,k,s,t:longint;
  a:array[0..10000] of longint;
begin
  readln(n,k);
  for i:=1 to n do read(a[i]);
  for i:=1 to n-1 do
    for j:=i+1 to n do
    if a[i]<a[j] then begin t:=a[i];a[i]:=a[j];a[j]:=t;end;
  for i:=1 to k do inc(s,a[i]);
end.

Score : 200 points

Problem Statement

Snuke has N sticks.
The length of the i-th stick is l_i.

Snuke is making a snake toy by joining K of the sticks together.

The length of the toy is represented by the sum of the individual sticks that compose it.
Find the maximum possible length of the toy.

Constraints

1 \leq K \leq N \leq 50

1 \leq l_i \leq 50

l_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
l_1 l_2 l_3 ... l_{N}

Output

Print the answer.

Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

12

You can make a toy of length 12 by joining the sticks of lengths 3, 4 and 5, which is the maximum possible length.

Sample Input 2

15 14
50 26 27 21 41 7 42 35 7 5 5 36 39 1 45

Sample Output 2

386


var
  a:array[1..200001]of longint;
  i,n,ans:longint;
procedure sort(l,r:longint);
var
  i,j,x,y:longint;
begin
  i:=l;
  j:=r;
  x:=a[(l+r) div 2];
  repeat
    while a[i]<x do
      inc(i);
    while x<a[j] do
      dec(j);
    if not(i>j) then
    begin
      y:=a[i];
      a[i]:=a[j];
      a[j]:=y;
      inc(i);
      j:=j-1;
    end;
  until i>j;
  if l<j then
    sort(l,j);
  if i<r then
    sort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  sort(1,n);
  //for i:=1 to n do
    //write(a[i],' ');
  ans:=a[2]-a[1];
  for i:=2 to n-1 do
    if a[i+1]-a[i]<ans then
      ans:=a[i+1]-a[i];
  writeln(ans);
end.

Score : 300 points

Problem Statement

Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer a_i written on it.

They will share these cards.
First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards.
Here, both Snuke and Raccoon have to take at least one card.

Let the sum of the integers on Snuke's cards and Raccoon's cards be x and y, respectively.
They would like to minimize |x-y|.
Find the minimum possible value of |x-y|.

Constraints

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

-10^{9} \leq a_i \leq 10^{9}

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print the answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x-y|=1. This is the minimum possible value.

Sample Input 2

2
10 -10

Sample Output 2

20

Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10, y=-10, and thus |x-y|=20.


program ec12;
var
	j:int64;
	s:array[0..100001] of int64;
	n,m,i,x:longint;
begin
	readln(n);
	s[0]:=0;
	for i:=1 to n do
	begin
		read(x);
		s[i]:=s[i-1]+x;
	end;
	j:=maxlongint-1;
	for i:=1 to n do
	begin
		if abs(s[n]-s[i]-s[i])<j then
		j:=abs(s[n]-s[i]-s[i]);
	end;
	writeln(j);
end.


program ec12;
var
	a:array[1..200001] of longint;
	s,s1:ansistring;
	n,m,i,j,dis:longint;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	s:='';
	dis:=1;
	for i:=1 to n*2 do
	begin
		if i mod 2=1 then
		begin
			if dis=1 then
			begin
				str(a[i div 2+1],s1);
				s:=s+s1;
			end
			else
			begin
				str(a[i div 2+1],s1);
				s:=s1+s;
			end;
		end
		else
		dis:=3-dis;
	end;
	if dis=1 then
	begin
		for i:=1 to length(s) do
		write(s[i],' ');
	end
	else
	begin
		for i:=length(s) downto 1 do
		write(s[i],' ');
	end;
end.

Score : 300 points

Problem Statement

You are given an integer sequence of length n, a_1, ..., a_n.
Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

Append a_i to the end of b.

Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

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

0 \leq a_i \leq 10^9

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print n integers in a line with spaces in between.
The i-th integer should be b_i.

Sample Input 1

4
1 2 3 4

Sample Output 1

4 2 1 3

After step 1 of the first operation, b becomes: 1.

After step 2 of the first operation, b becomes: 1.

After step 1 of the second operation, b becomes: 1, 2.

After step 2 of the second operation, b becomes: 2, 1.

After step 1 of the third operation, b becomes: 2, 1, 3.

After step 2 of the third operation, b becomes: 3, 1, 2.

After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.

After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.

Thus, the answer is 4 2 1 3.

Sample Input 2

3
1 2 3

Sample Output 2

3 1 2

As shown above in Sample Output 1, b becomes 3, 1, 2 after step 2 of the third operation. Thus, the answer is 3 1 2.

Sample Input 3

1
1000000000

Sample Output 3

1000000000

Sample Input 4

6
0 6 7 6 7 0

Sample Output 4

0 6 6 0 7 7


program ec12;
var
	a:array[1..100001] of longint;
	s,s1:ansistring;
	n,m,i,j,dis:longint;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	s:='';
	dis:=1;
	for i:=1 to n*2 do
	begin
		if i mod 2=1 then
		begin
			if dis=1 then
			begin
				str(a[i div 2+1],s1);
				s:=s+s1;
			end
			else
			begin
				str(a[i div 2+1],s1);
				s:=s1+s;
			end;
		end
		else
		dis:=3-dis;
	end;
	if dis=1 then
	begin
		for i:=1 to length(s) do
		write(s[i],' ');
	end
	else
	begin
		for i:=length(s) downto 1 do
		write(s[i],' ');
	end;
end.


program ec12;
var
	a:array[1..200001] of longint;
	queue:array[-100001..100001] of longint;
	n,m,i,j,dis,head,tail:longint;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	head:=1;
	tail:=1;
	dis:=1;
	queue[head]:=a[1];
	for i:=3 to n*2 do
	begin
		if i mod 2=1 then
		begin
			if dis=1 then
			begin
				inc(tail);
				queue[tail]:=a[i div 2+1];
			end
			else
			begin
				dec(head);
				queue[head]:=a[i div 2+1];
			end;
		end
		else
		dis:=3-dis;
	end;
	if dis=1 then
	begin
		for i:=head to tail do
		write(queue[i],' ');
	end
	else
	begin
		for i:=tail downto head do
		write(queue[i],' ');
	end;
end.


program Project1;

{$APPTYPE CONSOLE}

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

    if a-b>=0 then
      Writeln('delicious')
    else if x+a>=b then
      Writeln('safe')
    else
      Writeln('dangerous');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier.
He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though.

Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious.

Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it.

Write a program that outputs delicious if he found it delicious, safe if he did not found it delicious but did not get a stomachache either, and dangerous if he got a stomachache.

Constraints

1 ≤ X,A,B ≤ 10^9

Input

Input is given from Standard Input in the following format:

X A B

Output

Print delicious if Takahashi found the food delicious; print safe if he neither found it delicious nor got a stomachache; print dangerous if he got a stomachache.

Sample Input 1

4 3 6

Sample Output 1

safe

He ate the food three days after the "best-by" date. It was not delicious or harmful for him.

Sample Input 2

6 5 1

Sample Output 2

delicious

He ate the food by the "best-by" date. It was delicious for him.

Sample Input 3

3 7 12

Sample Output 3

dangerous

He ate the food five days after the "best-by" date. It was harmful for him.


const fi='trained.inp';
      fo='trained.out';
var a,dau:array[0..100005] of longint;
    n,d:longint;
    kt:boolean;
procedure mo;
begin
    assign(input,fi);
    reset(input);
    assign(output,fo);
    rewrite(output);
end;

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

procedure xuly;
var i:longint;
begin
    d:=0;   i:=1;
    repeat
        dau[i]:=1;
        i:=a[i];
        inc(d);
    until (dau[2]=1)or(dau[i]=1)or(kt);
end;

procedure xuat;
begin
    if dau[2]=1 then
        writeln(d-1)
    else
        writeln('-1');
end;

begin

    nhap;
    xuly;
    xuat;
end.


Var     n,kq,t: Longint;
        a: Array[0..1000005] Of Longint;

Procedure       Nhap;
Var     i: Longint;
Begin
Readln(n);
For i:=1 to n do
        Readln(a[i]);
End;

Procedure       XuLy;
Begin
t:=1; kq:=0;
Repeat
        Inc(kq);
        If a[a[t]]=t Then Break;
        t:=a[t];
Until (t=2) or (kq>n+1);
End;

Procedure       Xuat;
Begin
If t<>2 Then Writeln(-1)
Else    Writeln(kq);
End;

Begin
Nhap;
XuLy;
Xuat;
End.

Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3


Var     n,kq,i: Longint;
        a: Array[0..1000005] Of Longint;

Procedure       Nhap;
Var     i: Longint;
Begin
Readln(n);
For i:=1 to n do
        Readln(a[i]);
End;

Procedure       XuLy;
Begin
i:=1; kq:=0;
Repeat
        Inc(kq);
        If a[a[i]]=i Then Break;
        i:=a[i];
Until (i=2) or (kq>n+1);
End;

Procedure       Xuat;
Begin
If i<>2 Then Writeln(-1)
Else    Writeln(kq);
End;

Begin
Nhap;
XuLy;
Xuat;
End.


Var N,i,Dem:Longint;
    A:Array[1..100000] Of Longint;
    Free:Array[1..100000] Of Boolean;
    KT:Boolean;
Begin
        //Assign(Input,'TRAINED.INP'); Reset(Input);
       //// Assign(Output,'TRAINED.OUT'); Rewrite(Output);
        Readln(N);
        For i:=1 to N Do Readln(A[i]);
        Fillchar(Free,SizeOf(Free),True);
        i:=1;
        Dem:=1;
        KT:=False;
        While Free[i]<>False Do
                Begin
                Inc(Dem);
                Free[i]:=False;
                i:=A[i];
                If i=2 Then
                        Begin
                        KT:=True;
                        Break;
                        End;
                Free[i]:=False;
                i:=A[i];
                If i=2 Then
                        Begin
                        KT:=True;
                        Break;
                        End;
                End;
        If KT=True Then Write(Dem) Else Write('-1');
        //Close(Input);
        ///Close(Output);
End.


const fi='abc065_b.inp';
        fo='abc065_b.out';
var n,i:longint;
        a:array[1..100000] of longint;
        d:array[1..100000] of boolean;
function conan:longint;
        var i,kq:longint;
        begin
                i:=1;
                kq:=0;
                while i<n+1 do
                        begin
                                if d[i] then exit(-1);
                                if i=2 then exit(kq);
                                d[i]:=true;
                                i:=a[i];
                                inc(kq);
                        end;
        end;
begin

        readln(n);
        for i:=1 to n do readln(a[i]);
        writeln(conan);
end.


const MaxN=round(1e5)+10;
var A:array[0..MaxN] of longint;
    n,res:longint;

procedure nhap;
var i:longint;
begin

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

procedure xuli;
var x:longint;
begin
    x:=1;res:=0;
    repeat
        x:=a[x];
        inc(res);
        if x=2 then
        begin
            writeln(res);
            halt;
        end;
    until res>n;
end;
procedure xuat;
begin
    writeln(-1);

end;

begin
    nhap;
    xuli;
    xuat;
end.


var
  a,b,s:int64;
  i:longint;
begin
  readln(a,b);
  if abs(a-b)>1 then begin writeln(0);exit;end;
  s:=1;
  if a=b then s:=s*2;
  for i:=1 to a do s:=s*i mod 1000000007;
  for i:=1 to b do s:=s*i mod 1000000007;
  writeln(s);
end.

Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477


program reconciled;
uses math;
const
  md: int64 = 1000000007;
  xx: int64 = 765302181;
var
  n, m, x, i: int64;
begin
  read(n, m);
  if abs(n-m) > 1 then
  begin
    writeln(0);
    halt(0);
  end;
  if min(n, m) < 50000 then
  begin
    x := ifthen(n = m, 2, 1);
    for i := 1 to min(n, m) do
      x := ((i*i mod md)*x mod md);
    for i := min(n, m)+1 to max(n, m) do
      x := (i*x mod md);
    writeln(x);
    halt(0);
  end;
  x := xx*ifthen(n = m, 2, 1) mod md;
  for i := 50001 to min(n, m) do
    x := ((i*i mod md)*x mod md);
  for i := min(n, m)+1 to max(n, m) do
    x := (i*x mod md);
  writeln(x);
end.


Var A,B,N:Longint;
    D:Array[1..100000] Of Qword;

Procedure Xuli;
Var i:Longint;
Begin
        D[1]:=1;
        For i:=2 to 100000 Do D[i]:=((D[i-1] Mod 1000000007)*(i Mod 1000000007))Mod 1000000007;
End;

Begin
        //Assign(Input,'TRAINED.INP'); Reset(Input);
        ///Assign(Output,'TRAINED.OUT'); Rewrite(Output);
        Readln(A,B);
        Xuli;
        If ABS(A-B)>1 Then
                Begin
                Write(0);
                Halt;
                End;
        N:=(D[B]*D[A])Mod 1000000007;
        If A=B Then
                Begin
                N:=(D[B]*D[A]*2)Mod 1000000007;
                Write(N);
                Halt;
                End;
        Write(N);
        //Close(Input);
        //Close(Output);
End.


const fi='arc076_a.inp';
        fo='arc076_a.out';
        du=1000000007;
var a,b:longint;
        d:array[0..100000] of qword;
procedure mori;
        var i:longint;
        begin
                d[0]:=1;
                for i:=1 to 100000 do d[i]:=d[i-1]*i mod du;
        end;
function conan:qword;
        var kq:qword;
        begin
                if abs(a-b) > 1 then exit(0);
                kq:=d[b]*d[a] mod du;
                if a=b then kq:=kq*2 mod du;
                exit(kq);
        end;
begin

        readln(a,b);
        mori;
        writeln(conan);

end.


program  ec12;
var
	ans:int64;
	n,m,i:longint;
begin
	readln(n,m);
	if abs(n-m)>1 then
	begin
		writeln(0);
		halt;
	end;
	ans:=1;
	for i:=2 to n do
	ans:=(ans*i) mod 1000000007;
	for i:=2 to m do
	ans:=(ans*i) mod 1000000007;
	if n=m then
	writeln((ans*2) mod 1000000007)
	else
	writeln(ans);
end.


uses math;
var n,m,ans,p,ppp,i:int64;

begin
readln(n,m);
ppp:=1000000007;
if n=m then ans:=2;
if n=m+1 then ans:=1;
if n+1=m then ans:=1;
if ans=0 then begin writeln(0);halt;end;
p:=1;
while i<min(n,m) do begin inc(i);p:=(p*i)mod ppp;end;
p:=(p*p)mod ppp;
if n=m then writeln((p*2)mod ppp);
if n=m+1 then writeln((p*n)mod ppp);
if n+1=m then writeln((p*m)mod ppp);
end.


uses math;
var n,m,ans,p,ppp,i:int64;

begin
readln(n,m);
ppp:=1000000007;
if n=m then ans:=2;
if n=m+1 then ans:=1;
if n+1=m then ans:=1;
if ans=0 then begin writeln(0);halt;end;
p:=1;
while i<min(n,m) do begin inc(i);p:=(p*i)mod ppp;end;
if n=m then writeln((p*p*2)mod ppp);
if n=m+1 then writeln((p*p*n)mod ppp);
if n+1=m then writeln((p*p*m)mod ppp);
end.


program Project1;

{$APPTYPE CONSOLE}

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

    min:=1000;
    max:=0;
    for i := 0 to n - 1 do begin
      Read(a);
      min:=(min+a-abs(min-a))div 2;
      max:=(max+a+abs(max-a))div 2;
    end;
    //Writeln;
    Writeln(max-min);
    Readln;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

It is only six months until Christmas, and AtCoDeer the reindeer is now planning his travel to deliver gifts.

There are N houses along TopCoDeer street. The i-th house is located at coordinate a_i. He has decided to deliver gifts to all these houses.

Find the minimum distance to be traveled when AtCoDeer can start and end his travel at any positions.

Constraints

1 ≤ N ≤ 100

0 ≤ a_i ≤ 1000

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum distance to be traveled.

Sample Input 1

4
2 3 7 9

Sample Output 1

7

The travel distance of 7 can be achieved by starting at coordinate 9 and traveling straight to coordinate 2.

It is not possible to do with a travel distance of less than 7, and thus 7 is the minimum distance to be traveled.

Sample Input 2

8
3 1 4 1 5 9 2 6

Sample Output 2

8

There may be more than one house at a position.


program Project1;

{$APPTYPE CONSOLE}

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

    for i := 0 to n-1 do begin
      min := i;
      for j := i+1 to n-1 do begin
        if s[j] < s[min] then
        min := j;
      end;
      sw:=s[i];
      s[i]:=s[min];
      s[min]:=sw;
    end;

    for i := 1 to Length(s) - 1 do
      if s[i]=s[i+1] then begin
        Writeln('no');
        Readln;
        exit;
      end;

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

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters. Determine whether all the characters in S are different.

Constraints

2 ≤ |S| ≤ 26, where |S| denotes the length of S.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If all the characters in S are different, print yes (case-sensitive); otherwise, print no.

Sample Input 1

uncopyrightable

Sample Output 1

yes

Sample Input 2

different

Sample Output 2

no

Sample Input 3

no

Sample Output 3

yes


program Project1;

{$APPTYPE CONSOLE}

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

    SetLength(s,n);
    min:=101;
    count:=0;
    for i := 0 to n - 1 do begin
      Readln(s[i]);
      count:=count+s[i];
      if s[i] mod 10 <> 0 then
        min:=(min+s[i]-abs(min-s[i]))div 2;
    end;

    if (count mod 10 = 0)and (min<=100) then
      count:=count-min;

    if count mod 10 = 0 then
      count:=0;

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

Score : 300 points

Problem Statement

You are taking a computer-based examination. The examination consists of N questions, and the score allocated to the i-th question is s_i. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.

However, the examination system is actually flawed, and if your grade is a multiple of 10, the system displays 0 as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?

Constraints

All input values are integers.

1 ≤ N ≤ 100

1 ≤ s_i ≤ 100

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the maximum value that can be displayed as your grade.

Sample Input 1

3
5
10
15

Sample Output 1

25

Your grade will be 25 if the 10-point and 15-point questions are answered correctly and the 5-point question is not, and this grade will be displayed correctly. Your grade will become 30 if the 5-point question is also answered correctly, but this grade will be incorrectly displayed as 0.

Sample Input 2

3
10
10
15

Sample Output 2

35

Your grade will be 35 if all the questions are answered correctly, and this grade will be displayed correctly.

Sample Input 3

3
10
20
30

Sample Output 3

0

Regardless of whether each question is answered correctly or not, your grade will be a multiple of 10 and displayed as 0.


var
   n: integer;
   a: integer;
   b: integer;
   h: array of integer;
   i: integer;
   l: integer;
   r: integer;
   m: integer;
   diff: int64;
   cnt: int64;
begin
   readln(n, a, b);
   setlength(h, n);
   for i := 0 to n - 1 do begin
      readln(h[i]);
   end;
   l := 0;
   r := 1000000000;
   while r - l > 1 do begin
      cnt := 0;
      m := (r + l) div 2;
      for i := 0 to n - 1 do begin
         diff := h[i] - b * m;
         if diff > 0 then begin
            cnt := cnt + (diff div (a - b));
            if diff mod (a - b) <> 0 then begin
               inc(cnt);
            end;
         end;
      end;
      if cnt > m then begin
         l := m;
      end
      else begin
         r := m;
      end;
   end;
   writeln(r);
end.

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter N monsters. Each monster has a parameter called health, and the health of the i-th monster is h_i at the moment of encounter. A monster will vanish immediately when its health drops to 0 or below.

Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:

Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by A, and the health of each of the other monsters will decrease by B. Here, A and B are predetermined parameters, and A > B holds.

At least how many explosions do you need to cause in order to vanish all the monsters?

Constraints

All input values are integers.

1 ≤ N ≤ 10^5

1 ≤ B < A ≤ 10^9

1 ≤ h_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N A B
h_1
h_2
:
h_N

Output

Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.

Sample Input 1

4 5 3
8
7
4
2

Sample Output 1

2

You can vanish all the monsters in two explosion, as follows:

First, cause an explosion centered at the monster with 8 health. The healths of the four monsters become 3, 4, 1 and -1, respectively, and the last monster vanishes.

Second, cause an explosion centered at the monster with 4 health remaining. The healths of the three remaining monsters become 0, -1 and -2, respectively, and all the monsters are now vanished.

Sample Input 2

2 10 4
20
20

Sample Output 2

4

You need to cause two explosions centered at each monster, for a total of four.

Sample Input 3

5 2 1
900000000
900000000
1000000000
1000000000
1000000000

Sample Output 3

800000000


var
   n: integer;
   a: integer;
   b: integer;
   h: array of integer;
   i: integer;
   l: integer;
   r: integer;
   m: integer;
   diff: integer;
   cnt: int64;
begin
   readln(n, a, b);
   setlength(h, n);
   for i := 0 to n - 1 do begin
      readln(h[i]);
   end;
   l := 0;
   r := 1000000000;
   while r - l > 1 do begin
      cnt := 0;
      m := (r + l) div 2;
      for i := 0 to n - 1 do begin
         diff := h[i] - (b * m);
         if diff > 0 then begin
            cnt := cnt + (diff div (a - b));
            if diff mod (a - b) > 0 then begin
               inc(cnt);
            end;
            if cnt > m then begin
               break;
            end;
         end;
      end;
      if cnt > m then begin
         l := m;
      end
      else begin
         r := m;
      end;
   end;
   writeln(r);
end.


var
   n: integer;
   a: integer;
   b: integer;
   h: array of integer;
   i: integer;
   l: integer;
   r: integer;
   m: integer;
   diff: integer;
   cnt: int64;
begin
   readln(n, a, b);
   setlength(h, n);
   for i := 0 to n - 1 do begin
      readln(h[i]);
   end;
   l := 0;
   r := 1000000000;
   while r - l > 1 do begin
      cnt := 0;
      m := (r + l) div 2;
      for i := 0 to n - 1 do begin
         diff := h[i] - (b * m);
         if diff > 0 then begin
            cnt := cnt + (diff div (a - b));
            if diff mod (a - b) > 0 then begin
               inc(cnt);
               if cnt > m then begin
                  break;
               end;
            end;
         end;
      end;
      if cnt > m then begin
         l := m;
      end
      else begin
         r := m;
      end;
   end;
   writeln(r);
end.


var
	n,i:Longint;
	s,L,R,M,a,b:int64;
	h:array[1..100000]of int64;
begin
	read(n,a,b);
	for i:=1 to n do read(h[i]);
	R:=1000000000;
	while R-L>1 do begin
		M:=(L+R)div 2;
		s:=0;
		for i:=1 to n do begin
			if M*b<h[i]then s:=s+(h[i]-M*b+a-b-1)div(a-b);
		end;
		if s>M then L:=M else R:=M;
	end;
	writeln(R);
end.


var
	n,i:Longint;
	s,L,R,M,a,b:int64;
	h:array[1..100000]of int64;
begin
	read(n,a,b);
	for i:=1 to n do read(h[i]);
	R:=1000000000;
	while R-L>1 do begin
		M:=(L+R)div 2;
		s:=0;
		for i:=1 to n do begin
			if M*b<h[i]then s:=s+h[i]-M*b;
		end;
		if s>(a-b)*M then L:=M else R:=M;
	end;
	writeln(R);
end.


program j01;
const maxn=400086;
type opr=record w:int64;id:longint;end;
var sum:array[0..maxn]of int64;
    op:array[0..maxn]of opr;
    tot:longint;
    ans:int64;
    c,a:array[0..maxn]of longint;
    n,i,k:longint;

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

procedure disc;
var i:longint;
begin
  sort(1,n);
  tot:=1;a[op[1].id]:=1;
  for i:=2 to n do
  begin
    if op[i].w<>op[i-1].w then inc(tot);
    a[op[i].id]:=tot;
  end;
end;

procedure ins(i:longint);
begin
  while i<=tot do
  begin
    inc(c[i]);inc(i,i and(-i));
  end;
end;

function ask(i:longint):longint;
var res:longint;
begin
  res:=0;
  while i>0 do
  begin
    inc(res,c[i]);dec(i,i and(-i));
  end;
  exit(res);
end;

begin
  readln(n,k);
  for i:=1 to n do read(a[i]);
  for i:=1 to n do a[i]:=a[i]-k;
  sum[1]:=0;inc(n);for i:=2 to n do sum[i]:=sum[i-1]+a[i-1];
  for i:=1 to n do
  begin
    op[i].w:=sum[i];op[i].id:=i;
  end;
  //for i:=1 to n do write(sum[i],' ');writeln;
  disc;
  for i:=1 to n do
  begin
    inc(ans,ask(a[i]));ins(a[i]);
  end;
  writeln(ans);
end.

Score : 600 points

Problem Statement

You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K.

a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K?

Constraints

All input values are integers.

1 ≤ N ≤ 2 \times 10^5

1 ≤ K ≤ 10^9

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N K
a_1
a_2
:
a_N

Output

Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.

Sample Input 1

3 6
7
5
7

Sample Output 1

5

All the non-empty contiguous subsequences of a are listed below:

{a_1} = {7}

{a_1, a_2} = {7, 5}

{a_1, a_2, a_3} = {7, 5, 7}

{a_2} = {5}

{a_2, a_3} = {5, 7}

{a_3} = {7}

Their means are 7, 6, 19/3, 5, 6 and 7, respectively, and five among them are 6 or greater. Note that {a_1} and {a_3} are indistinguishable by the values of their elements, but we count them individually.

Sample Input 2

1 2
1

Sample Output 2

0

Sample Input 3

7 26
10
20
30
40
30
20
10

Sample Output 3

13


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

Score : 500 points

Problem Statement

Let N be a positive integer.

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

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

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

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

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

Sample Input 2

1
1 2 3

Sample Output 2

-1

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

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

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


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


const
        fi='arc074_b.inp';
        fo='arc074_b.out';
var
        res:int64;
        n,nheap:longint;
        P1,P2:Array[0..300001] of int64;
        A:Array[0..300001] of longint;
        Heap:Array[0..100001] of longint;
{------------------------------------}
procedure swap(var xx1,xx2:longint);
var tg:longint;
        begin
                tg:=xx1;
                xx1:=xx2;
                xx2:=tg;
        end;
{------------------------------------}
procedure upheapmin(k:longint);
        begin
                if (k=1) or (Heap[k]>=Heap[k div 2]) then exit
                else
                        begin
                                swap(Heap[k],Heap[k div 2]);
                                upheapmin(k div 2);
                        end;
        end;
{------------------------------------}
procedure downheapmin(k:longint);
var vtk:longint;
        begin
                if (2*k<=nheap) then
                begin
                        if (2*k+1<=nheap) and (Heap[2*k+1]<Heap[2*k]) then vtk:=2*k+1 else vtk:=2*k;
                        if Heap[k]>Heap[vtk] then
                        begin
                                swap(Heap[k],Heap[vtk]);
                                downheapmin(vtk);
                        end;
                end;
        end;
{------------------------------------}
procedure pushmin(pt:longint);
        begin
                inc(nheap);
                Heap[nheap]:=pt;
                upheapmin(nheap);
        end;
{------------------------------------}
procedure upheapmax(k:longint);
        begin
                if (k=1) or (Heap[k]<=Heap[k div 2]) then exit
                else
                        begin
                                swap(Heap[k],Heap[k div 2]);
                                upheapmax(k div 2);
                        end;
        end;
{------------------------------------}
procedure downheapmax(k:longint);
var vtk:longint;
        begin
                if (2*k<=nheap) then
                begin
                        if (2*k+1<=nheap) and (Heap[2*k+1]>Heap[2*k]) then vtk:=2*k+1 else vtk:=2*k;
                        if Heap[k]<Heap[vtk] then
                        begin
                                swap(Heap[k],Heap[vtk]);
                                downheapmax(vtk);
                        end;
                end;
        end;
{------------------------------------}
procedure pushmax(pt:longint);
        begin
                inc(nheap);
                Heap[nheap]:=pt;
                upheapmax(nheap);
        end;
{------------------------------------}
procedure main;
var o:longint;
        begin
                read(n);
                for o:=1 to 3*n do read(A[o]);
                nheap:=0;
                for o:=1 to n do
                begin
                        pushmin(A[o]);
                        inc(P1[n],A[o]);
                end;
                for o:=n+1 to 2*n do
                begin
                        P1[o]:=P1[o-1];
                        if A[o]>Heap[1] then
                        begin
                                P1[o]:=P1[o]-Heap[1]+A[o];
                                Heap[1]:=A[o];
                                downheapmin(1);
                        end;
                end;
                nheap:=0;
                for o:=3*n downto 2*n+1 do
                begin
                        pushmax(A[o]);
                        inc(P2[2*n+1],A[o]);
                end;
                for o:=2*n downto n+1 do
                begin
                        P2[o]:=P2[o+1];
                        if A[o]<Heap[1] then
                        begin
                                P2[o]:=P2[o]-Heap[1]+A[o];
                                Heap[1]:=A[o];
                                downheapmax(1);
                        end;
                end;
                res:=-1000000000000000;
                for o:=n to 2*n do if res<P1[o]-P2[o+1] then res:=P1[o]-P2[o+1];
                write(res);
        end;
{------------------------------------}
begin
//        assign(input,fi); reset(input);
  //      assign(output,fo);rewrite(output);
        main;
end.


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


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