integer a,b,c
read*, a,b,c
print*, mod(a-b,c)
end

Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


program ABC_087_A_BuyingSweets
   implicit none
   integer A, B, X
   read(*, *) X, A, B
   write(*, *) mod(X - A, B)
end program ABC_087_A_BuyingSweets

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

Submitted source-code file size, in bytes.

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

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


program main
	implicit none
    integer::n,i,j
    integer,allocatable::a(:),b(:),c(:,:)
    read(*,*) n
    allocate(a(n))
    allocate(b(n))
    read(*,*) a
    read(*,*) b
    allocate(c(2,n))
    c(1,1)=a(1)
    do j=2,n
        c(1,j)=c(1,j-1)+a(j)
    end do
    c(2,1)=a(1)+b(1)
    do i=2,n
    	c(2,i)=max(c(1,i),c(2,i-1))+b(i)
    end do
    write(*,*) c(2,n)
    deallocate(a)
    deallocate(b)
    deallocate(c)
    stop
end program main

Score : 300 points

Problem Statement

We have a 2 \times N grid. We will denote the square at the i-th row and j-th column (1 \leq i \leq 2, 1 \leq j \leq N) as (i, j).

You are initially in the top-left square, (1, 1).
You will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

The square (i, j) contains A_{i, j} candies.
You will collect all the candies you visit during the travel.
The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

1 \leq N \leq 100

1 \leq A_{i, j} \leq 100 (1 \leq i \leq 2, 1 \leq j \leq N)

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

move right three times, then move down once, then move right once.

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

You will always collect the same number of candies, regardless of how you travel.

Sample Input 3

7
3 3 4 5 4 5 3
5 3 4 4 2 3 2

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


module UnionFind_Potential

implicit none
integer,allocatable,dimension(:)::par
!parentより
integer,allocatable,dimension(:)::diff_weight

contains

recursive function root(x) result(res)
  integer :: x,res,r
  r=par(x)
  if(par(r) /= r)then
    diff_weight(x)=diff_weight(x)+diff_weight(par(x))
    par(x)=root(r)
  endif
  res = r
end function

subroutine unite(x,y,w)
  !x+w=yとなるように繋げます
  integer :: x,y,w,rx,ry,rw
  !rx,ryはそれぞれの根っこ
  !rwはrxとryの差分です

  rx = root(x);ry = root(y)

  !rx + weight(x) +w=ry+weight(y)から
  rw = w + weight(x)-weight(y)

  if(rx /= ry)then
    par(ry) = rx
    diff_weight(ry)=rw
  endif
end subroutine

function weight(x)
  integer::x,weight
  weight=root(x)
  weight=diff_weight(x)
end function

function diff(x,y)
  integer::x,y,diff
  diff=weight(y)-weight(x)
end function
end module UnionFind_Potential

use UnionFind_Potential

integer n,m
integer l,r,d
read*,n,m
allocate(par(n),diff_weight(n))

!初期化
do i=1,n
  par(i)=i
end do
diff_weight=0

do i=1,m
  read*,l,r,d

  if(root(l)==root(r))then
    if(diff(l,r)/=d)then
      print"(A)","No"
      STOP
    end if
   else
    call unite(l,r,d)
  endif
end do

print"(A)","Yes"

end

Score : 400 points

Problem Statement

There are N people standing on the x-axis.
Let the coordinate of Person i be x_i.
For every i, x_i is an integer between 0 and 10^9 (inclusive).
It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people.
The i-th piece of information has the form (L_i, R_i, D_i).
This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.

It turns out that some of these M pieces of information may be incorrect.
Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Constraints

1 \leq N \leq 100 000

0 \leq M \leq 200 000

1 \leq L_i, R_i \leq N (1 \leq i \leq M)

0 \leq D_i \leq 10 000 (1 \leq i \leq M)

L_i \neq R_i (1 \leq i \leq M)

If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).

D_i are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M

Output

If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.

Sample Input 1

3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Yes

Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103).

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

No

If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Yes

Sample Input 4

10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

No

Sample Input 5

100 0

Sample Output 5

Yes


module UnionFind_Potential

implicit none
integer,allocatable,dimension(:)::par
!parentより
integer,allocatable,dimension(:)::diff_weight

contains

recursive function root(x) result(res)
  integer :: x,res

  if(par(x) /= x)then
    diff_weight(x)=diff_weight(x)+diff_weight(par(x))
    par(x) = root(par(x))
  endif
  res = par(x)
end function

subroutine unite(x,y,w)
  !x+w=yとなるように繋げます
  integer :: x,y,w,rx,ry,rw
  !rx,ryはそれぞれの根っこ
  !rwはrxとryの差分です

  rx = root(x);ry = root(y)

  !rx + weight(x) +w=ry+weight(y)から
  rw = w + weight(x)-weight(y)

  if(rx /= ry)then
    par(ry) = rx
    diff_weight(ry)=rw
  endif
end subroutine

function weight(x)
  integer::x,weight
  if(root(x)==x)then
    weight=0
  else
    weight=diff_weight(x)
  endif
end function

function diff(x,y)
  integer::x,y,diff
  diff=weight(y)-weight(x)
end function
end module UnionFind_Potential

use UnionFind_Potential

integer n,m
integer l,r,d
read*,n,m
allocate(par(n),diff_weight(n))

!初期化
do i=1,n
  par(i)=i
end do
diff_weight=0

do i=1,m
  read*,l,r,d
  if(root(l)/=root(r))then
    call unite(l,r,d)
  else
    if(diff(l,r)/=d)then
      print"(A)","No"
      STOP
    end if
  endif
end do

print"(A)","Yes"

end


program sample
    implicit none

    integer(8) :: i,j,m,n,a,b,k

    integer(8),allocatable :: x(:),y(:)

    read(*,*) a,b
    k=int(log10(real(b)))+1
    m=a*10**k+b
    if (int(sqrt(real(m)))**2==m)then
       write(*,*) 'Yes'
    else
        write(*,*)'No'
    end if

    stop
end program sample

Score : 200 points

Problem Statement

AtCoDeer the deer has found two positive integers, a and b.
Determine whether the concatenation of a and b in this order is a square number.

Constraints

1 ≤ a,b ≤ 100

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

Sample Input 1

1 21

Sample Output 1

Yes

As 121 = 11 × 11, it is a square number.

Sample Input 2

100 100

Sample Output 2

No

100100 is not a square number.

Sample Input 3

12 10

Sample Output 3

No


program name

    implicit none
    character(3) a,b
    character(6) c
    integer num,i
    integer hei(100)
    do i = 1, 100, 1
        hei(i) = i**2
    end do
    read*, a,b
    a = a(1:len_trim(a))
    b = b(1:len_trim(b))
    c = a//b
    read (c,*) num
    if ( count(hei==num)==1 ) then
        print*, "Yes"
    else
        print*, "No"
    end if

end program


CHARACTER(20) S,T,U
REAL N
READ*,S,T
U=TRIM(S)//TRIM(T)
READ(U,*) N
PRINT"(A)",MERGE("Yes"," No",FLOOR(SQRT(N))**2==N)
END


program sample
    implicit none

    integer(8) :: i,j,m,n,a,b,t,a0,b0,t0
    integer(8),allocatable :: x(:),y(:)

    read(*,*) n
    a0=0
    b0=0
    t0=0
    do i=1,n
        read(*,*)t,a,b
        m=abs(a-a0)+abs(b-b0)
        if (m>t-t0)then
            write(*,*)'No'
            stop
        elseif(mod(m,2)/=mod(t-t0,2))then
            write(*,*)'No'
            stop
        end if
        a0=a
        b0=b
        t0=t
    end do

    write(*,*) 'Yes'

    stop
end program sample

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


program main
  integer, allocatable :: i(:, :)
  read (*, *) n
  allocate(i(3, 0:n))
  i(:, 0) = (/0, 0, 0/)
  read (*, *) i(:, 1:n)
  do k = 1, n
    jx = abs(i(2, k) - i(2, k - 1)) + abs(i(3, k) - i(3, k - 1))
    jt = i(1, k) - i(1, k - 1)
    if (jx > jt .or. ibits(jx, 0, 1) /= ibits(jt, 0, 1)) then
      write (*, *) "No"
      stop
    endif
  enddo
  write (*, *) "Yes"
end


implicit none

integer:: n,i,j,k,count,jj,count_max
character,allocatable,dimension(:) :: c*1
integer,allocatable,dimension(:):: x,y
read (5,*) n,k

allocate(x(n))
allocate(y(n))
allocate(c(n))

do i=1,n
    read(5,*) x(i),y(i),c(i)
enddo

do j=1,int(k/2)+1
    do jj=1,int(k/2)+1
    count=0
    do i=1,n
        if (mod(mod(x(i)-j+1,k),2)==1 .and. mod(mod(x(i)-jj+1,k),2)==1 .and. c(i)=="W" ) then
            count=count+1
        endif

enddo
enddo
        count_max=max(count,n-count)
enddo

!write(*,*) n,k
!write(*,*) x,y,c
write(*,*) count_max

stop
end

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

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

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


program AGC020A
    implicit none
    integer(8)::N,A,B
    read*,N,A,B
    if(mod(B-A,2)==0)print'(A)',"Alice"
    if(mod(B-A,2)==1)print'(A)',"Borys"
end program AGC020A

Score : 300 points

Problem Statement

A game is played on a strip consisting of N cells consecutively numbered from 1 to N.

Alice has her token on cell A. Borys has his token on a different cell B.

Players take turns, Alice moves first.
The moving player must shift his or her token from its current cell X to the neighboring cell on the left, cell X-1, or on the right, cell X+1.
Note that it's disallowed to move the token outside the strip or to the cell with the other player's token.
In one turn, the token of the moving player must be shifted exactly once.

The player who can't make a move loses, and the other player wins.

Both players want to win. Who wins if they play optimally?

Constraints

2 \leq N \leq 100

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print Alice if Alice wins, Borys if Borys wins, and Draw if nobody wins.

Sample Input 1

5 2 4

Sample Output 1

Alice

Alice can move her token to cell 3.
After that, Borys will be unable to move his token to cell 3, so he will have to move his token to cell 5.
Then, Alice moves her token to cell 4. Borys can't make a move and loses.

Sample Input 2

2 1 2

Sample Output 2

Borys

Alice can't make the very first move and loses.

Sample Input 3

58 23 42

Sample Output 3

Borys


implicit none

integer(8) :: ans_min=10000000000_8, ans_max=0, count=0
integer(8) :: k, i, j, n, n_org
integer,allocatable,dimension(:):: a

read(5,*) k
allocate(a(k))

read(5,*) (a(i),i=1,k)

n=maxval(a)-mod(maxval(a),a(1))

!write(6,*) n
do j=1,300
    n_org=n
    do i=1, k
        n=(int(n/a(i)))*a(i)
    enddo

    if (n==2) then
        ans_min=min(ans_min,n_org)
        ans_max=max(ans_max,n_org)
        count=1
    endif
n=n_org+a(1)

enddo

if (count==0)  then
    write(6,*), -1
else
    write(6,*) ans_min,ans_max+a(1)-1
endif

stop
end

Score : 500 points

Problem Statement

An adult game master and N children are playing a game on an ice rink.
The game consists of K rounds.
In the i-th round, the game master announces:

Form groups consisting of A_i children each!

Then the children who are still in the game form as many groups of A_i children as possible.
One child may belong to at most one group.
Those who are left without a group leave the game. The others proceed to the next round.
Note that it's possible that nobody leaves the game in some round.

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

You have heard the values of A_1, A_2, ..., A_K. You don't know N, but you want to estimate it.

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

Constraints

1 \leq K \leq 10^5

2 \leq A_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

Print two integers representing the smallest and the largest possible value of N, respectively,
or a single integer -1 if the described situation is impossible.

Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

For example, if the game starts with 6 children, then it proceeds as follows:

In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.

In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.

In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.

In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.

The last 2 children are declared the winners.

Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

This situation is impossible.
In particular, if the game starts with less than 100 children, everyone leaves after the third round.

Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3


Score : 100 points

Problem Statement

How many hours do we have until New Year at M o'clock (24-hour notation) on 30th, December?

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

If we have x hours until New Year at M o'clock on 30th, December, print x.

Sample Input 1

21

Sample Output 1

27

We have 27 hours until New Year at 21 o'clock on 30th, December.

Sample Input 2

12

Sample Output 2

36


program main
  implicit none
  integer :: m
  read *, m
  print "(i0)", 48 - m
end program main


program atcoder
  implicit none
  integer m
  read *, m    ! 変数mに、標準入力から数字を読み込み代入する

               ! このあたりに、解答を導く計算を書く

  print '(i0)',48-m    !  ","のあとに解答となる変数を書く
end program atcoder


program main
  implicit none
  integer :: a, b, i
  character(11) :: s
  logical :: ishyphen, needhyphen
  read(*, *) a, b
  read(*, *) s
  do i = 1, a + b
    ishyphen = (s(i:i) == "-")
    needhyphen = (i == a + 1)
    if (ishyphen .neqv. needhyphen) then
      write(*, "(a)") "No"
      stop 0
    end if
  end do
  write(*, "(a)") "Yes"
end program main

Score : 200 points

Problem Statement

The postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9.

You are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom.

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

Print Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise.

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

The (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format.

Sample Input 2

1 1
---

Sample Output 2

No

S contains unnecessary -s other than the (A+1)-th character, so it does not follow the format.

Sample Input 3

1 2
7444

Sample Output 3

No


program abc084c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,j
    integer(int32):: time, nt
    integer(int32), allocatable:: c(:), s(:), f(:),ans(:)

    read*, n
    allocate(c(n-1), s(n-1), f(n-1), ans(n))

    do i=1,n-1
        read*, c(i),s(i),f(i)
    end do

    do j=1,n-1
        time=0
        do i=j,n-1
            if (time <= s(i)) then
                time = s(i)+c(i)
            else
                nt=s(i)
                do while(time > nt)
                    nt=nt+f(i)
                end do
                time=nt+c(i)
            end if
        end do
        ans(j)=time
    end do

    print'(i0)', ans(:)
end program abc084c

Score : 300 points

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

There are N stations on the railroad, numbered 1 through N from west to east.

Tomorrow, the opening ceremony of the railroad will take place.

On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.

The first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.

Here, it is guaranteed that F_i divides S_i.

That is, for each Time t satisfying S_i≤t and t％F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A％B denotes A modulo B, and there will be no other trains.

For each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 S_1 F_1
:
C_{N-1} S_{N-1} F_{N-1}

Output

Print N lines. Assuming that we are at Station i (1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

5 seconds after the beginning: take the train to Station 2.

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0


program prob4
    implicit none
    integer::p(100005), a(100005)
    integer::i, j, Q, l, r

    p = 0
    do i = 2, 100000
        if(p(i) == 0) then
            do j = 2*i, 100000, i
                p(j) = 1
            end do
        end if
    end do

    a = 0
    do i = 3, 100000
        if(p(i) == 0 .and. p((i+1)/2) == 0) then
            a(i) = a(i-1) + 1
        else
            a(i) = a(i-1)
        end if
    end do

    read(*,*) Q
    do i = 1, Q
        read(*,*) l, r
        write(*,*) a(r) - a(l-1)
    end do
    stop
end program

Score : 400 points

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

You are given Q queries.

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

Sample Input 1

1
3 7

Sample Output 1

2

3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.

5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.

7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.

Thus, the response to the first query should be 2.

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

4
4
1
1
1
2


program prob47
    implicit none
    integer :: a,b,c,d
    read(*,*) a,b,c,d

    if(a + b > c + d)then
        write(*,*) "Left"
    else if(a + b == c + d) then
        write(*,*) "Balanced"
    else
        write(*,*) "Right"
    end if

    stop
contains
end program prob47

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

1\leq A,B,C,D \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


integer a, b, c, d
read*, a, b, c, d

if(a+b > c+d) then
    write(*,'(A)') 'Left'
else if(a+b == c+d) then
    write(*,'(A)') 'Balanced'
else
    write(*,'(A)') 'Right'
endif
end


program main
	implicit none
	integer counter, i
	integer(8) X, Y
	read(*, *) X, Y
	counter = 1
	do i = 2, 72
		X = X*2
		if(X <= Y) then
			counter = counter + 1
		else
			exit
		end if
	end do
		write(*, *) counter
end program main

Score : 300 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence A needs to satisfy the conditions below:

A consists of integers between X and Y (inclusive).

For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.

Find the maximum possible length of the sequence.

Constraints

1 \leq X \leq Y \leq 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


program main
implicit none
integer(8) X,Y,An
integer length

read(*,*) X,Y
An=X+X
length=1
do
	An = An+An
    if(An<=Y)then
	    length = length + 1
    else
    	exit
    endif
enddo

write(*,*) length

end program


program shift_only
  implicit none
  integer :: n, a(200) = 0, i
  read(*,*) n
  read(*,*) a(1:n)
  do i = 0, 31
    if (any(btest(a(1:n), 0))) then
      write(*,'(i0)') i
      stop
    end if
    a(1:n) = rshift(a(1:n), 1)
  end do
end program shift_only

Score : 200 points

Problem Statement

There are N positive integers written on a blackboard: A_1, ..., A_N.

Snuke can perform the following operation when all integers on the blackboard are even:

Replace each integer X on the blackboard by X divided by 2.

Find the maximum possible number of operations that Snuke can perform.

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of operations that Snuke can perform.

Sample Input 1

3
8 12 40

Sample Output 1

2

Initially, [8, 12, 40] are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.

After the operation is performed once, [4, 6, 20] are written on the blackboard.
Since all those integers are again even, he can perform the operation.

After the operation is performed twice, [2, 3, 10] are written on the blackboard.
Now, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


program ABC081B
    implicit none
    integer(8)::N,i,calc,ans=1000000000
    integer(8),allocatable::A(:)
    read*,N
    allocate(A(N))
    read*,A

    do i=1,N
        calc=0
        do
            if(mod(A(i),2)==1)exit
            A(i)=A(i)/2
            calc=calc+1
        end do
        ans=min(ans,calc)
    end do
    print'(i0)',ans
end program ABC081B


program ARC086C
    implicit none
    integer(8)::N,K,valiation,i
    integer(8)::remove=0
    integer(8)::result=0
    integer(8),allocatable,dimension(:)::A,L
    read(5,*)N,K
    allocate(A(N))
    allocate(L(N))
    valiation=N
    L=0
    read(5,*)(A(i),i=1,N)

    do i=1,N
        L(A(i))=L(A(i))+1
    end do

    L=iqsort(L)

    do i=1,N
        if(L(i)==0)then
            valiation=valiation-1
        else
            exit
        end if
    end do

    remove=valiation-K

    if(remove>0)then
        do i=1,N
            if(L(i)/=0)then
                result=result+L(i)
                remove=remove-1
            end if

            if(remove==0)then
                exit
            end if
        end do
    end if

    print'(i0)',result

contains
    recursive function iqsort(ix) result(ires)
      integer(8), allocatable :: ires(:)
      integer(8), intent(in)  :: ix(:)
      integer :: k
      if (size(ix) <= 1) then
        ires = ix
      else
        k = ix(size(ix) / 2)
        ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
      end if
    end function iqsort
end program ARC086C

Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

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

Sample Output 3

3


integer :: a,n,b
read *,n,a,b
print*,min(a*n,b)
end

Score : 100 points

Problem Statement

You are parking at a parking lot. You can choose from the following two fee plans:

Plan 1: The fee will be A×T yen (the currency of Japan) when you park for T hours.

Plan 2: The fee will be B yen, regardless of the duration.

Find the minimum fee when you park for N hours.

Constraints

1≤N≤20

1≤A≤100

1≤B≤2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

When the minimum fee is x yen, print the value of x.

Sample Input 1

7 17 120

Sample Output 1

119

If you choose Plan 1, the fee will be 7×17=119 yen.

If you choose Plan 2, the fee will be 120 yen.

Thus, the minimum fee is 119 yen.

Sample Input 2

5 20 100

Sample Output 2

100

The fee might be the same in the two plans.

Sample Input 3

6 18 100

Sample Output 3

100


program ABC_080_B_HarshadNumber
   implicit none
   integer i, N, Nold, fN
   read(*, *) N
   fN = 0
   Nold = N
   do i = 1, 9
       fN = fN + mod(N, 10)
	   N = N / 10
   end do
   if(mod(Nold, fN) == 0) then
       write(*, *) 'Yes'
   else
       write(*, *) 'No'
   end if
end program ABC_080_B_HarshadNumber

Score : 200 points

Problem Statement

An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.

Given an integer N, determine whether it is a Harshad number.

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print Yes if N is a Harshad number; print No otherwise.

Sample Input 1

12

Sample Output 1

Yes

f(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number.

Sample Input 2

57

Sample Output 2

No

f(57)=5+7=12. Since 57 is not divisible by 12, 12 is not a Harshad number.

Sample Input 3

148

Sample Output 3

No


program recording
  implicit none
  integer :: n, k, s, t, c, x(200000,30), i, m
  s = 0
  x = 0
  read(*,*) n, k
  do i = 1, n
    read(*,*) s, t, c
    x(2*s-1,c) = x(2*s-1,c) + 1
    x(2*t,c) = x(2*t,c) - 1
  end do
  do i = 2, 200000
    x(i,1:k) = x(i,1:k) + x(i-1,1:k)
  end do
  m = 0
  do i = 1, 200000
    c = sum(x(i,1:k))
    if (m.lt.c) m = c
  end do
  write(*,'(i0)') m
  stop
end program recording

Score : 400 points

Problem Statement

Joisino is planning to record N TV programs with recorders.

The TV can receive C channels numbered 1 through C.

The i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.

Here, there will never be more than one program that are broadcast on the same channel at the same time.

When the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).

Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

Constraints

1≤N≤10^5

1≤C≤30

1≤s_i<t_i≤10^5

1≤c_i≤C

If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N C
s_1 t_1 c_1
:
s_N t_N c_N

Output

When the minimum required number of recorders is x, print the value of x.

Sample Input 1

3 2
1 7 2
7 8 1
8 12 1

Sample Output 1

2

Two recorders can record all the programs, for example, as follows:

With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.

With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.

Sample Input 2

3 4
1 3 2
3 4 4
1 4 3

Sample Output 2

3

There may be a channel where there is no program to record.

Sample Input 3

9 4
56 60 4
33 37 2
89 90 3
32 43 1
67 68 3
49 51 3
31 32 3
70 71 1
11 12 3

Sample Output 3

2


program main
    implicit none
    integer(8)::A(6)=(/1,0,0,1,1,1/),a_count=0,flg=1,cur=1,i
    character(50)::S,T="KIHBR"
    read*,S
    if(len_trim(S)>9)flg=0
    do i=1,len_trim(S)
        if(S(i:i)=="A")then
            a_count=a_count+1
        else if(S(i:i)==T(cur:cur))then
            if(a_count>A(cur))then
                flg=0
                exit
            else
                a_count=0
                cur=cur+1
            end if
        else
            flg=0
            exit
        end if
    end do
    if(flg==1.and.a_count>A(6))flg=0
    if(flg==1)print'(A)',"YES"
    if(flg==0)print'(A)',"NO"
end program main

Score : 300 points

Problem Statement

You are given a string S.

Takahashi can insert the character A at any position in this string any number of times.

Can he change S into AKIHABARA?

Constraints

1 \leq |S| \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If it is possible to change S into AKIHABARA, print YES; otherwise, print NO.

Sample Input 1

KIHBR

Sample Output 1

YES

Insert one A at each of the four positions: the beginning, immediately after H, immediately after B and the end.

Sample Input 2

AKIBAHARA

Sample Output 2

NO

The correct spell is AKIHABARA.

Sample Input 3

AAKIAHBAARA

Sample Output 3

NO


character(9)mihon(16)
character(9) S
mihon(1)="AKIHABARA"
mihon(2)="AKIHABAR"
mihon(3)="AKIHABRA"
mihon(4)="AKIHABR"
mihon(5)="AKIHBARA"
mihon(6)="AKIHBAR"
mihon(7)="AKIHBRA"
mihon(8)="AKIHBR"
mihon(9)="KIHABARA"
mihon(10)="KIHABAR"
mihon(11)="KIHABRA"
mihon(12)="KIHABR"
mihon(13)="KIHBARA"
mihon(14)="KIHBAR"
mihon(15)="KIHBRA"
mihon(16)="KIHBR"
read*,S
do i=1,16
  if(S==mihon(i))then
  print"(A)","YES"
  stop
  endif
end do
print"(A)","NO"
end


program sample
    implicit none
    character(len=100000)::s
    integer(8) :: i,j,m,n,a,b,c,x(3)
    integer(8),allocatable :: y(:)

    read(*,*) s
    x(:)=0
    n=len_trim(s)
    do i=1,n
        if (s(i:i) .eq. 'a')then
            x(1)=x(1)+1
        else if (s(i:i) .eq. 'b')then
            x(2)=x(2)+1
        else
            x(3)=x(3)+1
        end if
    end do

    if (abs(x(1)-x(2))<2 .and. abs(x(2)-x(3))<2 .and. abs(x(1)-x(3))<2 )then
     write(*,*) 'YES'
    else
        write(*,*)'NO'
    end if

    stop
end program sample

Score : 400 points

Problem Statement

Snuke has a string S consisting of three kinds of letters: a, b and c.

He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible.

Constraints

1 \leq |S| \leq 10^5

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If the objective is achievable, print YES; if it is unachievable, print NO.

Sample Input 1

abac

Sample Output 1

YES

As it stands now, S contains a palindrome aba, but we can permute the characters to get acba, for example, that does not contain a palindrome of length 2 or more.

Sample Input 2

aba

Sample Output 2

NO

Sample Input 3

babacccabab

Sample Output 3

YES


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    character(100000):: s
    integer(int32):: cnt(3),l,i

    read*, s
    l= len_trim(s)
    cnt(:) = 0
    do i=1,l
        cnt(ichar(s(i:i))-ichar('a')+1)=cnt(ichar(s(i:i))-ichar('a')+1)+1
    end do

    if (maxval(cnt) - minval(cnt) <= 1) then
        print'(a)', 'YES'
    else
        print'(a)', 'NO'
    end if
end program name


program time_gap
  implicit none
  integer :: n, d(50), t(0:12), x, y(11), u, m, s, i, j, k
  logical :: c(0:24), f(0:24)
  d = 0
  t = 0
  y = 0
  c = .false.
  t(0) = 1
  read(*,*) n
  read(*,*) d(1:n)
  do i = 1, n
    t(d(i)) = t(d(i))+1
  end do
  if (t(0).gt.1.or.t(12).gt.1) then
    write(*,'(i0)') 0
    stop
  end if
  x = 0
  c(0) = .true.
  c(24) = .true.
  do i = 1, 11
    if (t(i).gt.2) then
      write(*,'(i0)') 0
      stop
    else if (t(i).eq.2) then
      c(i) = .true.
      c(24-i) = .true.
    else if (t(i).eq.1) then
      x = x+1
      y(x) = i
    end if
  end do
  if (t(12).eq.1) c(12) = .true.
  u = 2**x-1
  m = 0
  do k = 0, u
    f = c
    do i = 1, x
      if (btest(k,i-1)) then
        f(y(i)) = .true.
      else
        f(24-y(i)) = .true.
      end if
    end do
    s = 24
    i = 0
    do
      j = i+1
      do while (.not.f(j))
        j = j+1
      end do
      s = min(s,j-i)
      if (j.eq.24) exit
      i = j
    end do
    m = max(m,s)
  end do
  write(*,'(i0)') m
  stop
end program time_gap

Score : 500 points

Problem Statement

In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi.

Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours.
The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours.
Here, we are using 24-hour notation.
That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example.

Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours.

Find the maximum possible value of s.

Constraints

1 \leq N \leq 50

0 \leq D_i \leq 12

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N

Output

Print the maximum possible value of s.

Sample Input 1

3
7 12 8

Sample Output 1

4

For example, consider the situation where it is 7, 12 and 16 o'clock in each person's city at the moment when it is 0 o'clock in Takahashi's city. In this case, the time gap between the second and third persons' cities is 4 hours.

Sample Input 2

2
11 11

Sample Output 2

2

Sample Input 3

1
0

Sample Output 3

0

Note that Takahashi himself is also a participant.


program main
	implicit none
	character a*4
	integer j(4),i
	read(*,*)a
	do i=1,4
		read(a(i:i),*)j(i)
	enddo
	if((j(1)==j(2).and.j(1)==j(3)).or.(j(1)==j(3).and.j(3)==j(4)).or.&
	&(j(3)==j(2).and.j(4)==j(3)).or.(j(1)==j(2).and.j(2)==j(4)))then
		write(*,*)"Yes"
	else
		write(*,*)"No"
	endif
end program main

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


program name

    implicit none
    integer(8) nums(87)
    integer i,N
    nums(1) = 2
    nums(2) = 1
    do i = 3, 87, 1
        nums(i) = nums(i - 1)+nums(i - 2)
        print*, nums(i)
    end do
    read*, N
    print*, (nums(N + 1))

end program

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


character a,b
read*,a,b
if(ichar(a)<ichar(b))then
	print"(A)","<"
else if(ichar(a)>ichar(b))then
	print"(A)",">"
else
	print"(A)","="
endif
end

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

When X and Y are seen as hexadecimal numbers, which is larger?

Constraints

Each X and Y is A, B, C, D, E or F.

Input

Input is given from Standard Input in the following format:

X Y

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


program name

    implicit none
    integer X,Y,Z
    read*, X,Y,Z
    print*, (X-Z)/(Y+Z)

end program

Score : 200 points

Problem Statement

We have a long seat of width X centimeters.
There are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length Y centimeters.

We would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least Z centimeters between two people, and between the end of the seat and a person.

At most how many people can sit on the seat?

Constraints

All input values are integers.

1 \leq X, Y, Z \leq 10^5

Y+2Z \leq X

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.

Sample Input 1

13 3 1

Sample Output 1

3

There is just enough room for three, as shown below:

Figure

Sample Input 2

12 3 1

Sample Output 2

2

Sample Input 3

100000 1 1

Sample Output 3

49999

Sample Input 4

64146 123 456

Sample Output 4

110

Sample Input 5

64145 123 456

Sample Output 5

109


program main
	implicit none
	integer x,y,z,a
	read(*,*)x,y,z
	x=x-z
	a=x/(y+z)
	write(*,*)a
end program main


program main
	implicit none
	integer :: N, M
	read(*, *) N, M
	write(*, *) (1900*M+100*(N-M))*2**M
end program main

Score : 300 points

Problem Statement

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.

When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.

Then, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.

Now, he goes through the following process:

Submit the code.

Wait until the code finishes execution on all the cases.

If the code fails to correctly solve some of the M cases, submit it again.

Repeat until the code correctly solve all the cases in one submission.

Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

Constraints

All input values are integers.

1 \leq N \leq 100

1 \leq M \leq {\rm min}(N, 5)

Input

Input is given from Standard Input in the following format:

N M

Output

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.

Sample Input 1

1 1

Sample Output 1

3800

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.

The code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.

Thus, the answer is 1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800.

Sample Input 2

10 2

Sample Output 2

18400

The code will take 1900 milliseconds in each of the 2 cases, and 100 milliseconds in each of the 10-2=8 cases. The probability of the code correctly solving all the cases is 1/2 \times 1/2 = 1/4.

Sample Input 3

100 5

Sample Output 3

608000


program main
	implicit none
	integer n,z,w
	integer,allocatable,dimension(:)::a
	read(*,*)n,z,w
	allocate(a(n))
	read(*,*)a(1:n)
	z=max(maxval(a),z)
	w=min(minval(a(2:n)),w)
	write(*,*)abs(z-w)
end program main

Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

1 \leq N \leq 2000

1 \leq Z, W, a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be |1000 - 100| = 900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999


character(3) ::s,ss
read*,s
read*,ss

if( s(1:1)==ss(3:3).and.s(2:2)==ss(2:2).and.s(3:3)==ss(1:1) )then
  print*,'YES'
else
  print*,'NO'
end if
end

Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


program main
  implicit none
  integer a,b
  integer i
  read(*,*)a
  do i=1,a
	b=i**2
	if(b>a)exit
  enddo
  write(*,*)(i-1)**2
end program main

Score : 200 points

Problem Statement

Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.

Constraints

1 \leq N \leq 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the largest square number not exceeding N.

Sample Input 1

10

Sample Output 1

9

10 is not square, but 9 = 3 × 3 is. Thus, we print 9.

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169


integer(16) N
integer(16),allocatable,dimension(:)::A,B,C,D
integer(16) ans,i,j,k
read*,N
allocate(A(N),B(N),C(N),D(N))
read*,A
read*,B
read*,C
call heapsort(n,a)
call heapsort(n,b)
call heapsort(n,c)

D=0
j=1
do i = 1, n
   do while (j <= n .and. a(j) < b(i))
      j = j + 1
   end do
   D(i) = j - 1
end do
ans=0
k=0
j=1
do i = 1, n
   do while (j <= n .and. b(j) < c(i))
      k = k + D(j)
      j = j + 1
   enddo
   ans = ans + K
enddo

print"(i0)",ans

contains

subroutine heapsort(n,array)
  implicit none
  integer(16),intent(in) :: n
  integer(16),intent(inout) :: array(1:n)
  integer(16)::i,k,j,l
  integer(16):: t

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
     else
        t=array(k)
        array(k)=array(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j<=k)
        if(j < k)then
           if(array(j) < array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
  enddo
  return
end subroutine heapsort

end

Score : 300 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

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

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

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

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

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

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

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

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

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

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

Sample Output 3

87


module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort, double_merge_sort
    interface merge_sort
        module procedure ms32, ms64
    end interface

    interface double_merge_sort
        module procedure msd3232, msd6464
    end interface

contains
    recursive subroutine ms32(ar, fst, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap32(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms32(ar, fst, mdl)
        call ms32(ar, mdl+1, lst)
        call merge32(ar, fst, mdl, lst)
    end subroutine

    subroutine merge32(ar, fst, mdl, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst, mdl, lst
        integer(int32),allocatable:: tmp(:)
        integer(int32):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap32(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine ms64(ar, fst, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap64(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms64(ar, fst, mdl)
        call ms64(ar, mdl+1, lst)
        call merge64(ar, fst, mdl, lst)
    end subroutine

    subroutine merge64(ar, fst, mdl, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst, mdl, lst
        integer(int64),allocatable:: tmp(:)
        integer(int64):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap64(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine msd3232(ar1, ar2, fst, lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap32(ar1(fst), ar1(lst))
                call swap32(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd3232(ar1,ar2,fst,mdl)
        call msd3232(ar1,ar2,mdl+1,lst)
        call merged3232(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged3232(ar1,ar2,fst,mdl,lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,mdl,lst
        integer(int32),allocatable:: t1(:),t2(:)
        integer(int32):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    recursive subroutine msd6464(ar1, ar2, fst, lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap64(ar1(fst), ar1(lst))
                call swap64(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd6464(ar1,ar2,fst,mdl)
        call msd6464(ar1,ar2,mdl+1,lst)
        call merged6464(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged6464(ar1,ar2,fst,mdl,lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,mdl,lst
        integer(int64),allocatable:: t1(:),t2(:)
        integer(int64):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine
end module

program main
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int64):: n,i,j,ans
    integer(int64), allocatable:: a(:), b(:), c(:)
    integer(int64), allocatable:: ca(:), cb(:)

    read*, n
    allocate(a(n), b(n), c(n))
    allocate(ca(0:n), cb(0:n+1), source=0_8)
    read*, a(:)
    read*, b(:)
    read*, c(:)

    call merge_sort(a, 1_8, n)
    call merge_sort(b, 1_8, n)
    call merge_sort(c, 1_8, n)

    j=1
    cb(0) = n
    do i=1,n
        cb(i) = cb(i-1)
        if (j > n) cycle
        do while(b(i) >= c(j))
            j=j+1
            cb(i)=cb(i)-1
            if (j > n) exit
        end do
    end do
    j=1
    ca(0)=n
    do i=1,n
        ca(i) = ca(i-1)
        if (j > n) cycle
        do while(a(i) >= b(j))
            j=j+1
            ca(i)=ca(i)-1
            if (j > n) exit
        end do
    end do
    do i=n-1,1,-1
        cb(i)=cb(i)+cb(i+1)
    end do

    ans=0
    do i=1,n
        ans=ans+cb(n+1-ca(i))
    end do
    print'(i0)', ans
end program main


program main
	implicit none
	integer r,g,a
	read(*,*)r,g
	a=2*g-r
	write(*,*)a
end program main

Score : 100 points

Problem Statement

Takahashi is a user of a site that hosts programming contests.

When a user competes in a contest, the rating of the user (not necessarily an integer) changes according to the performance of the user, as follows:

Let the current rating of the user be a.

Suppose that the performance of the user in the contest is b.

Then, the new rating of the user will be the avarage of a and b.

For example, if a user with rating 1 competes in a contest and gives performance 1000, his/her new rating will be 500.5, the average of 1 and 1000.

Takahashi's current rating is R, and he wants his rating to be exactly G after the next contest.

Find the performance required to achieve it.

Constraints

0 \leq R, G \leq 4500

All input values are integers.

Input

Input is given from Standard Input in the following format:

R
G

Output

Print the performance required to achieve the objective.

Sample Input 1

2002
2017

Sample Output 1

2032

Takahashi's current rating is 2002.

If his performance in the contest is 2032, his rating will be the average of 2002 and 2032, which is equal to the desired rating, 2017.

Sample Input 2

4500
0

Sample Output 2

-4500

Although the current and desired ratings are between 0 and 4500, the performance of a user can be below 0.


program main
  implicit none
  integer :: n, i, k, res
  read(*, *) n
  read(*, *) k
  res = 1
  do i = 1, n
    if (res > k) then
      res = res + k
    else
      res = res * 2
    end if
  end do
  write(*, "(i0)") res
end program main

Score : 200 points

Problem Statement

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

Operation A: The displayed value is doubled.

Operation B: The displayed value increases by K.

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

Constraints

1 \leq N, K \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
K

Output

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

Sample Input 1

4
3

Sample Output 1

10

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

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

Sample Input 2

10
10

Sample Output 2

76

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

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

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


program prob44
    implicit none
    character(5) :: s
    integer :: i
    read(*,*) s
    do i = 1, 4
        if(s(i:i+1) == "AC") then
            write(*,*) "Yes"
            stop
        end if
    end do
    write(*,*) "No"

    stop
contains
end program prob44

Score : 100 points

Problem Statement

Snuke built an online judge to hold a programming contest.

When a program is submitted to the judge, the judge returns a verdict, which is a two-character string that appears in the string S as a contiguous substring.
(The judge can return any two-character substring of S.)

Determine whether the judge can return the string AC as the verdict to a program.

Constraints

2 \leq |S| \leq 5

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If the judge can return the string AC as a verdict to a program, print Yes; if it cannot, print No.

Sample Input 1

BACD

Sample Output 1

Yes

The string AC appears in BACD as a contiguous substring (the second and third characters).

Sample Input 2

ABCD

Sample Output 2

No

Although the string ABCD contains both A and C (the first and third characters), the string AC does not appear in ABCD as a contiguous substring.

Sample Input 3

CABD

Sample Output 3

No

Sample Input 4

ACACA

Sample Output 4

Yes

Sample Input 5

XX

Sample Output 5

No


program sample
  implicit none
  integer N, a, k, t, i
  integer, allocatable :: x(:)
  read(*,*) N
  allocate(x(N))
  k = 1
  t = 1
  do i = 1,N
   read(*,*) a
   x(i) = a
   if ( mod(x(i), 2) == 0) then
    k = k * 2
   endif
   if (x(i) /= 1) then
    t = t * 3
   else
    t = t * 2
   endif
  end do
  write(*,*) t - k

  stop
end program sample

Score : 200 points

Problem Statement

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

In particular, any integer sequence is similar to itself.

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

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

Constraints

1 \leq N \leq 10

1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of integer sequences that satisfy the condition.

Sample Input 1

2
2 3

Sample Output 1

7

There are seven integer sequences that satisfy the condition:

1, 2

1, 4

2, 2

2, 3

2, 4

3, 2

3, 4

Sample Input 2

3
3 3 3

Sample Output 2

26

Sample Input 3

1
100

Sample Output 3

1

Sample Input 4

10
90 52 56 71 44 8 13 30 57 84

Sample Output 4

58921


program main
    implicit none
    integer::n,ans,i,t
    integer,allocatable::x(:)
    read(*,*) n
    allocate(x(n))
    read(*,*) x

    t=1

    do i=1,n
        if(mod(x(i),2)==0)then
        	t=t*2
        end if
    end do

    ans=3**n-t

    write(*,*) ans
    deallocate(x)

    stop
end program main


program sample
    implicit none
    character(100000)::s,t
    character::top,las
    integer(8) :: i,j,m,n,a,b,c0,c1
    integer(8),allocatable :: x(:),y(:)

    read(*,*) s
    n=len_trim(s)
    if(n==1)then
        write(*,*)0
    end if
    a=1
    b=n
    c0=0
    c1=0
    m=0

    do i=1,n
        do
        if(s(a:a) .eq. 'x' .and. a<n)then
            a=a+1
            c0=c0+1
        elseif(a==n)then
            write(*,*)0
            stop
        else
            top=s(a:a)
            exit
        end if
        end do
        do
        if(s(b:b) .eq. 'x')then
            b=b-1
            c1=c1+1
        else
            las=s(b:b)
            exit
        end if
        end do
        if(top .ne. las)then
            write(*,*)-1
            stop
        else
            m=m+abs(c0-c1)
            a=a+1
            b=b-1
            c0=0
            c1=0
        end if
        if(a>=b)then
            exit
        endif
    end do

    write(*,*) m

    stop
end program sample

Score : 400 points

Problem Statement

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

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

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

Notes

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

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

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

Sample Input 1

xabxa

Sample Output 1

2

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

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx


program prob5
    implicit none
    character(len=100005)::S
    integer, allocatable::l(:), r(:)
    integer::m, i, j, tmp, ans
    read(*,*) S
    m = len_trim(S)
    allocate(l(2*(m+1)), r(2*(m+1)))
    tmp = 200000
    l = 0
    r = 0
    j = 1
    do i = 1, m
        if(S(i:i) .eq. "x") then
            tmp = tmp + 1
            if(i == m) then
                l(j) = tmp
            end if
        else
            l(j) = tmp
            l(j+1) = ichar(S(i:i)) - ichar('a')
            tmp = 200000
            j = j+2
        end if
    end do

    tmp = 200000
    j = 1
    do i = 1, m
        if(S(m-i+1:m-i+1) .eq. "x") then
            tmp = tmp + 1
            if(i == m) then
                r(j) = tmp
            end if
        else
            r(j) = tmp
            r(j+1) = ichar(S(m-i+1:m-i+1)) - ichar('a')
            tmp = 200000
            j = j+2
        end if
    end do
    !write(*,*) j

    ans = 0
    do i = 1, j/2
        if(mod(i,2) == 1) then
            ans = ans + max(l(i),l(j-i+1)) - 200000
        else
            if(l(i) /= l(j-i+1)) then
                write(*,*) -1
                stop
            end if
        end if
    end do

    write(*,*) ans
    stop
end program


integer a,b,c
read*, a,b,c
print*, a+b+c-2*(a+b+c-min(a,b,c)-max(a,b,c))
end

Score : 100 points

Problem Statement

You are given three integers, A, B and C.

Among them, two are the same, but the remaining one is different from the rest.

For example, when A=5,B=7,C=5, A and C are the same, but B is different.

Find the one that is different from the rest among the given three integers.

Constraints

-100 \leq A,B,C \leq 100

A, B and C are integers.

The input satisfies the condition in the statement.

Input

Input is given from Standard Input in the following format:

A B C

Output

Among A, B and C, print the integer that is different from the rest.

Sample Input 1

5 7 5

Sample Output 1

7

This is the same case as the one in the statement.

Sample Input 2

1 1 7

Sample Output 2

7

In this case, C is the one we seek.

Sample Input 3

-100 100 100

Sample Output 3

-100


program prob5
    implicit none
    integer, allocatable::c(:,:), dist(:), cnt(:), ct(:,:), disted(:), ans(:,:)
    integer::n, m, i, j, k, a, b
    read(*,*) n, m
    allocate(c(n,n),dist(n),cnt(n),ct(n,n),disted(n),ans(n,n))
    c = 0
    do i = 1, m
        read(*,*) a, b
        c(a,b) = 1
        c(b,a) = 1
    end do
    ans = c
    do i = 1, n
        dist = 0
        disted = 0
        disted(i) = 1
        cnt = 0
        ct = c
        do j = 1, n
            if(ct(i,j) == 1) then
                dist(j) = 1
                cnt(j) = 1
                ct(i,j) = 0
                ct(j,i) = 0
                disted(j) = 1
            end if
        end do
        do while(sum(ct) > 0)
            do j = 1, n
                if(dist(j) == 1) then
                    do k = 1, n
                        if(ct(j,k) == 1) then
                            dist(k) = 1
                            if(cnt(k) > 0) then
                                ans(j,k) = 0
                                ans(k,j) = 0
                            end if
                            disted(k) = 1
                            cnt(k) = cnt(k) + 1
                            ct(j,k) = 0
                            ct(k,j) = 0
                        end if
                    end do
                end if
                dist(j) = 0
            end do
        end do
    end do
    write(*,*) sum(ans)/2
    stop
end program

Score : 300 points

Problem Statement

You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.

The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.

An edge whose removal disconnects the graph is called a bridge.

Find the number of the edges that are bridges among the M edges.

Notes

A self-loop is an edge i such that a_i=b_i (1 \leq i \leq M).

Double edges are a pair of edges i,j such that a_i=a_j and b_i=b_j (1 \leq i<j \leq M).

An undirected graph is said to be connected when there exists a path between every pair of vertices.

Constraints

2 \leq N \leq 50

N-1 \leq M \leq min(N(N−1)⁄2,50)

1 \leq a_i<b_i \leq N

The given graph does not contain self-loops and double edges.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of the edges that are bridges among the M edges.

Sample Input 1

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

Sample Output 1

4

The figure below shows the given graph:

The edges shown in red are bridges. There are four of them.

Sample Input 2

3 3
1 2
1 3
2 3

Sample Output 2

0

It is possible that there is no bridge.

Sample Input 3

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

Sample Output 3

5

It is possible that every edge is a bridge.


character s*50
read*, s
print*, s(:len_trim(s)-8)
end

Score : 100 points

Problem Statement

Rng is going to a festival.

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

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

Constraints

9 \leq |S| \leq 50

S consists of uppercase English letters.

S ends with FESTIVAL.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

CODEFESTIVAL

Sample Output 1

CODE

This is the same as the example in the statement.

Sample Input 2

CODEFESTIVALFESTIVAL

Sample Output 2

CODEFESTIVAL

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

Sample Input 3

YAKINIKUFESTIVAL

Sample Output 3

YAKINIKU


implicit none
integer:: n,a,b,i,k,s
read(5,*) n
k=0
s=0
do i=1,n
   read(5,*) a,b
   if (a>=k) then
     k=a
     s=b
   endif
enddo
write(6,"(i0)") k+s
end

Score : 200 points

Problem Statement

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

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

Find the maximum possible number of players in the game.

Constraints

1 \leq N \leq 10^5

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

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

If i ≠ j, A_i ≠ A_j.

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

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible number of players in the game.

Sample Input 1

3
4 7
2 9
6 2

Sample Output 1

8

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

Sample Input 2

5
1 10
3 6
5 2
4 4
2 8

Sample Output 2

7

Sample Input 3

2
1 1000000000
1000000000 1

Sample Output 3

1000000001


implicit none
integer(8) :: i,j,k,n
read(5,*) n
do i = 1,int(3*n/4)
  do j = int(3*n/4), 3500
       if (4*i*j-N*i-N*j<=0) then
            continue
       else if (mod(N*i*j,4*i*j-N*i-N*j)==0) then
            goto 100
             endif
enddo
enddo
100 write(6,"(i0,1x,i0,1x,i0)") i,j,(N*i*j)/(4*i*j-N*i-N*j)
end

Score : 300 points

Problem Statement

You are given an integer N.

Find a triple of positive integers h, n and w such that 4/N = 1/h + 1/n + 1/w.

If there are multiple solutions, any of them will be accepted.

Constraints

It is guaranteed that, for the given integer N, there exists a solution such that h,n,w \leq 3500.

Inputs

Input is given from Standard Input in the following format:

N

Outputs

Print a triple of positive integers h, n and w that satisfies the condition, in the following format:

h n w

Sample Input 1

2

Sample Output 1

1 2 2

4/2 = 1/1 + 1/2 + 1/2.

Sample Input 2

3485

Sample Output 2

872 1012974 1539173474040

It is allowed to use an integer exceeding 3500 in a solution.

Sample Input 3

4664

Sample Output 3

3498 3498 3498


implicit none
integer(8) :: i,j,k,n

read(5,*) n
#do i = 1, int(3*n/4)
do i = int(3*n/4),1,-1
  do j = int(3*n/4), 3500
      do k = i, j
          if(4*i*j*k==n*(i*j+j*k+k*i)) then
          goto 100
          endif
enddo
enddo
enddo

100 write(6,"(i0,1x,i0,1x,i0)") i,j,k
end


program main
	implicit none
	character:: a*10
	read(*,*)a
	if(a(1:4)=="YAKI")then
		write(*,*)"YES"
	else
		write(*,*)"NO"
	endif
end program main

Score : 100 points

Problem Statement

Ringo is giving a present to Snuke.

Ringo has found out that Snuke loves yakiniku (a Japanese term meaning grilled meat. yaki: grilled, niku: meat). He supposes that Snuke likes grilled things starting with YAKI in Japanese, and does not like other things.

You are given a string S representing the Japanese name of Ringo's present to Snuke. Determine whether S starts with YAKI.

Constraints

1 \leq |S| \leq 10

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S starts with YAKI, print Yes; otherwise, print No.

Sample Input 1

YAKINIKU

Sample Output 1

Yes

YAKINIKU starts with YAKI.

Sample Input 2

TAKOYAKI

Sample Output 2

No

TAKOYAKI (a Japanese snack. tako: octopus) does not start with YAKI.

Sample Input 3

YAK

Sample Output 3

No


program prob6
    implicit none
    integer::H, W, i, j, tmp
    character(len=100)::S
    integer::c(26)
    read(*,*) H, W
    c = 0
    do i = 1, H
        read(*,*) S
        do j = 1, W
            tmp = ichar(S(j:j)) - ichar('a') + 1
            c(tmp) = c(tmp) + 1
        end do
    end do
    if(mod(H*W,2) == 0) then
    	tmp = 0
        do i = 1, 26
            if(mod(c(i),2) == 1) then
                write(*,'(a)') "No"
                stop
            else if(c(i) > 0) then
            	tmp = tmp + 1
            end if
        end do
        if(mod(tmp,2) == 0) then
        	write(*,'(a)') "No"
         	stop
        end if
    else
        tmp = 0
        do i = 1, 26
            if(mod(c(i),2) == 1) then
                tmp = tmp + 1
            end if
        end do
        if(tmp /= 1) then
            write(*,'(a)') "No"
        end if
    end if
    write(*,'(a)') "Yes"
    stop
end program

Score : 400 points

Problem Statement

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

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

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

Determine whether he can create a matrix satisfying the condition.

Note

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

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

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

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes


integer(8) :: n,a
read*,n
read*,a
print*,n*n-a
end

Score : 100 points

Problem Statement

We have an N \times N square grid.

We will paint each square in the grid either black or white.

If we paint exactly A squares white, how many squares will be painted black?

Constraints

1 \leq N \leq 100

0 \leq A \leq N^2

Inputs

Input is given from Standard Input in the following format:

N
A

Outputs

Print the number of squares that will be painted black.

Sample Input 1

3
4

Sample Output 1

5

There are nine squares in a 3 \times 3 square grid.
Four of them will be painted white, so the remaining five squares will be painted black.

Sample Input 2

19
100

Sample Output 2

261

Sample Input 3

10
0

Sample Output 3

100

As zero squares will be painted white, all the squares will be painted black.


program ABC074A
    implicit none
    integer(8)::N,A
    read(5,*)N,A
    print'(i0)',N**2-A
end program ABC074A


program sample
    implicit none

    integer(8) :: a,b,c,i,j,m,n,k,a1,b1,c1
    integer(8), allocatable  ::x(:)

    read(*,*) n

    read(*,*) k

    allocate(x(n))
    read(*,*) x
    m=0
    do i=1,n
        m=m+k-abs(k-2*x(i))
    end do
    write(*,*)m
    deallocate(x)
    stop
end program sample

Score : 200 points

Problem Statement

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

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

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

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

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

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

Constraints

1 \leq N \leq 100

1 \leq K \leq 100

0 < x_i < K

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
K
x_1 x_2 ... x_N

Outputs

Print the minimum possible total distance covered by robots.

Sample Input 1

1
10
2

Sample Output 1

4

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

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

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

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

Sample Input 2

2
9
3 6

Sample Output 2

12

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

Sample Input 3

5
20
11 12 9 17 12

Sample Output 3

74


program main
    integer :: A,B,C,D,E,F, asn1, ans2, ac=0, bc = 0, nw = 0, max_num, cc=0, dc=0, sg, agres, k=1
    logical :: flag = .true.
    integer :: water(1000) = 0

    read *, A,B,C,D,E,F
    do i = 0,1000
        do j = 0,1000
            if(A*i+B*j <= F/100) then
                water(k) = A*i+B*j
                k = k + 1
            else
                exit
            end if
        end do
    end do

    max_num = k-1
    ans1 = A*100
    ans2 = 0

    do i=2,max_num
        do j=0,(F/100)*E/C
            do l=0,E*F/100/D
                if(real(C*j+D*l) <= E*water(i) .and. water(i)*100 + C*j+D*l <= F) then
                    sg = C*j+D*l
                else
                    exit
                end if
            end do
            if(real(ans2)/real(ans1) < real(sg)/real(water(i)*100+sg)) then
                ans1 = water(i)*100 + sg
                ans2 = sg
            end if
        end do
    end do
    print *, int(ans1), ans2
end program main

Score : 300 points

Problem Statement

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

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

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

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

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

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

Snuke will make sugar water with the highest possible density.

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

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

Constraints

1 \leq A < B \leq 30

1 \leq C < D \leq 30

1 \leq E \leq 100

100A \leq F \leq 3 000

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

Inputs

Input is given from Standard Input in the following format:

A B C D E F

Outputs

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

Sample Input 1

1 2 10 20 15 200

Sample Output 1

110 10

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

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

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

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

Sample Input 2

1 2 1 2 100 1000

Sample Output 2

200 100

There are other acceptable outputs, such as:

400 200

However, the output below is not acceptable:

300 150

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

Sample Input 3

17 19 22 26 55 2802

Sample Output 3

2634 934


program main
	implicit none
	integer n,modn
	read(*,*)n
	modn=mod(n,10)
	n=n-modn
	if(n==90 .or. modn==9)then
		write(*,"(a3)")"Yes"
	else
		write(*,"(a2)")"No"
	endif
end program main

Score : 100 points

Problem Statement

It is September 9 in Japan now.

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

Constraints

10≤N≤99

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

29

Sample Output 1

Yes

The one's digit of 29 is 9.

Sample Input 2

72

Sample Output 2

No

72 does not contain 9.

Sample Input 3

91

Sample Output 3

Yes


program sample
    implicit none

    integer(8) :: i,j,m,n,b
    integer(8),allocatable :: a(:),y(:)

    read(*,*) n
    allocate(a(n))
    do i=1,n
        read(*,*)a(i)
    end do
    call msort(a)
    b=0
    m=0
    do i=1,n-1
        if(a(i)==a(i+1))then
            a(i)=0
            b=i+1
        else if(b==i)then
            a(i)=0
            b=0
        else
            m=m+1
        end if
    end do
    write(*,*)m
    stop
    contains

    recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:)

    n = size(x)

    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call msort(x(:mid))
    call msort(x(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        i = i + 1
      else
        x(k) = tmp(j)
        j = j - 1
      end if
    end do

    deallocate(tmp)
    return
  end subroutine msort
end program sample

Score : 300 points

Problem Statement

You are playing the following game with Joisino.

Initially, you have a blank sheet of paper.

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

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

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

Constraints

1≤N≤100000

1≤A_i≤1000000000(=10^9)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

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

Sample Input 1

3
6
2
6

Sample Output 1

1

The game proceeds as follows:

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

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

6 is written on the sheet, so erase 6.

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

Sample Input 2

4
2
5
5
2

Sample Output 2

0

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

Sample Input 3

6
12
22
16
22
18
12

Sample Output 3

2


program ABC_072_B_OddString
   implicit none
   integer(8) i
   character(100000) s
   read(*, *) s
   do i = 2, len_trim(s)
       s(i:i)= s(2*i-1:2*i-1)
   end do
   write(*, *) trim(s)
end program ABC_072_B_OddString

Score : 200 points

Problem Statement

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

Constraints

Each character in s is a lowercase English letter.

1≤|s|≤10^5

Input

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

s

Output

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

Sample Input 1

atcoder

Sample Output 1

acdr

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

Sample Input 2

aaaa

Sample Output 2

aa

Sample Input 3

z

Sample Output 3

z

Sample Input 4

fukuokayamaguchi

Sample Output 4

fkoaaauh


program ABC_072_B_OddString
   implicit none
   integer i
   character(100000) s
   character(50000) ans
   read(*, *) s
   ans = ''
   do i = 2, len_trim(s)
       s(i:i)= s(2*i-1:2*i-1)
   end do
   write(*, *) trim(s)
end program ABC_072_B_OddString


program sandglass
  implicit none
  integer :: x, k, q, a(100000), tt(200000,2), ans(100000), i
  integer :: tlft, trit, alft, arit, t0, t1, s
  logical :: rev
  a = 0
  tt = 0
  ans = 0
  read(*,*) x
  read(*,*) k
  read(*,*) tt(1:k,1)
  read(*,*) q
  do i = 1, q
    read(*,*) tt(k+i,1), a(i)
    tt(k+i,2) = i
  end do
  call merge_sort(tt(1:k+q,1:2))
  tlft = 0
  trit = x
  alft = 0
  arit = x
  rev = .false.
  t0 = 0
  do i = 1, k+q
    t1 = tt(i,1)
    s = tt(i,2)
    alft = alft-(t1-t0)
    if (alft.lt.0) then
      tlft = tlft-alft
      if (tlft.gt.x) tlft = x
      alft = 0
    end if
    arit = arit-(t1-t0)
    if (arit.lt.0) then
      trit = trit+arit
      if (trit.lt.0) trit = 0
      arit = 0
    end if
    if (tlft.ge.trit) then
      if (rev) then
        tlft = trit-1
        if (tlft.lt.0) tlft = 0
      else
        trit = tlft+1
        if (trit.gt.x) trit = x
      end if
    end if
    t0 = t1
    if (s.eq.0) then
      rev = .not.rev
      alft = x-alft
      arit = x-arit
      cycle
    end if
    if (a(s).le.tlft) then
      ans(s) = alft
    else if (a(s).ge.trit) then
      ans(s) = arit
    else
      ans(s) = a(s)-tlft+alft
      if (rev) ans(s) = -a(s)+tlft+alft
    end if
    if (rev) ans(s) = x-ans(s)
  end do
  do i = 1, q
    write(*,'(i0)') ans(i)
  end do
  stop
contains
  subroutine merge_sort(c)
    implicit none
    integer, intent(inout) :: c(:,:)
    integer :: n, m, l, u, i
    n = size(c(:,1))
    m = n
    l = 1
    do while (m.gt.1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(c(2*(i-1)*l+1:(2*i-1)*l,:),c((2*i-1)*l+1:u,:))
      end do
      l = 2*l
      m = (m+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merger(c1,c2)
    implicit none
    integer, intent(inout) :: c1(:,:), c2(:,:)
    integer :: c(size(c1(:,1))+size(c2(:,1)),size(c2(1,:)))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while ((i1.le.n1).and.(i2.le.n2))
      if (c1(i1,1).le.c2(i2,1)) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2.le.n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
    return
  end subroutine merger
end program sandglass

Score : 700 points

Problem Statement

We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contain some amount of sand.
When we put the sandglass, either bulb A or B lies on top of the other and becomes the upper bulb. The other bulb becomes the lower bulb.

The sand drops from the upper bulb to the lower bulb at a rate of 1 gram per second.
When the upper bulb no longer contains any sand, nothing happens.

Initially at time 0, bulb A is the upper bulb and contains a grams of sand; bulb B contains X-a grams of sand (for a total of X grams).

We will turn over the sandglass at time r_1,r_2,..,r_K. Assume that this is an instantaneous action and takes no time. Here, time t refer to the time t seconds after time 0.

You are given Q queries.
Each query is in the form of (t_i,a_i).
For each query, assume that a=a_i and find the amount of sand that would be contained in bulb A at time t_i.

Constraints

1≤X≤10^9

1≤K≤10^5

1≤r_1<r_2< .. <r_K≤10^9

1≤Q≤10^5

0≤t_1<t_2< .. <t_Q≤10^9

0≤a_i≤X (1≤i≤Q)

All input values are integers.

Input

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

X
K
r_1 r_2 .. r_K
Q
t_1 a_1
t_2 a_2
:
t_Q a_Q

Output

For each query, print the answer in its own line.

Sample Input 1

180
3
60 120 180
3
30 90
61 1
180 180

Sample Output 1

60
1
120

In the first query, 30 out of the initial 90 grams of sand will drop from bulb A, resulting in 60 grams.
In the second query, the initial 1 gram of sand will drop from bulb A, and nothing will happen for the next 59 seconds. Then, we will turn over the sandglass, and 1 second after this, bulb A contains 1 gram of sand at the time in question.

Sample Input 2

100
1
100000
4
0 100
90 100
100 100
101 100

Sample Output 2

100
10
0
0

In every query, the upper bulb initially contains 100 grams, and the question in time comes before we turn over the sandglass.

Sample Input 3

100
5
48 141 231 314 425
7
0 19
50 98
143 30
231 55
342 0
365 100
600 10

Sample Output 3

19
52
91
10
58
42
100


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    character(200000):: a
    integer(16):: i,l,ia,ans
    integer(int32):: cnt(ichar("a"):ichar("z"))

    read*, a
    cnt(:) = 0
    l = len_trim(a)
    do i=1,l
        ia = ichar(a(i:i))
        cnt(ia) = cnt(ia) + 1
    end do

    ans = (l*(l-1))/2
    do i=ichar("a"),ichar("z")
        ans=ans-(cnt(i)*(cnt(i)-1))/2
    end do
    print'(i0)', ans+1
end program main

Score : 500 points

Problem Statement

You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters.

You can choose any two indices i and j such that 1 \leq i \leq j \leq n and reverse substring A_i A_{i+1} ... A_j.

You can perform this operation at most once.

How many different strings can you obtain?

Constraints

1 \leq |A| \leq 200,000

A consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

A

Output

Print the number of different strings you can obtain by reversing any substring in A at most once.

Sample Input 1

aatt

Sample Output 1

5

You can obtain aatt (don't do anything), atat (reverse A[2..3]), atta (reverse A[2..4]), ttaa (reverse A[1..4]) and taat (reverse A[1..3]).

Sample Input 2

xxxxxxxxxx

Sample Output 2

1

Whatever substring you reverse, you'll always get xxxxxxxxxx.

Sample Input 3

abracadabra

Sample Output 3

44