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

Score : 300 points

Problem Statement

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

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

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

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

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

S

Output

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

Sample Input 1

BBBWW

Sample Output 1

1

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

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

Sample Input 2

WWWWWW

Sample Output 2

0

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

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


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


var a,b,lens:longint; s:ansistring;

begin
   readln(s);
   lens:=length(s);
   a:=pos('BB',s);
   b:=pos('WW',s);
   while a<>0 do begin
      delete(s,a,1);
      a:=pos('BB',s);
   end;
   while b<>0 do begin
      delete(s,b,1);
      b:=pos('WW',s);
   end;
   lens:=length(s);
   writeln (lens-1);
end.


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


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


program task1016;
var
    a,b,c : ansistring;
    x,y,i,t : longint;
    sum : int64;
begin
    read(a);
    sum := 0;
    b := a;
    c := a;
    delete(b,pos(' ',a),length(a)-pos(' ',a)+1);
    val(b,x,i);
    delete(a,1,pos(' ',a)+2);
    val(a,y,t);
    if c[pos(' ',c) + 1] = '+' then
        sum := x + y
    else
        sum := x - y;
    writeln(sum);
end.


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


var
	N,x,w,i:Longint;
begin
	read(N,x);
	w:=2*N-1;
	if x mod w>1 then begin
		writeln('Yes');
		for i:=1 to w do writeln(w-(N-x-i+w*2)mod w);
	end else writeln('No');
end.

Score : 400 points

Problem Statement

We have a pyramid with N steps, built with blocks.
The steps are numbered 1 through N from top to bottom.
For each 1≤i≤N, step i consists of 2i-1 blocks aligned horizontally.
The pyramid is built so that the blocks at the centers of the steps are aligned vertically.

A pyramid with N=4 steps

Snuke wrote a permutation of (1, 2, ..., 2N-1) into the blocks of step N.
Then, he wrote integers into all remaining blocks, under the following rule:

The integer written into a block b must be equal to the median of the three integers written into the three blocks directly under b, or to the lower left or lower right of b.

Writing integers into the blocks

Afterwards, he erased all integers written into the blocks.
Now, he only remembers that the integer written into the block of step 1 was x.

Construct a permutation of (1, 2, ..., 2N-1) that could have been written into the blocks of step N, or declare that Snuke's memory is incorrect and such a permutation does not exist.

Constraints

2≤N≤10^5

1≤x≤2N-1

Input

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

N x

Output

If no permutation of (1, 2, ..., 2N-1) could have been written into the blocks of step N, print No.

Otherwise, print Yes in the first line, then print 2N-1 lines in addition.

The i-th of these 2N-1 lines should contain the i-th element of a possible permutation.

Sample Input 1

4 4

Sample Output 1

Yes
1
6
3
7
4
5
2

This case corresponds to the figure in the problem statement.

Sample Input 2

2 1

Sample Output 2

No

No matter what permutation was written into the blocks of step N, the integer written into the block of step 1 would be 2.


var
	i,l,r,mid,n:longint;
	a:array[1..1000001] of longint;

function check(k:longint):longint;
var	i:longint;
begin
	if ((a[n-1]<=k)and(a[n]<=k))or((a[n+1]<=k)and(a[n]<=k)) then exit(0);
	if ((a[n-1]>k)and(a[n]>k))or((a[n+1]>k)and(a[n]>k)) then exit(1);
	for i:=1 to n-1 do begin
		if ((a[n-i-1]<=k)and(a[n-i]<=k))or((a[n+i+1]<=k)and(a[n+i]<=k)) then exit(0);
		if ((a[n-i-1]>k)and(a[n-i]>k))or((a[n+i+1]>k)and(a[n+i]>k)) then exit(1);
	end;
	if a[1]>k then exit(1) else exit(0);
end;

begin
	readln(n);
	for i:=1 to 2*n-1 do read(a[i]);
	l:=1;
	r:=2*n-1;
	while l<r-1 do begin
		mid:=(l+r) shr 1;
		//writeln(l,' ',r);
		if check(mid)=0 then begin
			//writeln(mid);
			r:=mid
		end
		else l:=mid;
	end;
	writeln(r);
end.

Score : 1300 points

Problem Statement

We have a pyramid with N steps, built with blocks.
The steps are numbered 1 through N from top to bottom.
For each 1≤i≤N, step i consists of 2i-1 blocks aligned horizontally.
The pyramid is built so that the blocks at the centers of the steps are aligned vertically.

A pyramid with N=4 steps

Snuke wrote a permutation of (1, 2, ..., 2N-1) into the blocks of step N.
Then, he wrote integers into all remaining blocks, under the following rule:

The integer written into a block b must be equal to the median of the three integers written into the three blocks directly under b, or to the lower left or lower right of b.

Writing integers into the blocks

Afterwards, he erased all integers written into the blocks.
Now, he only remembers that the permutation written into the blocks of step N was (a_1, a_2, ..., a_{2N-1}).

Find the integer written into the block of step 1.

Constraints

2≤N≤10^5

(a_1, a_2, ..., a_{2N-1}) is a permutation of (1, 2, ..., 2N-1).

Input

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

N
a_1 a_2 ... a_{2N-1}

Output

Print the integer written into the block of step 1.

Sample Input 1

4
1 6 3 7 4 5 2

Sample Output 1

4

This case corresponds to the figure in the problem statement.

Sample Input 2

2
1 2 3

Sample Output 2

2


var
 n,m,i,sum,j,p:longint;
 a,b:array[0..10001] of longint;
 f:array[0..10001,0..10001] of boolean;
begin
 readln(n,m);
 for i:=1 to m do
  begin
   readln(a[i],b[i]);
   f[a[i],b[i]]:=true;
   inc(sum);
  end;
 i:=1;
 while i<=m+p do
  begin
   for j:=1 to n do
    if f[b[i],j] then
     if f[j,a[i]]=false then
      begin
       inc(p);f[j,a[i]]:=true; inc(sum);
       a[m+p]:=j; b[m+p]:=a[i];
      end;inc(i);
  end;
 writeln(sum);
end.

Score : 1700 points

Problem Statement

We have a grid with N rows and N columns.
The cell at the i-th row and j-th column is denoted (i, j).

Initially, M of the cells are painted black, and all other cells are white.
Specifically, the cells (a_1, b_1), (a_2, b_2), ..., (a_M, b_M) are painted black.

Snuke will try to paint as many white cells black as possible, according to the following rule:

If two cells (x, y) and (y, z) are both black and a cell (z, x) is white for integers 1≤x,y,z≤N, paint the cell (z, x) black.

Find the number of black cells when no more white cells can be painted black.

Constraints

1≤N≤10^5

1≤M≤10^5

1≤a_i,b_i≤N

All pairs (a_i, b_i) are distinct.

Input

The 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

Print the number of black cells when no more white cells can be painted black.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

3

It is possible to paint one white cell black, as follows:

Since cells (1, 2) and (2, 3) are both black and a cell (3, 1) is white, paint the cell (3, 1) black.

Sample Input 2

2 2
1 1
1 2

Sample Output 2

4

It is possible to paint two white cells black, as follows:

Since cells (1, 1) and (1, 2) are both black and a cell (2, 1) is white, paint the cell (2, 1) black.

Since cells (2, 1) and (1, 2) are both black and a cell (2, 2) is white, paint the cell (2, 2) black.

Sample Input 3

4 3
1 2
1 3
4 4

Sample Output 3

3

No white cells can be painted black.


Var s,x:ansistring;
    n,i:longint;
Begin
        readln(s);
        for i:=1 to length(s) do //遍历字符串s
        Begin
                if s[i] in ['C','F'] then //当s串的第i个位置是C或F就存入x串。
                Begin
                        x:=x+s[i];
                end;

                if pos('CF',x)<>0 then //判断是否出现过CF，如果有，就输出Yes并跳出
                Begin
                        write('Yes');
                        halt;
                end;
        end;
        write('No'); //因为如果有前面就跳出程序了，所以如果执行到这一步肯定是找不到
end.

Score : 100 points

Problem Statement

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

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

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

Constraints

2 ≤ |s| ≤ 100

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

Input

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

s

Output

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

Sample Input 1

CODEFESTIVAL

Sample Output 1

Yes

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

Sample Input 2

FESTIVALCODE

Sample Output 2

No

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

Sample Input 3

CF

Sample Output 3

Yes

It is also possible not to delete any characters.

Sample Input 4

FCF

Sample Output 4

Yes

CF is obtained by deleting the first character.


program hrn;
 var
        s:ansistring;
        i,l,s1,s2:longint;
begin
        readln(s);
        l:=length(s);
        s2:=maxlongint;
        for i:=1 to l do
        begin
                if (s[i]='F') then s1:=i;
        end;
        for i:=l downto 1 do
        begin
                if (s[i]='C') then s2:=i;
        end;
        if s2<s1 then
        begin
                writeln('Yes');
        end
        else
        begin
                writeln('No');
        end;
end.


program hrn;
 var
  s:string;
  i,l,sc,sf:longint;
   //l表示字符串长度; sc,sf分别表示第一个C的坐标和最后一个F的坐标.
 begin
  read(s);
  l:=length(s);sc:=maxlongint; //sc一定要赋值为maxlongint！
  for i:=1 to l do if s[i]='F'then sf:=i; //寻找最后一个F
  for i:=l downto 1 do if s[i]='C'then sc:=i;//寻找第一个C
  if sc<sf then writeln('Yes')
           else writeln('No');  //注意输出格式
 end.


var
  s:ansistring;
begin
  readln(s);
  if pos('C',s)=0 then writeln('No')else
  begin
    delete(s,1,pos('C',s));
    if pos('F',s)=0 then writeln('No')else writeln('Yes');
  end;
end.


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


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

Score : 300 points

Problem Statement

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

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

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

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

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

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

Constraints

1≦N≦10^5

N=|s|

Each character in s is g or p.

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

Input

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

s

Output

Print the AtCoDeer's maximum possible score.

Sample Input 1

gpg

Sample Output 1

0

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

Sample Input 2

ggppgggpgg

Sample Output 2

2

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


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


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


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


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


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

Score : 300 points

Problem Statement

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

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

Constraints

1≦N≦1000

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

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

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

Input

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

N
T_1 A_1
T_2 A_2
:
T_N A_N

Output

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

Sample Input 1

3
2 3
1 1
3 2

Sample Output 1

10

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

Sample Input 2

4
1 1
1 1
1 5
1 100

Sample Output 2

101

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

Sample Input 3

5
3 10
48 17
31 199
231 23
3 2

Sample Output 3

6930


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


uses math;
var n,m,i,j:longint;
    a,t:array[0..1010]of int64;
    aa,tt,ka,kt,l,r,mid,ans:int64;

begin
readln(n);
for i:=1 to n do readln(a[i],t[i]);
l:=0;r:=1100000000000000000 div (a[n]+t[n]);
ans:=r+1;
while l<=r do begin
  mid:=(l+r)div 2;
  aa:=mid*a[n];tt:=mid*t[n];
  for j:=n-1 downto 1 do begin
    ka:=(aa div a[j]);
    kt:=tt div t[j];
    if ka<kt then begin aa:=(aa div a[j])*a[j];tt:=(aa div a[j])*t[j];end
             else begin tt:=(tt div t[j])*t[j];aa:=(tt div t[j])*a[j];end;
    if (aa=0)or(tt=0) then break;
  end;
  if (aa=0)or(tt=0) then l:=mid+1 else begin ans:=min(ans,mid);r:=mid-1;end;
end;
writeln(ans*(a[n]+t[n]));
end.


var s,correct:string;cnt,i:integer;
begin
cnt:=0;
readln(s);
correct:='CODEFESTIVAL2016';
for i:=1 to 16 do begin
if(s[i]<>correct[i])then cnt:=cnt+1;
end;
writeln(cnt);
end.

Score : 100 points

Problem Statement

CODE FESTIVAL 2016 is going to be held. For the occasion, Mr. Takahashi decided to make a signboard.

He intended to write CODEFESTIVAL2016 on it, but he mistakenly wrote a different string S. Fortunately, the string he wrote was the correct length.

So Mr. Takahashi decided to perform an operation that replaces a certain character with another in the minimum number of iterations, changing the string to CODEFESTIVAL2016.

Find the minimum number of iterations for the rewrite operation.

Constraints

S is 16 characters long.

S consists of uppercase and lowercase alphabet letters and numerals.

Input

Inputs are provided from Standard Input in the following form.

S

Output

Output an integer representing the minimum number of iterations needed for the rewrite operation.

Sample Input 1

C0DEFESTIVAL2O16

Sample Output 1

2

The second character 0 must be changed to O and the 14th character O changed to 0.

Sample Input 2

FESTIVAL2016CODE

Sample Output 2

16


//{$R-,Q-,S-,I-}
{$OPTIMIZATION LEVEL2}
{$INLINE ON}

uses math;

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

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

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

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

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

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

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

procedure dfs(p,x,i:longint);
var
        j:char;
begin
        if x=length(s[i])+1 then exit;
        for j:='`' to 'z' do
        begin
                inc(cnt[i,j,s[i,x]],c[p,j]);
        end;
        dfs(a[p][s[i,x]],x+1,i);
end;

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

                res:=0;
                for i:=1 to 26 do
                for j:=i+1 to 27 do
                res:=res+cnt[x,al[i],al[j]];

                writeln(res+1);
        end;
end;

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

Score : 1100 points

Problem Statement

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

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

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

Constraints

1 ≦ N,Q ≦ 100000

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

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

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

Every S_i is unique.

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

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

Input

Inputs are provided from standard inputs in the following form.

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

Output

Output Q lines.

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

Sample Input 1

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

Sample Output 1

1
2
5
4
2

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

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

Sample Input 2

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

Sample Output 2

4
8
2
3
4
7


var
    a,b,c,i,ans:longint;
    a1:array[1..100000]of longint;//定义数组变量
begin
    read(a,b,c);
    for i:=1 to a do
    begin
        read(a1[i]);
        if(a1[i]<b)or(a1[i]>=c)then inc(ans);//暴力判断
    end;
    writeln(ans);
end.

Score : 66 points

Problem Statement

Summer vacation ended at last and the second semester has begun.
You, a Kyoto University student, came to university and heard a rumor that somebody will barricade the entrance of your classroom.
The barricade will be built just before the start of the A-th class and removed by Kyoto University students just before the start of the B-th class.
All the classes conducted when the barricade is blocking the entrance will be cancelled and you will not be able to attend them.
Today you take N classes and class i is conducted in the t_i-th period.
You take at most one class in each period.
Find the number of classes you can attend.

Constraints

1 \leq N \leq 1000

1 \leq A < B \leq 10^9

1 \leq t_i \leq 10^9

All t_i values are distinct.

Input

N, A and B are given on the first line and t_i is given on the (i+1)-th line.

N A B
t1
:
tN

Output

Print the number of classes you can attend.

Sample Input 1

5 5 9
4
3
6
9
1

Sample Output 1

4

You can not only attend the third class.

Sample Input 2

5 4 9
5
6
7
8
9

Sample Output 2

1

You can only attend the fifth class.

Sample Input 3

4 3 6
9
6
8
1

Sample Output 3

4

You can attend all the classes.

Sample Input 4

2 1 2
1
2

Sample Output 4

1

You can not attend the first class, but can attend the second.


var
i,n,x,y:Longint;
begin
read(n);
for i:=1 to n do begin
read(x,y);
if(x and 1)=1 then writeln(127*(x-1)+y)else writeln(127*x-y);
end;
end.

Score : 100 points

Problem Statement

A professor invented Cookie Breeding Machine for his students who like cookies very much.

When one cookie with the taste of x is put into the machine and a non-negative integer y less than or equal to 127 is input on the machine,
it consumes the cookie and generates two cookies with the taste of y and (x XOR y).

Here, XOR represents Bitwise Exclusive OR.

At first, there is only one cookie and the taste of it is D .

Find the maximum value of the sum of the taste of the exactly N cookies generated after the following operation is conducted N-1 times.

Put one of the cookies into the machine.

Input a non-negative integer less than or equal to 127 on the machine.

Constraints

1 \leq T \leq 1000

1 \leq N_t \leq 1000 (1 \leq t \leq T)

1 \leq D_t \leq 127 (1 \leq t \leq T)

Input

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

T
N_1 D_1
:
N_T D_T

The input consists of multiple test cases. An Integer T that represents the number of test cases is given on line 1.

Each test case is given on the next T lines.

In the t-th test case ( 1 \leq t \leq T ),
N_t that represents the number of cookies generated through the operations and D_t that represents the taste of the initial cookie are given separated by space.

Output

For each test case, print the maximum value of the sum of the taste of the N cookies generated through the operations on one line.

Sample Input 1

3
3 1
4 108
1 10

Sample Output 1

255
400
10

On the 1st test case, if the machine is used as follows, three cookies with the taste of 61, 95 and 99 are generated.
Since the sum of these values is maximum among all possible ways, the answer is 255.

Put the cookie with the taste of 1 and input an integer 60 on the machine, lose the cookie and get two cookies with the taste of 60 and 61.

Put the cookie with the taste of 60 and input an integer 99 on the machine, lose the cookie and get two cookies with the taste of 99 and 95.

On the 3rd test case, the machine may not be used.


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

Score : 300 points

Problem Statement

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

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

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

Find the eventual length of X.

Constraints

2 ≦ |X| ≦ 200,000

The length of X is even.

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

Partial Scores

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

Input

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

X

Output

Print the eventual length of X.

Sample Input 1

TSTTSS

Sample Output 1

4

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

Sample Input 2

SSTTST

Sample Output 2

0

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

Sample Input 3

TSSTTTSS

Sample Output 3

4

X will become: TSSTTTSS ⇒ TSTTSS ⇒ TTSS.


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


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


var
s:string;
begin
read(s);
writeln(copy(s,1,4),' ',copy(s,5,8));
end.

Score : 100 points

Problem Statement

This contest is CODE FESTIVAL.
However, Mr. Takahashi always writes it CODEFESTIVAL, omitting the single space between CODE and FESTIVAL.

So he has decided to make a program that puts the single space he omitted.

You are given a string s with 12 letters.
Output the string putting a single space between the first 4 letters and last 8 letters in the string s.

Constraints

s contains exactly 12 letters.

All letters in s are uppercase English letters.

Input

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

s

Output

Print the string putting a single space between the first 4 letters and last 8 letters in the string s.
Put a line break at the end.

Sample Input 1

CODEFESTIVAL

Sample Output 1

CODE FESTIVAL

Putting a single space between the first 4 letters and last 8 letters in CODEFESTIVAL makes it CODE FESTIVAL.

Sample Input 2

POSTGRADUATE

Sample Output 2

POST GRADUATE

Sample Input 3

ABCDEFGHIJKL

Sample Output 3

ABCD EFGHIJKL


var a:array[1..100000] of integer;n,i,cnt:integer;
begin
readln(n);
cnt:=0;
for i:=1 to n do begin
read(a[i]);
end;
for i:=1 to n do begin
if ((i=a[a[i]]) and (a[i]>i)) then cnt:=cnt+1;
end;
writeln(cnt);
end.

Score : 200 points

Problem Statement

There are N rabbits, numbered 1 through N.

The i-th (1≤i≤N) rabbit likes rabbit a_i.
Note that no rabbit can like itself, that is, a_i≠i.

For a pair of rabbits i and j (i＜j), we call the pair (i，j) a friendly pair if the following condition is met.

Rabbit i likes rabbit j and rabbit j likes rabbit i.

Calculate the number of the friendly pairs.

Constraints

2≤N≤10^5

1≤a_i≤N

a_i≠i

Input

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

N
a_1 a_2 ... a_N

Output

Print the number of the friendly pairs.

Sample Input 1

4
2 1 4 3

Sample Output 1

2

There are two friendly pairs: (1，2) and (3，4).

Sample Input 2

3
2 3 1

Sample Output 2

0

There are no friendly pairs.

Sample Input 3

5
5 5 5 5 1

Sample Output 3

1

There is one friendly pair: (1，5).


var
  ans, i, n, t: integer;
  a: array[1..100005] of integer;
begin
  readln(n);
  for i := 1 to n do
  begin
    read(t);
    if a[t] = i then ans := ans + 1;
    a[i] := t;
  end;
  writeln(ans);
end.


var a,b,h:longint;
begin
readln(a,b,h);
writeln((a+b)*h/2);
end.

Score : 100 points

Problem Statement

You are given a trapezoid. The lengths of its upper base, lower base, and height are a, b, and h, respectively.

An example of a trapezoid

Find the area of this trapezoid.

Constraints

1≦a≦100

1≦b≦100

1≦h≦100

All input values are integers.

h is even.

Input

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

a
b
h

Output

Print the area of the given trapezoid. It is guaranteed that the area is an integer.

Sample Input 1

3
4
2

Sample Output 1

7

When the lengths of the upper base, lower base, and height are 3, 4, and 2, respectively, the area of the trapezoid is (3+4)×2/2 = 7.

Sample Input 2

4
4
4

Sample Output 2

16

In this case, a parallelogram is given, which is also a trapezoid.


program Project1;

{$APPTYPE CONSOLE}

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

    Writeln((a+b)*h div 2);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


program abc045a;
var a,b,h:integer;
begin
	readLn(a,b,h);
	writeln((a+b)*h/2)
end.


program abc045a;
var a,b,h:integer;
begin
	readLn(a);
	readLn(b);
	readLn(h);
	writeln((a+b)*h/2)
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes;
var
  sa,sb,sc:string;
  turn,temp:char;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(sa);
    Readln(sb);
    Readln(sc);

    turn:='A';
    while True do begin
      case turn of
        'A':begin
          if (sa='') then
            Break;
          temp:=sa[1];
          Delete(sa, 1, 1);
        end;
        'B':begin
          if (sb='') then
            Break;
          temp:=sb[1];
          Delete(sb, 1, 1);
        end;
        'C':begin
          if (sc='') then
            Break;
          temp:=sc[1];
          Delete(sc, 1, 1);
        end;
      end;

      case temp of
        'a':turn:='A';
        'b':turn:='B';
        'c':turn:='C';
      end;
    end;
    Writeln(turn);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

Alice, Bob and Charlie are playing Card Game for Three, as below:

At first, each of the three players has a deck consisting of some number of cards. Each card has a letter a, b or c written on it. The orders of the cards in the decks cannot be rearranged.

The players take turns. Alice goes first.

If the current player's deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says a, Alice takes the next turn.)

If the current player's deck is empty, the game ends and the current player wins the game.

You are given the initial decks of the players.
More specifically, you are given three strings S_A, S_B and S_C. The i-th (1≦i≦|S_A|) letter in S_A is the letter on the i-th card in Alice's initial deck. S_B and S_C describes Bob's and Charlie's initial decks in the same way.

Determine the winner of the game.

Constraints

1≦|S_A|≦100

1≦|S_B|≦100

1≦|S_C|≦100

Each letter in S_A, S_B, S_C is a, b or c.

Input

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

S_A
S_B
S_C

Output

If Alice will win, print A. If Bob will win, print B. If Charlie will win, print C.

Sample Input 1

aca
accc
ca

Sample Output 1

A

The game will progress as below:

Alice discards the top card in her deck, a. Alice takes the next turn.

Alice discards the top card in her deck, c. Charlie takes the next turn.

Charlie discards the top card in his deck, c. Charlie takes the next turn.

Charlie discards the top card in his deck, a. Alice takes the next turn.

Alice discards the top card in her deck, a. Alice takes the next turn.

Alice's deck is empty. The game ends and Alice wins the game.

Sample Input 2

abcb
aacb
bccc

Sample Output 2

C


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

Score : 400 points

Problem Statement

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

Snuke can perform the following two actions:

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

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

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

Constraints

2≤N≤2,000

a_i are integers.

1≤a_i≤10^9

x is an integer.

1≤x≤10^9

Input

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

N x
a_1 a_2 ... a_N

Output

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

Sample Input 1

2 10
1 100

Sample Output 1

12

Snuke can act as follows:

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

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

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

Sample Input 2

3 10
100 1 100

Sample Output 2

23

Snuke can act as follows:

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

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

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

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

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

Sample Input 3

4 10
1 2 3 4

Sample Output 3

10

Snuke can act as follows:

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

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

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

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


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

Score : 700 points

Problem Statement

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

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

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

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

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

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

Constraints

3≤H,W≤500

a_{ij} is # or ..

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

At least one of a_{ij} is #.

Input

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

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

Output

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

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

The following 1 line should be empty.

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

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

Sample Input 1

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

Sample Output 1

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

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

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

Sample Input 2

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

Sample Output 2

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

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

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


var
  a, f, h, tmp: array [0..100000] of longint;
  u, v: array [1..200000] of longint;
  n, m, i, x, y, s, rt, re, cnt: longint;
procedure swap(var x, y: longint); inline;
var
  t: longint;
begin
  t := x;
  x := y;
  y := t;
end;
function find(x: longint): longint;
begin
  if x <> f[x] then f[x] := find(f[x]);
  exit(f[x]);
end;
procedure add(x, y: longint); inline;
begin
  inc(cnt);
  u[cnt] := h[x];
  v[cnt] := y;
  h[x] := cnt;
end;
procedure dfs(x, s: longint);
var
  i: longint;
begin
  a[x] := s;
  i := h[x];
  while i > 0 do
  begin
    if v[i] <> f[x] then
    begin
      f[v[i]] := x;
      dfs(v[i], -s);
      inc(a[x], a[v[i]]);
    end;
    i := u[i];
  end;
end;
procedure quicksort(l, r: longint);
var
  i, j, m: longint;
begin
  i := l;
  j := r;
  m := tmp[(l + r) shr 1];
  repeat
    while tmp[i] < m do inc(i);
    while tmp[j] > m do dec(j);
    if i <= j then
    begin
      swap(tmp[i], tmp[j]);
      inc(i);
      dec(j);
    end;
  until i > j;
  if l < j then quicksort(l, j);
  if i < r then quicksort(i, r);
end;
begin
  read(n, m);
  for i := 1 to n do f[i] := i;
  cnt := 0;
  rt := 1;
  re := 1;
  for i := 1 to m do
  begin
    read(x, y);
    if find(x) <> find(y) then
    begin
      f[find(x)] := find(y);
      add(x, y);
      add(y, x);
    end
    else
    begin
      rt := x;
      re := y;
    end;
  end;
  f[rt] := 0;
  dfs(rt, 1);
  if n = m then
  begin
    cnt := 0;
    i := re;
    while i > 0 do
    begin
      inc(cnt);
      tmp[cnt] := a[i];
      i := f[i];
    end;
    if odd(cnt) then
    begin
      if odd(a[rt]) then
      begin
        writeln(-1);
        halt;
      end;
      i := re;
      while i > 0 do
      begin
        dec(a[i], a[rt] div 2);
        i := f[i];
      end;
    end
    else
    begin
      if a[rt] <> 0 then
      begin
        writeln('-1');
        halt;
      end;
      quicksort(1, cnt);
      i := re;
      while i > 0 do
      begin
        dec(a[i], tmp[cnt shr 1]);
        i := f[i];
      end;
    end;
  end
  else
    if a[rt] <> 0 then
    begin
      writeln(-1);
      halt;
    end;
  s := 0;
  for i := 1 to n do inc(s, abs(a[i]));
  writeln(s);
end.

Score : 2200 points

Problem Statement

You are given an undirected graph with N vertices and M edges.
Here, N-1≤M≤N holds and the graph is connected.
There are no self-loops or multiple edges in this graph.

The vertices are numbered 1 through N, and the edges are numbered 1 through M.
Edge i connects vertices a_i and b_i.

The color of each vertex can be either white or black.
Initially, all the vertices are white.
Snuke is trying to turn all the vertices black by performing the following operation some number of times:

Select a pair of adjacent vertices with the same color, and invert the colors of those vertices. That is, if the vertices are both white, then turn them black, and vice versa.

Determine if it is possible to turn all the vertices black. If the answer is positive, find the minimum number of times the operation needs to be performed in order to achieve the objective.

Constraints

2≤N≤10^5

N-1≤M≤N

1≤a_i,b_i≤N

There are no self-loops or multiple edges in the given graph.

The given graph is connected.

Partial Score

In the test set worth 1500 points, M=N-1.

Input

The 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 turn all the vertices black, print the minimum number of times the operation needs to be performed in order to achieve the objective.
Otherwise, print -1 instead.

Sample Input 1

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

Sample Output 1

5

For example, it is possible to perform the operations as shown in the following diagram:

Sample Input 2

3 2
1 2
2 3

Sample Output 2

-1

It is not possible to turn all the vertices black.

Sample Input 3

4 4
1 2
2 3
3 4
4 1

Sample Output 3

2

This case is not included in the test set for the partial score.

Sample Input 4

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

Sample Output 4

7

This case is not included in the test set for the partial score.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes;
var
  i,j,min:Integer;
  w:string;
  sw:Char;
  beau:Boolean;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(w);

    for i := 1 to Length(w) do begin
      min := i;
      for j := i+1 to Length(w) do begin
        if w[j] < w[min] then
        min := j;
      end;
      sw:=w[i];
      w[i]:=w[min];
      w[min]:=sw;
    end;

    beau:=True;
    if Length(w) mod 2 = 1 then
      beau:=false
    else begin
      i:=1;
      while i<Length(w) do
        if w[i]=w[i+1] then
          i:=i+2
        else begin
          beau:=false;
          Break;
        end;
    end;

    if beau then
      Writeln('YES')
    else
      Writeln('NO');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

Let w be a string consisting of lowercase letters.
We will call w beautiful if the following condition is satisfied:

Each lowercase letter of the English alphabet occurs even number of times in w.

You are given the string w. Determine if w is beautiful.

Constraints

1 \leq |w| \leq 100

w consists of lowercase letters (a-z).

Input

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

w

Output

Print Yes if w is beautiful. Print No otherwise.

Sample Input 1

abaccaba

Sample Output 1

Yes

a occurs four times, b occurs twice, c occurs twice and the other letters occur zero times.

Sample Input 2

hthth

Sample Output 2

No


var
	n,A,j,k,x:Longint;
	dp:array[1..51,-5000..5000]of int64;
begin
	read(n,A);
	dp[1,0]:=1;
	for k:=1 to n do begin
		read(x);
		dec(x,A);
		for j:=-5000 to 5000 do begin
			inc(dp[k+1,j],dp[k,j]);
			if(j+x<=5000)and(j+x>=-5000)then inc(dp[k+1,j+x],dp[k,j]);
		end;
	end;
	writeln(dp[n+1,0]-1);
end.

Score : 300 points

Problem Statement

Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i.
He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A.
In how many ways can he make his selection?

Constraints

1 \leq N \leq 50

1 \leq A \leq 50

1 \leq x_i \leq 50

N,\,A,\,x_i are integers.

Partial Score

200 points will be awarded for passing the test set satisfying 1 \leq N \leq 16.

Input

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

N A
x_1 x_2 ... x_N

Output

Print the number of ways to select cards such that the average of the written integers is exactly A.

Sample Input 1

4 8
7 9 8 9

Sample Output 1

5

The following are the 5 ways to select cards such that the average is 8:

Select the 3-rd card.

Select the 1-st and 2-nd cards.

Select the 1-st and 4-th cards.

Select the 1-st, 2-nd and 3-rd cards.

Select the 1-st, 3-rd and 4-th cards.

Sample Input 2

3 8
6 6 9

Sample Output 2

0

Sample Input 3

8 5
3 6 2 8 7 6 5 9

Sample Output 3

19

Sample Input 4

33 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

Sample Output 4

8589934591

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


const  MAXN = 100000;
var n,a,i,j,k,x: longint;
    dp:array[0..MAXN] of int64;

procedure Nhap;
begin
        readln(n,a);
end;

procedure Init;
begin
        dp[n*a]:= 1;
end;

procedure QHD;
begin
        for k:= 1 to n do
        begin
                read(x);
		for j:= 0 to 4999 do
                begin
			dp[5000 - i + j]:= dp[i + j];
			if (j - x + a >= 0) and (j - x + a < 5000) then
                        dp[5000 - i + j]:= dp[5000 - i + j] + dp[i + j -x + a];
		end;
		i:= 5000 - i;
	end;
end;

BEGIN
        Nhap;
        Init;
        QHD;
	writeln(dp[i + n*A] - 1);
END.


var
	n,a,i,j,x,L,R:Longint;
	dp:array[0..50,0..5000]of int64;
begin
	read(n,a);
	dp[0,n*a]:=1;
	for i:=1 to n do begin
		for j:=0 to 5000 do dp[i,j]:=dp[i-1,j];
		read(x);
		x:=a-x;
		L:=-x;
		if L<0 then L:=0;
		R:=5000-x;
		if R>5000 then R:=5000;
		for j:=L to R do begin
			inc(dp[i,j+x],dp[i-1,j]);
		end;
	end;
	writeln(dp[n,n*a]-1);
end.


var
	n,s,b,t,c,ans,i:int64;
function f(b,n:int64):int64;
var t,c:int64;
begin
	t:=n;
	c:=0;
	while t>0 do begin
		c:=c+t mod b;
		t:=t div b;
	end;
	f:=c;
end;
begin
	read(n,s);
	if n=s then begin
		writeln(n+1);
		exit;
	end;
	i:=1;
	ans:=n+s;
	while i*i<=n do begin
		if(i>1)and(f(i,n)=s)and(ans>i)then ans:=i;
		if n div i>1 then begin
			t:=f(n div i,n);
			if(t<=s)and((s-t)mod i=0)then begin
				b:=n div i+(s-t)div i;
				if(b>1)and(f(b,n)=s)and(ans>b)then ans:=b;
			end;
		end;
		inc(i);
	end;
	if ans=n+s then writeln(-1)else writeln(ans);
end.

Score : 500 points

Problem Statement

For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:

f(b,n) = n, when n < b

f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b

Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b,
and n \ {\rm mod} \ b denotes the remainder of n divided by b.

Less formally, f(b,n) is equal to the sum of the digits of n written in base b.
For example, the following hold:

f(10,\,87654)=8+7+6+5+4=30

f(100,\,87654)=8+76+54=138

You are given integers n and s.
Determine if there exists an integer b (b \geq 2) such that f(b,n)=s.
If the answer is positive, also find the smallest such b.

Constraints

1 \leq n \leq 10^{11}

1 \leq s \leq 10^{11}

n,\,s are integers.

Input

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

n
s

Output

If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b.
If such b does not exist, print -1 instead.

Sample Input 1

87654
30

Sample Output 1

10

Sample Input 2

87654
138

Sample Output 2

100

Sample Input 3

87654
45678

Sample Output 3

-1

Sample Input 4

31415926535
1

Sample Output 4

31415926535

Sample Input 5

1
31415926535

Sample Output 5

-1


var a,b,c,d:longint;
    e:char;
begin
  while true do
  begin
    read(e);
    if e=chr(13) then
      break;
    case e of
      'W':inc(a);
      'N':inc(b);
      'E':inc(c);
      'S':inc(d);
    end;
  end;
  if (a=c) and (a in [0,1]) and (b=d) and (b in [0,1]) then
    writeln('Yes')
  else
    writeln('No');
end.

Score : 200 points

Problem Statement

Snuke lives on an infinite two-dimensional plane. He is going on an N-day trip.
At the beginning of Day 1, he is at home. His plan is described in a string S of length N.
On Day i(1 ≦ i ≦ N), he will travel a positive distance in the following direction:

North if the i-th letter of S is N

West if the i-th letter of S is W

South if the i-th letter of S is S

East if the i-th letter of S is E

He has not decided each day's travel distance. Determine whether it is possible to set each day's travel distance so that he will be back at home at the end of Day N.

Constraints

1 ≦ | S | ≦ 1000

S consists of the letters N, W, S, E.

Input

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

S

Output

Print Yes if it is possible to set each day's travel distance so that he will be back at home at the end of Day N. Otherwise, print No.

Sample Input 1

SENW

Sample Output 1

Yes

If Snuke travels a distance of 1 on each day, he will be back at home at the end of day 4.

Sample Input 2

NSNNSNSN

Sample Output 2

Yes

Sample Input 3

NNEW

Sample Output 3

No

Sample Input 4

W

Sample Output 4

No


var N,i:Longint;
pre,ans,c:int64;
begin
read(N);
for i:=1 to N do begin
read(c);
if c=0 then pre:=0 else begin
inc(ans,(c+pre)div 2);
pre:=(c+pre)mod 2;
end;
end;
writeln(ans);
end.

Score : 400 points

Problem Statement

Snuke has a large collection of cards. Each card has an integer between 1 and N, inclusive, written on it.
He has A_i cards with an integer i.

Two cards can form a pair if the absolute value of the difference of the integers written on them is at most 1.

Snuke wants to create the maximum number of pairs from his cards, on the condition that no card should be used in multiple pairs. Find the maximum number of pairs that he can create.

Constraints

1 ≦ N ≦ 10^5

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

All input values are integers.

Input

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

N
A_1
:
A_N

Output

Print the maximum number of pairs that Snuke can create.

Sample Input 1

4
4
0
3
2

Sample Output 1

4

For example, Snuke can create the following four pairs: (1,1),(1,1),(3,4),(3,4).

Sample Input 2

8
2
0
1
6
0
8
2
1

Sample Output 2

9


program taska;
var n, a, i, temp, cnt : longint;
    p : array[1..1000] of longint;
begin
    readln(n);
    for i := 1 to n do
    begin
        readln(a);
        p[i] := a;
    end;

    for i := 1 to n do
    begin
        if p[i] > p[i + 2] then
        begin
            temp := p[i];
            p[i] := p[i + 2];
            p[i + 2] := temp;
        end
        else
        begin
            temp := p[i];
            p[i] := p[i + 1];
            p[i + 1] := temp;
            inc(cnt);
        end;
    end;
    writeln(cnt);
end.

Score : 600 points

Problem Statement

Snuke got an integer sequence of length N from his mother, as a birthday present. The i-th (1 ≦ i ≦ N) element of the sequence is a_i. The elements are pairwise distinct.
He is sorting this sequence in increasing order.
With supernatural power, he can perform the following two operations on the sequence in any order:

Operation 1: choose 2 consecutive elements, then reverse the order of those elements.

Operation 2: choose 3 consecutive elements, then reverse the order of those elements.

Snuke likes Operation 2, but not Operation 1. Find the minimum number of Operation 1 that he has to perform in order to sort the sequence in increasing order.

Constraints

1 ≦ N ≦ 10^5

0 ≦ A_i ≦ 10^9

If i ≠ j, then A_i ≠ A_j.

All input values are integers.

Input

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

N
A_1
:
A_N

Output

Print the minimum number of times Operation 1 that Snuke has to perform.

Sample Input 1

4
2
4
3
1

Sample Output 1

1

The given sequence can be sorted as follows:

First, reverse the order of the last three elements. The sequence is now: 2,1,3,4.

Then, reverse the order of the first two elements. The sequence is now: 1,2,3,4.

In this sequence of operations, Operation 1 is performed once. It is not possible to sort the sequence with less number of Operation 1, thus the answer is 1.

Sample Input 2

5
10
8
5
3
2

Sample Output 2

0


program taskB;
var o:array[1..100000]of longint;
    n,i,j,sum,temp:longint;
begin
readln(n);
for i:=1 to n do
readln(o[i]);
for i:=1 to n do
begin
  for j:=1 to n-2 do
  begin
    if o[j]>o[j+2] then
    begin
      temp:=o[j];
      o[j]:=o[j+2];
      o[j+2]:=temp;
    end;
  end;
end;
for i:=1 to n do
begin
  for j:=1 to n-1 do
  begin
    if o[j]>o[j+1] then
    begin
      sum:=sum+1;
      temp:=o[j];
      o[j]:=o[j+1];
      o[j+1]:=temp;
    end;
  end;
end;
writeln(sum);
end.


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

Score : 400 points

Problem Statement

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

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

Constraints

2 ≦ |s| ≦ 10^5

s consists of lowercase letters.

Partial Score

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

Input

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

s

Output

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

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

Sample Input 1

needed

Sample Output 1

2 5

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

Sample Input 2

atcoder

Sample Output 2

-1 -1

The string atcoder contains no unbalanced substring.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function tousa(n: Integer):Integer;
begin
  if n=0 then
    result:=0
  else begin
    result:=n+tousa(n-1);
  end;
end;

var
  n:Integer;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(n);
    Writeln(tousa(n));
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

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

Constraints

1≦N≦100

Input

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

N

Output

Print the necessary number of candies in total.

Sample Input 1

3

Sample Output 1

6

The answer is 1+2+3=6.

Sample Input 2

10

Sample Output 2

55

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

Sample Input 3

1

Sample Output 3

1

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


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

Problem Statement

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

Constraints

a and b are integers.

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

Partial Score

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

Input

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

a b

Output

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

Sample Input 1

1 3

Sample Output 1

Positive

1×2×3=6 is positive.

Sample Input 2

-3 -1

Sample Output 2

Negative

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

Sample Input 3

-1 1

Sample Output 3

Zero

(-1)×0×1=0.


var n,i,tot,min:longint;
s:array[1..100000] of longint;
begin
    readln(n);
    for i:=1 to n do begin
        read(s[i]);
        if s[i]<min then min:=s[i];
    end;
    for i:=1 to n do begin
        if s[i]=min then inc(tot);
    end;
    if ((tot mod 2+min mod 2+n mod 2) mod 2=0)  then writeln('First')
    else writeln('Second');
end.

Problem Statement

There are N piles of candies on the table. The piles are numbered 1 through N. At first, pile i contains a_i candies.

Snuke and Ciel are playing a game. They take alternating turns. Snuke goes first. In each turn, the current player must perform one of the following two operations:

Choose a pile with the largest number of candies remaining, then eat all candies of that pile.

From each pile with one or more candies remaining, eat one candy.

The player who eats the last candy on the table, loses the game. Determine which player will win if both players play the game optimally.

Constraints

1≤N≤10^5

1≤a_i≤10^9

Input

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

N
a_1 a_2 … a_N

Output

If Snuke will win, print First. If Ciel will win, print Second.

Sample Input 1

2
1 3

Sample Output 1

First

At the beginning of the game, pile 2 contains the most candies. If Snuke eats all candies of this pile, Ciel has no choice but to eat the last candy.

Sample Input 2

3
1 2 1

Sample Output 2

First

If Snuke eats one candy from each pile, Ciel is again left with the last candy.

Sample Input 3

3
1 2 3

Sample Output 3

Second


var
	N,K,i,a:Longint;
	D:Array[0..9]of Boolean;
function f(n:Longint):Boolean;
begin
	while n>0 do begin
		if D[n mod 10]then begin
			f:=True;
			exit;
		end;
		n:=n div 10;
	end;
	f:=False;
end;
begin
	read(N,K);
	for i:=1 to K do begin
		read(a);
		D[a]:=True;
	end;
	while f(N)do inc(N);
	writeln(N);
end.

Score : 300 points

Problem Statement

Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K.

She is shopping, and now paying at the cashier.
Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change).

However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.

Find the amount of money that she will hand to the cashier.

Constraints

1 ≦ N < 10000

1 ≦ K < 10

0 ≦ D_1 < D_2 < … < D_K≦9

\{D_1,D_2,...,D_K\} ≠ \{1,2,3,4,5,6,7,8,9\}

Input

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

N K
D_1 D_2 … D_K

Output

Print the amount of money that Iroha will hand to the cashier.

Sample Input 1

1000 8
1 3 4 5 6 7 8 9

Sample Output 1

2000

She dislikes all digits except 0 and 2.

The smallest integer equal to or greater than N=1000 whose decimal notation contains only 0 and 2, is 2000.

Sample Input 2

9999 1
0

Sample Output 2

9999


var
  a,b,c,che5,che7:integer;
begin
  readln(a,b,c);
  if a=5 then che5:=che5+1;
  if a=7 then che7:=che7+1;
  if b=5 then che5:=che5+1;
  if b=7 then che7:=che7+1;
  if c=5 then che5:=che5+1;
  if c=7 then che7:=che7+1;
  if (che5=2) and (che7=1) then writeLN('YES')
                          ELSE WRITELN('NO');
END.

Score : 100 points

Problem Statement

Iroha loves Haiku. Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.

To create a Haiku, Iroha has come up with three different phrases. These phrases have A, B and C syllables, respectively. Determine whether she can construct a Haiku by using each of the phrases once, in some order.

Constraints

1≦A,B,C≦10

Input

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

A B C

Output

If it is possible to construct a Haiku by using each of the phrases once, print YES (case-sensitive). Otherwise, print NO.

Sample Input 1

5 5 7

Sample Output 1

YES

Using three phrases of length 5, 5 and 7, it is possible to construct a Haiku.

Sample Input 2

7 7 5

Sample Output 2

NO


var
    n,i,j,m,k,d,t,t0,bit_count,bit_need,s,first_ava: Integer;
    flag,flag2:Boolean;
    bit:array[1..4] of Integer;
    bit_avaliable:array[0..9] of Boolean;
    match:array[1..6] of Integer;
    match2:array[1..6] of Integer;
    bit_out:array[1..5] of Integer;

procedure my_output(n:Integer);
var
    i: Integer;
begin
    for i := 1 to n do
    begin
        write(bit_out[i]);
    end;
    writeln;
    readln;
    readln;
end;

begin
    readln(n,k);
    for i := 0 to 9 do
    begin
        bit_avaliable[i]:=true;
    end;
    for i := 1 to k do
    begin
        read(d);
        bit_avaliable[d]:=false;
    end;

    bit_count:=0;
    while n>0 do
    begin
        inc(bit_count);
        bit[bit_count]:=n mod 10;
        n:=n div 10;
    end;
    bit_need:=bit_count;

    t:=0;
    for j := bit_count downto 1 do
    begin
        flag2:=true;
        for i := bit[j] to 9 do
        begin
            if bit_avaliable[i] then
            begin
                flag2:=false;
                inc(t);
                match[t]:=i;
                flag:=true;
                for m := i+1 to 9 do
                begin
                    if bit_avaliable[m] then
                    begin
                        match2[t]:=m;
                        flag:=false;
                        break;
                    end;
                end;
                if flag then
                begin
                    match2[t]:=-1;
                end;
                break;
            end;
        end;
        if flag2 then
        begin
            t0:=t;
            while (t0>0) and (match2[t0]=-1) do
            begin
                dec(t0);
            end;
            if t0=0 then
            begin
                inc(bit_need);
                for i := 1 to 9 do
                begin//get first avaliable
                    if bit_avaliable[i] then
                    begin
                        first_ava:=i;
                        break;
                    end;
                end;
                for s := 1 to bit_need do
                begin
                    bit_out[s]:=first_ava;
                end;
                my_output(bit_need);
                exit;
            end
            else
            begin
                for s := 1 to bit_need do
                begin
                    bit_out[s]:=match[s];
                end;
                bit_out[t0]:=match2[t0];
                my_output(bit_need);
                exit;
            end;
        end;
    end;

    for s := 1 to bit_need do
    begin
        bit_out[s]:=match[s];
    end;
    my_output(bit_need);
end.


uses math;
var
  n,i,j,s,t:longint;
  a:array[1..1000]of longint;
begin
  readln(n);
  for i:=1 to n*2 do read(a[i]);
  for i:=1 to n*2-1 do
    for j:=i+1 to n*2 do
    if a[i]<a[j] then
    begin
      t:=a[i];
      a[i]:=a[j];
      a[j]:=t;
    end;
  for i:=1 to n do s:=s+min(a[i*2-1],a[i*2]);
  writeln(s);
end.

Score : 200 points

Problem Statement

Snuke is having a barbeque party.

At the party, he will make N servings of Skewer Meal.

Example of a serving of Skewer Meal

He has a stock of 2N skewers, all of which will be used in Skewer Meal. The length of the i-th skewer is L_i.
Also, he has an infinite supply of ingredients.

To make a serving of Skewer Meal, he picks 2 skewers and threads ingredients onto those skewers.
Let the length of the shorter skewer be x, then the serving can hold the maximum of x ingredients.

What is the maximum total number of ingredients that his N servings of Skewer Meal can hold, if he uses the skewers optimally?

Constraints

1≦N≦100

1≦L_i≦100

For each i, L_i is an integer.

Input

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

N
L_1 L_2 ... L_{2N}

Output

Print the maximum total number of ingredients that Snuke's N servings of Skewer Meal can hold.

Sample Input 1

2
1 3 1 2

Sample Output 1

3

If he makes a serving using the first and third skewers, and another using the second and fourth skewers, each serving will hold 1 and 2 ingredients, for the total of 3.

Sample Input 2

5
100 1 2 3 14 15 58 58 58 29

Sample Output 2

135