str = gets.chomp
opnum = gets.to_i

opnum.times do
    opary = gets.chomp.split
    a, b = opary[1].to_i, opary[2].to_i
    case opary[0]
    when "print"
        puts str[a..b]
    when "reverse"
        str[a..b] = str[a..b].reverse
    when "replace"
        str[a..b] = opary[3]
    end
end

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Transformation

Write a program which performs a sequence of commands to a given string $str$. The command is one of:

print a b: print from the a-th character to the b-th character of $str$

reverse a b: reverse from the a-th character to the b-th character of $str$

replace a b p: replace from the a-th character to the b-th character of $str$ with p

Note that the indices of $str$ start with 0.

Input

In the first line, a string $str$ is given. $str$ consists of lowercase letters. In the second line, the number of commands q is given. In the next q lines, each command is given in the above mentioned format.

Output

For each print command, print a string in a line.

Constraints

$1 \leq $ length of $str \leq 1000$

$1 \leq q \leq 100$

$0 \leq a \leq b < $ length of $str$

for replace command, $b - a + 1 = $ length of $p$

Sample Input 1

abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4

Sample Output 1

xaze

Sample Input 2

xyz
3
print 0 2
replace 0 2 abc
print 0 2

Sample Output 2

xyz
abc


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

Enumeration of Subsets I

Print all subsets of a set $S$, which contains $0, 1, ... n-1$ as elements. Note that we represent $0, 1, ... n-1$ as 00...0001, 00...0010, 00...0100, ..., 10...0000 in binary respectively and the integer representation of a subset is calculated by bitwise OR of existing elements.

Input

The input is given in the following format.

$n$

Output

Print subsets ordered by their decimal integers. Print a subset in a line in the following format.

$d$: $e_0$ $e_1$ ...

Print ':' after the integer value $d$, then print elements $e_i$ in the subset in ascending order. Seprate two adjacency elements by a space character.

Constraints

$1 \leq n \leq 18$

Sample Input 1

4

Sample Output 1

0:
1: 0
2: 1
3: 0 1
4: 2
5: 0 2
6: 1 2
7: 0 1 2
8: 3
9: 0 3
10: 1 3
11: 0 1 3
12: 2 3
13: 0 2 3
14: 1 2 3
15: 0 1 2 3

Note that if the subset is empty, your program should not output a space character after ':'.

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.


Set Union

Find the union of two sets $A = \{a_0, a_1, ..., a_{n-1}\}$ and $B = \{b_0, b_1, ..., b_{m-1}\}$.

Input

The input is given in the following format.

$n$
$a_0 \; a_1 \; ... \; a_{n-1}$
$m$
$b_0 \; b_1 \; ... \; b_{m-1}$

Elements of $A$ and $B$ are given in ascending order respectively. There are no duplicate elements in each set.

Output

Print elements in the union in ascending order. Print an element in a line.

Constraints

$1 \leq n, m \leq 200,000$

$0 \leq a_0 < a_1 < ... < a_{n-1} \leq 10^9$

$0 \leq b_0 < b_1 < ... < b_{m-1} \leq 10^9$

Sample Input 1

3
1 5 8
2
5 9

Sample Output 1

1
5
8
9


#!ruby -plrprime
$_<<[?:,$_.to_i.prime_division.map{|*p,e|p*e}]*" "

Prime Factorization

Factorize a given integer n.

Input

n

An integer n is given in a line.

Output

Print the given integer n and :. Then, print prime factors in ascending order. If n is divisible by a prime factor several times, the prime factor should be printed according to the number of times. Print a space before each prime factor.

Constraints

2 ≤ n ≤ 109

Sample Input 1

12

Sample Output 1

12: 2 2 3

Sample Input 2

126

Sample Output 2

126: 2 3 3 7


Power

For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M.

Input

m n

Two integers m and n are given in a line.

Output

Print mn (mod 1,000,000,007) in a line.

Constraints

1 ≤ m ≤ 100

1 ≤ n ≤ 109

Sample Input 1

2 3

Sample Output 1

8

Sample Input 2

5 8

Sample Output 2

390625


class UnionFind
  attr_reader :rank
  attr_reader :parent

  def initialize(size)
    @rank = Array.new(size, 0)
    @parent = Array.new(size, &:itself)
  end

  def unite(id_x, id_y)
    x_parent = get_parent(id_x)
    y_parent = get_parent(id_y)
    return if x_parent == y_parent

    if @rank[x_parent] > @rank[y_parent]
      @parent[y_parent] = x_parent
    else
      @parent[x_parent] = y_parent
      @rank[y_parent] += 1 if @rank[x_parent] == @rank[y_parent]
    end
  end

  def get_parent(id_x)
    @parent[id_x] == id_x ? id_x : (@parent[id_x] = get_parent(@parent[id_x]))
  end

  def same_parent?(id_x, id_y)
    get_parent(id_x) == get_parent(id_y)
  end
end

n, m = gets.strip.split.map(&:to_i)
union = UnionFind.new n + 1

ab = []
m.times do
  a, b = gets.strip.split.map(&:to_i)
  ab << [a, b]
end
ab.sort!

ab.each do |a, b|
  union.unite(a, b)
end

puts union.parent.uniq.length - 2

Score : 300 points

Problem Statement

There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M.
Road i connects City A_i and City B_i.

Snuke can perform the following operation zero or more times:

Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities.

After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times).

What is the minimum number of roads he must build to achieve the goal?

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 100,000

1 \leq A_i < B_i \leq N

No two roads connect the same pair of cities.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

Print the answer.

Sample Input 1

3 1
1 2

Sample Output 1

1

Initially, there are three cities, and there is a road between City 1 and City 2.

Snuke can achieve the goal by building one new road, for example, between City 1 and City 3.
After that,

We can travel between 1 and 2 directly.

We can travel between 1 and 3 directly.

We can travel between 2 and 3 by following both roads (2 - 1 - 3).


#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
using vi  = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  dsu d(n);
  rep(i,m) {
    int a, b;
    cin >> a >> b;
    d.merge(a, b);
  }
  cout << d.groups().size()-1 << endl;
}


n = $stdin.readline.to_i
m = Math::sqrt(2 * n).to_i

def ext_gcd(a, b)
  if b == 0
    return [a, 1, 0]
  end

  pg, px, py = ext_gcd(b, a % b)
  return [pg, py, px - (a / b) * py]
end

def chinese_rem(b1, m1, b2, m2)
  d, x, y = ext_gcd(m1, m2)
  if (b2 - b1) % d != 0
    return [nil, nil]
  end

  m = m1 * (m2 / d)
  t = (x * ((b2 - b1) / d)) % (m2 / d)
  r = (b1 + m1 * t) % m
  return [r, m]
end

k = []
1.step(m) do |x|
  if (2 * n) % x == 0
    y = (2 * n) / x
    kx, _ = chinese_rem(0, x, y - 1, y)
    k << kx if !kx.nil?
    kx, _ = chinese_rem(0, y, x - 1, x)
    k << kx if !kx.nil? && kx > 0
  end
end
puts k.min

Score : 600 points

Problem Statement

Given is an integer N.
Find the minimum possible positive integer k such that (1+2+\cdots+k) is a multiple of N.
It can be proved that such a positive integer k always exists.

Constraints

1 \leq N \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer in a line.

Sample Input 1

11

Sample Output 1

10

1+2+\cdots+10=55 holds and 55 is indeed a multple of N=11.
There are no positive integers k \leq 9 that satisfy the condition, so the answer is k = 10.

Sample Input 2

20200920

Sample Output 2

1100144


s = gets

if (s[-1] == 's')
	puts ("#{s}es")
else
  puts ("#{s}s")
end

Score : 100 points

Problem Statement

In the Kingdom of AtCoder, people use a language called Taknese, which uses lowercase English letters.

In Taknese, the plural form of a noun is spelled based on the following rules:

If a noun's singular form does not end with s, append s to the end of the singular form.

If a noun's singular form ends with s, append es to the end of the singular form.

You are given the singular form S of a Taknese noun. Output its plural form.

Constraints

S is a string of length 1 between 1000, inclusive.

S contains only lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the plural form of the given Taknese word.

Sample Input 1

apple

Sample Output 1

apples

apple ends with e, so its plural form is apples.

Sample Input 2

bus

Sample Output 2

buses

bus ends with s, so its plural form is buses.

Sample Input 3

box

Sample Output 3

boxs


N = gets.to_i
X = Array.new N
Y = Array.new N
dmax = 0
dmin = (10**9) * 2
N.times do |i|
    X[i], Y[i] = gets.split.map(&:to_i)
    d = X[i] + Y[i]
    if d > dmax
        dmax = d
    end
    if d < dmin
        dmin = d
    end
end

puts dmax - dmin

Score : 500 points

Problem Statement

There are N points on the 2D plane, i-th of which is located on (x_i, y_i).
There can be multiple points that share the same coordinate.
What is the maximum possible Manhattan distance between two distinct points?

Here, the Manhattan distance between two points (x_i, y_i) and (x_j, y_j) is defined by |x_i-x_j| + |y_i-y_j|.

Constraints

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

1 \leq x_i,y_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the answer.

Sample Input 1

3
1 1
2 4
3 2

Sample Output 1

4

The Manhattan distance between the first point and the second point is |1-2|+|1-4|=4, which is maximum possible.

Sample Input 2

2
1 1
1 1

Sample Output 2

0


Score : 100 points

Problem Statement

You are given two integer arrays a_0, a_1, ..., a_{N - 1} and b_0, b_1, ..., b_{M - 1}. Calculate the array c_0, c_1, ..., c_{(N - 1) + (M - 1)}, defined by c_i = \sum_{j = 0}^i a_j b_{i - j} \bmod 998244353.

Constraints

1 \leq N, M \leq 524288

0 \leq a_i, b_i < 998244353

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 ... a_{N-1}
b_0 b_1 ... b_{M-1}

Output

Print the answer in the following format:

c_0 c_1 ... c_{(N - 1) + (M - 1)}

Sample Input 1

4 5
1 2 3 4
5 6 7 8 9

Sample Output 1

5 16 34 60 70 70 59 36

Sample Input 2

1 1
10000000
10000000

Sample Output 2

871938225


### SNIPPET
  # n = gets.split.map(&:to_i)
  # array = n.times.map { gets.split.map(&:to_i) }
  # [].all?(&:even?)
  # a = [*1..m].repeated_combination(n).to_a
  # [1,2,3,4,5].select { |num| num.even? }  # => [2, 4]
  # ["a","a","b"].group_by(&:itself).map{|k,v| [k, v.count]}.to_h #=> {"a"=>2, "b"=>1}
  # 切り捨て: .floor(2).to_f ,切り上げ： .ceil(2).to_f,四捨五入: round(2)
  # 3.upto(6) do |i|, 6.downto(3) do |i|
  # 公約数125.gcd(100)、公倍数125.lcm(100)
  # PI = Math::PI
  # 高さ = a * Math.sin(w / 180.0 * Math::PI), 底辺 = a * Math.cos(w / 180.0 * Math::PI)
  # def chmax(a, b) a > b ? a : b end
  INF = Float::INFINITY
  # def chmin(a, b) a < b ? a : b end

s = gets.chomp
t = gets.chomp

# 7文字 -> 1文字
sa = s.length - t.length

def levenshtein_distance(s, t)
  di = 0
  s.length.times do |i|
    di += 1 if s[i] != t[i]
  end
  di
end

# puts sa
min = INF
0.upto(sa) do |i|
  bu = s.slice(i,t.length)
  # puts bu
  bu = levenshtein_distance(bu, t)
  # puts bu

  min = bu if min > bu
end

puts min

Score : 200 points

Problem Statement

Given are two strings S and T.

Let us change some of the characters in S so that T will be a substring of S.

At least how many characters do we need to change?

Here, a substring is a consecutive subsequence. For example, xxx is a substring of yxxxy, but not a substring of xxyxx.

Constraints

The lengths of S and T are each at least 1 and at most 1000.

The length of T is at most that of S.

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the minimum number of characters in S that need to be changed.

Sample Input 1

cabacc
abc

Sample Output 1

1

For example, changing the fourth character a in S to c will match the second through fourth characters in S to T.

Since S itself does not have T as its substring, this number of changes - one - is the minimum needed.

Sample Input 2

codeforces
atcoder

Sample Output 2

6


S = gets.chop.to_s
T = gets.chop.to_s

#puts S,T

count=T.length
for i in 0..S.length-T.length
  count_tmp=0
  for j in 0..T.length-1
    if(S[i+j]!=T[j])
      count_tmp+=1
    end
  end
  if(count_tmp<count)
    count=count_tmp
  end
end


s = gets.chomp.split('')
t = gets.chomp.split('')

s_l = s.length
t_l = t.length
max = s_l - t_l
min_cnt = t.length

(0..max).each do |i|
  cnt = 0
  s[i..(i + t_l - 1)].each_with_index do |ele,  i|
    cnt += 1 unless ele == t[i]
  end
  min_cnt = cnt if cnt < min_cnt
end

puts min_cnt


s = gets.chomp
t = gets.chomp

array = []
(s.length - t.length + 1).times do |i|
#   puts "#{s[i..t.length - 1 + i]} #{t}"
  cnt = 0
  t.length.times do |j|
    # puts "#{s[i..t.length - 1 + i][j]} #{t[j]}"
    if s[i..t.length - 1 + i][j] != t[j]
      cnt += 1
    end
  end
  array << cnt
end

puts array.min


n,x,t=gets.split.map(&:to_i)

v=n.div(x)
if n%x!=0
    v=v+1
end
p v*t

Score : 100 points

Problem Statement

Takahashi loves takoyaki - a ball-shaped snack.

With a takoyaki machine, he can make at most X pieces of takoyaki at a time, taking T minutes regardless of the number of pieces to make.

How long does it take to make N takoyaki?

Constraints

1 \leq N,X,T \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X T

Output

Print an integer representing the minimum number of minutes needed to make N pieces of takoyaki.

Sample Input 1

20 12 6

Sample Output 1

12

He can make 12 pieces of takoyaki in the first 6 minutes and 8 more in the next 6 minutes, so he can make 20 in a total of 12 minutes.

Note that being able to make 12 in 6 minutes does not mean he can make 2 in 1 minute.

Sample Input 2

1000 1 1000

Sample Output 2

1000000

It seems to take a long time to make this kind of takoyaki.


n = gets.chomp.split("").map(&:to_i)
puts (n.inject(:+) % 9 == 0) ? "Yes" : "No"

Score : 200 points

Problem Statement

An integer N is a multiple of 9 if and only if the sum of the digits in the decimal representation of N is a multiple of 9.

Determine whether N is a multiple of 9.

Constraints

0 \leq N < 10^{200000}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a multiple of 9, print Yes; otherwise, print No.

Sample Input 1

123456789

Sample Output 1

Yes

The sum of these digits is 1+2+3+4+5+6+7+8+9=45, which is a multiple of 9, so 123456789 is a multiple of 9.

Sample Input 2

0

Sample Output 2

Yes

Sample Input 3

31415926535897932384626433832795028841971693993751058209749445923078164062862089986280

Sample Output 3

No


  N = gets.to_i
  a = gets.split.map(&:to_i)

  ans=0
  high=0
  a.each do |ai|
    if ai<high
      ans += high - ai
    else
      high = ai
    end
  end

  puts ans

Score : 300 points

Problem Statement

N persons are standing in a row. The height of the i-th person from the front is A_i.

We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:

Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.

Find the minimum total height of the stools needed to meet this goal.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum total height of the stools needed to meet the goal.

Sample Input 1

5
2 1 5 4 3

Sample Output 1

4

If the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively, their heights will be 2, 2, 5, 5, and 5, satisfying the condition.

We cannot meet the goal with a smaller total height of the stools.

Sample Input 2

5
3 3 3 3 3

Sample Output 2

0

Giving a stool of height 0 to everyone will work.


require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7

h,w = gets.chomp.split.map(&:to_i)
ch, cw = gets.chomp.split.map(&:to_i).map{|e| e-1}
dh, dw = gets.chomp.split.map(&:to_i).map{|e| e-1}
grid = Array.new(h)
# h = w= 100
# dh, dw = 99, 99
h.times do |i|
  grid[i] = gets.chomp.chars.map(&:to_sym)
  # grid.push(('.' * w).chars)
end

q = []
q.push [0, ch, cw]
grid[ch][cw] = 0

ans = 10000000
until q.empty?
  count, y, x = q.shift
  break if count >= ans
  gridyx = grid[y][x]
  # p ['main', count, [y, x], gridyx]
  next if gridyx != :'#' && gridyx != :'.' && gridyx < count

  [y-2, 0].max.upto([y+2,h-1].min) do |ny|
    [x-2, 0].max.upto([x+2,w-1].min) do |nx|
      next if ny == y && nx == x
      ncount = count
      is_near = (ny-y).abs + (nx-x).abs == 1
      ncount += 1 unless is_near

      val = grid[ny][nx]
      if ny == dh && nx == dw
        ans = [ans, ncount].min
      elsif val == :'#'
      # skip
      elsif val == :'.' || val > ncount
        grid[ny][nx] = ncount
        if is_near
          q.unshift [ncount, ny, nx]
        else
          q.push [ncount, ny, nx]
        end
      end
    end
  end
end

# grid.each{|l| p l }
if ans == 10000000
  puts -1
else
  puts ans
end

Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2


require 'pp'

h, w = gets.split.map(&:to_i)
ch, cw = gets.split.map(&:to_i)
dh, dw = gets.split.map(&:to_i)
s = []
h.times do |i|
    t = gets.chomp!.chars
    s[i] = t
end

# grouping
added = Array.new(h)
groups = Array.new(h)
h.times do |i|
    added[i] = Array.new(w, 0)
    groups[i] = Array.new(w, 0)
end
groups = {}
g_num = 1
h.times do |i2|
    w.times do |j2|
        next if s[i2][j2] != "."

        #added[i2][j2] = g_num
        queue = []
        queue << [i2, j2, g_num]

        while queue.empty?.!
            i, j, num = queue.pop
            if added[i][j] == 0
                added[i][j] = num
                groups[num] = {}
            end
            if j > 0 && s[i][j-1] && s[i][j-1] == '.'
                if added[i][j-1] == 0
                    added[i][j-1] = num
                    groups[num] = {}
                    queue << [i, j-1, num]
                end
            end
            if j < w - 1 && s[i][j+1] && s[i][j+1] == "."
                if added[i][j+1] == 0
                    added[i][j+1] = num
                    groups[num] = {}
                    queue << [i, j+1, num]
                end
            end
            if i > 0 && s[i-1][j] && s[i-1][j] == "."
                if added[i-1][j] == 0
                    added[i-1][j] = num
                    groups[num] = {}
                    queue << [i-1, j, num]
                end
            end
            if i < h - 1 && s[i+1][j] && s[i+1][j] == "."
                if added[i+1][j] == 0
                    added[i+1][j] = num
                    groups[num] = {}
                    queue << [i+1, j, num]
                end
            end

            #pp queue
            #gets
        end

        g_num += 1
    end
end

#pp added
#pp groups

h.times do |i2|
    w.times do |j2|
        next if added[i2][j2] == 0

        g = added[i2][j2]

        (-2).upto(2) do |i|
            (-2).upto(2) do |j|
                next if i2 + i < 0 || i2 + i >= h || j2 + j < 0 || j2 + j >= w
                next if added[i2+i][j2+j] == 0

                if g != added[i2+i][j2+j]
                    groups[g][added[i2+i][j2+j]] = 1
                end
            end
        end
    end
end

#pp groups

start = added[ch-1][cw-1]
ed = added[dh-1][dw-1]

#pp start
#pp ed

v = Array.new(g_num, 10**10)
q2 = []
q2 << [start, 0]

while q2.empty?.!
    t, d = q2.pop

    next if d >= v[t]

    v[t] = d

    groups[t].each do |k, _|
        q2.push([k, d+1])
    end
end

if v[ed] == 10 ** 10
    puts "-1"
else
    puts v[ed]
end


x,k,d = gets.split(" ").map(&:to_i)

if x > 0 && x >= k * d
  p x - (k * d)
  return
elsif x < 0 && - x >= k * d
  p -x - (k * d)
  return
end

for num in 1..k do
  if x > 0
    x = x - d
  else
    x = x + d
  end
  if (k - num).even?
    if x == 0
      p x
      return
    elsif x > 0 && x <= d
      p x
      return
    elsif x < 0 && -x <= d
      p -x
      return
    end
  end
end

if x > 0
  p x
else
  p -x
end

Score : 100 points

Problem Statement

We have weather records at AtCoder Town for some consecutive three days. A string of length 3, S, represents the records - if the i-th character is S, it means it was sunny on the i-th day; if that character is R, it means it was rainy on that day.

Find the maximum number of consecutive rainy days in this period.

Constraints

|S| = 3

Each character of S is S or R.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of consecutive rainy days in the period.

Sample Input 1

RRS

Sample Output 1

2

We had rain on the 1-st and 2-nd days in the period. Here, the maximum number of consecutive rainy days is 2, so we should print 2.

Sample Input 2

SSS

Sample Output 2

0

It was sunny throughout the period. We had no rainy days, so we should print 0.

Sample Input 3

RSR

Sample Output 3

1

We had rain on the 1-st and 3-rd days - two "streaks" of one rainy day, so we should print 1.


n=gets.to_i
a=gets.split.map(&:to_i).sort
cnt=0
n=a.size
for i in 0...n-2
  for j in i+1...n-1
    for k in j+1...n
      if a[i]+a[j]<a[k]&&(!(a[i]==a[j]||a[j]==a[k]))
        cnt+=1
      end
    end
  end
end
puts cnt

Score : 200 points

Problem Statement

We have sticks numbered 1, \cdots, N. The length of Stick i (1 \leq i \leq N) is L_i.

In how many ways can we choose three of the sticks with different lengths that can form a triangle?

That is, find the number of triples of integers (i, j, k) (1 \leq i < j < k \leq N) that satisfy both of the following conditions:

L_i, L_j, and L_k are all different.

There exists a triangle whose sides have lengths L_i, L_j, and L_k.

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 \cdots L_N

Output

Print the number of ways to choose three of the sticks with different lengths that can form a triangle.

Sample Input 1

5
4 4 9 7 5

Sample Output 1

5

The following five triples (i, j, k) satisfy the conditions: (1, 3, 4), (1, 4, 5), (2, 3, 4), (2, 4, 5), and (3, 4, 5).

Sample Input 2

6
4 5 4 3 3 5

Sample Output 2

8

We have two sticks for each of the lengths 3, 4, and 5. To satisfy the first condition, we have to choose one from each length.

There is a triangle whose sides have lengths 3, 4, and 5, so we have 2 ^ 3 = 8 triples (i, j, k) that satisfy the conditions.

Sample Input 3

10
9 4 6 1 9 6 10 6 6 8

Sample Output 3

39

Sample Input 4

2
1 1

Sample Output 4

0

No triple (i, j, k) satisfies 1 \leq i < j < k \leq N, so we should print 0.


# frozen_string_literal: true

X, K, D = gets.chomp.split.map(&:to_i)

count_moveing_to_near = X / D
nokori = K - count_moveing_to_near
near_point = X - count_moveing_to_near * D

if count_moveing_to_near >= K
  if X >= 0
    puts (X - D * K).abs
  else
    puts (X + D * K).abs
  end
  exit
end

if X % D != 0

  if nokori == 0
    puts near_point.abs
  elsif nokori.odd? && near_point > 0
    puts (near_point - D).abs
  elsif nokori.odd? && near_point < 0
    puts (near_point + D).abs
  elsif nokori.even?
    puts near_point.abs
  end
else
  if nokori == 0
    puts near_point
  elsif nokori.odd?
    puts D
  else
    puts 0
  end
end

Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.


Score : 100 points

Problem Statement

You will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above.

The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner?

Constraints

-40 \leq X \leq 40

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print Yes if you will turn on the air conditioner; print No otherwise.

Sample Input 1

25

Sample Output 1

No

Sample Input 2

30

Sample Output 2

Yes


x = gets.to_i
if x >= 30
  puts "Yes"
else
  puts "No"
end


N,D = gets.chomp.split.map(&:to_i)
XY = N.times.map{gets.chomp.split.map(&:to_i)}
count = 0

(0...N).each do |i|
  if ((XY[i][0]**2) + (XY[i][1]**2)) <= D**2
    count += 1
  end
end

puts count

Score : 200 points

Problem Statement

We have N points in the two-dimensional plane. The coordinates of the i-th point are (X_i,Y_i).

Among them, we are looking for the points such that the distance from the origin is at most D. How many such points are there?

We remind you that the distance between the origin and the point (p, q) can be represented as \sqrt{p^2+q^2}.

Constraints

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

0 \leq D \leq 2\times 10^5

|X_i|,|Y_i| \leq 2\times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D
X_1 Y_1
\vdots
X_N Y_N

Output

Print an integer representing the number of points such that the distance from the origin is at most D.

Sample Input 1

4 5
0 5
-2 4
3 4
4 -4

Sample Output 1

3

The distance between the origin and each of the given points is as follows:

\sqrt{0^2+5^2}=5

\sqrt{(-2)^2+4^2}=4.472\ldots

\sqrt{3^2+4^2}=5

\sqrt{4^2+(-4)^2}=5.656\ldots

Thus, we have three points such that the distance from the origin is at most 5.

Sample Input 2

12 3
1 1
1 1
1 1
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Sample Output 2

7

Multiple points may exist at the same coordinates.

Sample Input 3

20 100000
14309 -32939
-56855 100340
151364 25430
103789 -113141
147404 -136977
-37006 -30929
188810 -49557
13419 70401
-88280 165170
-196399 137941
-176527 -61904
46659 115261
-153551 114185
98784 -6820
94111 -86268
-30401 61477
-55056 7872
5901 -163796
138819 -185986
-69848 -96669

Sample Output 3

6


n, d = gets.split.map(&:to_i)
d = d**2
ans = 0
$<.map{|l|
  x, y = l.split.map(&:to_i)
  ans += 1 if x**2 + y**2 <= d
}
p ans


k = gets.to_s.to_i

if k.even? || (k % 5).zero?
  puts -1
  exit
end

s = 0
(1 .. 10**6).each do |i|
  s = (s * 10 + 7) % k
  if s == 0
    puts i
    exit
  end
end

puts -1

Score : 300 points

Problem Statement

Takahashi loves the number 7 and multiples of K.

Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots? (Also see Output and Sample Input/Output below.)

If the sequence contains no multiples of K, print -1 instead.

Constraints

1 \leq K \leq 10^6

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

Sample Input 1

101

Sample Output 1

4

None of 7, 77, and 777 is a multiple of 101, but 7777 is.

Sample Input 2

2

Sample Output 2

-1

All elements in the sequence are odd numbers; there are no multiples of 2.

Sample Input 3

999983

Sample Output 3

999982


K = gets.chomp.to_i

a = 1

unless [1, 3, 7, 9].include?(K % 10)
  puts -1
  exit
end

tmp = 0
ichi = K % 10
while true
  tmp_mod_10 = tmp % 10
  kakeru = if tmp_mod_10 <= 7
             (0..9).find{ |n| (n * ichi % 10) == (7 - tmp_mod_10) }
           else
             (0..9).find{ |n| (n * ichi % 10) == (17 - tmp_mod_10) }
           end
  kaketa = K * kakeru
  tmp = (tmp + kaketa) / 10
  break if tmp.to_s.each_char.all?{ |c| c == "7" }
  a += 1
end

puts (a + tmp.to_s.length)


K = gets.chomp.to_i

a = 1

unless [1, 3, 7, 9].include?(K % 10)
  puts -1
  exit
end

tmp = 0
ichi = K % 10
while true
  tmp_mod_10 = tmp % 10
  kakeru = (0..9).find{ |n| (n * ichi % 10) == if (tmp_mod_10 <= 7) then (7 - tmp_mod_10) else (17 - tmp_mod_10) end }
  kaketa = K * kakeru
  tmp = (tmp + kaketa) / 10
  break if tmp.to_s.each_char.all?{ |c| c == "7" }
  a += 1
end

puts (a + tmp.to_s.length)


k = gets.chomp.to_i

arr = k.to_s.split("")
if arr.uniq.length == 1 && arr[0] == "7"
  puts arr.length
  exit
end

if k % 2 == 0 || k % 5 == 0
  puts -1
  exit
end

count = 0
i = 0
while true do
  i = i * 10 + 1
  i %= k
  count += 1
  if i == 0
    break
  end
end

puts count


N = gets.to_i
c = gets.chomp.split('').map(&:to_s)
a = c.count('R')

if a == 0
puts 0
else
x = c[0..a-1].count('W')
y = c[a..N-1].count('R')
puts [x,y].max
end

Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3


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

sum = 1
ok = true
for r in 0...n
    l = r - k
    pre = sum
    sum *= a[r]
    next if l < 0

    sum /= a[l]
    if pre < sum
        puts "Yes"
    else
        puts "No"
    end
end

Score: 300 points

Problem Statement

M-kun is a student in Aoki High School, where a year is divided into N terms.

There is an exam at the end of each term. According to the scores in those exams, a student is given a grade for each term, as follows:

For the first through (K-1)-th terms: not given.

For each of the K-th through N-th terms: the multiplication of the scores in the last K exams, including the exam in the graded term.

M-kun scored A_i in the exam at the end of the i-th term.

For each i such that K+1 \leq i \leq N, determine whether his grade for the i-th term is strictly greater than the grade for the (i-1)-th term.

Constraints

2 \leq N \leq 200000

1 \leq K \leq N-1

1 \leq A_i \leq 10^{9}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 A_3 \ldots A_N

Output

Print the answer in N-K lines.

The i-th line should contain Yes if the grade for the (K+i)-th term is greater than the grade for the (K+i-1)-th term, and No otherwise.

Sample Input 1

5 3
96 98 95 100 20

Sample Output 1

Yes
No

His grade for each term is computed as follows:

3-rd term: (96 \times 98 \times 95) = 893760

4-th term: (98 \times 95 \times 100) = 931000

5-th term: (95 \times 100 \times 20) = 190000

Sample Input 2

3 2
1001 869120 1001

Sample Output 2

No

Note that the output should be No if the grade for the 3-rd term is equal to the grade for the 2-nd term.

Sample Input 3

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

Sample Output 3

Yes
Yes
No
Yes
Yes
No
Yes
Yes


l,r,d = gets.chomp.split(' ').map(&:to_i)
count = 0
(l..r).each { |i| count += 1if i % d == 0 }
puts count

Score : 100 points

Problem Statement

How many multiples of d are there among the integers between L and R (inclusive)?

Constraints

All values in input are integers.

1 \leq L \leq R \leq 100

1 \leq d \leq 100

Input

Input is given from Standard Input in the following format:

L R d

Output

Print the number of multiples of d among the integers between L and R (inclusive).

Sample Input 1

5 10 2

Sample Output 1

3

Among the integers between 5 and 10, there are three multiples of 2: 6, 8, and 10.

Sample Input 2

6 20 7

Sample Output 2

2

Among the integers between 6 and 20, there are two multiples of 7: 7 and 14.

Sample Input 3

1 100 1

Sample Output 3

100


io = STDIN
l,r,d=io.gets.split.map(&:to_i)
puts r/d - (l-1)/d


l, r, d = gets.chomp.split.map(&:to_i)
ans = 0
l.upto(r) do |i|
    ans += 1 if i % d == 0
end
puts ans


require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
n = gets.chomp.to_i

ans = Array.new(n+1, 0)
1.upto(100) do |i|
  num = i*i
  1.upto(100) do |j|
    num2 = num + j*j + i * j
    # break if num2 > n
    1.upto(100) do |k|
      num3 = num2 + k*k + i*k + j*k
      if num3 > n
        break
      else
        ans[num3] += 1
      end
    end
  end
end

ans.shift
ans.each do |e|
  puts e
end

Score : 300 points

Problem Statement

Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions:

1 \leq x,y,z

x^2 + y^2 + z^2 + xy + yz + zx = n

Given an integer N, find each of f(1),f(2),f(3),\ldots,f(N).

Constraints

All values in input are integers.

1 \leq N \leq 10^4

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the value f(i).

Sample Input 1

20

Sample Output 1

0
0
0
0
0
1
0
0
0
0
3
0
0
0
0
0
3
3
0
0

For n=6, only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1.

For n=11, three triples, (1,1,2), (1,2,1), and (2,1,1), satisfy both of the conditions. Thus, f(6) = 3.

For n=17, three triples, (1,2,2), (2,1,2), and (2,2,1), satisfy both of the conditions. Thus, f(17) = 3.

For n=18, three triples, (1,1,3), (1,3,1), and (3,1,1), satisfy both of the conditions. Thus, f(18) = 3.


# 解説込み
N = gets.to_i
X = gets.chomp

time_arr = [0]
count = X.count("1")
x_num = X.to_i(2)

count.times do |i|
  n = i + 1
  c = n.to_s(2).count("1")
  surp = n % c
  time_arr[n] = time_arr[surp] + 1
end

N.times do |i|
  n = x_num.dup
  if X[i] == "1"
    c = count - 1
    n -= 2 ** (N - i - 1)
  else
    c = count + 1
    n += 2 ** (N - i - 1)
  end

  if n == 0
    puts 0
    next
  end

  surp = n % c
  puts time_arr[surp] + 1
end

Score : 400 points

Problem Statement

Let \mathrm{popcount}(n) be the number of 1s in the binary representation of n.
For example, \mathrm{popcount}(3) = 2, \mathrm{popcount}(7) = 3, and \mathrm{popcount}(0) = 0.

Let f(n) be the number of times the following operation will be done when we repeat it until n becomes 0: "replace n with the remainder when n is divided by \mathrm{popcount}(n)." (It can be proved that, under the constraints of this problem, n always becomes 0 after a finite number of operations.)

For example, when n=7, it becomes 0 after two operations, as follows:

\mathrm{popcount}(7)=3, so we divide 7 by 3 and replace it with the remainder, 1.

\mathrm{popcount}(1)=1, so we divide 1 by 1 and replace it with the remainder, 0.

You are given an integer X with N digits in binary.
For each integer i such that 1 \leq i \leq N, let X_i be what X becomes when the i-th bit from the top is inverted.
Find f(X_1), f(X_2), \ldots, f(X_N).

Constraints

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

X is an integer with N digits in binary, possibly with leading zeros.

Input

Input is given from Standard Input in the following format:

N
X

Output

Print N lines. The i-th line should contain the value f(X_i).

Sample Input 1

3
011

Sample Output 1

2
1
1

X_1 = 7, which will change as follows: 7 \rightarrow 1 \rightarrow 0. Thus, f(7) = 2.

X_2 = 1, which will change as follows: 1 \rightarrow 0. Thus, f(1) = 1.

X_3 = 2, which will change as follows: 2 \rightarrow 0. Thus, f(2) = 1.

Sample Input 2

23
00110111001011011001110

Sample Output 2

2
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
3


N = gets.to_i
X = gets.chomp
arr = {}

N.times do |i|
  x = X.dup
  x[i] = (1 - x[i].to_i).to_s

  time = 0
  N.times do
    time += 1

    num = x.to_i(2)
	count = arr[num] || x.chars.count("1")
  	arr[num] = count

    surp = num % count
    if surp == 0
      puts time
      break
    end

    num = surp
    x = num.to_s(2)
  end
end


ac = 0
wa = 0
tle = 0
re = 0

n = gets.to_i

results = n.times.map { gets.chomp }

results.each do |e|
  case e
  when 'AC'
    ac += 1
  when 'WA'
    wa += 1
  when 'TLE'
    tle += 1
  when 'RE'
    re += 1
  end
end

print "AC x #{ac}\n"
print "WA x #{wa}\n"
print "TLE x #{tle}\n"
print "RE x #{re}\n"

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

1 \leq N \leq 10^5

S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.

Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0


N = gets.to_i
A = gets.chomp.split.map(&:to_i).sort.reverse

sum = 0
A.size.times{ |i|
  next if i == 0
  sum += A[i / 2]
}

puts sum

Score: 400 points

Problem Statement

Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.

The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.

When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.

What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the maximum total comfort the N players can get.

Sample Input 1

4
2 2 1 3

Sample Output 1

7

By arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.

They cannot get the total comfort greater than 7, so the answer is 7.

Sample Input 2

7
1 1 1 1 1 1 1

Sample Output 2

6


M=10**9+7
N,K=gets.split.map(&:to_i)
A=gets.split.map(&:to_i).sort{|l,r|l.abs<=>r.abs}
PLS=A.select{|a|0<=a}.sort{|l,r|r<=>l}
MNS=A.select{|a|a<0}.sort

def calc(n)
  p=K-n
  return 0 if MNS.size<n
  return 0 if PLS.size<p
  PLS[0...p].inject(1){|s,a|s*a%M}*MNS[0...n].inject(1){|s,a|s*a%M}%M
end

if K.odd? && A.all?{|a|a<=0}
  s=A[0...K].inject(1){|s,a|s*a%M}
  s+=M
  p s%M
  exit
end

n=A[-K..-1].count{|a|a<0}
p n.even? ? calc(n) : [calc(n-1),calc(n+1)].max

Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

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

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).


N, K = gets.split.map(&:to_i)
AS = gets.split.map(&:to_i)

def nums
  return AS if N == K

  sorted = AS.sort_by(&:abs).reverse

  return sorted.last(K) if AS.all? { |n| n < 0 } && K % 2 == 1

  nums = sorted.first(K)

  return nums unless nums.select { |n| n < 0 }.count % 2 == 1

  posplus = begin
      plus = sorted[K..-1].find { |n| n > 0 } || 0
      swap = nums.reverse.find { |n| n < 0 } || 0
      [plus, swap]
    end

  negplus = begin
      plus = sorted[K..-1].find { |n| n < 0 } || 0
      swap = nums.find { |n| n > 0 } || 0
      [plus, swap]
    end

  if (posplus[0] * posplus[1]).abs >= (negplus[0] * negplus[1]).abs
    nums.map { |n| n == posplus[1] ? posplus[0] : n }
  else
    nums.map { |n| n == negplus[1] ? negplus[0] : n }
  end
end

result = 1
mod = 10 ** 9 + 7
nums.each do |n|
  result = result * n % mod
end

puts result


require "pp"

a = gets.to_i
puts a + a*a + a*a*a

Score : 100 points

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

Constraints

1 \leq a \leq 10

a is an integer.

Input

Input is given from Standard Input in the following format:

a

Output

Print the value a + a^2 + a^3 as an integer.

Sample Input 1

2

Sample Output 1

14

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.

Sample Input 2

10

Sample Output 2

1110


input = gets
input.chomp!

# input = readlines()
# meta = input.shift.chomp.split("\s").map{|x| x.to_i}

n = input.to_i

puts n + n**2 + n**3


s = gets.chomp
t = gets.chomp

c = 0
s.length.times do |i|
  c += 1 if s[i] != t[i]
end

p c

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

S and T have lengths between 1 and 2\times 10^5 (inclusive).

S and T consists of lowercase English letters.

S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.

Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

First, replace the sixth character c with h.

Second, replace the eighth character f with t.

Third, replace the ninth character f with t.

Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.


s = gets.chomp
t = gets.chomp
cnt = 0

s.length.times do |i|
  if s[i] != t[i]
    cnt += 1
  end
end

puts cnt


s=gets.chars
t=gets.chars
c=0
for i in 0..(s.length-2)
  c+=1 if s[i]!=t[i]
end
puts c


N = gets.to_i
A = gets.split.map &:to_i
ans = 0
while true
    if (A.inject(:^) == 0)
        p ans
        exit
    end
    if (A[0] == 1)
        p -1
        exit
    end
    A[0] -= 1
    A[1] += 1
    ans += 1
end

Score : 600 points

Problem Statement

There are N piles of stones. The i-th pile has A_i stones.

Aoki and Takahashi are about to use them to play the following game:

Starting with Aoki, the two players alternately do the following operation:

Operation: Choose one pile of stones, and remove one or more stones from it.

When a player is unable to do the operation, he loses, and the other player wins.

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

Constraints

2 \leq N \leq 300

1 \leq A_i \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

Sample Input 1

2
5 3

Sample Output 1

1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

Sample Input 2

2
3 5

Sample Output 2

-1

It is not allowed to move stones from the 2-nd pile to the 1-st.

Sample Input 3

3
1 1 2

Sample Output 3

-1

It is not allowed to move all stones from the 1-st pile.

Sample Input 4

8
10 9 8 7 6 5 4 3

Sample Output 4

3

Sample Input 5

3
4294967297 8589934593 12884901890

Sample Output 5

1

Watch out for overflows.


N,K = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)

b = a.sort

sum = 0
b[0..K-1].each do |i|
  sum += i
end

puts sum

Score : 200 points

Problem Statement

A shop sells N kinds of fruits, Fruit 1, \ldots, N, at prices of p_1, \ldots, p_N yen per item, respectively. (Yen is the currency of Japan.)

Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits.

Constraints

1 \leq K \leq N \leq 1000

1 \leq p_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 p_2 \ldots p_N

Output

Print an integer representing the minimum possible total price of fruits.

Sample Input 1

5 3
50 100 80 120 80

Sample Output 1

210

This shop sells Fruit 1, 2, 3, 4, and 5 for 50 yen, 100 yen, 80 yen, 120 yen, and 80 yen, respectively.

The minimum total price for three kinds of fruits is 50 + 80 + 80 = 210 yen when choosing Fruit 1, 3, and 5.

Sample Input 2

1 1
1000

Sample Output 2

1000


n = gets.to_i
letters = "abcdefghijklmnopqrstuvwxyz".split("")
name = []

while n > 0
    name.push(letters[n % 26 - 1])
    n = n / 26
end

print name.reverse.join("")

Score : 300 points

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

the dogs numbered 1,2,\cdots,26 were respectively given the names a, b, ..., z;

the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;

the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;

the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;

the dogs numbered 475255,475256,\cdots were respectively given the names aaaaa, aaaab, ...;

and so on.

To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names:

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

Now, Roger asks you:

"What is the name for the dog numbered N?"

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


ans = 0
d = []
11.times do |i|
  ans += 26 ** (i+1)
  d << ans
end

n = gets.to_s.to_i
base = d.bsearch{ n <= _1 }
count = d.bsearch_index{ n <= _1 }
count += 1 #桁数
n -= base / 26

a = []
count.times do
  a << n % 26
  n /= 26
end
puts a.map{ (_1 + 97).chr }.reverse.join


x = []
x = gets.chomp.split(' ').map(&:to_i)

for i in 0...5 do
  if(x[i] == 0)
   puts i+1
  end
end

Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.

Sample Input 2

1 2 0 4 5

Sample Output 2

3


x, y = gets.split.map(&:to_i)

if (y/6)*2 + ((y%6)/4) + (((y%6)%4)/2) == x
  puts 'Yes'
else puts 'No'
end

Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.

Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.


### SNIPPET
  # n = gets.split.map(&:to_i)
  # array = n.times.map { gets.split.map(&:to_i) }
  # [].all?(&:even?)
  # a = [*1..m].repeated_combination(n).to_a
  # [1,2,3,4,5].select { |num| num.even? }  # => [2, 4]
  # ["a","a","b"].group_by(&:itself).map{|k,v| [k, v.count]}.to_h #=> {"a"=>2, "b"=>1}
  # 切り捨て: .floor(2).to_f ,切り上げ： .ceil(2).to_f,四捨五入: round(2)
  # 3.upto(6) do |i|, 6.downto(3) do |i|
  # 公約数125.gcd(100)、公倍数125.lcm(100)
  # PI = Math::PI
  # 高さ = a * Math.sin(w / 180.0 * Math::PI), 底辺 = a * Math.cos(w / 180.0 * Math::PI)
  # def chmax(a, b) a > b ? a : b end
  # INF = Float::INFINITY
  # def chmin(a, b) a < b ? a : b end

x,y = gets.split.map(&:to_i)

flag = false
0.upto(x) do |tsuru|  # 0,1,2,3
  (x-tsuru).downto(0) do |kame| # 3    0
    if ((tsuru*2) + (kame*4)) == y
      flag = true
    end
  end
end

puts flag ? "Yes" : "No"


x, y = gets.split.map(&:to_i)

if (4 * x - y) % 2 == 0 && (y - x * 2) % 2 == 0
  puts 'Yes'
else
  puts 'No'
end


X, N = gets.split.map(&:to_i)
PS = gets.split.map(&:to_i)
x = X
i = 0
loop do
  if !PS.include?(x+i)
    puts x+i
    exit
  end
  if i >= 0
    i = (-i)-1
  else
    i = (-i)
  end
end

Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.


x, n = gets.chomp.split.map(&:to_i)
num = gets.chomp.split.map(&:to_i)

array = (1..100).to_a

num.each do |e|
  array.delete(e)
end

puts array.min_by{|i| (i - x).abs}


x, n = gets.split.map(&:to_i)
pn = gets.split.map(&:to_i)
serial_number = [*1..100]
array = serial_number - pn
puts array.min_by {|num| (num - x).abs}


class InputLine
  def self.to_s
    gets.chomp
  end

  def self.to_i
    InputLine.to_s.to_i
  end

  def self.to_f
    InputLine.to_s.to_f
  end

  def self.to_sa
    InputLine.to_s.split(' ')
  end

  def self.to_ia
    InputLine.to_sa.map(&:to_i)
  end

  def self.to_fa
    InputLine.to_sa.map(&:to_f)
  end
end

def main(argv)
  (x, n) = InputLine.to_ia
  p = InputLine.to_ia.sort

  min_diff_abs = 1e+8.to_i
  min_val = x
  (-200..200).each do |v|
    u = p.bsearch{|q| q >= v}

    next if u == nil || u == v

    if (x - v).abs < min_diff_abs then
      min_diff_abs = (x - v).abs
      min_val = v
    end
  end
  puts min_val.to_s
end

if self.to_s == 'main' then
  main(ARGV)
end


Score : 100 points

Problem Statement

When you asked some guy in your class his name, he called himself S, where S is a string of length between 3 and 20 (inclusive) consisting of lowercase English letters.
You have decided to choose some three consecutive characters from S and make it his nickname. Print a string that is a valid nickname for him.

Constraints

3 \leq |S| \leq 20

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print your answer.

Sample Input 1

takahashi

Sample Output 1

tak

Sample Input 2

naohiro

Sample Output 2

nao


A, V = gets.split.map(&:to_i)
B, W = gets.split.map(&:to_i)
T = gets.to_i

d = (A - B).abs
t = (V - W)

if d <= T * t
  puts 'YES'
else
  puts 'NO'
end

Score : 200 points

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second.
The other child is now at coordinate B, and she can travel the distance of W per second.

He can catch her when his coordinate is the same as hers.
Determine whether he can catch her within T seconds (including exactly T seconds later).
We assume that both children move optimally.

Constraints

-10^9 \leq A,B \leq 10^9

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

If "it" can catch the other child, print YES; otherwise, print NO.

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


A,V = gets.chomp.split.map(&:to_i)
B,W = gets.chomp.split.map(&:to_i)
T = gets.to_i

if (A-B).abs <= (V-W)*T
  puts "YES"
else
  puts "NO"
end


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;
using ll = long long int;

#define rep(i, n) for (ll i = 0; i < (n); i++)

ll INF = 1000000000000000000;

ll fun(ll fs) {
  ll i = 1;
  ll a = 0;
  while (1) {
    a += i;
    if (fs < a) {
      return i - 1;
    }
    i++;
  }
}

int main() {
  ll n;
  cin >> n;

  ll k = 2;
  ll sq = static_cast<ll>(sqrt(n));

  ll sum = 0;

  while (k <= sq + 10) {
    ll fs = 0;
    while (n % k == 0) {
      fs++;
      n /= k;
    }

    if (fs > 0) {
//      cout << k << endl;
    }

    ll tmp = fun(fs);
//    cout << tmp << endl;
    sum += tmp;

    k++;
  }

  // prime number
  if (n > 1) {
//    cout << "prime" << endl;
    sum++;
  }

  cout << sum << endl;

  return 0;
}

Score : 400 points

Problem Statement

Given is a positive integer N. Consider repeatedly applying the operation below on N:

First, choose a positive integer z satisfying all of the conditions below:

z can be represented as z=p^e, where p is a prime number and e is a positive integer;

z divides N;

z is different from all integers chosen in previous operations.

Then, replace N with N/z.

Find the maximum number of times the operation can be applied.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum number of times the operation can be applied.

Sample Input 1

24

Sample Output 1

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=12.)

Choose z=3 (=3^1). (Now we have N=4.)

Choose z=4 (=2^2). (Now we have N=1.)

Sample Input 2

1

Sample Output 2

0

We cannot apply the operation at all.

Sample Input 3

64

Sample Output 3

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=32.)

Choose z=4 (=2^2). (Now we have N=8.)

Choose z=8 (=2^3). (Now we have N=1.)

Sample Input 4

1000000007

Sample Output 4

1

We can apply the operation once by, for example, making the following choice:

z=1000000007 (=1000000007^1). (Now we have N=1.)

Sample Input 5

997764507000

Sample Output 5

7


N = gets.to_i
A, B = N.times.map{ gets.split.map &:to_i }.transpose.map &:sort
B.reverse!
if N.even?
    left = A[N / 2] - A[N / 2 - 1]
    mid = B[N / 2] - A[N / 2]
    right = B[N / 2 - 1] - B[N / 2]
    p (left > 0 ? ((left + 2) / 2) : 0) + (mid >= 0 ? 2 * mid + 1 : 0) + (right > 0 ? ((right + 2) / 2) : 0)
else
    left = A[N / 2]
    right = B[N / 2]
    p right - left + 1
end

Score : 500 points

Problem Statement

There are N integers X_1, X_2, \cdots, X_N, and we know that A_i \leq X_i \leq B_i.
Find the number of different values that the median of X_1, X_2, \cdots, X_N can take.

Notes

The median of X_1, X_2, \cdots, X_N is defined as follows. Let x_1, x_2, \cdots, x_N be the result of sorting X_1, X_2, \cdots, X_N in ascending order.

If N is odd, the median is x_{(N+1)/2};

if N is even, the median is (x_{N/2} + x_{N/2+1}) / 2.

Constraints

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

1 \leq A_i \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

2
1 2
2 3

Sample Output 1

3

If X_1 = 1 and X_2 = 2, the median is \frac{3}{2};

if X_1 = 1 and X_2 = 3, the median is 2;

if X_1 = 2 and X_2 = 2, the median is 2;

if X_1 = 2 and X_2 = 3, the median is \frac{5}{2}.

Thus, the median can take three values: \frac{3}{2}, 2, and \frac{5}{2}.

Sample Input 2

3
100 100
10 10000
1 1000000000

Sample Output 2

9991


require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=998244353

class Combi
  def initialize(max_n, mod = 10**9+7)
    @mod = mod
    @max_n = max_n

    @fac = Array.new(@max_n+1)
    @inv = Array.new(@max_n+1)
    @finv = Array.new(@max_n+1)

    @fac[0] = @fac[1] = 1
    @finv[0] = @finv[1] = 1
    @inv[1] = 1
    2.upto(@max_n) do |i|
      @fac[i] = @fac[i-1] * i % @mod
      @inv[i] = @mod - @inv[@mod%i] * (@mod/i) % @mod
      @finv[i] = @finv[i-1] * @inv[i] % @mod
    end
  end

  def nCk(n,k)
    @fac[n] * @finv[n - k] * @finv[k] % @mod
  end
end

n,s = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i)

@h = Hash.new(0)
a.each do |e|
  @h[e] += 1
end
# p @h

@combi = Combi.new(@h.values.max+1, MOD)

@gans = 0

@n = n
def calc(current)
  anum = 1
=begin
  current.each do |k, v|
    next if v == 0
    anum *= @combi.nCk(@h[k], v)
  end
=end
  nokori = @n - current.values.sum
  @gans = (@gans + anum * 2.pow(nokori, MOD)) % MOD
  # p [current, anum, nokori, @gans]
end

def combination_sum2(cans, target)
  @cans = cans
  @target = target

  @cans.sort!
  @cans.select{|can| can == target}.each do |can|
    calc({can => 1})
  end
  @cans.select!{|can| can < target}
  @size = cans.size

  solve(0, Hash.new(0), 0)
end

def solve(index, current, current_sum)
  index.upto(@size - 1) do |i|
    if current_sum + @cans[i] == @target
      current[@cans[i]] += 1
      calc(current)
      current[@cans[i]] -= 1
    elsif current_sum + @cans[i] < @target
      current[@cans[i]] += 1
      solve(i + 1, current, current_sum + @cans[i])
      current[@cans[i]] -= 1
    else
      break
    end
  end
end

combination_sum2(a, s)

=begin
combi = @combi

ans = 0
p comb = combination_sum2(a, s)
comb.each do |ar|
  ah = Hash.new(0)
  ar.each do |e|
    ah[e] += 1
  end
  anum = 1
  ah.each do |k, v|
    anum *= combi.nCk(h[k], v)
  end

  nokori = n - ar.size
  ans = (ans + anum * 2.pow(nokori, MOD)) % MOD
end

=end

puts @gans

Score : 600 points

Problem Statement

Given are a sequence of N positive integers A_1, A_2, \ldots, A_N and another positive integer S.

For a non-empty subset T of the set \{1, 2, \ldots , N \}, let us define f(T) as follows:

f(T) is the number of different non-empty subsets \{x_1, x_2, \ldots , x_k \} of T such that A_{x_1}+A_{x_2}+\cdots +A_{x_k} = S.

Find the sum of f(T) over all 2^N-1 subsets T of \{1, 2, \ldots , N \}. Since the sum can be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq S \leq 3000

1 \leq A_i \leq 3000

Input

Input is given from Standard Input in the following format:

N S
A_1 A_2 ... A_N

Output

Print the sum of f(T) modulo 998244353.

Sample Input 1

3 4
2 2 4

Sample Output 1

6

For each T, the value of f(T) is shown below. The sum of these values is 6.

f(\{1\}) = 0

f(\{2\}) = 0

f(\{3\}) = 1 (One subset \{3\} satisfies the condition.)

f(\{1, 2\}) = 1 (\{1, 2\})

f(\{2, 3\}) = 1 (\{3\})

f(\{1, 3\}) = 1 (\{3\})

f(\{1, 2, 3\}) = 2 (\{1, 2\}, \{3\})

Sample Input 2

5 8
9 9 9 9 9

Sample Output 2

0

Sample Input 3

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

Sample Output 3

3296


def die; p(-1); exit; end

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

B = [1-L[0]]+[0]*N
N.downto(1).inject(0){|s,i|
	next L[i] + B[i]=s
}
N.times{|d|
	b = B[d]*2 - L[d+1]
	die if b<0
	if b < B[d+1]
		B[d+1] = b
	else
		die if 0 < d.downto(1).inject(B[d]-B[d+1]-L[d+1]){|c,d|
			break c if c<=0
			B[d] -= c
			die if B[d]<=0
			next B[d-1]-B[d]-L[d]
		}
	end
}
die if B[0...N].any?(&:zero?)

p B.sum+L.sum

Score : 600 points

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

Notes

A binary tree is a rooted tree such that each vertex has at most two direct children.

A leaf in a binary tree is a vertex with zero children.

The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)

The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

0 \leq N \leq 10^5

0 \leq A_i \leq 10^{8} (0 \leq i \leq N)

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5

264


x = gets.to_i
t = gets.chomp

len = t.length

if x >= len
  puts t
else
  puts t[0..x-1] + "..."
end

Score: 100 points

Problem Statement

The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.

When counting pencils in Japanese, the counter word "本" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "本" in the phrase "N 本" for a positive integer N not exceeding 999 is as follows:

hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;

pon when the digit in the one's place of N is 0, 1, 6 or 8;

bon when the digit in the one's place of N is 3.

Given N, print the pronunciation of "本" in the phrase "N 本".

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

The digit in the one's place of 16 is 6, so the "本" in "16 本" is pronounced pon.

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon


require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

def get_ints
  gets.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

N = get_int

mod = N % 10

case mod
when 2, 4, 5, 7, 9
  puts 'hon'
when 0, 1, 6, 8
  puts 'pon'
when 3
  puts 'bon'
end


num = gets.chomp.to_i　% 10

case num
when 3 then
  puts 'bon'
when 0,1,6,8 then
  puts 'pon'
else
  puts 'hon'
end


n = gets.to_i % 10
if [2, 4, 5, 7, 9].include?(n)
  puts 'hon'
elsif [0, 1, 6, 8].include?(n)
  puts 'pon'
else
  puts 'bon'
end


k = gets.to_i
s = gets

if k < s.length then

    puts s[0..k-1] + "..."
else
    puts s
end

Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print S without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Constraints

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

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


number = gets.to_i
string = gets.chomp.split('')

length = string.length

if number >= length
  puts string.join
else
  for i in 0..number-1
    print string[i]
  end
  puts '...'
end


Score: 300 points

Problem Statement

Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.

An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

Print the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

The two hands will be in the positions shown in the figure below, so the answer is 5 centimeters.

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

The two hands will be in the positions shown in the figure below. Note that each hand always rotates at constant angular velocity.


Score: 500 points

Problem Statement

We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively.

We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.

The i-th and j-th sardines (i \neq j) are on bad terms if and only if A_i \cdot A_j + B_i \cdot B_j = 0.

In how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007.

Constraints

All values in input are integers.

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

-10^{18} \leq A_i, B_i \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the count modulo 1000000007.

Sample Input 1

3
1 2
-1 1
2 -1

Sample Output 1

5

There are five ways to choose the set of sardines, as follows:

The 1-st

The 1-st and 2-nd

The 2-nd

The 2-nd and 3-rd

The 3-rd

Sample Input 2

10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4

Sample Output 2

479


#!/usr/bin/env ruby
S = gets.chomp.to_s
T = gets.chomp.to_s

puts T.start_with?(S) ? "Yes" : "No"

Score : 100 points

Problem Statement

Takahashi wants to be a member of some web service.

He tried to register himself with the ID S, which turned out to be already used by another user.

Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.

He is now trying to register with the ID T. Determine whether this string satisfies the property above.

Constraints

S and T are strings consisting of lowercase English letters.

1 \leq |S| \leq 10

|T| = |S| + 1

Input

Input is given from Standard Input in the following format:

S
T

Output

If T satisfies the property in Problem Statement, print Yes; otherwise, print No.

Sample Input 1

chokudai
chokudaiz

Sample Output 1

Yes

chokudaiz can be obtained by appending z at the end of chokudai.

Sample Input 2

snuke
snekee

Sample Output 2

No

snekee cannot be obtained by appending one character at the end of snuke.

Sample Input 3

a
aa

Sample Output 3

Yes


s = gets.chomp
t = gets.chomp

puts s == t[0..-2] ? "Yes" : "No"


array = gets.split.map(&:to_i)

sum = 0
k = array[3]

sum = sum + array[0]
k = k - array[0]

if k <= 0
    puts(sum)
elsif k - array[1] <= 0
    puts(sum)
else
    puts(sum - (k - array[2]))
end

Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


Score : 300 points

Problem

Takahashi, who is a novice in competitive programming, wants to learn M algorithms.
Initially, his understanding level of each of the M algorithms is 0.

Takahashi is visiting a bookstore, where he finds N books on algorithms.
The i-th book (1\leq i\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\leq j\leq M).
There is no other way to increase the understanding levels of the algorithms.

Takahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.

Constraints

All values in input are integers.

1\leq N, M\leq 12

1\leq X\leq 10^5

1\leq C_i \leq 10^5

0\leq A_{i, j} \leq 10^5

Input

Input is given from Standard Input in the following format:

N M X
C_1 A_{1,1} A_{1,2} \cdots A_{1,M}
C_2 A_{2,1} A_{2,2} \cdots A_{2,M}
\vdots
C_N A_{N,1} A_{N,2} \cdots A_{N,M}

Output

If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it.

Sample Input 1

3 3 10
60 2 2 4
70 8 7 9
50 2 3 9

Sample Output 1

120

Buying the second and third books makes his understanding levels of all the algorithms 10 or higher, at the minimum cost possible.

Sample Input 2

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

Sample Output 2

-1

Buying all the books is still not enough to make his understanding levels of all the algorithms 10 or higher.

Sample Input 3

8 5 22
100 3 7 5 3 1
164 4 5 2 7 8
334 7 2 7 2 9
234 4 7 2 8 2
541 5 4 3 3 6
235 4 8 6 9 7
394 3 6 1 6 2
872 8 4 3 7 2

Sample Output 3

1067


#!/usr/bin/env ruby

def calc_costs(buy_lists, costs)
  res = 0
  buy_lists.each_with_index do |buyed, idx|
    # puts "#{idx}: cost #{costs[idx]}" if buyed
    res += costs[idx + 1] if buyed
  end
  return res
end

n, m, x = gets.split.map(&:to_i)
costs = [-1000000000]
exps = [1000000000]
n.times do
  nums = gets.split.map(&:to_i)
  c = nums[0]
  as = nums[1..-1]
  costs << c
  exps << as
end

# dp[cost][idx] = [exps]
# dp = Array.new(100001).map { Array.new(100).map {Array.new(100, -1)} }

# [books_idx][true/false] = [exps]
# dfs = Array.new(100).map { Array.new(2).map {Array.new(100, 0)} }

status = Struct.new('Status', :books, :exps, :idx)

buyed = status.new([true], exps[1], 1)
not_buyed = status.new([false], Array.new(m, 0), 1)

queue = [buyed, not_buyed]
cands = []

while !queue.empty?
  top = queue.pop
  if top.books.size >= n
    # p top
    # p top
    next if top.exps.any? { |exp| exp < x }

    cost = calc_costs(top.books, costs)
    # puts cost
    cands << cost if cost >= 0
    next
  end
  next_idx = top.idx + 1
  current_books = top.books
  current_exps = top.exps
  added_exps = current_exps.dup

  unless exps[next_idx].nil?
    exps[next_idx].each_with_index do |exp, idx|
      added_exps[idx] += exp
    end
  end

  buyed = status.new(current_books + [true], added_exps, next_idx)
  # p buyed
  not_buyed = status.new(current_books + [false], current_exps, next_idx)
  queue << buyed
  queue << not_buyed
end

cands.sort!
puts cands.empty? ? -1 : cands[0]


N = gets.to_i
S = (0...N).map{
  stack = []
  gets.chomp.split("").each{ |c|
    c = c == ")" ? -1 : +1
    if c == -1
      if stack[-1] == 1
        stack.pop
        next
      end
    end
    stack.push(c)
  }
  d = stack.reduce(0){ |a,b| a + (b < 0 ? b : 0) }
  u = stack.reduce(0){ |a,b| a + (b > 0 ? b : 0) }
  [d, u + d]
}

if S.map{ |a,b| b }.reduce(0,:+) != 0
  puts "No"; exit
end

S.sort_by!{ |a,b| -a }

U = S.select{ |a,b| b >= 0 }
D = S.select{ |a,b| b < 0 }.map{ |a,b| [-a,-b] }
D.reverse!
#p U, D

ua,da = [U,D].map{ |ss|
  x = 0
  ss.each{ |a,b|
    if x + a < 0
      puts "No"; exit
    end
    x += b
  }
  x
}
puts ua == da ? "Yes" : "No"

Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No


test = gets

if test == ABC
  puts "ARC"
elsif test == ARC
  puts "ABC"
end

Score : 100 points

Problem Statement

AtCoder Inc. holds a contest every Saturday.

There are two types of contests called ABC and ARC, and just one of them is held at a time.

The company holds these two types of contests alternately: an ARC follows an ABC and vice versa.

Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.

Constraints

S is ABC or ARC.

Input

Input is given from Standard Input in the following format:

S

Output

Print the string representing the type of the contest held this week.

Sample Input 1

ABC

Sample Output 1

ARC

They held an ABC last week, so they will hold an ARC this week.


N,K = gets.split.map(&:to_i)
has = Array.new(N, false)

K.times {
  gets
  a = gets.split.map(&:to_i)
  a.each { |_a|
    has[_a-1] = true
  }
}

cnt = 0
#p has
has.each { |v|
  if v == false
    cnt = 1
  end
}

puts cnt

Score : 200 points

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}.

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N

Input

Input is given from Standard Input in the following format:

N K
d_1
A_{1, 1} \cdots A_{1, d_1}
\vdots
d_K
A_{K, 1} \cdots A_{K, d_K}

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


n,m = gets.chomp.split(" ").map(&:to_i)
h = gets.chomp.split(" ").map(&:to_i)
abs = m.times.map{ gets.chomp.split(" ").map(&:to_i) }
c = []
0.upto(n-1){|i|
  d = []
  abs.map{|ab|
    if (ab[0] == i+1 && h[i] <= h[ab[1]-1]) || ab[1] == i+1 && h[i] <= h[ab[0]-1]
      d << 0
    end
  }
  if d.index(0)
    c << 0
  else
    c << 1
  end
}
puts c.count(1) || 0

Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

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

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

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

Sample Output 2

3


Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


n = gets.to_i
heights = gets.split.map(&:to_i)

low_connector = n.times.map{Array.new}
high_connector = n.times.map{Array.new}

n.times do |i|
  low_connect = i - heights[i]
  high_connect = i + heights[i]
  if low_connect >= 0
    low_connector[low_connect].push i
  end
  if high_connect < n
    high_connector[high_connect].push i
  end
end

count = 0
n.times.each do |i|
  lower = low_connector[i].size
  higher = high_connector[i].size
  count += lower * higher
end

puts count

Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

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

Sample Output 3

22


#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']
require 'prime'

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

n,a,b,c = inp
abc = [a,b,c]
if abc.count(0) >= 2
  puts "No"
else
  p fdajkshdfkj[-1]
end

Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A


k = readline.to_i
a, b = readline.split(' ').map(&:to_i)

r = 'NG'

(a..b).each do |n|
  puts n
  if (n % k).zero?
    r = 'OK'
    break
  end
end

puts r

Score: 100 points

Problem Statement

Takahashi the Jumbo will practice golf.

His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive).

If he can achieve the objective, print OK; if he cannot, print NG.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 1000

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K
A B

Output

If he can achieve the objective, print OK; if he cannot, print NG.

Sample Input 1

7
500 600

Sample Output 1

OK

Among the multiples of 7, for example, 567 lies between 500 and 600.

Sample Input 2

4
5 7

Sample Output 2

NG

No multiple of 4 lies between 5 and 7.

Sample Input 3

1
11 11

Sample Output 3

OK


k = gets.chomp.to_i
a, b = gets.chomp.split(" ").map(&:to_i)

if k > b
    puts "NG"
elsif k >= a && K <= b
    puts "OK"
elsif k * 2 < b
    puts "OK"
else
    puts "NG"
end


x = gets.to_i

ans = 0
m = 100
loop{
	if m > x then
		puts ans
		exit
	else
		ans += 1
		m = (m * 1.01).to_i

	end
}

Score : 200 points

Problem Statement

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

Constraints

101 \le X \le 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

Sample Input 1

103

Sample Output 1

3

The balance after one year is 101 yen.

The balance after two years is 102 yen.

The balance after three years is 103 yen.

Thus, it takes three years for the balance to reach 103 yen or above.

Sample Input 2

1000000000000000000

Sample Output 2

3760

Sample Input 3

1333333333

Sample Output 3

1706


x=gets.to_i
i=100
c=0
while i < x do
  i = (i*1.01).floor
  c += 1
end
puts c


io = STDIN
n,m,q=io.gets.split.map(&:to_i)
abcd=q.times.map{io.gets.split.map(&:to_i)}
def scores(ar,abcd)
  abcd.map do |a,b,c,d|
    ar[b-1]-ar[a-1]==c ? d : 0
  end
end
ans=0
(1..m).to_a.repeated_combination(n).each do |ar|
  ans=[scores(ar,abcd).sum,ans].max
end
p ans

Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1


N = gets.chomp!.to_i
hash = {}
N.times do
  str = gets.chomp!
  hash[str] = 0 unless hash.has_key?(str)
end
puts hash.length

Score : 300 points

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

How many kinds of items did you get?

Constraints

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

S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


n = gets.to_i
a = []
n.times do
  a << gets.chomp
end
p a.uniq.size


input = readlines()

n, m = input.shift.chomp.split("\s").map{|x| x.to_i}

rem = n
input.each do |v|
  rem -= v.chomp.to_i
end

puts rem >= 0 ? rem : '-1'

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


n, m = gets.chomp.split(' ').map(&:to_i)
a = gets.chomp.split(' ').map(&:to_i)

if (n >= a.sum)
  puts n - a.sum
elsif (n < a.sum)
  puts -1
end


n, m = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i)

aso = n - a.sum
if aso < 0
  p -1
else
  p aso
end


n, k = gets.chomp.split(' ').map(&:to_i)
sums = Array.new(n + 1, 0)
n.times { |i| sums[i + 1] = sums[i] + i + 1 }
count = 0
mod = (1e9 + 7).to_i
(k..n).each do |i|
  count += sums[n] - sums[n - i] - sums[i - 1] + 1
  count %= mod
end
puts (count + 1) % mod


n, k = gets.chomp.split.map(&:to_i)
arr = (0..n).to_a
arr2 = arr.reverse
min = arr[0, k].inject(:+)
max = arr2[0, k].inject(:+)
answer = (max - min + 1) % 1000000007
(k + 1).upto(n + 1){|i|
    min += arr[i - 1]
    max += arr2[i - 1]
    answer = (answer + (max - min + 1) % 1000000007) % 1000000007
}
puts answer


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

ans = n % k

p [ans, (ans - k).abs].min

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


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

if n >= k
  x = n / k
  n = n - x * k
end

puts [n, (n - k).abs].min


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

amari=n%k
amari2=(k-amari).abs
puts [amari,amari2].min