n, x = gets.chomp.split
a_arr = gets.chomp.split.map {|i| i.to_i}.insert(0, 0)

if x.to_i == 0
  puts a_arr.inject {|mem, i| mem + i.to_i}
  return
end

count = 0

a_arr.each_with_index do |item, i|
  next if i == 0 || i == a_arr.size - 1
  while item + a_arr[i + 1] > x.to_i
    a_arr[i + 1] -= 1
    count += 1
  end
end

puts count

Score : 300 points

Problem Statement

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

Snuke can perform the following operation any number of times:

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

His objective is as follows:

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

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

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i ≤ 10^9

0 ≤ x ≤ 10^9

Input

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

N x
a_1 a_2 ... a_N

Output

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

Sample Input 1

3 3
2 2 2

Sample Output 1

1

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

Sample Input 2

6 1
1 6 1 2 0 4

Sample Output 2

11

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

Sample Input 3

5 9
3 1 4 1 5

Sample Output 3

0

The objective is already achieved without performing operations.

Sample Input 4

2 0
5 5

Sample Output 4

10

All the candies need to be eaten.


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

Score : 100 points

Problem Statement

We have an N×N checkerboard.

From the square at the upper left corner, a square that is i squares to the right and j squares below is denoted as (i, j). Particularly, the square at the upper left corner is denoted as (0, 0).

Each square (i, j) such that i+j is even, is colored black, and the other squares are colored white.

We will satisfy the following condition by painting some of the white squares:

Any black square can be reached from square (0, 0) by repeatedly moving to a black square that shares a side with the current square.

Achieve the objective by painting at most 170000 squares black.

Constraints

1 \leq N \leq 1,000

Input

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

N

Output

Print the squares to paint in the following format:

K
x_1 y_1
x_2 y_2
:
x_K y_K

This means that a total of K squares are painted black, the i-th of which is (x_i, y_i).

Judging

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

0 \leq K \leq 170000

0 \leq x_i, y_i \leq N-1

For each i, x_i + y_i is odd.

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

The condition in the statement is satisfied by painting all specified squares.

Sample Input 1

2

Sample Output 1

1
1 0

Sample Input 2

4

Sample Output 2

3
0 1
2 1
2 3

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.


h,w = gets.chomp.split(" ").map{|i| i.to_i}

ans = []

h.times do |i|
	line = gets.chomp
	if line =~ /snuke/ then
		ans[0] = (line.index("snuke")/6.0).ceil
		ans[1] = i+1
		break
	end
end

puts "#{("A".."Z").to_a[ans[0]]}#{ans[1]}"

Score : 100 points

Problem Statement

There is a grid with H rows and W columns.

The square at the i-th row and j-th column contains a string S_{i,j} of length 5.

The rows are labeled with the numbers from 1 through H, and the columns are labeled with the uppercase English letters from A through the W-th letter of the alphabet.

Exactly one of the squares in the grid contains the string snuke. Find this square and report its location.

For example, the square at the 6-th row and 8-th column should be reported as H6.

Constraints

1≦H, W≦26

The length of S_{i,j} is 5.

S_{i,j} consists of lowercase English letters (a-z).

Exactly one of the given strings is equal to snuke.

Input

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

H W
S_{1,1} S_{1,2} ... S_{1,W}
S_{2,1} S_{2,2} ... S_{2,W}
:
S_{H,1} S_{H,2} ... S_{H,W}

Output

Print the labels of the row and the column of the square containing the string snuke, with no space inbetween.

Sample Input 1

15 10
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snuke snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake

Sample Output 1

H6

Sample Input 2

1 1
snuke

Sample Output 2

A1


Score : 1600 points

Problem Statement

There are N squares in a row, numbered 1 through N from left to right.
Snuke and Rng are playing a board game using these squares, described below:

First, Snuke writes an integer into each square.

Each of the two players possesses one piece. Snuke places his piece onto square 1, and Rng places his onto square 2.

The player whose piece is to the left of the opponent's, moves his piece. The destination must be a square to the right of the square where the piece is currently placed, and must not be a square where the opponent's piece is placed.

Repeat step 3. When the pieces cannot be moved any more, the game ends.

The score of each player is calculated as the sum of the integers written in the squares where the player has placed his piece before the end of the game.

Snuke has already written an integer A_i into square i (1≦i≦N-1), but not into square N yet.
He has decided to calculate for each of M integers X_1,X_2,...,X_M, if he writes it into square N and the game is played, what the value "(Snuke's score) - (Rng's score)" will be.
Here, it is assumed that each player moves his piece to maximize the value "(the player's score) - (the opponent's score)".

Constraints

3≦N≦200,000

0≦A_i≦10^6

The sum of all A_i is at most 10^6.

1≦M≦200,000

0≦X_i≦10^9

Partial Scores

700 points will be awarded for passing the test set satisfying M=1.

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

Input

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

N
A_1 A_2 ... A_{N-1}
M
X_1
X_2
:
X_M

Output

For each of the M integers X_1, ..., X_M, print the value "(Snuke's score) - (Rng's score)" if it is written into square N, one per line.

Sample Input 1

5
2 7 1 8
1
2

Sample Output 1

0

The game proceeds as follows, where S represents Snuke's piece, and R represents Rng's.

Both player scores 10, thus "(Snuke's score) - (Rng's score)" is 0.

Sample Input 2

9
2 0 1 6 1 1 2 6
5
2016
1
1
2
6

Sample Output 2

2001
6
6
7
7


class Shikstone
  def solve
    h,w = gets.scan(/\d+/).map(&:to_i)
    tbl = Array.new(h)
    h.times do |i|
      tbl[i] = gets
    end

    check(tbl, 0, 0, h-1, w-1)

  end

  def check(t, i, j, mh, mw)
    if i == mh && j == mw
      return "Possible"
    end

    if i == mh
      if t[i][j+1] == '#' && ( j == 0 || t[i][j-1] == '.')
        t[i][j] = '.'
        return check(t, i, j+1, mh, mw)
      else
        return "Impossible"
      end
    end

    if j == mw
      if t[i+1][j] == '#' && (i == 0 || t[i-1][j] == '.')
        t[i][j] = '.'
        return check(t, i+1, j, mh, mw)
      else
        return "Impossible"
      end
    end

    if ((t[i+1][j] == '#' && t[i][j+1] == '.') || (t[i+1][j] == '.' && t[i][j+1] == '#')) && (j == 0 || t[i][j-1] == '.') && (i == 0 || t[i-1][j] == '.')
      t[i][j] = '.'
      if t[i+1][j] == '#'
        return check(t, i+1, j, mh, mw)
      else
        return check(t, i, j+1, mh, mw)
      end
    else
      return "Impossible"
    end
  end
end

if $0 == __FILE__
  puts Shikstone.new.solve
end

Score : 200 points

Problem Statement

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

We have a grid of H rows and W columns. Initially, there is a stone in the top left cell. Shik is trying to move the stone to the bottom right cell. In each step, he can move the stone one cell to its left, up, right, or down (if such cell exists). It is possible that the stone visits a cell multiple times (including the bottom right and the top left cell).

You are given a matrix of characters a_{ij} (1 \leq i \leq H, 1 \leq j \leq W). After Shik completes all moving actions, a_{ij} is # if the stone had ever located at the i-th row and the j-th column during the process of moving. Otherwise, a_{ij} is .. Please determine whether it is possible that Shik only uses right and down moves in all steps.

Constraints

2 \leq H, W \leq 8

a_{i,j} is either # or ..

There exists a valid sequence of moves for Shik to generate the map a.

Input

The 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 it is possible that Shik only uses right and down moves, print Possible. Otherwise, print Impossible.

Sample Input 1

4 5
##...
.##..
..##.
...##

Sample Output 1

Possible

The matrix can be generated by a 7-move sequence: right, down, right, down, right, down, and right.

Sample Input 2

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

Sample Output 2

Impossible

Sample Input 3

4 5
##...
.###.
.###.
...##

Sample Output 3

Impossible


gets
m=[*$<]

def get(m, x, y)
  if m[y]
    if m[y][x].chr == '#'
      true
    else
      false
    end
  else
    false
  end
end

def q(m)
  x = 0
  y = 0
  w = m[0].size - 1
  h = m.size
  while true
    if !get(m, x, y)
      return false
    end
    if x == w - 1 && y == h - 1
      return true
    end

    r = get(m, x+1, y)
    d = get(m, x, y+1)
    if r == d
      return false
    end

    if r
      x += 1
    else
      y += 1
    end
  end
end

if q(m)
  puts "Possible"
else
  puts "Impossible"
end


w,h,n = gets.split.map(&:to_i)
xl = 0
xr = w
yl = 0
yh = h
n.times do
  x,y,a = gets.split.map(&:to_i)
  if a == 1
    xl = x if x >= xl
  elsif a == 2
    xr = x if x <= xr
  elsif a == 3
    yl = y if y > yl
  else
    yh = y if y < yh
  end
end

ans = (yh-yl) * (xr - xl)
if yh -yl > 0 && xr - xl > 0
  puts ans
else
  puts 0
end

Score : 200 points

Problem Statement

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

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

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

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

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

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

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

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

Constraints

1 ≦ W, H ≦ 100

1 ≦ N ≦ 100

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

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

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

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

Input

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

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

Output

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

Sample Input 1

5 4 2
2 1 1
3 3 4

Sample Output 1

9

The figure below shows the rectangle before Snuke starts painting.

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

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

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

Sample Input 2

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

Sample Output 2

0

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

Sample Input 3

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

Sample Output 3

64


S=gets.chomp.chars
puts S.chunk{ |e| e }.to_a.length - 1

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


n=gets.to_i
ha=[[]]*n
(n-1).times do
  g=gets.split.map{|i|i.to_i-1}
  2.times do |i|
      ha[g[i]]+=[g[(i+1)%2]]
  end
end
m=gets.to_i
hb=[]
hc=[nil]*n
m.times do
  x,y=gets.split.map(&:to_i)
  hb<< [x-1,y]
  hc[x-1]=y
end
hb=hb.sort{|x,y|x[1]<=>y[1]}
st=[]
now=hb[-1][1]
j=true
while hb!=[] || st!=[] do
  if st==[]
    g=hb.pop
  else
    g=st.pop
  end
  ha[g[0]].each do |e|
    if hc[e]
      if hc[e]!=now+1
        j=false
        break
      end
    else
      hc[e]=now-1
      st<< [e,now-1]
    end
  end
end
puts j ? :Yes : :No
puts hc.*("\n") if j

Score : 800 points

Problem Statement

We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.

Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.

Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:

Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.

Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.

Constraints

1 ≦ N ≦ 10^5

1 ≦ K ≦ N

1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)

1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)

0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)

The given graph is a tree.

All v_j are distinct.

Input

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

N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K

Output

If it is possible to write integers into all empty vertices so that the condition is satisfied, print Yes. Otherwise, print No.

If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.

Sample Input 1

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

Sample Output 1

Yes
5
6
6
5
7

The figure below shows the tree when Takahashi fell asleep. For each vertex, the integer written beside it represents the index of the vertex, and the integer written into the vertex is the integer written by Takahashi.

Aoki can, for example, satisfy the condition by writing integers into the remaining vertices as follows:

This corresponds to Sample Output 1. Note that other outputs that satisfy the condition will also be accepted, such as:

Yes
7
6
8
7
7

Sample Input 2

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

Sample Output 2

No

Sample Input 3

4
1 2
2 3
3 4
1
1 0

Sample Output 3

Yes
0
-1
-2
-3

The integers written by Aoki may be negative or exceed 10^6.


Score : 100 points

Problem Statement

AtCoDeer the deer recently bought three paint cans.
The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c.
Here, the color of each paint can is represented by an integer between 1 and 100, inclusive.

Since he is forgetful, he might have bought more than one paint can in the same color.
Count the number of different kinds of colors of these paint cans and tell him.

Constraints

1≦a,b,c≦100

Input

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

a b c

Output

Print the number of different kinds of colors of the paint cans.

Sample Input 1

3 1 4

Sample Output 1

3

Three different colors: 1, 3, and 4.

Sample Input 2

3 3 33

Sample Output 2

2

Two different colors: 3 and 33.


n = gets.to_i
takahashi = 1
aoki = 1

n.times do
  t, a = gets.split.map(&:to_i)
  i = [takahashi.to_f / t, aoki.to_f / a].max.ceil
  takahashi = t * i
  aoki = a * i
end

puts takahashi + aoki

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


    n = gets.chomp.to_i
    a = Array.new(n)
    b = Array.new(n)

    n.times{|i|
        a[i],b[i] =  gets.chomp.split(" ").map!(&:to_f)
    }
    am = a[0]
    bm = b[0]
    n.times{|i|
        j = (am /a[i]).ceil
        w = (bm /b[i]).ceil
        am = a[i]*([j, w].max)
        bm = b[i]*([j, w].max)
        puts w ,j
    }
    puts (am + bm).to_i


a = gets.chomp
ans = 0
gu = 0
a.size.times do |i|
  if (gu == 0)
    gu += 1
    ans -= 1 if a[i] == "p"
  else
    if(a[i] == "g")
      ans += 1
      gu -= 1
    else
      gu -= 1
    end
  end
end
puts ans

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.


s = gets.chomp
n = s.size
chart = {
  g: { g: 0, p: -1},
  p: { g: 1, p: 0 }
}

ans = 0
n.times do |i|
  me = i.even? ? :g : :p
  you = s[i].to_sym
  ans += chart[me][you]
end

puts ans


nab = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp.split("")

kakutei = 0
kaigai = 0

nab[0].times do |i|
  if s[i] == 'a'
    if kakutei < nab[1]+nab[2]
      puts "Yes"
      kakutei +=1
    else
      puts "No"
    end
  elsif s[i] == 'b'
    if kaigai < nab[2] && kakutei < nab[1]+nab[2]
      puts "Yes"
      kaigai += 1# if kaigai < n
      kakutei +=1
    else
      puts "No"
    end
  else
    puts "No"
  end
end

Score : 200 points

Problem Statement

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

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

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

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

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

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

Constraints

1≦N,A,B≦100000

A+B≦N

S is N characters long.

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

Input

Inputs are provided from Standard Input in the following form.

N A B
S

Output

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

Sample Input 1

10 2 3
abccabaabb

Sample Output 1

Yes
Yes
No
No
Yes
Yes
Yes
No
No
No

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

Sample Input 2

12 5 2
cabbabaacaba

Sample Output 2

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

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

Sample Input 3

5 2 2
ccccc

Sample Output 3

No
No
No
No
No


io = STDIN
x=io.gets.chomp
y=0
x.gsub!(/(ST)+/,"")
x.gsub!(/(S+)(T+)/) do |str|
  ssize = str.index("T")
  tsize = str.size - ssize
  if (ssize>tsize)
    "S" * (ssize-tsize)
  else
    "T" * (tsize-ssize)
  end
end
x.gsub!(/(ST)+/,"")
x.gsub!(/(S+)(T+)/) do |str|
  ssize = str.index("T")
  tsize = str.size - ssize
  if (ssize>tsize)
    "S" * (ssize-tsize)
  else
    "T" * (tsize-ssize)
  end
end
puts x.size

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.


#coding: utf-8
  2 a=gets().chomp
  3 a.insert(4," ")
  4 puts(a)

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


n = gets.to_i
a = gets.split(" ").map(&:to_i)

count = 0
n.times do |i|
  if a[a[i] - 1] - 1 == i
    count += 1
  end
end
puts count / 2

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


s=gets.chomp
k=gets.to_i
s.each_char.with_index do |c,i|
  l=122-c.ord+1
  if c!="a"&&l<=k
    k-=l
    s[i]="a"
  end
end
s[-1]=((s[-1].ord-97+k)%26+97).chr
puts s

Score : 400 points

Problem Statement

Mr. Takahashi has a string s consisting of lowercase English letters.
He repeats the following operation on s exactly K times.

Choose an arbitrary letter on s and change that letter to the next alphabet. Note that the next letter of z is a.

For example, if you perform an operation for the second letter on aaz, aaz becomes abz.
If you then perform an operation for the third letter on abz, abz becomes aba.

Mr. Takahashi wants to have the lexicographically smallest string after performing exactly K operations on s.
Find the such string.

Constraints

1≤|s|≤10^5

All letters in s are lowercase English letters.

1≤K≤10^9

Input

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

s
K

Output

Print the lexicographically smallest string after performing exactly K operations on s.

Sample Input 1

xyz
4

Sample Output 1

aya

For example, you can perform the following operations: xyz, yyz, zyz, ayz, aya.

Sample Input 2

a
25

Sample Output 2

z

You have to perform exactly K operations.

Sample Input 3

codefestival
100

Sample Output 3

aaaafeaaivap


a,b,h = 3.times.map{gets.to_i}
puts (a+b)*h/2

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.


S = gets.chomp.split("")
$debug = false

### DFS: Depth-First Search = 深さ優先探索
# スタックによる実装
def iterative_dfs(patterns, limit)
  # スタックとなる配列を用意し、初期状態である空文字列を格納する
  stack = [""]
  until stack.empty?
    # スタックから1つ取り出す（末尾から取り出すのでpopを使う）
    string = stack.pop

    if string.length == limit
      # 頂点に達した際、文字列を配列に格納する
      patterns << string.split("").to_a.unshift(nil) unless string.empty?
    else
      # 分岐処理
      stack << string + " "
      stack << string + "+"
    end
  end
end

patterns = [] # 結果を格納する配列を用意しておく
iterative_dfs(patterns, S.size - 1)
p patterns if $debug # 組み合わせの表示

answer = 0
patterns.each do |pattern|
  expression = S[0]
  (1...S.length).each do |i|
    expression += "+" if pattern[i] == "+"
    expression += S[i]
  end
  p expression if $debug
  p eval expression if $debug
  answer += eval expression
end

# 入力が1桁の場合の考慮を忘れていた……。
if S.size == 1
  answer = S[0].to_i
end

puts answer

Score : 300 points

Problem Statement

You are given a string S consisting of digits between 1 and 9, inclusive.
You can insert the letter + into some of the positions (possibly none) between two letters in this string.
Here, + must not occur consecutively after insertion.

All strings that can be obtained in this way can be evaluated as formulas.

Evaluate all possible formulas, and print the sum of the results.

Constraints

1 \leq |S| \leq 10

All letters in S are digits between 1 and 9, inclusive.

Input

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

S

Output

Print the sum of the evaluated value over all possible formulas.

Sample Input 1

125

Sample Output 1

176

There are 4 formulas that can be obtained: 125, 1+25, 12+5 and 1+2+5. When each formula is evaluated,

125

1+25=26

12+5=17

1+2+5=8

Thus, the sum is 125+26+17+8=176.

Sample Input 2

9999999999

Sample Output 2

12656242944


# 愚直にやる以外の方法がまっっっっったく浮かばないのだが？？？
# いくつ入れるか+どこにいれるか

# 実装が頭ハゲるかと思った...1時間くらいかかったのでは...

s = gets.chomp.chars.map(&:to_i)
ans = s.map(&:to_s).join.to_i
def perm(i, current_arr, source_arr, limit)
  length = source_arr.size
  # return  if length < limit
  if current_arr.size == limit
    @res_arr << current_arr
  else
    i.upto(length) do |j|
      next if i==j
      perm(j, current_arr.clone<<source_arr[j-1], source_arr, limit)
    end
  end
end

def s(arr, s)
  ai = 0
  ret = 0
  cur = 0
  s.each.with_index do |sv, sidx|
    if ai < arr.size && sidx==arr[ai]
      ret += cur*10+sv
      cur = 0
      ai+=1
      next
    end
    cur *=10
    cur += sv
  end
  ret += cur
  return ret
end

# 入れる場所は0からs.size-1箇所
1.upto(s.size-1) do |i|
  @res_arr = []
  perm(0, [], (0...s.size-1).to_a, i)
  @res_arr.each do |a|
    ans+=s(a, s)
  end
end
p ans


=begin
－－－－－－－－－－－－－－
S = 文字列で表した数値
N = S の文字数
ans = 合計値
res = '+'ごとのに区切った数値の合計を格納
tmp = '+'ごとに区切った数値を格納
－－－－－－－－－－－－－－

1: '+' が挿入される箇所をbit全探索(N-1)する(フラグが立っている場合 + が挿入されるとする)
  1.1: S をループし、+ が挿入されるまで S[i] を区切って tmp に代入していく
    1.1.1: bit の i番目 にフラグが立っている場合、 現時点での tmp を ans に 加算して tmp を 0 に戻す
2: ans を出力する
=end

S = gets.chomp.chars;
N = S.size() - 1;
ans = 0;

bit = 0;
while bit < (1<<N) do
  res = tmp = 0;
  S.each_with_index do |v,i|
    tmp *= 10;
    tmp += v.to_i;
    if bit[i] != 0
      res += tmp;
      tmp = 0;
    end
  end
  ans += res + tmp;
  bit += 1;
end

puts ans


nums = gets.split('')

total = 0
[0 , 1].repeated_permutation(nums.length-1) do |bits|
    arr = [nums[0]]
    arr_i = 0
    0.upto(nums.length-2) do |i|
        if bits[i] == 0
            arr[arr_i] += nums[i+1]
        else
            arr_i += 1
            arr[arr_i] = nums[i+1]
        end
    end
    total += arr.map(&:to_i).inject(:+)
end
print(total)


S = gets.to_i.to_s
N = S.length - 1

s = 0
(1<<N).times do |bit|
  stock = 0
  now_s = 0
  N.times do |i|
    puts "bit: #{bit}, i: #{i}"
    if(bit & (1 << i) > 0)
      puts "true"
      stock += now_s*10 + S[i].to_i
      now_s = 0
    else
      now_s = now_s*10 + S[i].to_i
    end
    puts now_s
  end
  stock += now_s * 10 + S[N].to_i
  s += stock
end
puts s


S = gets.chomp
n = S.size

def f(s, i)
  return if i <= 0
  $ans << s
  a = String.new(s)
  a[i,0] = "+"
  $ans << a
  f(s, i-1)
  f(a, i-1)
end

$ans = []
(n-1).downto(1) do |i|
  f(S, i)
end
puts $ans.uniq.map { |a| eval(a) }.inject(:+)


#!ruby -E Windows-31J:utf-8

def f_hs a,b
    a*1000000000+b
end
def i_hs k
    return k.div(1000000000),k%1000000000
end

blacks = {}
well_known = {}
result = [0,0,0,0,0,0,0,0,0,0]

h,w,n = $stdin.gets.chomp.split(/\s+/).map{|str|str.to_i}
#storage
(0...n).each{|ln|
    ia,ib = $stdin.gets.chomp.split(/\s+/).map{|str|str.to_i-1}
    blacks[ f_hs ia,ib ] = true
}

#calc
blacks.each{|k,v|
    ia,ib = i_hs k

    ca=ia-1
    while ca<=ia+1 do
        if ca+1>=h || ca-1<0
            ca += 1
            next
        end
        (ib-1..ib+1).each{|cb|
            next if cb+1>=w || cb-1<0
            next if well_known[ f_hs ca,cb ]!=nil

            # counts
            cnt = 0
            (ca-1..ca+1).each{|fa|
                cnt +=1 if blacks.has_key? f_hs(fa,cb+1)
                cnt +=1 if blacks.has_key? f_hs(fa,cb)
                cnt +=1 if blacks.has_key? f_hs(fa,cb-1)
            }
            result[cnt]+=1

            # checker
            well_known[ f_hs ca,cb ] = true
        }
        ca+=1
    end
}

#possible all
result[0] = (h-2)*(w-2)
(1..9).each{|i| result[0]-=result[i]}

#output
result.each{|r| puts r}

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. At first, all cells were painted white.

Snuke painted N of these cells. The i-th ( 1 \leq i \leq N ) cell he painted is the cell at the a_i-th row and b_i-th column.

Compute the following:

For each integer j ( 0 \leq j \leq 9 ), how many subrectangles of size 3×3 of the grid contains exactly j black cells, after Snuke painted N cells?

Constraints

3 \leq H \leq 10^9

3 \leq W \leq 10^9

0 \leq N \leq min(10^5,H×W)

1 \leq a_i \leq H (1 \leq i \leq N)

1 \leq b_i \leq W (1 \leq i \leq N)

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

Input

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

H W N
a_1 b_1
:
a_N b_N

Output

Print 10 lines.
The (j+1)-th ( 0 \leq j \leq 9 ) line should contain the number of the subrectangles of size 3×3 of the grid that contains exactly j black cells.

Sample Input 1

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

Sample Output 1

0
0
0
2
4
0
0
0
0
0

There are six subrectangles of size 3×3. Two of them contain three black cells each, and the remaining four contain four black cells each.

Sample Input 2

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

Sample Output 2

4
26
22
10
2
0
0
0
0
0

Sample Input 3

1000000000 1000000000 0

Sample Output 3

999999996000000004
0
0
0
0
0
0
0
0
0


Score : 200 points

Problem Statement

We have a rectangular parallelepiped of size A×B×C, built with blocks of size 1×1×1. Snuke will paint each of the A×B×C blocks either red or blue, so that:

There is at least one red block and at least one blue block.

The union of all red blocks forms a rectangular parallelepiped.

The union of all blue blocks forms a rectangular parallelepiped.

Snuke wants to minimize the difference between the number of red blocks and the number of blue blocks. Find the minimum possible difference.

Constraints

2≤A,B,C≤10^9

Input

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

A B C

Output

Print the minimum possible difference between the number of red blocks and the number of blue blocks.

Sample Input 1

3 3 3

Sample Output 1

9

For example, Snuke can paint the blocks as shown in the diagram below.
There are 9 red blocks and 18 blue blocks, thus the difference is 9.

Sample Input 2

2 2 4

Sample Output 2

0

For example, Snuke can paint the blocks as shown in the diagram below.
There are 8 red blocks and 8 blue blocks, thus the difference is 0.

Sample Input 3

5 3 5

Sample Output 3

15

For example, Snuke can paint the blocks as shown in the diagram below.
There are 45 red blocks and 30 blue blocks, thus the difference is 9.


n = gets.to_i
boder = gets.to_i - 1
fir = gets.to_i
fin = gets.to_i
arr = Array.new(n)
boder.times do |i|
  arr[i] = fir
end
puts arr.inject(0){|sum,i|sum += !i.nil? ? i : fin}

Score : 100 points

Problem Statement

There is a hotel with the following accommodation fee:

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

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

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

Constraints

1 \leq N, K \leq 10000

1 \leq Y < X \leq 10000

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

Input

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

N
K
X
Y

Output

Print Tak's total accommodation fee.

Sample Input 1

5
3
10000
9000

Sample Output 1

48000

The accommodation fee is as follows:

10000 yen for the 1-st night

10000 yen for the 2-nd night

10000 yen for the 3-rd night

9000 yen for the 4-th night

9000 yen for the 5-th night

Thus, the total is 48000 yen.

Sample Input 2

2
3
10000
9000

Sample Output 2

20000


words = gets.chomp.split('')

until words.empty?
  if words.count(words[0]).odd?
    puts 'No'
    exit 0
  else
    words -= words[0..words.count(words[0])]
  end
end

puts 'Yes'

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


N,*as=$<.read.split.map(&:to_i)
as << 0

i = 0
ret = 0
while i < N
    ret += as[i]/2
    if as[i].odd? && 0 < as[i+1]
        ret += 1
        as[i+1] -= 1
    end
    i += 1
end

puts ret

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


require 'prime'
P=Prime.each(2155).map{|x|x**3}
h=Hash.new(0)
gets
$<.each{|_|
	n=_.to_i
	P.each{|x|n/=x while n%x==0}
	h[n]+=1
}
r=0
h.each{|k,v|
	if k==1
		r+=[1,v].min
	else
		n=k*k
		P.each{|x|n/=x while n%x==0}
		r+=[v,h[n]].max
		h[k]=h[n]=0
	end
}
p r

Score : 1100 points

Problem Statement

Snuke got positive integers s_1,...,s_N from his mother, as a birthday present. There may be duplicate elements.

He will circle some of these N integers. Since he dislikes cubic numbers, he wants to ensure that if both s_i and s_j (i ≠ j) are circled, the product s_is_j is not cubic. For example, when s_1=1,s_2=1,s_3=2,s_4=4, it is not possible to circle both s_1 and s_2 at the same time. It is not possible to circle both s_3 and s_4 at the same time, either.

Find the maximum number of integers that Snuke can circle.

Constraints

1 ≦ N ≦ 10^5

1 ≦ s_i ≦ 10^{10}

All input values are integers.

Input

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

N
s_1
:
s_N

Output

Print the maximum number of integers that Snuke can circle.

Sample Input 1

8
1
2
3
4
5
6
7
8

Sample Output 1

6

Snuke can circle 1,2,3,5,6,7.

Sample Input 2

6
2
4
8
16
32
64

Sample Output 2

3

Sample Input 3

10
1
10
100
1000000007
10000000000
1000000009
999999999
999
999
999

Sample Output 3

9


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.


a = []
a = gets.split("")
b =[]
c = 0
for i in 0..a.length-1
  if a[i] == "B"
    c = b.pop
  else
    b.push(a[i].to_i)
  end
end
puts b.join("")

Score : 200 points

Problem Statement

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

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

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

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

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

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

Constraints

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

s consists of the letters 0, 1 and B.

The correct answer is not an empty string.

Input

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

s

Output

Print the string displayed in the editor in the end.

Sample Input 1

01B0

Sample Output 1

00

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

Sample Input 2

0BB1

Sample Output 2

1

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


arr = []
STDIN.gets.chomp.split(//).each do |char|
	if char == "B"
		arr.pop
	else
		arr.push char
	end
end

STDOUT.print arr.join
STDOUT.print "\n"


n = gets.to_i
a = gets.chomp.split.map(&:to_i)
ava = a.inject(:+) / n
puts [ava, ava + 1].map {|x| a.map {|y| (x - y) ** 2 }.inject(:+) }.min

Score : 200 points

Problem Statement

Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them.

He may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if a_i=a_j (i≠j), he has to pay the cost separately for transforming each of them (See Sample 2).

Find the minimum total cost to achieve his objective.

Constraints

1≦N≦100

-100≦a_i≦100

Input

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

N
a_1 a_2 ... a_N

Output

Print the minimum total cost to achieve Evi's objective.

Sample Input 1

2
4 8

Sample Output 1

8

Transforming the both into 6s will cost (4-6)^2+(8-6)^2=8 dollars, which is the minimum.

Sample Input 2

3
1 1 3

Sample Output 2

3

Transforming the all into 2s will cost (1-2)^2+(1-2)^2+(3-2)^2=3 dollars. Note that Evi has to pay (1-2)^2 dollar separately for transforming each of the two 1s.

Sample Input 3

3
4 2 5

Sample Output 3

5

Leaving the 4 as it is and transforming the 2 and the 5 into 4s will achieve the total cost of (2-4)^2+(5-4)^2=5 dollars, which is the minimum.

Sample Input 4

4
-100 -100 -100 -100

Sample Output 4

0

Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is 0.


s=gets.chomp
m=s.size
ans = false
if s.size <= 100
0.upto(m-3) do |i|
  break if ans
  (i+2).upto(m-1) do |j|
    c=Array.new(26,0)
    b = s[i..j]
    b.chars do |d|
      c[d.ord-97] += 1
    end
    c.sort!.reverse!
    if c[0] > b.size / 2 && b.size >= 3
      puts "#{i+1} #{j+1}"
      ans = true
      break
    end
  end

end
puts "-1 -1" unless ans
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.


n,l,*a=`dd`.split.map &:to_i;puts (z=(1...n).find{|i|a[i-1]+a[i]>=l})?[:Possible,*1...z,[*z...n].reverse]: :Impossible

Problem Statement

We have N pieces of ropes, numbered 1 through N. The length of piece i is a_i.

At first, for each i (1≤i≤N-1), piece i and piece i+1 are tied at the ends, forming one long rope with N-1 knots. Snuke will try to untie all of the knots by performing the following operation repeatedly:

Choose a (connected) rope with a total length of at least L, then untie one of its knots.

Is it possible to untie all of the N-1 knots by properly applying this operation? If the answer is positive, find one possible order to untie the knots.

Constraints

2≤N≤10^5

1≤L≤10^9

1≤a_i≤10^9

All input values are integers.

Input

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

N L
a_1 a_2 ... a_n

Output

If it is not possible to untie all of the N-1 knots, print Impossible.

If it is possible to untie all of the knots, print Possible, then print another N-1 lines that describe a possible order to untie the knots. The j-th of those N-1 lines should contain the index of the knot that is untied in the j-th operation. Here, the index of the knot connecting piece i and piece i+1 is i.

If there is more than one solution, output any.

Sample Input 1

3 50
30 20 10

Sample Output 1

Possible
2
1

If the knot 1 is untied first, the knot 2 will become impossible to untie.

Sample Input 2

2 21
10 10

Sample Output 2

Impossible

Sample Input 3

5 50
10 20 30 40 50

Sample Output 3

Possible
1
2
3
4

Another example of a possible solution is 3, 4, 1, 2.


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


count5 = 0
count7 = 0

arr = $stdin.gets.chomp.split(/\s+/).map(&:to_i)

arr.map do |str|
  if str == 5
    count5 += 1
  end
  if str == 7
    count7 += 1
  end
end

answer = (count5 == 2 && count7 == 1) ? "YES" : "NO"

puts answer


N,k=gets.chomp.split(" ")
N_i = N.to_i
K = k.to_i
d_array=gets.chomp.split(" ").map(&:to_i)
E_array= (0..9).to_a - d_array

if 10-K == 1
    ret = E_array[0]
    while ret < N_i
        ret = ret*10 + E_array[0]
    end
    puts ret
    exit
end

# 使える数字を元にn進数を10進数にする
def to_i10(n)
    n_s = n.to_s(10-K)
    ret = 0

    start = 0
    diff = 0
    if E_array[0]!=0 && n_s.length>1
        if n_s[0]=="1" && n_s[1]=="0"
	        diff = E_array[-1] - E_array[0]
        else
            ret = E_array[n_s[0].to_i-1]
            diff = -1 if n_s.length==2
        end
        start=1
    end

    (start..(n_s.length-1)).each do |i|
        ret = ret*10 + E_array[n_s[i].to_i] + diff
        diff = 0
    end
    ret
end

sup = (10**N.length).to_s(10-K).to_i
inf = 0
loop do
    mid = (sup + inf)/2
    if inf == mid
        puts to_i10(sup)
        break
    end

    mid_i10 = to_i10(mid)
    if mid_i10 < N_i
        inf = mid
    else
        sup = mid
    end
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


n,k = gets.chomp.split().map(&:to_i)
d = gets.chomp.split

while true
  if d.any?{|e| n.to_s.include?(e)}
    n += 1
  else
    puts n
    exit
  end
end


N = gets.to_i
L = gets.split.map(&:to_i).sort

ans = 0
N.times{ |i| ans += L[i*2]}
puts ans

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


_ = gets.to_i
sum = 0
gets.split(' ').map{|e|e.to_i}.sort.each_with_index{|e, i| sum += e if i.even?}
puts sum