program ec12;
var
	n,m,i,j:longint;
	ans:int64;
	a,b,c,s,sum:array[0..100001] of longint;
procedure qsrt(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=b[(l+r) div 2];
	repeat
	while b[i]<v do inc(i);
	while b[j]>v do dec(j);
	if i<=j then
	begin
		s:=b[i];
		b[i]:=b[j];
		b[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;
procedure qsrt1(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=c[(l+r) div 2];
	repeat
	while c[i]<v do inc(i);
	while c[j]>v do dec(j);
	if i<=j then
	begin
		s:=c[i];
		c[i]:=c[j];
		c[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;
procedure qsrt2(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]);
	qsrt2(1,n);
	for i:=1 to n do
	read(b[i]);
	qsrt(1,n);
	for i:=1 to n do
	read(c[i]);
	qsrt1(1,n);
	i:=1;
	j:=1;
	while (i<=n) and (j<=n) do
	begin
		if b[i]>=c[j] then
		inc(j)
		else
		begin
			s[i]:=n-j+1;
			inc(i);
		end;
	end;
	i:=1;
	j:=1;
	sum[0]:=0;
	for i:=1 to n do
	sum[i]:=sum[i-1]+s[i];
	ans:=0;
	i:=1;
	j:=1;
	while (i<=n) and (j<=n) do
	begin
		if a[i]>=b[j] then
		inc(j)
		else
		begin
			ans:=ans+sum[n]-sum[j-1];
			inc(i);
		end;
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

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

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

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87


program V1501;
 type
  w=array[0..100001] of int64;
 var
  a,b,c,d,e,f:w;
  i,n,p:longint;
 procedure haha(var a:w;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(a,o,r);
   if l<p then haha(a,l,p);
  end;
 begin
  readln(n);
  for i:=1 to n do
   read(a[i]);
  readln;
  a[n+1]:=1008208820;
  haha(a,1,n+1);
  for i:=1 to n do
   read(b[i]);
  readln;
  b[n+1]:=1008208820;
  haha(b,1,n+1);
  for i:=1 to n do
   read(c[i]);
  readln;
  c[n+1]:=1008208820;
  haha(c,1,n+1);
  for i:=1 to n+1 do
   f[i]:=n-i+1;
  p:=1;
  for i:=1 to n do
   begin
    while c[p]<=b[i] do inc(p);
    e[i]:=f[p];
   end;
  e[n+1]:=0;
  for i:=n downto 1 do
   inc(e[i],e[i+1]);
  p:=1;
  for i:=1 to n do
   begin
    while b[p]<=a[i] do inc(p);
    d[i]:=e[p];
   end;
  d[n+1]:=0;
  for i:=n downto 1 do
   inc(d[i],d[i+1]);
  writeln(d[1]);
 end.

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

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


var n,w,k,i,j:longint;
    ans:int64;
    a,b,c:array[0..100001]of 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;
    procedure sort1(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=b[(l+r) div 2];
         repeat
           while b[i]<x do
            inc(i);
           while x<b[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=b[i];
                b[i]:=b[j];
                b[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort1(l,j);
         if i<r then
           sort1(i,r);
      end;
    procedure sort2(l,r: longint);
      var
         i,j,x,y:longint;
      begin
         i:=l;
         j:=r;
         x:=c[(l+r) div 2];
         repeat
           while c[i]<x do
            inc(i);
           while x<c[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=c[i];
                c[i]:=c[j];
                c[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort2(l,j);
         if i<r then
           sort2(i,r);
      end;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  for i:=1 to n do
    read(b[i]);
  for i:=1 to n do
    read(c[i]);
  sort(1,n);
  sort1(1,n);
  sort2(1,n);
  for i:=1 to n do
    begin
      if b[1]>a[i]
      then k:=k+1;
    end;
  for i:=1 to n do
    begin
      if c[i]>b[1]
      then w:=w+1;
    end;
  for i:=1 to n do
    begin
      while (b[i]>a[k+1])and(k+1<=n) do k:=k+1;
      while (b[i]>=c[n-w+1])and(n-w+1<=n) do w:=w-1;
      ans:=ans+k*w;
    end;
  writeln(ans);
end.


var n:string;
    i,w,ans,min,k,j:longint;
    a,b:array[0..100000]of longint;
procedure gao;
var i:longint;
begin
  for i:=1 to k do
    a[i]:=a[i]+b[i];
  for i:=1 to w do
    if a[i]>9
    then begin
           a[i+1]:=a[i+1]+1;
           a[i]:=a[i]-10;
         end;
  if a[w+1]>0
  then w:=w+1;
  for i:=1 to w do
    ans:=ans+a[i];
end;
begin
  readln(n);
  min:=maxlongint;
  for i:=length(n) downto 1 do
    a[length(n)-i+1]:=ord(n[i])-48;
  for i:=1 to length(n) do
    b[i]:=a[i];
  k:=length(n);
  w:=length(n);
  for i:=1 to 200000 do
    begin
      ans:=0;
      gao;
      if ans<min
      then min:=ans;
    end;
  writeln(min);
end.

Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

2 \leq K \leq 10^5

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Sample Input 1

6

Sample Output 1

3

12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

11111=41×271 yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36


var a,b,c,tt:array[1..100000]of longint;
b1:array[0..100000]of int64;
bao,t,w,i,x,n:longint;sum:int64;
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]);
for i:=1 to n do read(b[i]);
for I:=1 to n do read(c[i]);
sort(1,n);
tt:=a;a:=b;sort(1,n);b:=a;a:=tt;
tt:=a;a:=c;sort(1,n);c:=a;a:=tt;
for i:=1 to n do
  begin
  t:=1;w:=n;bao:=n+1;
  while t<=w do
    begin
    x:=(t+w)div 2;
    if c[x]<=b[i] then t:=x+1
    else
      begin
      bao:=x;w:=x-1;
      end;
    end;
  b1[i]:=n-bao+1;
  end;
for i:=1 to n do b1[i]:=b1[i-1]+b1[i];
for i:=1 to n do
  begin
  t:=1;w:=n;bao:=n+1;
  while t<=w do
    begin
    x:=(t+w)div 2;
    if b[x]<=a[i] then t:=x+1
    else
      begin
      bao:=x;w:=x-1;
      end;
    end;
  sum:=sum+b1[n]-b1[bao-1];
  end;
write(sum);

end.


var
	deque:array[0..199999]of Longint;
	size:Longint=200000;
	qhead,qtail:Longint;
	k,i,u:Longint;
	d:array[0..99999]of Longint;
procedure pushf(a:Longint);
begin
	dec(qhead);
	if qhead<0 then qhead:=size+qhead;
	deque[qhead]:=a;
end;
procedure pusht(a:Longint);
begin
	deque[qtail]:=a;
	inc(qtail);
	if qtail>=size then qtail:=qtail-size;
end;
function popf():Longint;
var s:Longint;
begin
	s:=deque[qhead];
	inc(qhead);
	if qhead>=size then qhead:=qhead-size;
	popf:=s;
end;
begin
	read(k);
	for i:=0 to k-1 do d[i]:=1000000000;
	d[1]:=1;
	pusht(1);
	while qhead<>qtail do begin
		u:=popf();
		if d[10*u mod k]>d[u]then begin
			pushf(10*u mod k);
			d[10*u mod k]:=d[u];
		end;
		if d[(u+1)mod k]>d[u]+1 then begin
			pusht((u+1)mod k);
			d[(u+1)mod k]:=d[u]+1;
		end;
	end;
	writeln(d[0]);
end.


var n:longint;
    dist:array[0..100050]of longint;
    q:array[0..2000050]of longint;
    v:array[0..100050]of boolean;
procedure spfa;
var  head,tail:longint;
     i,now,y:longint;
begin
  head:=1;
  tail:=0;
  for i:=1 to n-1 do
  dist[i]:=i;
  dist[0]:=maxint;
  for i:=0 to n-1 do
  begin
    inc(tail);
    q[tail]:=i;
    v[i]:=true;
  end;
  while head<=tail do
  begin
    now:=q[head];
    for i:=0 to 9 do
    begin
      y:=(now*10+i) mod n;
      if dist[y]>dist[now]+i then
      begin
        dist[y]:=dist[now]+i;
        if not v[y] then
        begin
          v[y]:=true;
          inc(tail);
          q[tail]:=y;
        end;
      end;
    end;
    inc(head);
    v[y]:=false;
  end;
end;
begin
  read(n);
  spfa;
  writeln(dist[0]);
end.


uses math;
var
    a:array[0..1000000]of longint;
    n,k,i:longint;
begin
    a[0]:=1;
    readln(n,k);
    for i:=1 to n do
    a[i]:=min(a[i-1]+k,a[i-1]*2);
    writeln(a[n]);
end.

Score : 200 points

Problem Statement

Square1001 has seen an electric bulletin board displaying the integer 1.
He can perform the following operations A and B to change this value:

Operation A: The displayed value is doubled.

Operation B: The displayed value increases by K.

Square1001 needs to perform these operations N times in total.
Find the minimum possible value displayed in the board after N operations.

Constraints

1 \leq N, K \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the minimum possible value displayed in the board after N operations.

Sample Input 1

4
3

Sample Output 1

10

The value will be minimized when the operations are performed in the following order: A, A, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 7 → 10.

Sample Input 2

10
10

Sample Output 2

76

The value will be minimized when the operations are performed in the following order: A, A, A, A, B, B, B, B, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 8 → 16 → 26 → 36 → 46 → 56 → 66 → 76.

By the way, this contest is AtCoder Beginner Contest 076.


var ans:int64;
    i,n:longint;
    a:array[0..15]of longint;
procedure dfs(dep,bool:longint);
var i:longint;
begin
  if dep=n+1 then
  begin
    if bool=1 then inc(ans);
    exit;
  end;
  for i:=a[dep]-1 to a[dep]+1 do
  if i mod 2=0 then dfs(dep+1,bool or 1) else
  dfs(dep+1,bool);
end;
begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  dfs(1,0);
  writeln(ans);
end.

Score : 200 points

Problem Statement

We will say that two integer sequences of length N, x_1, x_2, ..., x_N and y_1, y_2, ..., y_N, are similar when |x_i - y_i| \leq 1 holds for all i (1 \leq i \leq N).

In particular, any integer sequence is similar to itself.

You are given an integer N and an integer sequence of length N, A_1, A_2, ..., A_N.

How many integer sequences b_1, b_2, ..., b_N are there such that b_1, b_2, ..., b_N is similar to A and the product of all elements, b_1 b_2 ... b_N, is even?

Constraints

1 \leq N \leq 10

1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of integer sequences that satisfy the condition.

Sample Input 1

2
2 3

Sample Output 1

7

There are seven integer sequences that satisfy the condition:

1, 2

1, 4

2, 2

2, 3

2, 4

3, 2

3, 4

Sample Input 2

3
3 3 3

Sample Output 2

26

Sample Input 3

1
100

Sample Output 3

1

Sample Input 4

10
90 52 56 71 44 8 13 30 57 84

Sample Output 4

58921


var s:ansistring;
    i:longint;
    n,l,r:longint;
    ans:int64;
begin
  readln(s);
  n:=length(s);
  l:=1;
  r:=n;
  while l<r do
  begin
    if (s[l]<>s[r]) then
    begin
      if (s[l]='x') then
      begin
        inc(ans);
        inc(l);
      end else
      if (s[r]='x') then
      begin
        inc(ans);
        dec(r);
      end else
      begin
        writeln(-1);
        exit;
      end;
    end else
    begin
      inc(l);
      dec(r);
    end;
  end;
  writeln(ans);
end.

Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx


var a:array['a'..'z'] of longint;
var s:ansistring;
var ans,i,j:longint;
begin
  readln(s);for i:=1 to length(s) do inc(a[s[i]]);
  for i:=1 to 26 do if a[chr(i+ord('a')-1)] mod 2=1 then inc(ans);
  if ans=0 then inc(ans);writeln(ans);
end.

Score : 700 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke is partitioning s into some number of non-empty substrings.
Let the subtrings obtained be s_1, s_2, ..., s_N from left to right. (Here, s = s_1 + s_2 + ... + s_N holds.)
Snuke wants to satisfy the following condition:

For each i (1 \leq i \leq N), it is possible to permute the characters in s_i and obtain a palindrome.

Find the minimum possible value of N when the partition satisfies the condition.

Constraints

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

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum possible value of N when the partition satisfies the condition.

Sample Input 1

aabxyyzz

Sample Output 1

2

The solution is to partition s as aabxyyzz = aab + xyyzz.
Here, aab can be permuted to form a palindrome aba, and xyyzz can be permuted to form a palindrome zyxyz.

Sample Input 2

byebye

Sample Output 2

1

byebye can be permuted to form a palindrome byeeyb.

Sample Input 3

abcdefghijklmnopqrstuvwxyz

Sample Output 3

26

Sample Input 4

abcabcxabcx

Sample Output 4

3

The solution is to partition s as abcabcxabcx = a + b + cabcxabcx.


var a:string;
begin
  readln(a);
  writeln(copy(a,1,length(a)-8));
end.

Score : 100 points

Problem Statement

Rng is going to a festival.

The name of the festival is given to you as a string S, which ends with FESTIVAL, from input. Answer the question: "Rng is going to a festival of what?" Output the answer.

Here, assume that the name of "a festival of s" is a string obtained by appending FESTIVAL to the end of s.
For example, CODEFESTIVAL is a festival of CODE.

Constraints

9 \leq |S| \leq 50

S consists of uppercase English letters.

S ends with FESTIVAL.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer to the question: "Rng is going to a festival of what?"

Sample Input 1

CODEFESTIVAL

Sample Output 1

CODE

This is the same as the example in the statement.

Sample Input 2

CODEFESTIVALFESTIVAL

Sample Output 2

CODEFESTIVAL

This string is obtained by appending FESTIVAL to the end of CODEFESTIVAL, so it is a festival of CODEFESTIVAL.

Sample Input 3

YAKINIKUFESTIVAL

Sample Output 3

YAKINIKU


var cc : array [1..100000000] of longint ;
    a,b,c,d,e,f,g : longint ;
begin
 readln(a);
 for b:=1 to a do
  begin
   read(c);
   inc(cc[c]);
  end;
 readln(a);
 for b:=1 to a do
  begin
   read(c);
   dec(cc[c]);
   if cc[c]<0 then
    g:=1;
  end;
 if g=1 then
  writeln('NO')
 else
  writeln('YES');
end.

Score : 200 points

Problem Statement

Rng is preparing a problem set for a qualification round of CODEFESTIVAL.

He has N candidates of problems. The difficulty of the i-th candidate is D_i.

There must be M problems in the problem set, and the difficulty of the i-th problem must be T_i. Here, one candidate of a problem cannot be used as multiple problems.

Determine whether Rng can complete the problem set without creating new candidates of problems.

Constraints

1 \leq N \leq 200,000

1 \leq D_i \leq 10^9

1 \leq M \leq 200,000

1 \leq T_i \leq 10^9

All numbers in the input are integers.

Partial Score

100 points will be awarded for passing the test set satisfying N \leq 100 and M \leq 100.

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N
M
T_1 T_2 ... T_M

Output

Print YES if Rng can complete the problem set without creating new candidates of problems; print NO if he cannot.

Sample Input 1

5
3 1 4 1 5
3
5 4 3

Sample Output 1

YES

Sample Input 2

7
100 200 500 700 1200 1600 2000
6
100 200 500 700 1600 1600

Sample Output 2

NO

Not enough 1600s.

Sample Input 3

1
800
5
100 100 100 100 100

Sample Output 3

NO

Sample Input 4

15
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
9
5 4 3 2 1 2 3 4 5

Sample Output 4

YES


var
  flag:boolean;
  da,sl:int64;
  hs:array[1..2] of int64;
  x,y,n,u,m,i:longint;
  hd,nx,t,ys:array[0..200100] of longint;
  procedure jb(x,y:longint);
    begin
      u:=u+1;
      nx[u]:=hd[x];
      hd[x]:=u;
      t[u]:=y;
    end;
  function ss(q,sc:longint):boolean;
    var
      i:longint;
      cg:boolean;
    begin
      ys[q]:=sc;
      sl:=sl+1;
      hs[sc]:=hs[sc]+1;
      cg:=true;
      i:=hd[q];
      while i<>0 do
        begin
          if ys[t[i]]=0 then cg:=cg and ss(t[i],3-sc);
          if ys[t[i]]=sc then
            begin
              cg:=false;
              flag:=false;
            end;
          i:=nx[i];
        end;
      exit(cg);
    end;
begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(x,y);
      jb(x,y);
      jb(y,x);
    end;
  for i:=1 to n do
    if ys[i]=0 then
      begin
        hs[1]:=0;
        hs[2]:=0;
        sl:=0;
        flag:=true;
        if ss(i,1)<>flag then writeln('?');
        if flag then da:=da+hs[1]*hs[2]
        else da:=da+sl*(sl-1) div 2;
      end;
  writeln(da-m);
end.

Score : 500 points

Problem Statement

Rng has a connected undirected graph with N vertices.
Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.

Rng will add new edges to the graph by repeating the following operation:

Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.

Find the maximum possible number of edges that can be added.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N

The graph has no self-loops or multiple edges.

The graph is connected.

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

Find the maximum possible number of edges that can be added.

Sample Input 1

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

Sample Output 1

4

If we add edges as shown below, four edges can be added, and no more.

Sample Input 2

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

Sample Output 2

5

Five edges can be added, for example, as follows:

Add an edge connecting Vertex 5 and Vertex 3.

Add an edge connecting Vertex 5 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 1.

Add an edge connecting Vertex 4 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 3.


var
  da,sl:int64;
  hs:array[1..2] of int64;
  x,y,n,u,m,i:longint;
  hd,nx,t,ys:array[0..200100] of longint;
  procedure jb(x,y:longint);
    begin
      u:=u+1;
      nx[u]:=hd[x];
      hd[x]:=u;
      t[u]:=y;
    end;
  function ss(q,sc:longint):boolean;
    var
      i:longint;
      cg:boolean;
    begin
      ys[q]:=sc;
      sl:=sl+1;
      hs[sc]:=hs[sc]+1;
      cg:=true;
      i:=hd[q];
      while i<>0 do
        begin
          if ys[t[i]]=sc then cg:=false;
          if ys[t[i]]=0 then cg:=cg and ss(t[i],3-sc);
          i:=nx[i];
        end;
      exit(cg);
    end;
begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(x,y);
      jb(x,y);
      jb(y,x);
    end;
  for i:=1 to n do
    if ys[i]=0 then
      begin
        hs[1]:=0;
        hs[2]:=0;
        sl:=0;
        if ss(i,1) then da:=da+hs[1]*hs[2]
        else da:=da+sl*(sl-1) div 2;
      end;
  writeln(da-m);
end.


var
  da,sl:int64;
  hs:array[1..2] of int64;
  x,y,n,u,m,i:longint;
  hd,nx,t,ys:array[0..200100] of longint;
  procedure jb(x,y:longint);
    begin
      u:=u+1;
      nx[u]:=hd[x];
      hd[x]:=u;
      t[u]:=y;
    end;
  function ss(q,sc:longint):boolean;
    var
      i:longint;
      cg:boolean;
    begin
      ys[q]:=sc;
      sl:=sl+1;
      hs[sc]:=hs[sc]+1;
      cg:=true;
      i:=hd[q];
      while i<>0 do
        begin
          if ys[t[i]]=0 then cg:=cg and ss(t[i],3-sc);
          if ys[t[i]]=sc then cg:=false;
          i:=nx[i];
        end;
      exit(cg);
    end;
begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(x,y);
      jb(x,y);
      jb(y,x);
    end;
  for i:=1 to n do
    if ys[i]=0 then
      begin
        hs[1]:=0;
        hs[2]:=0;
        sl:=0;
        if ss(i,1) then da:=da+hs[1]*hs[2]
        else da:=da+sl*(sl-1) div 2;
      end;
  writeln(da-m);
end.


  Uses Math;

 var S : AnsiString;
     N : LongInt;
     ans : Int64;

 var dp : array [ 0 .. 500000 , 1 .. 2 ] of LongInt;
     size : array [ 1 .. 500000 + 12 ] of LongInt;

 procedure Solve ( l , R : LongInt ) ;
   var i : LongInt;
       x , cnt : LongInt;
 begin
  if ( L + 2 > R ) then exit();

  for i := 1 to R - L + 1 do
  begin
    dp[i][1] := 0;
    dp[i][2] := 0;
    size[i] := 0;
  end;

  cnt := 0;
  x := 0;

  for i := L to R do
  begin
    if ( s[i] = '1' ) then
    begin
      cnt += 1;
    end
    else
    begin
      inc ( x ) ;
      size[x] := cnt - 1;
      cnt := 0;
    end;
  end;
  size[x+1] := cnt - 1;

  dp[0][1] := 0;
  dp[0][2] := 0;

  dp[1][1] := size[1] + 1;
  dp[1][2] := 1 + size[2];

  for i := 2 to X do
  begin

    if ( size[i-1] <> 0 ) then // not 10101
    begin
      dp[i][1] := max ( dp[i-1][1] + size[i] + 1 , dp[i-1][2] + 1 );
      dp[i][2] := max ( dp[i-1][1] , dp[i-1][2] ) + size[i+1] + 1;
    end
    else
    begin
      dp[i][1] := max ( max ( dp[i-1][1] , dp[i-1][2] ) , max(dp[i-2][1] , dp[i-2][2] ) + 1 );
      dp[i][2] := max ( max ( dp[i-1][1] , dp[i-1][2] ) , max(dp[i-2][1] , dp[i-2][2] ) + size[x+1] + 1 );
    end;
    dp[i][1] := max ( dp[i][1] , dp[i][2] );
  end;

  ans += max ( dp[x][1] , dp[x][2] );

 end;

 var last : LongINt;

 var i : LongInt;

begin
  ans := 0;

  readLn ( N ) ;
  readLn ( S ) ;

  last := 0;

  for i := 1 to N do
  begin
    if ( S[i] = '0' ) and ( S[i+1] = '0' ) then
    begin
      Solve ( last + 1 , i - 1 );
      last := i + 1;
    end;
  end;

  if ( S[n] = '0' ) then
    Solve ( last + 1 , n-1 )
  else
    Solve ( last + 1 , n );

  writeLn ( ans );

end.

Score : 700 points

Problem Statement

N cells are arranged in a row.
Some of them may contain tokens.
You are given a string s that consists of 0s and 1s.
If the i-th character of s is 1, the i-th cell (from left) contains a token.
Otherwise, it doesn't contain a token.

Snuke wants to perform the following operation as many times as possible.
In each operation, he chooses three consecutive cells.
Let's call the cells X, Y, Z from left to right.
In order for the operation to be valid, both X and Z must contain tokens and Y must not contain a token.
Then, he removes these two tokens and puts a new token on Y.

How many operations can he perform if he performs operations in the optimal way?

Constraints

1 \leq N \leq 500,000

|s| = N

Each character in s is either 0 or 1.

Input

Input is given from Standard Input in the following format:

N
s

Output

Print the answer.

Sample Input 1

7
1010101

Sample Output 1

2

For example, he can perform two operations in the following way:

Perform an operation on the last three cells. Now the string that represents tokens becomes 1010010.

Perform an operation on the first three cells. Now the string that represents tokens becomes 0100010.

Note that the choice of operations matters.
For example, if he chooses three cells in the middle first, he can perform no more operations.

Sample Input 2

50
10101000010011011110001001111110000101010111100110

Sample Output 2

10


var n,i:longint;
    a,x:longint;
    max,maxx,min:longint;
    z:longint;
begin
  read(n);
  min:=maxlongint;
  for i:=1 to n do
  begin
    read(a,x);
    if a>max then max:=a;
    if x<min then min:=x;
  end;
  writeln(max+min);
end.

Score : 200 points

Problem Statement

A group of people played a game. All players had distinct scores, which are positive integers.

Takahashi knows N facts on the players' scores. The i-th fact is as follows: the A_i-th highest score among the players is B_i.

Find the maximum possible number of players in the game.

Constraints

1 \leq N \leq 10^5

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

0 \leq B_i \leq 10^9(1\leq i\leq N)

If i ≠ j, A_i ≠ A_j.

There exists a possible outcome of the game that are consistent with the facts.

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible number of players in the game.

Sample Input 1

3
4 7
2 9
6 2

Sample Output 1

8

The maximum possible number of players is achieved when, for example, the players have the following scores: 12,9,8,7,5,2,1,0.

Sample Input 2

5
1 10
3 6
5 2
4 4
2 8

Sample Output 2

7

Sample Input 3

2
1 1000000000
1000000000 1

Sample Output 3

1000000001


var n,i:longint;
    a,x:longint;
    max,maxx:longint;
begin
  read(n);
  for i:=1 to n do
  begin
    read(a,x);
    if a>max then
    begin
      max:=a;
      maxx:=x;
    end;
  end;
  writeln(max+maxx);
end.


var x,y,n,i,j,max,maxi:longint;
begin
  read(n);for i:=1 to n do begin read(x,y);if x>max then begin max:=x;maxi:=y;end;end;
  writeln(max+maxi);end.


var n,k,ans,a1,kk:int64;
var i,j:longint;
var a,b,c:array[0..100020] of int64;
procedure qsort(l,r:int64);
var i,j,mid,m2,t:int64;
begin
  i:=l;j:=r;mid:=b[(l+r) >>1];m2:=a[(l+r)>>1];
  repeat
    while (b[i]>mid) or ((b[i]=mid) and (a[i]<m2)) do inc(i);
    while (b[j]<mid) or ((b[j]=mid) and (a[j]>m2)) do dec(j);
    if i<=j then begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      t:=b[i];b[i]:=b[j];b[j]:=t;
      t:=c[i];c[i]:=c[j];c[j]:=t;
      inc(i);dec(j);
    end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
procedure qsort1(l,r:int64);
var i,j,mid,m2,t,m3:int64;
begin
  i:=l;j:=r;mid:=b[(l+r) >>1];m2:=a[(l+r)>>1];m3:=c[(l+r)>>1];
  repeat
    while (c[i]>m3) or ((c[i]=m3) and (b[i]>mid))
    or ((c[i]=m3) and (b[i]=mid) and (a[i]<m2)) do inc(i);
    while (c[j]<m3) or ((c[j]=m3) and (b[j]<mid))
    or ((c[j]=m3) and (b[j]=mid) and (a[j]>m2)) do dec(j);
    if i<=j then begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      t:=b[i];b[i]:=b[j];b[j]:=t;
      t:=c[i];c[i]:=c[j];c[j]:=t;
      inc(i);dec(j);
    end;
  until i>j;
  if l<j then qsort1(l,j);
  if i<r then qsort1(i,r);
end;
begin
  read(n,k);     kk:=k;
  for i:=1 to n do begin read(a[i],b[i]);c[i]:=b[i]-a[i];end;qsort(1,n);
  for i:=1 to n do
    if k-a[i]>=0 then begin inc(ans,b[i]);dec(k,a[i]);end;
  qsort1(1,n);a1:=0;   k:=kk;
  for i:=1 to n do
    if k-a[i]>=0 then begin inc(a1,b[i]);dec(k,a[i]);end;
    writeln(a1);
  if ans>a1 then writeln(ans)else writeln(a1);
end.

Score : 500 points

Problem Statement

Seisu-ya, a store specializing in non-negative integers, sells N non-negative integers. The i-th integer is A_i and has a utility of B_i.
There may be multiple equal integers with different utilities.

Takahashi will buy some integers in this store. He can buy a combination of integers whose bitwise OR is less than or equal to K. He wants the sum of utilities of purchased integers to be as large as possible.

Find the maximum possible sum of utilities of purchased integers.

Constraints

1 \leq N \leq 10^5

0 \leq K < 2^{30}

0 \leq A_i < 2^{30}(1\leq i\leq N)

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

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N K
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible sum of utilities of purchased integers.

Sample Input 1

3 5
3 3
4 4
2 5

Sample Output 1

8

Buy 2 and 3 to achieve the maximum possible total utility, 8.

Sample Input 2

3 6
3 3
4 4
2 5

Sample Output 2

9

Buy 2 and 4 to achieve the maximum possible total utility, 9.

Sample Input 3

7 14
10 5
7 4
11 4
9 8
3 6
6 2
8 9

Sample Output 3

32


var i,j,k,t,s,n,m:longint;
begin
	readln(n,m,k);
	for i:=0 to m do for j:=0 to n do if i*n+j*m=k+i*j*2 then
		begin write('Yes');exit;end;
	write('No');
end.

Score : 200 points

Problem Statement

We have a grid with N rows and M columns of squares. Initially, all the squares are white.

There is a button attached to each row and each column.
When a button attached to a row is pressed, the colors of all the squares in that row are inverted; that is, white squares become black and vice versa.
When a button attached to a column is pressed, the colors of all the squares in that column are inverted.

Takahashi can freely press the buttons any number of times. Determine whether he can have exactly K black squares in the grid.

Constraints

1 \leq N,M \leq 1000

0 \leq K \leq NM

Input

Input is given from Standard Input in the following format:

N M K

Output

If Takahashi can have exactly K black squares in the grid, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

Press the buttons in the order of the first row, the first column.

Sample Input 2

2 2 1

Sample Output 2

No

Sample Input 3

3 5 8

Sample Output 3

Yes

Press the buttons in the order of the first column, third column, second row, fifth column.

Sample Input 4

7 9 20

Sample Output 4

No


var n,m,k:longint;
    i,j:longint;
    x:longint;
begin
  read(n,m,k);
  for i:=0 to m do
  begin
    x:=i*n;
    for j:=0 to n do
    begin
      if x-(2*j*i)+j*m=k then
      begin
        writeln('Yes');
        exit;
      end;
    end;
  end;
  writeln('No');
end.


var i,j:longint;
    n,m,num2,num4:longint;
    ch:char;
    num:array['a'..'z']of longint;
    have:longint;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(ch);
      inc(num[ch]);
    end;
    readln;
  end;
  if (n mod 2=1)and(m mod 2=1) then num4:=((n-1)div 2) * ((m-1) div 2) else
  if (n mod 2=0)and(m mod 2=1) then num4:=(  n  div 2) * ((m-1) div 2) else
  if (n mod 2=1)and(m mod 2=0) then num4:=((n-1)div 2) * (  m   div 2) else
  if (n mod 2=0)and(m mod 2=0) then num4:=(  n  div 2) * (  m   div 2);
  for i:=97 to 122 do
  if (num[char(i)]>=4)and(num4>0) then
  begin
    have:=num[char(i)]div 4;
    if have>num4 then
    begin
      num4:=0;
      dec(num[char(i)],num4*4);
    end else
    begin
      dec(num4,have);
      dec(num[char(i)],have*4);
    end;
  end;
  if num4>0 then
  begin
    writeln('No');
    exit;
  end;
  if (n mod 2=1)and(m mod 2=1) then num2:=(n-1) div 2+(m-1) div 2 else
  if (n mod 2=0)and(m mod 2=1) then num2:=m div 2 else
  if (n mod 2=1)and(m mod 2=0) then num2:=n div 2 else
  if (n mod 2=0)and(m mod 2=0) then num2:=0;
  for i:=97 to 122 do
  if (num[char(i)]>=2)and(num2>0) then
  begin
    have:=num[char(i)] div 2;
    if have>num2 then num2:=0 else dec(num2,have);
  end;
  if num2>0 then
  begin
    writeln('No');
    exit;
  end;
  writeln('Yes');
end.

Score : 400 points

Problem Statement

We have an H-by-W matrix.
Let a_{ij} be the element at the i-th row from the top and j-th column from the left.
In this matrix, each a_{ij} is a lowercase English letter.

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A.
Here, he wants to satisfy the following condition:

Every row and column in A' can be read as a palindrome.

Determine whether he can create a matrix satisfying the condition.

Note

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes


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.

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


var
 n,m,j,i,d,x,y:longint;
begin
 readln(n,m,d);
 for i:=0 to n-1 do
  begin
   for j:=0 to m-1 do
    begin
     x:=(i+j+m) 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.


uses math;
var
  n,k,i,x,s:longint;
begin
  readln(n);
  readln(k);
  for i:=1 to n do
    begin
      readln(x);
      s:=s+2*min(k-x,x);
    end;
  writeln(s);
end.

Score : 200 points

Problem Statement

There are N balls in the xy-plane. The coordinates of the i-th of them is (x_i, i).
Thus, we have one ball on each of the N lines y = 1, y = 2, ..., y = N.

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the i-th type-A robot at coordinates (0, i), and the i-th type-B robot at coordinates (K, i).
Thus, now we have one type-A robot and one type-B robot on each of the N lines y = 1, y = 2, ..., y = N.

When activated, each type of robot will operate as follows.

When a type-A robot is activated at coordinates (0, a), it will move to the position of the ball on the line y = a, collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

When a type-B robot is activated at coordinates (K, b), it will move to the position of the ball on the line y = b, collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.

Constraints

1 \leq N \leq 100

1 \leq K \leq 100

0 < x_i < K

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
K
x_1 x_2 ... x_N

Outputs

Print the minimum possible total distance covered by robots.

Sample Input 1

1
10
2

Sample Output 1

4

There are just one ball, one type-A robot and one type-B robot.

If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4.

Similarly, if the type-B robot is used, the total distance covered will be 16.

Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 4.

Sample Input 2

2
9
3 6

Sample Output 2

12

The total distance covered will be minimized when the first ball is collected by the type-A robot, and the second ball by the type-B robot.

Sample Input 3

5
20
11 12 9 17 12

Sample Output 3

74


var a,b,c,d,e,f:longint;
    max:real;
    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)*100)/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*100)/(x+sugar)>max then
          begin
            max:=(sugar*100)/(x+sugar);
            ansx:=x;
            ansy:=sugar;
          end;
        end;
    end;
  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,ff,fff,ffff,i,j,k,l,ansa,ansb,tmp:Longint;
begin
	read(a,b,c,d,e,f);
	ansa:=100*a;
	while f>0 do begin
		ff:=f;
		j:=0;
		while ff>0 do begin
			tmp:=a*i+b*j;
			fff:=f;
			k:=0;
			while fff>0 do begin
				ffff:=fff;
				l:=0;
				while ffff>0 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;
					inc(l);
					dec(ffff,d);
				end;
				inc(k);
				dec(fff,c);
			end;
			inc(j);
			dec(ff,100*b);
		end;
		inc(i);
		dec(f,100*a);
	end;
	write(ansa);
	write(' ');
	writeln(ansb);
end.


var
	n,i,j,k:Longint;
	s:int64;
	a,f:array[1..300,1..300]of Longint;
begin
	read(n);
	for i:=1 to n do for j:=1 to n do read(a[i,j]);
	for i:=1 to n do for j:=1 to n do for k:=1 to n do begin
		if a[i,k]+a[k,j]<a[i,j]then begin
			writeln(-1);
			exit;
		end;
		if(a[i,k]+a[k,j]=a[i,j])and(i<>k)and(j<>k)then f[i,j]:=1;
	end;
	for i:=1 to n do for j:=i to n do inc(s,a[i,j]*(1-f[i,j]));
	writeln(s);
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: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.


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
                  begin
                    inc(t);
                    b[i,j]:=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];
      end;
  writeln(total);
end.


var
  n:string;
begin
  readln(n);
  if pos('9',n)>0 then writeln('Yes') else writeln('No');
end.

Score : 100 points

Problem Statement

It is September 9 in Japan now.

You are given a two-digit integer N. Answer the question: Is 9 contained in the decimal notation of N?

Constraints

10≤N≤99

Input

Input is given from Standard Input in the following format:

N

Output

If 9 is contained in the decimal notation of N, print Yes; if not, print No.

Sample Input 1

29

Sample Output 1

Yes

The one's digit of 29 is 9.

Sample Input 2

72

Sample Output 2

No

72 does not contain 9.

Sample Input 3

91

Sample Output 3

Yes


var
 n,a,b,i,s,ans:longint;
begin
 readln(n);
 readln(a);
 s:=1;
 for i:=2 to n do
  begin
   readln(b);
   if a=b then s:=(s+1) mod 2
   else begin ans:=ans+s;s:=1;end;
   a:=b;
  end;
  if odd(s mod 2) then inc(ans);
  writeln(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


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.


var
 a:array[1..100001] of longint;
 n,m,k,i,j:longint;
procedure swap(var a, b: longint);
var
  temp: longint;
begin
  temp := a;
  a := b;
  b := temp;
end;

procedure quicksort(l, r: longint);
var
  border, i: longint;
begin
  if l < r then begin
    swap(a[random(r - l + 1) + l], a[r]);
    border := l;
    for i := l to r - 1 do
      if (a[i] < a[r]) or ((a[i] = a[r]) and (random(2) = 1)) then begin
        swap(a[border], a[i]);
        inc(border);
      end;
    swap(a[border], a[r]);
    quicksort(l, border - 1);
    quicksort(border + 1, r);
  end;
end;

begin
 readln(n);
 for i:=1 to n do
  readln(a[i]);
 quicksort(1,n);
 i:=1;
 k:=0;
 while (i<=n) do
  begin
   j:=i;
   while (j<=n) and (a[j]=a[j+1]) do
    inc(j);
    inc(k,(j-i+1) mod 2);
    i:=j+1;
  end;
 writeln(k);
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);
  writeln(ans);
end.


var min,n,i,j,x,m,r,a1,b1,c1:longint;
	a:array[-5..205,-5..205] of longint;
	b:array[-5..205]of longint;
	f:array[-5..205]of boolean;
procedure main(s,pre,now:longint);
var i,j,k:longint;
begin
	if now=r then begin
		for i:=1 to r do if f[b[i]] then begin
			if s+a[pre,b[i]]<min then min:=s+a[pre,b[i]];
			break;
		end;
	end else
	for i:=1 to r do if f[b[i]] then begin
		f[b[i]]:=false;
		main(s+a[pre,b[i]],b[i],now+1);
		f[b[i]]:=true;
	end;
end;

begin
	fillchar(f,sizeof(f),true);
	readln(n,m,r);
	fillchar(a,sizeof(a),1);
	for i:=1 to n do a[i,i]:=0;
	//writeln(a[1,2]);
	for i:=1 to r do read(b[i]);
	for i:=1 to m do begin
		readln(a1,b1,c1);
		a[a1,b1]:=c1;
		a[b1,a1]:=c1;
	end;

	for x:=1 to n do for i:=1 to n do for j:=1 to n do
	if a[i,j]>a[i,x]+a[x,j] then a[i,j]:=a[i,x]+a[x,j];
	//for i:=1 to n do for j:=1 to n do writeln(i:5,j:5,a[i,j]:5);

	min:=maxlongint div 3*2;
	for i:=1 to r do begin
		f[b[i]]:=false;
		main(0,b[i],2);
		f[b[i]]:=true;
	end;
	writeln(min);
end.

Score : 400 points

Problem Statement

There are N towns in the State of Atcoder, connected by M bidirectional roads.

The i-th road connects Town A_i and B_i and has a length of C_i.

Joisino is visiting R towns in the state, r_1,r_2,..,r_R (not necessarily in this order).

She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.

If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?

Constraints

2≤N≤200

1≤M≤N×(N-1)/2

2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)

r_i≠r_j (i≠j)

1≤A_i,B_i≤N, A_i≠B_i

(A_i,B_i)≠(A_j,B_j),(A_i,B_i)≠(B_j,A_j) (i≠j)

1≤C_i≤100000

Every town can be reached from every town by road.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M R
r_1 ... r_R
A_1 B_1 C_1
:
A_M B_M C_M

Output

Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.

Sample Input 1

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

Sample Output 1

2

For example, if she visits the towns in the order of 1, 2, 3, the distance traveled will be 2, which is the minimum possible.

Sample Input 2

3 3 2
1 3
2 3 2
1 3 6
1 2 2

Sample Output 2

4

The shortest distance between Towns 1 and 3 is 4. Thus, whether she visits Town 1 or 3 first, the distance traveled will be 4.

Sample Input 3

4 6 3
2 3 4
1 2 4
2 3 3
4 3 1
1 4 1
4 2 2
3 1 6

Sample Output 3

3


var a:array[0..250,0..250]of int64;
    v:array[0..50]of boolean;
    i,j,k:longint;
    n,m,r:longint;
    x,y,z:longint;
    go:array[0..50]of longint;
    ans:int64;
procedure dfs(dep,last:longint;sum:int64);
var i:longint;
begin
  if sum>=ans then exit;
  if dep=r then
  begin
    if sum<ans then ans:=sum;
    exit;
  end;
  for i:=1 to r do
  if not v[i] then
  begin
    v[i]:=true;
    dfs(dep+1,i,sum+a[go[last],go[i]]);
    v[i]:=false;
  end;
end;
begin
  read(n,m,r);
  for i:=1 to n do
  for j:=1 to n do
  a[i,j]:=maxlongint;
  for i:=1 to n do
  begin
    a[0,i]:=0;
    a[i,0]:=0;
  end;
  for i:=1 to r do
  read(go[i]);
  for i:=1 to m do
  begin
    read(x,y,z);
    a[x,y]:=z;
    a[y,x]:=z;
  end;
  ans:=maxlongint;
  for k:=1 to n do
  for i:=1 to n do
  for j:=1 to n do
  if a[i,k]+a[j,k]<a[i,j] then a[i,j]:=a[i,k]+a[j,k];
  dfs(0,0,0);
  writeln(ans);
end.


var    s:ansistring; i:longint;
begin
   readln(s);
   for i:=1 to  length(s) do
        if odd(i) then write(s[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
  i:longint;
  s:string;
begin
  readln(s);
  for i:=1 to length(s) do if odd(i) then write(s[i]);
end.


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


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


var
f:string;
i:longint;
begin
read(f);
for i:=1 to length(f) do
    begin
        if i mod 2=1 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<>0 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-(1-ord(odd(length(s)))) do write(s[i*2+1]);
 writeln;
end.


var
 s:ansistring;
 i:longint;
begin
 readln(s);
 for i:=1 to length(s) div 2 do
  write(s[(i-1)*2+1]);
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]);
end.


var s:String;i:Longint;
begin
readln(s);
i:=1;
while i<=length(s) do begin
	write(s[i]);
	inc(i,2);
end;
end.


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


uses math ;
var n,t1,t2,i,ans,k:longint ;
       a:array[-1000000..1000000] 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]) ;
		 t2:=max(t2,k+1) ;
		 t1:=min(t1,k-1) ;
	 end;
	 for i:=t1 to t2 do
	 if a[i]>ans then ans:=a[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[-1000000..1000000] 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,i,j,ans,num,maxi:longint;
var a,b:array[0..100020] of longint;
begin
  read(n);for i:=1 to n do begin read(a[i]);inc(b[a[i]]);inc(b[a[i]+1]);
  inc(b[a[i]-1]);end;ans:=0;
  for i:=0 to 100000 do if b[i]>ans then ans:=b[i];
  writeln(ans);
end.


var a:array[1..100000] of longint ;
      n,i,ans,j:longint ;
procedure swap(n,m:longint) ;
var t:longint ;
begin
   a[n]:=t ;a[n]:=a[m];a[m]:=t ;
end;
procedure solve(k:longint) ;
begin
   if a[k+1]=k then swap(k,k+1)
	 else if a[k]=k+1 then swap(k,k-1)
	 else if a[k]=k-1 then swap(k,k+1) ;
end;
begin
   readln(n) ;
	 for i:=1 to n do read(a[i]) ;
	 i:=1;
	 while i<=n do
	 begin
	   if a[i]<>i then inc(i)
		 else
		 begin
		   solve(i) ;//挑选好的替换搭档。
			 inc(ans) ;
		   i:=i+2 ;
		 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 a:array[1..100000] of longint ;
      n,i,ans,j:longint ;
procedure swap(n,m:longint) ;
var t:longint ;
begin
   a[n]:=t ;a[n]:=a[m];a[m]:=t ;
end;
begin
   readln(n) ;
	 for i:=1 to n do read(a[i]) ;
	 for i:=1 to n do
	 begin
	   if a[i]<>i then continue ;
		 if i=1 then swap(1,2)
		 else if i=n then
		 begin
			 swap(n,n-1) ;
			 j:=a[n-1] ;
			 while (j>1) and (a[j]=j) do
			 begin
			   if a[j-1]=j then swap(j,j+1)
				 else swap(j,j-1);
				 inc(ans) ;
			 end;
		 end
		 else if a[i-1]=i then swap(i,i+1)
		 else swap(i,i-1) ;
		 inc(ans) ;
	 end;
	 writeln(ans) ;
end.


var
	n,a,i,s,m:Longint;
	v:array[0..100000]of Longint;
begin
	read(n);
	for i:=1 to n do begin
		read(a);
		inc(v[a]);
	end;
	for i:=1 to 99999 do begin
		m:=v[i-1]+v[i]+v[i+1];
		if s<m then s:=m;
	end;
	writeln(s);
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.


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);{233333333333333333}
end.


var cc : array [1..4] of int64 ;
    so : array [1..4] of real ;
    a,b,c,d,e,f,g : longint ;
    sum,count,h,i,o,j,k,l : int64 ;

procedure swap(var x,y : int64) ;
var z : int64;
begin
 z:=x;
 x:=y;
 y:=z;
end;
procedure swap2(var x,y : real) ;
var z : real;
begin
 z:=x;
 x:=y;
 y:=z;
end;

begin
 for a:=1 to 4 do
  begin
   read(cc[a]);
  end;
 so[1]:=1;
 so[2]:=1;
 so[3]:=1;
 so[4]:=2;
 cc[1]:=cc[1]*4;
 cc[2]:=cc[2]*2;
 for a:=1 to 3 do
  for b:=1 to 3 do
   if cc[b]*(1/so[b])>cc[b+1]*(1/so[b+1]) then
    begin
     swap(cc[b],cc[b+1]);
     swap2(so[b],so[b+1]);
    end;
//    for a:=1 to 4 do  write(cc[a]:5);
    readln(sum);
  while sum>0 do
   begin
    for a:=1 to 4 do
     begin
      if sum=0 then
       break
      else if so[a]<=sum then
       begin
//        writeln(cc[a]);
        if so[a]<>2 then
         begin
          count:=count+cc[a]*sum;
          sum:=0;
          break;
         end
        else
         begin
          count:=count+cc[a]*(sum div 2);
          sum:=sum-(sum div 2)*2;
         end;
       end;
     end;
   end;
  writeln(count);
end.

Score : 300 points

Problem Statement

You've come to your favorite store Infinitesco to buy some ice tea.

The store sells ice tea in bottles of different volumes at different costs.
Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen.
The store has an infinite supply of bottles of each type.

You want to buy exactly N liters of ice tea. How many yen do you have to spend?

Constraints

1 \leq Q, H, S, D \leq 10^8

1 \leq N \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q H S D
N

Output

Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.

Sample Input 1

20 30 70 90
3

Sample Output 1

150

Buy one 2-liter bottle and two 0.5-liter bottles. You'll get 3 liters for 90 + 30 + 30 = 150 yen.

Sample Input 2

10000 1000 100 10
1

Sample Output 2

100

Even though a 2-liter bottle costs just 10 yen, you need only 1 liter.
Thus, you have to buy a 1-liter bottle for 100 yen.

Sample Input 3

10 100 1000 10000
1

Sample Output 3

40

Now it's better to buy four 0.25-liter bottles for 10 + 10 + 10 + 10 = 40 yen.

Sample Input 4

12345678 87654321 12345678 87654321
123456789

Sample Output 4

1524157763907942


var a:array[0..100020] 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.

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 n:longint;
    i:longint;
    a,num:array[0..500000]of int64;
    max,max2,x:int64;
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
  begin
    read(a[i]);
    num[i]:=1;
  end;
  qs(1,n);
  for i:=1 to n do
  if a[i]=a[i-1] then inc(num[i],num[i-1]);
  for i:=1 to n do
  if num[i]>=4 then
  begin
    if x=0 then x:=a[i]*a[i];
  end else
  if num[i]>=2 then
  begin
    if max=0 then max:=a[i] else
    if (max2=0)and(a[i]<>max) then max2:=a[i];
  end;
  if x>max*max2 then writeln(x) else
  writeln(max*max2);
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;
	if tot=1 then
	begin
		writeln(b[1]*b[1]);
		halt;
	end;
	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 V1002;
 var
  a:array[0..100001] of longint;
  i,n:longint;
  s,s1:int64;
 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
   read(a[i]);
  readln;
  haha(1,n);
  i:=n;
  while i>=2 do
   if a[i]=a[i-1] then
    begin
     if s=0 then s:=a[i]
            else if s1=0 then s1:=a[i];
     dec(i,2);
    end
                  else dec(i,1);
  writeln(s*s1);
 end.


{$goto+}
uses math;
label 1,2,3,4,5,6,7,8,9,a;
var go:array[0..2000,0..2000]of boolean;
    mp:array[0..2000,0..2000]of 0..1;
    de:array[0..2000,0..2000]of longint;
    l,r,s:array[0..2000]of longint;
    top,h,w,ans,i,j,cnt:longint;
    t:char;
begin
  readln(h,w);
  i:=1;4:
  j:=1;6:
  read(t);
  if t='#' then goto 1;
  mp[i,j]:=0;
  goto 2;
  1:mp[i,j]:=1;
  2:j:=-not j;
  if j>w then goto 5;
  goto 6;
  5:readln;
  i:=-not i;
  if i>h then goto 3;
  goto 4;
  3:i:=1;
  7:j:=1;
  9:go[i,j]:=(mp[i,j]+mp[-not i,j]+mp[i,-not j]+mp[-not i,-not j]) and 1=0;
  if j>w-1 then
    goto a;
  j:=-not j;
  goto 9;
  a:if i>h-1 then goto 8;
  i:=-not i;
  goto 7;
  8:
  for i:=1 to h do
    for j:=1 to w do
      if not go[i,j] then
        de[i,j]:=0
      else
        de[i,j]:=de[i-1,j]+1;
  for i:=1 to h do
  begin
    top:=1;
    s[1]:=0;
    for j:=1 to w-1 do
    begin
      while (top<>0) and (de[i-1,s[top]]>=de[i-1,j]) do
        dec(top);
      l[j]:=s[top];
      inc(top);
      s[top]:=j;
    end;
    top:=1;
    s[top]:=w;
    for j:=w-1 downto 1 do
    begin
      while (top<>0) and (de[i-1,s[top]]>=de[i-1,j]) do
        dec(top);
      r[j]:=s[top];
      inc(top);
      s[top]:=j;
    end;
    for j:=1 to w-1 do
      ans:=max(ans,(de[i-1,j]+1)*(r[j]-l[j]));
  end;
  writeln(max(ans,max(h,w)));
end.

Score : 700 points

Problem Statement

We have a board with an H \times W grid.
Each square in the grid is painted in black or white. The square at the i-th row from the top and j-th column from the left is black if the j-th character in S_i is #, and white if that character is ..

Snuke can perform the following operation on the grid any number of times:

Select a row or column in the grid, and invert the color of all the squares in that row or column (that is, black squares become white and vice versa).

Then, Snuke draws a rectangle along grid lines. Here, all the squares contained in the rectangle must be painted in black.

Find the maximum possible area of Snuke's rectangle when the operation is performed optimally.

Constraints

2 \leq H \leq 2000

2 \leq W \leq 2000

|S_i| = W

S_i consists of # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the maximum possible area of Snuke's rectangle.

Sample Input 1

3 3
..#
##.
.#.

Sample Output 1

6

If the first row from the top and the third column from the left are inverted, a 2 \times 3 rectangle can be drawn, as shown below:

Sample Input 2

4 4
....
....
....
....

Sample Output 2

16

Sample Input 3

10 8
##...#.#
##...#.#
..###.#.
#.##.#.#
.#..#.#.
..##.#.#
##.#.#..
...#.#..
###.#.##
###..###

Sample Output 3

27


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);

    if (n mod 10)=(n div 100) then
      Writeln('Yes')
    else
      Writeln('No');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

You are given a three-digit positive integer N.

Determine whether N is a palindromic number.

Here, a palindromic number is an integer that reads the same backward as forward in decimal notation.

Constraints

100≤N≤999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a palindromic number, print Yes; otherwise, print No.

Sample Input 1

575

Sample Output 1

Yes

N=575 is also 575 when read backward, so it is a palindromic number. You should print Yes.

Sample Input 2

123

Sample Output 2

No

N=123 becomes 321 when read backward, so it is not a palindromic number. You should print No.

Sample Input 3

812

Sample Output 3

No


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 : ここにコードを記述してください }
    Read(n);
    Readln(m);

    Writeln((n-1)*(m-1));
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

In K-city, there are n streets running east-west, and m streets running north-south. Each street running east-west and each street running north-south cross each other. We will call the smallest area that is surrounded by four streets a block. How many blocks there are in K-city?

Constraints

2 ≤ n, m ≤ 100

Input

Input is given from Standard Input in the following format:

n m

Output

Print the number of blocks in K-city.

Sample Input 1

3 4

Sample Output 1

6

There are six blocks, as shown below:

Sample Input 2

2 2

Sample Output 2

1

There are one block, as shown below:


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


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 of Integer;
  s,s123,s1,s2,s3:string;
  tf:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(n);
    Write('ABC');
    Writeln(n);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

This contest, AtCoder Beginner Contest, is abbreviated as ABC.

When we refer to a specific round of ABC, a three-digit number is appended after ABC. For example, ABC680 is the 680th round of ABC.

What is the abbreviation for the N-th round of ABC? Write a program to output the answer.

Constraints

100 ≤ N ≤ 999

Input

Input is given from Standard Input in the following format:

N

Output

Print the abbreviation for the N-th round of ABC.

Sample Input 1

100

Sample Output 1

ABC100

The 100th round of ABC is ABC100.

Sample Input 2

425

Sample Output 2

ABC425

Sample Input 3

999

Sample Output 3

ABC999


var
 n,s:longint;
begin
 readln(n);
 s:=1;
 while s*2<=n do
  s:=s*2;
 writeln(s);
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


program ec12;
var
	head,last,next,side:array[1..400002] of longint;
	n,m,i,j,p,q,tot,x,y:longint;
procedure add(x,y:longint);
begin
	inc(tot);
	if head[x]=-1 then
	begin
		last[x]:=tot;
		next[tot]:=-1;
		side[tot]:=y;
		head[x]:=tot;
	end
	else
	begin
		next[last[x]]:=tot;
		next[tot]:=-1;
		side[tot]:=y;
		last[x]:=tot;
	end;
end;
begin
	readln(n,m);
	for i:=1 to n do
	head[i]:=-1;
	tot:=0;
	for i:=1 to m do
	begin
		readln(x,y);
		add(x,y);
	end;
	p:=head[1];
	while p>0 do
	begin
		q:=head[side[p]];
		while q>0 do
		begin
			if side[q]=n then
			begin
				writeln('POSSIBLE');
				halt;
			end;
			q:=next[q];
		end;
		p:=next[p];
	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.


type mang=array[0..1000005] of longint;
var h,a,x,y:mang;
    n,m,dem:longint;
    kt:boolean;
procedure nhap;
var i:longint;
begin
   read(n,m);
   for i:=1 to m do
      begin
      read(x[i],y[i]);
      inc(h[x[i]]);
      end;

   for i:=1 to n do
      h[i]:=h[i-1]+h[i];

   for i:=1 to m do
      begin
      a[h[x[i]]]:=y[i];
      dec(h[x[i]]);
      end;
   h[n+1]:=m;

end;

procedure DFS(i:Longint);
var j:longint;
begin
   if kt=true then exit;
   for j:=h[i]+1 to h[i+1] do
      begin
         dem:=dem+1;
         if (a[j]=n ) and (dem=2) then
            begin
            kt:=true;
            exit;
            end;
         if dem<2 then DFS(a[j]);
         dem:=dem-1;
      end;
end;

procedure xuly;
begin
   kt:=false;
   dem:=0;
   DFS(1);
end;

procedure xuat;
begin
   if kt=true then write('POSSIBLE')
      else write('IMPOSSIBLE');
end;

begin

   nhap;
   xuly;
   xuat;

end.


var x , y , i , n , m : longint;
	a , b : array[1..200000] of boolean;
	ok : boolean;

begin
	readln(n , m);
	for i := 1 to m do begin
		readln(x , y);
		if x = 1 then a[y] := true;
		if y = n then b[x] := true;
	end;
	ok := false;
	for i := 2 to n - 1 do
		ok := ok or (a[i] and b[i]);
	if ok then writeln('POSSIBLE') else writeln('IMPOSSIBLE');
end.


var N,K,i,G,A,M:Longint;
function gcd(a,b:Longint):Longint;
begin
if b=0 then gcd:=a else gcd:=gcd(b,a mod b);
end;
begin
read(N,K);
for i:=1 to N do begin
read(A);
G:=gcd(A,G);
if M<A then M:=A;
end;
if(M>=K)and(K mod G=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      ='';
        fo      ='';

var     f,g     :text;
        m,n     :longint;
        tro,dem,xd      :array[0..301] of longint;
        a       :array[0..301,0..301] of longint;
procedure       nhap;
var     i,j     :longint;
begin
        assign(f,fi); reset(f);
        readln(f,m,n);
        for i:=1 to m do
                for j:=1 to n do read(f,a[i,j]);
        close(f);
end;

function        min(i,j :longint):longint;
begin
        if i<j then exit(i) else exit(j);
end;

function        tinh(tt :longint):longint;
var     i,sl,vt :longint;
begin
        if tt=0 then exit(round(1e9));
        for i:=1 to n do dem[i]:=0;
        for i:=1 to m do
                begin
                        while (xd[a[i,tro[i]]]=0) and (tro[i]<=n) do inc(tro[i]);
                        if xd[a[i,tro[i]]]=1 then inc(dem[a[i,tro[i]]]);
                end;
        sl:=0;
        for i:=1 to n do if (xd[i]=1) then
                begin
                        if dem[i]>sl then
                                begin
                                        sl:=dem[i];
                                        vt:=i;
                                end;
                end;
        xd[vt]:=0;
        exit(min(sl,tinh(tt-1)));
end;
procedure       xuli;
var     i,kq    :longint;
begin
        for i:=1 to n do xd[i]:=1;
        kq:=tinh(n);
        assign(g,fo); rewrite(g);
        writeln(g,kq);
        close(g);
end;

begin
        nhap;
        xuli;
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.


const
        fi='';
        fo='';
var     n,m,res:integer;
        a:array[0..3000,0..3000]of integer;
        c,s:array[0..3000]of integer;
        cx:array[0..3000]of byte;
        g:text;

procedure solve;
var     i,j,mj:integer;
begin
        for i:=1 to m do c[i]:=1;
        res:=m;
        for i:=1 to n do
        begin
                for j:=1 to n do s[j]:=0;
                mj:=1;
                for j:=1 to m do
                begin
                        inc(s[a[j,c[j]]]);
                        if s[mj]<s[a[j,c[j]]] then mj:=a[j,c[j]];
                end;
                if s[mj]<res then res:=s[mj];
                cx[mj]:=1;
                for j:=1 to m do while (c[j]<=n) and (cx[a[j,c[j]]]=1) do inc(c[j]);
        end;
end;

procedure main;
var     i,j:integer;
begin
        assign(g,fi);
        reset(g);
        read(g,m,n);
        for i:=1 to m do for j:=1 to n do read(g,a[i,j]);
        close(g);
        assign(g,fo);
        rewrite(g);
        solve;
        writeln(g,res);
        close(g);
end;

BEGIN
        main;
END.


const
        fi='';
        fo='';
var     n,m,res:longint;
        a:array[1..300,1..300]of longint;
        c,s:array[1..300]of longint;
        cx:array[0..300]of byte;
        g:text;

procedure solve;
var     i,j,mj:longint;
begin
        for i:=1 to m do c[i]:=1;
        res:=m;
        for i:=1 to n do
        begin
                for j:=1 to n do s[j]:=0;
                mj:=1;
                for j:=1 to m do
                begin
                        inc(s[a[j,c[j]]]);
                        if s[mj]<s[a[j,c[j]]] then mj:=a[j,c[j]];
                end;
                if s[mj]<res then res:=s[mj];
                cx[mj]:=1;
                for j:=1 to m do
                begin
                        while cx[a[j,c[j]]]=1 do inc(c[j]);
                end;
        end;
end;

procedure main;
var     i,j:longint;
begin
        assign(g,fi);
        reset(g);
        read(g,m,n);
        for i:=1 to m do for j:=1 to n do read(g,a[i,j]);
        close(g);
        assign(g,fo);
        rewrite(g);
        solve;
        writeln(g,res);
        close(g);
end;

BEGIN
        main;
END.


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 n,m:longint;
begin
  readln(n,m);
  if (n mod 3=0)or(m mod 3=0)or((n+m) mod 3=0) then writeln('Possible')
  else writeln('Impossible');
end.

Score : 100 points

Problem Statement

Snuke is giving cookies to his three goats.

He has two cookie tins. One contains A cookies, and the other contains B cookies. He can thus give A cookies, B cookies or A+B cookies to his goats (he cannot open the tins).

Your task is to determine whether Snuke can give cookies to his three goats so that each of them can have the same number of cookies.

Constraints

1 \leq A,B \leq 100

Both A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If it is possible to give cookies so that each of the three goats can have the same number of cookies, print Possible; otherwise, print Impossible.

Sample Input 1

4 5

Sample Output 1

Possible

If Snuke gives nine cookies, each of the three goats can have three cookies.

Sample Input 2

1 1

Sample Output 2

Impossible

Since there are only two cookies, the three goats cannot have the same number of cookies no matter what Snuke gives to them.


program Project1;

{$APPTYPE CONSOLE}

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

    if (a mod 3=0)or(b mod 3=0)or((a+b)mod 3=0) then
      Writeln('Possible')
    else
      Writeln('Impossible');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  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 of Integer;
  s,s123,s1,s2,s3:string;
  tf:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(n);
    Readln(k);
    SetLength(l,n);
    for i := 0 to n - 1 do
      Read(l[i]);

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

    sum:=0;
    for i := n-1 downto n-k do
      sum:=sum+l[i];
    Writeln(sum);
    Readln;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
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


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 of Integer;
  s,s123,s1,s2,s3:string;
  tf:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(n);
    Readln(k);
    SetLength(l,n);
    for i := 0 to n - 1 do
      Read(l[i]);

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

    sum:=0;
    for i := n downto n-k do
      sum:=sum+l[i];

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


var
 a:array[0..200005] of longint;
 n,i,j,min: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;
function minn(x,y:longint):longint;
begin
 if x<y then exit(x)
  else exit(y);
end;
begin
 readln(n);
 min:=maxlongint;
 for i:=1 to n do read(a[i]);
 sort(1,n);
 min:=abs(a[2]-a[1]);
 for i:=2 to n-1 do
  begin
   min:=minn(abs(a[i]-a[i-1]),min);
   min:=minn(abs(a[i]-a[i+1]),min);
  end;
 min:=minn(a[n]-a[n-1],min);
 writeln(min);
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.


var
 n,i,min:longint;
 a:array[0..100005] of 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);
 min:=a[2]-a[1];
 for i:=2 to n do
  if (abs(a[i]-a[i-1])<min) then min:=abs(a[i]-a[i-1]);
 writeln(min);
end.


var
 n,i,min:longint;
 a:array[0..10000] of 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);
 min:=a[2]-a[1];
 for i:=2 to n do
  if (abs(a[i]-a[i-1])<min) then min:=abs(a[i]-a[i-1]);
 writeln(min);
end.


var
 n,i,min:longint;
 a:array[0..10000] of 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);
 min:=a[2]-a[1];
 for i:=2 to n do
  if (abs(a[i]-a[i-1])<min) then min:=abs(a[i]-a[i-1]);
 write(min);
end.


program ec12;
var
	j:int64;
	s:array[0..200001] 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
	s:array[0..100001] of int64;
	n,m,i,j,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:=s[n];
	for i:=1 to n do
	begin
		if abs(s[n]-s[i]-s[i])<j then
		j:=(s[n]-s[i]-s[i]);
	end;
	writeln(j);
end.


program ec12;
var
	s:array[0..100001] of longint;
	n,m,i,j,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:=s[n];
	for i:=1 to n do
	begin
		if abs(s[n]-s[i]-s[i])<j then
		j:=(s[n]-s[i]-s[i]);
	end;
	writeln(j);
end.