program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: r

    read*, r
    if (r < 1200) then
        print'(a)', 'ABC'
    else if (r < 2800) then
        print'(a)', 'ARC'
    else
        print'(a)', 'AGC'
    end if
end program main

Score : 100 points

Problem Statement

A programming competition site AtCode regularly holds programming contests.

The next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200.

The contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800.

The contest after the ARC is called AGC, which is rated for all contestants.

Takahashi's rating on AtCode is R. What is the next contest rated for him?

Constraints

0 ≤ R ≤ 4208

R is an integer.

Input

Input is given from Standard Input in the following format:

R

Output

Print the name of the next contest rated for Takahashi (ABC, ARC or AGC).

Sample Input 1

1199

Sample Output 1

ABC

1199 is less than 1200, so ABC will be rated.

Sample Input 2

1200

Sample Output 2

ARC

1200 is not less than 1200 and ABC will be unrated, but it is less than 2800 and ARC will be rated.

Sample Input 3

4208

Sample Output 3

AGC


program main
implicit None
integer(8)::d,g,i,s,n,j,k,ma
integer(8),allocatable::p(:),c(:),a(:),e(:)
character::b*10,f*1

	read*,d,g
	g = g/100
	allocate(p(d),c(d),a(d),e(d))
	do i = 1,d
		read *,p(i),c(i)
		c(i) = c(i)/100
		a(i) = i*p(i)+c(i)
	enddo

	k = sum(p)

	do i = 0,2**d-1
		write(b,"(b10.10)")i
		e =0
		do j = 11-d,10
			f = b(j:j)
			read(f,"(i1)")e(11-j)
		enddo
		s =0;n =0
		do j = 1,d
			s = s + a(j)*e(j)
			n = n + p(j)*e(j)
		enddo

		if(s >=g)then
			if(n < k) k = n
			cycle
		endif

		do j = d,1,-1
			if(e(j) == 0) exit
		enddo

		if(g-s <= j*p(j))then
			n = n + (g-s)/j
			if(mod(g-s,j)/=0) n = n+1
			if(n < k) k = n
		endif
	enddo

	print "(i0)",k

end program main

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

Score : 300 points

Problem Statement

A programming competition site AtCode provides algorithmic problems.
Each problem is allocated a score based on its difficulty.
Currently, for each integer i between 1 and D (inclusive), there are p_i problems with a score of 100i points.
These p_1 + … + p_D problems are all of the problems available on AtCode.

A user of AtCode has a value called total score.
The total score of a user is the sum of the following two elements:

Base score: the sum of the scores of all problems solved by the user.

Perfect bonuses: when a user solves all problems with a score of 100i points, he/she earns the perfect bonus of c_i points, aside from the base score (1 ≤ i ≤ D).

Takahashi, who is the new user of AtCode, has not solved any problem.
His objective is to have a total score of G or more points.
At least how many problems does he need to solve for this objective?

Constraints

1 ≤ D ≤ 10

1 ≤ p_i ≤ 100

100 ≤ c_i ≤ 10^6

100 ≤ G

All values in input are integers.

c_i and G are all multiples of 100.

It is possible to have a total score of G or more points.

Input

Input is given from Standard Input in the following format:

D G
p_1 c_1
:
p_D c_D

Output

Print the minimum number of problems that needs to be solved in order to have a total score of G or more points. Note that this objective is always achievable (see Constraints).

Sample Input 1

2 700
3 500
5 800

Sample Output 1

3

In this case, there are three problems each with 100 points and five problems each with 200 points. The perfect bonus for solving all the 100-point problems is 500 points, and the perfect bonus for solving all the 200-point problems is 800 points. Takahashi's objective is to have a total score of 700 points or more.

One way to achieve this objective is to solve four 200-point problems and earn a base score of 800 points. However, if we solve three 100-point problems, we can earn the perfect bonus of 500 points in addition to the base score of 300 points, for a total score of 800 points, and we can achieve the objective with fewer problems.

Sample Input 2

2 2000
3 500
5 800

Sample Output 2

7

This case is similar to Sample Input 1, but the Takahashi's objective this time is 2000 points or more. In this case, we inevitably need to solve all five 200-point problems, and by solving two 100-point problems additionally we have the total score of 2000 points.

Sample Input 3

2 400
3 500
5 800

Sample Output 3

2

This case is again similar to Sample Input 1, but the Takahashi's objective this time is 400 points or more. In this case, we only need to solve two 200-point problems to achieve the objective.

Sample Input 4

5 25000
20 1000
40 1000
50 1000
30 1000
1 1000

Sample Output 4

66

There is only one 500-point problem, but the perfect bonus can be earned even in such a case.

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.


Score : 400 points

Problem Statement

The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:

1 ≤ i < j < k ≤ |T| (|T| is the length of T.)

T_i = A (T_i is the i-th character of T from the beginning.)

T_j = B

T_k = C

For example, when T = ABCBC, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.

You are given a string S. Each character of S is A, B, C or ?.

Let Q be the number of occurrences of ? in S. We can make 3^Q strings by replacing each occurrence of ? in S with A, B or C. Find the sum of the ABC numbers of all these strings.

This sum can be extremely large, so print the sum modulo 10^9 + 7.

Constraints

3 ≤ |S| ≤ 10^5

Each character of S is A, B, C or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.

Sample Input 1

A??C

Sample Output 1

8

In this case, Q = 2, and we can make 3^Q = 9 strings by by replacing each occurrence of ? with A, B or C. The ABC number of each of these strings is as follows:

AAAC: 0

AABC: 2

AACC: 0

ABAC: 1

ABBC: 2

ABCC: 2

ACAC: 0

ACBC: 1

ACCC: 0

The sum of these is 0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8, so we print 8 modulo 10^9 + 7, that is, 8.

Sample Input 2

ABCBC

Sample Output 2

3

When Q = 0, we print the ABC number of S itself, modulo 10^9 + 7. This string is the same as the one given as an example in the problem statement, and its ABC number is 3.

Sample Input 3

????C?????B??????A???????

Sample Output 3

979596887

In this case, the sum of the ABC numbers of all the 3^Q strings is 2291979612924, and we should print this number modulo 10^9 + 7, that is, 979596887.


program answer
  implicit none

  character(len=100):: S, T, U
  integer :: i, j, a

  read(*,*) S
  read(*,*) T

  a=len_trim(S)

  if(a==1) then
     if(S==T) then
        write(*,*) 'Yes'
        stop
     else
        write(*,*) 'No'
        stop
     end if
  end if

  i=1
  do while(i<a)
     if(S==T) then
        i=0
        exit
     end if
     U=S
     do j = 1, a-1
        S(j+1:j+1)=U(j:j)
     end do
     S(1:1)=U(a:a)
     if(S==T) then
        i=0
        exit
     end if
     i=i+1
  end do

  if(i==0) then
     write(*,*) 'Yes'
  else
     write(*,*) 'No'
  end if

  stop
 end program answer

Score : 100 points

Problem Statement

You have three tasks, all of which need to be completed.

First, you can complete any one task at cost 0.

Then, just after completing the i-th task, you can complete the j-th task at cost |A_j - A_i|.

Here, |x| denotes the absolute value of x.

Find the minimum total cost required to complete all the task.

Constraints

All values in input are integers.

1 \leq A_1, A_2, A_3 \leq 100

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

Print the minimum total cost required to complete all the task.

Sample Input 1

1 6 3

Sample Output 1

5

When the tasks are completed in the following order, the total cost will be 5, which is the minimum:

Complete the first task at cost 0.

Complete the third task at cost 2.

Complete the second task at cost 3.

Sample Input 2

11 5 5

Sample Output 2

6

Sample Input 3

100 100 100

Sample Output 3

0


program main
  implicit none

  character(len=100) :: s, t, s2
  integer :: i, n, l

  read(*,*) s, t

  n = len_trim(s)

  do i = 1, n
     s2 = s(i:n)//s(1:i-1)
     if(t == s2) then
        write(*,*) 'Yes'
        stop
     end if

  end do

  write(*,*) 'No'

end program main

Score : 200 points

Problem Statement

You are given string S and T consisting of lowercase English letters.

Determine if S equals T after rotation.

That is, determine if S equals T after the following operation is performed some number of times:

Operation: Let S = S_1 S_2 ... S_{|S|}. Change S to S_{|S|} S_1 S_2 ... S_{|S|-1}.

Here, |X| denotes the length of the string X.

Constraints

2 \leq |S| \leq 100

|S| = |T|

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S equals T after rotation, print Yes; if it does not, print No.

Sample Input 1

kyoto
tokyo

Sample Output 1

Yes

In the first operation, kyoto becomes okyot.

In the second operation, okyot becomes tokyo.

Sample Input 2

abc
arc

Sample Output 2

No

abc does not equal arc after any number of operations.

Sample Input 3

aaaaaaaaaaaaaaab
aaaaaaaaaaaaaaab

Sample Output 3

Yes


program main
	implicit none
	integer::ans=0,i,n
    integer,dimension(3005)::a
    read(*,*) n
    read(*,*) (a(i),i=1,n)
    do i=1,n
    	ans=ans+a(i)-1
    end do
    write(*,'(i0)') ans
    stop
end program main

Score : 300 points

Problem Statement

You are given N positive integers a_1, a_2, ..., a_N.

For a non-negative integer m, let f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N).

Here, X\ mod\ Y denotes the remainder of the division of X by Y.

Find the maximum value of f.

Constraints

All values in input are integers.

2 \leq N \leq 3000

2 \leq a_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum value of f.

Sample Input 1

3
3 4 6

Sample Output 1

10

f(11) = (11\ mod\ 3) + (11\ mod\ 4) + (11\ mod\ 6) = 10 is the maximum value of f.

Sample Input 2

5
7 46 11 20 11

Sample Output 2

90

Sample Input 3

7
994 518 941 851 647 2 581

Sample Output 3

4527


program test

integer(8) :: n,m,i,j,k,ans
integer(8) :: a(100000),b(100000),aa(100000)=999999

read(*,*) n,m

do i=1,m
	read(*,*) a(i),b(i)
enddo

do i=1,m
	aa(a(i))=min(aa(a(i)),b(i)-1)
enddo

ans = 0

do i=1,n
 if(aa(i)<999999) then
 	ans = ans + 1
 	do j=i+1,aa(i)
 			aa(j) = 999999
 	enddo
 	aa(i) = 999999
 endif
enddo

write(*,*) ans

end program

Score : 400 points

Problem Statement

There are N islands lining up from west to east, connected by N-1 bridges.

The i-th bridge connects the i-th island from the west and the (i+1)-th island from the west.

One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:

Request i: A dispute took place between the a_i-th island from the west and the b_i-th island from the west. Please make traveling between these islands with bridges impossible.

You decided to remove some bridges to meet all these M requests.

Find the minimum number of bridges that must be removed.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq a_i < b_i \leq N

All pairs (a_i, b_i) are distinct.

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 minimum number of bridges that must be removed.

Sample Input 1

5 2
1 4
2 5

Sample Output 1

1

The requests can be met by removing the bridge connecting the second and third islands from the west.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4


program main
implicit None
integer::n,i,s,t
integer,allocatable::a(:)

	read *,n
	allocate(a(n))
	read *,a

	t = 0
	i = 1

	do while(i <= n)
		call count(a,n,i,s)
		t = t + s/2
		i = i + s
	enddo

	print 101,t

101 format(i0)
end program main

subroutine count(a,n,i,s)
implicit None
integer::n,i,s,t
integer::a(n)

t = a(i)
s = 1

do while(a(i+1) == t .and. i < n +1)
i = i + 1
s = s + 1
enddo

end subroutine count

Score : 200 points

Problem Statement

Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000.

Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i.
If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.

Takahashi can change the color of one slime to any of the 10000 colors by one spell.
How many spells are required so that no slimes will start to combine themselves?

Constraints

2 \leq N \leq 100

1 \leq a_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of spells required.

Sample Input 1

5
1 1 2 2 2

Sample Output 1

2

For example, if we change the color of the second slime from the left to 4, and the color of the fourth slime to 5, the colors of the slimes will be 1, 4, 2, 5, 2, which satisfy the condition.

Sample Input 2

3
1 2 1

Sample Output 2

0

Although the colors of the first and third slimes are the same, they are not adjacent, so no spell is required.

Sample Input 3

5
1 1 1 1 1

Sample Output 3

2

For example, if we change the colors of the second and fourth slimes from the left to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the condition.

Sample Input 4

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

Sample Output 4

4


program prob3
    implicit none
    integer::n, m, d
    real(8)::ans
    read(*,*) n, m, d
    if(d == 0) then
        ans = dble(1)*dble(m-1) / dble(n)
    else
        ans = dble(2)*dble(n-d)*dble(m-1) / dble(n)**2
    end if
    write(*,*) ans
    stop
end program

Score : 300 points

Problem Statement

Let us define the beauty of a sequence (a_1,... ,a_n) as the number of pairs of two adjacent elements in it whose absolute differences are d.
For example, when d=1, the beauty of the sequence (3, 2, 3, 10, 9) is 3.

There are a total of n^m sequences of length m where each element is an integer between 1 and n (inclusive).
Find the beauty of each of these n^m sequences, and print the average of those values.

Constraints

0 \leq d < n \leq 10^9

2 \leq m \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m d

Output

Print the average of the beauties of the sequences of length m where each element is an integer between 1 and n.
The output will be judged correct if the absolute or relative error is at most 10^{-6}.

Sample Input 1

2 3 1

Sample Output 1

1.0000000000

The beauty of (1,1,1) is 0.

The beauty of (1,1,2) is 1.

The beauty of (1,2,1) is 2.

The beauty of (1,2,2) is 1.

The beauty of (2,1,1) is 1.

The beauty of (2,1,2) is 2.

The beauty of (2,2,1) is 1.

The beauty of (2,2,2) is 0.

The answer is the average of these values: (0+1+2+1+1+2+1+0)/8=1.

Sample Input 2

1000000000 180707 0

Sample Output 2

0.0001807060


module mod_graph
  implicit none
  integer(8), parameter :: inf = 1000000000000000000_8

  type item
    integer :: id = 0
    integer(8) :: wt = 0_8
  end type

  type priorityqueue
    integer :: num = 0
    type(item), pointer :: heap(:) => null()
  end type

  type edge
    integer :: fm = 0, to = 0, cp = 0, rv = 0
    integer(8) :: wt = 0_8
  end type

  type arraylist
    integer :: num = 0
    type(edge), pointer :: arr(:) => null()
  contains
    procedure :: add => adde
    procedure :: bellman_ford => bellman_ford
  end type

  type graph
    type(arraylist), pointer :: egs(:) => null()
    logical, pointer :: used(:) => null()
  contains
    procedure :: add => add
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
  end type

  interface graph
    module procedure :: newg
  end interface graph

contains

  function newi(id,wt) result(ret)
    integer, intent(in) :: id
    integer(8), intent(in) :: wt
    type(item) :: ret
    ret%id = id
    ret%wt = wt
  end

  subroutine swapi(a,b)
    type(item), intent(inout) :: a, b
    type(item) :: c
    c = a
    a = b
    b = c
  end

  subroutine appendi(a)
    type(item), pointer, intent(inout) :: a(:)
    integer :: n
    type(item), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lessi(a,b) result(ret)
    type(item), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizei(pq)
    type(priorityqueue), intent(inout) :: pq
    if (associated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
  end

  subroutine offer(pq,it)
    type(priorityqueue), intent(inout) :: pq
    type(item), intent(in) :: it
    integer :: i, j
    if (.not.associated(pq%heap)) allocate(pq%heap(1))
    if (pq%num == size(pq%heap)) call appendi(pq%heap)
    pq%num = pq%num+1
    pq%heap(pq%num) = it
    i = pq%num
    do while (i > 1)
      j = i/2
      if (lessi(pq%heap(i),pq%heap(j))) call swapi(pq%heap(i),pq%heap(j))
      i = j
    end do
  end

  function poll(pq) result(ret)
    type(priorityqueue), intent(inout) :: pq
    type(item) :: ret
    integer :: n, i, j
    n = pq%num
    ret = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. lessi(pq%heap(j+1),pq%heap(j))) j = j+1
      if (lessi(pq%heap(j),pq%heap(i))) call swapi(pq%heap(j),pq%heap(i))
      i = j
    end do
  end

  function newe(to,wt,fm,cp,rv) result(ret)
    integer, intent(in) :: to
    integer(8), intent(in) :: wt
    integer, intent(in), optional :: fm, cp, rv
    type(edge) :: ret
    if (present(fm)) ret%fm = fm
    ret%to = to
    ret%wt = wt
    if (present(cp) .and. present(rv)) then
      ret%cp = cp
      ret%rv = rv
    end if
  end

  subroutine appende(a)
    type(edge), pointer, intent(inout) :: a(:)
    integer :: n
    type(edge), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lesse(a,b) result(ret)
    type(edge), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizee(list)
    type(arraylist), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    list%num = 0
  end

  subroutine adde(list,e)
    class(arraylist), intent(inout) :: list
    type(edge), intent(in) :: e
    if (.not.associated(list%arr)) allocate(list%arr(1))
    if (list%num == size(list%arr)) call appende(list%arr)
    list%num = list%num+1
    list%arr(list%num) = e
  end

  function pope(list) result(ret)
    type(arraylist), intent(inout) :: list
    type(edge) :: ret
    ret = list%arr(list%num)
    list%num = list%num-1
  end

  function newg(n) result(ret)
    integer, intent(in) :: n
    type(graph) :: ret
    allocate(ret%egs(n))
  end

  subroutine add(g,fm,to,wt,cp)
    class(graph), intent(inout) :: g
    integer, intent(in) :: fm, to
    integer, intent(in), optional :: cp
    integer(8) :: wt
    if (present(cp)) then
      call adde(g%egs(fm),newe(to,wt,cp=cp,rv=g%egs(to)%num))
      call adde(g%egs(to),newe(fm,wt,cp=0,rv=g%egs(fm)%num-1))
    else
      call adde(g%egs(fm),newe(to,wt))
    end if
  end

  function dijkstra(g,s) result(ret)
    class(graph), intent(in) :: g
    integer, intent(in) :: s
    integer(8) :: ret(size(g%egs))
    type(priorityqueue) :: pq
    type(item) :: it
    type(edge) :: e
    integer :: i
    ret = inf
    ret(s) = 0_8
    call offer(pq,newi(s,ret(s)))
    do while (pq%num > 0)
      it = poll(pq)
      if (it%wt > ret(it%id)) cycle
      do i = 1, g%egs(it%id)%num
        e = g%egs(it%id)%arr(i)
        if (ret(e%to) > ret(it%id)+e%wt) then
          ret(e%to) = ret(it%id)+e%wt
          call offer(pq,newi(e%to,ret(e%to)))
        end if
      end do
    end do
  end

  recursive subroutine dfs_bf(g,u)
    type(graph), intent(inout) :: g
    integer, intent(in) :: u
    integer :: i, v
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      if (g%used(v)) cycle
      call dfs_bf(g,v)
    end do
  end

  function bellman_ford(es,s,t) result(ret)
    class(arraylist), intent(in) :: es
    integer, intent(in) :: s, t
    integer(8) :: ret
    integer :: n, i, fm, to, step
    integer(8) :: wt
    logical :: updated
    integer(8), allocatable :: tmp(:)
    type(graph) :: gs, gt
    type(arraylist) :: reach
    n = 0
    do i = 1, es%num
      n = max(n,es%arr(i)%fm,es%arr(i)%to)
    end do
    gs = newg(n)
    gt = newg(n)
    allocate(gs%used(n),gt%used(n))
    gs%used = .false.
    gt%used = .false.
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      wt = es%arr(i)%wt
      call add(gs,fm,to,wt)
      call add(gt,to,fm,wt)
    end do
    call dfs_bf(gs,s)
    call dfs_bf(gt,t)
    n = 0
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      if (gs%used(fm) .and. gt%used(fm) .and. gs%used(to) .and. gt%used(to)) then
        call adde(reach,es%arr(i))
        n = max(n,fm,to)
      end if
    end do
    deallocate(gs%used,gt%used)
    allocate(tmp(n))
    tmp = inf
    tmp(s) = 0_8
    step = 0
    updated = .true.
    do while (updated)
      updated = .false.
      do i = 1, reach%num
        fm = reach%arr(i)%fm
        to = reach%arr(i)%to
        wt = reach%arr(i)%wt
        if (tmp(to) > tmp(fm)+wt) then
          tmp(to) = tmp(fm)+wt
          updated = .true.
        end if
      end do
      step = step+1
      if (step > n) then
        ret = -inf
        return
      end if
    end do
    ret = tmp(t)
    deallocate(tmp)
  end

  recursive function dfs_ff(g,u,t,f) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: u, t, f
    integer :: ret
    integer :: i, v, cp, rv
    ret = f
    if (u == t) return
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      cp = g%egs(u)%arr(i)%cp
      if (g%used(v) .or. cp <= 0) cycle
      ret = dfs_ff(g,v,t,min(f,cp))
      rv = g%egs(u)%arr(i)%rv
      if (ret > 0) then
        g%egs(u)%arr(i)%cp = g%egs(u)%arr(i)%cp-ret
        g%egs(v)%arr(rv)%cp = g%egs(v)%arr(rv)%cp+ret
        return
      end if
    end do
    ret = 0
  end

  function ford_fulkerson(g,s,t) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: s, t
    integer :: ret
    integer :: f
    ret = 0
    if (.not.associated(g%used)) allocate(g%used(size(g%egs)))
    do
      g%used = .false.
      f = dfs_ff(g,s,t,1000000000)
      if (f == 0) then
        deallocate(g%used)
        return
      end if
      ret = ret+f
    end do
  end

end module mod_graph
program main
   use mod_graph
   implicit none
   integer::N,m,s,t
   integer::u,v
   integer(8)::a,b
   integer(8),allocatable,dimension(:)::FARE1,FARE2
   integer(8),allocatable,dimension(:)::ANS
   type(graph)::YEN,SNUKE

   integer::i
   read*,N,M,S,T
   YEN=graph(N)
   SNUKE=graph(N)
   do i=1,m
      read*,u,v,a,b
      call add(YEN,u,v,a)
      call add(YEN,v,u,a)
      call add(SNUKE,u,v,b)
      call add(SNUKE,v,u,b)
   end do
   FARE1=dijkstra(YEN,S)
   FARE2=dijkstra(SNUKE,T)

   allocate(ANS(N+1))
   ANS=0
   do i=N,1,-1
      ANS(i)=max(ANS(i+1),10_8**15-FARE1(i)-FARE2(i))
   end do
   do i=1,N
      print"(I0)",ANS(I)
   end do
contains

end program main

Score : 400 points

Problem Statement

Kenkoooo is planning a trip in Republic of Snuke.
In this country, there are n cities and m trains running.
The cities are numbered 1 through n, and the i-th train connects City u_i and v_i bidirectionally.
Any city can be reached from any city by changing trains.

Two currencies are used in the country: yen and snuuk.
Any train fare can be paid by both yen and snuuk.
The fare of the i-th train is a_i yen if paid in yen, and b_i snuuk if paid in snuuk.

In a city with a money exchange office, you can change 1 yen into 1 snuuk.
However, when you do a money exchange, you have to change all your yen into snuuk.
That is, if Kenkoooo does a money exchange when he has X yen, he will then have X snuuk.
Currently, there is a money exchange office in every city, but the office in City i will shut down in i years and can never be used in and after that year.

Kenkoooo is planning to depart City s with 10^{15} yen in his pocket and head for City t, and change his yen into snuuk in some city while traveling.
It is acceptable to do the exchange in City s or City t.

Kenkoooo would like to have as much snuuk as possible when he reaches City t by making the optimal choices for the route to travel and the city to do the exchange.
For each i=0,...,n-1, find the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i years.
You can assume that the trip finishes within the year.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq s,t \leq n

s \neq t

1 \leq u_i < v_i \leq n

1 \leq a_i,b_i \leq 10^9

If i\neq j, then u_i \neq u_j or v_i \neq v_j.

Any city can be reached from any city by changing trains.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m s t
u_1 v_1 a_1 b_1
:
u_m v_m a_m b_m

Output

Print n lines.
In the i-th line, print the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i-1 years.

Sample Input 1

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

Sample Output 1

999999999999998
999999999999989
999999999999979
999999999999897

After 0 years, it is optimal to do the exchange in City 1.

After 1 years, it is optimal to do the exchange in City 2.

Note that City 1 can still be visited even after the exchange office is closed.
Also note that, if it was allowed to keep 1 yen when do the exchange in City 2 and change the remaining yen into snuuk, we could reach City 3 with 999999999999998 snuuk, but this is NOT allowed.

After 2 years, it is optimal to do the exchange in City 3.

After 3 years, it is optimal to do the exchange in City 4.
Note that the same train can be used multiple times.

Sample Input 2

8 12 3 8
2 8 685087149 857180777
6 7 298270585 209942236
2 4 346080035 234079976
2 5 131857300 22507157
4 8 30723332 173476334
2 6 480845267 448565596
1 4 181424400 548830121
4 5 57429995 195056405
7 8 160277628 479932440
1 6 475692952 203530153
3 5 336869679 160714712
2 7 389775999 199123879

Sample Output 2

999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994


module module_sort
    implicit none
    interface sort
        module procedure int_heap_sort, real_heap_sort
    end interface

    contains

        subroutine int_heap_down(a,node,las)
            implicit none
            integer(8) a(*),x
            integer(8) node,par,ch1,ch2,las
            par = node
            x = a(par); ch1 = par*2
            do while(ch1 <= las)
                ch2 = ch1+1
                if(ch2 <= las .and. a(ch1) < a(ch2)) ch1 = ch2
                if(a(ch1) > x) then
                    a(par) = a(ch1); a(ch1) = x
                else
                    exit
                endif
                par = ch1; ch1 = par*2
            enddo
        end subroutine int_heap_down

        subroutine int_heap_sort(a,n)
            implicit none
            integer(8) a(*),x
            integer(8) n,i
            do i = n/2, 1, -1
                call int_heap_down(a,i,n)
            enddo
            do i=n,2,-1
                x = a(i); a(i) = a(1); a(1) = x
                call int_heap_down(a,1_8,i-1)
            enddo
        end subroutine int_heap_sort

        subroutine real_heap_down(a,node,las)
            implicit none
            real a(*), x
            integer(8) node,par,ch1,ch2,las
            par = node
            x = a(par); ch1 = par*2
            do while(ch1 <= las)
                ch2 = ch1+1
                if(ch2 <= las .and. a(ch1) < a(ch2)) ch1 = ch2
                if(a(ch1) > x) then
                    a(par) = a(ch1); a(ch1) = x
                else
                    exit
                endif
                par = ch1; ch1 = par*2
            enddo
        end subroutine real_heap_down

        subroutine real_heap_sort(a,n)
            implicit none
            real a(*),x
            integer(8) n,i
            do i = n/2, 1, -1
                call real_heap_down(a,i,n)
            enddo
            do i=n,2,-1
                x = a(i); a(i) = a(1); a(1) = x
                call real_heap_down(a,1_8,i-1)
            enddo
        end subroutine real_heap_sort

end module module_sort

module module_deque
    implicit none
    type deque_node
        type(deque_node) ,pointer:: prev => null(),next => null()
        integer(8) val
    end type deque_node

    type deque
        type(deque_node) ,pointer:: first => null(),last => null()
        integer(8):: size=0
        contains
            procedure:: push_back => deque_push_back
            procedure:: push_front => deque_push_front
            procedure:: pop_back => deque_pop_back
            procedure:: pop_front => deque_pop_front
    end type deque

    contains

        subroutine deque_push_back(self,val)
            class(deque)::self
            integer(8)::val
            if(self%size == 0) then
                allocate(self%first)
                self%last => self%first
                self%first%val = val
                self%size = 1
            else
                allocate(self%last%next)
                self%last%next%prev => self%last
                self%last => self%last%next
                self%last%val = val
                self%size = self%size+1
            endif
        end subroutine deque_push_back

        subroutine deque_push_front(self,val)
            class(deque)::self
            integer(8)::val
            if(self%size == 0) then
                allocate(self%first)
                self%last => self%first
                self%first%val = val
                self%size = 1
            else
                allocate(self%first%prev)
                self%first%prev%next => self%first
                self%first => self%first%prev
                self%first%val = val
                self%size = self%size+1
            endif
        end subroutine deque_push_front

        subroutine deque_pop_back(self)
            class(deque)::self
            if(self%size == 0) then
                return
            else if(self%size == 1) then
                self%size = 0
                deallocate(self%last)
                self%last => null(); self%first => null()
            else
                self%last => self%last%prev
                deallocate(self%last%next)
                self%last%next => null()
                self%size = self%size-1
            endif
        end subroutine deque_pop_back

        subroutine deque_pop_front(self)
            class(deque)::self
            if(self%size == 0) then
                return
            else if(self%size == 1) then
                self%size = 0
                deallocate(self%first)
                self%last => null(); self%first => null()
            else
                self%first => self%first%next
                deallocate(self%first%prev)
                self%first%prev => null()
                self%size = self%size-1
            endif
        end subroutine deque_pop_front

end module module_deque

module module_RedBlackTree
    implicit none
    integer(8),parameter :: red = 1,black = 0
    type RBT_KEY
        character(11) :: k
    end type RBT_KEY
    type RBT_node
        type(RBT_node),pointer :: par=>null(), left => null(),right => null()
        type(RBT_KEY)      :: key
        integer(8)         :: val=0
        integer(8)         :: color=red
    end type RBT_node

    type(RBT_node) ,pointer,save:: rbt_nil

    type RedBlackTree
        type(RBT_node),pointer :: root => null()
        integer(8)         :: size = 0
        contains
            procedure:: insert  => RedBlackTree_Insert
            procedure:: get_val => RedBlackTree_Get
            procedure:: find    => RedBlackTree_Find
            procedure:: add     => RedBlackTree_Add
            procedure:: FixUp   => RedBlackTree_FixUp
            end type RedBlackTree

    contains

        subroutine RedBlackTree_Init()
            implicit none

            if(.not.associated(rbt_nil)) then
                allocate(rbt_nil)
                rbt_nil%color = black
            endif

        end subroutine  RedBlackTree_Init

        recursive function RedBlackTree_Get(self,key) result(val)
            implicit none
            class(RedBlackTree), intent(in) :: self
            type(RBT_KEY)                   :: key
            integer(8)                      :: val
            type(RBT_node),pointer              :: u

            u   => SearchTree(self%root,key)
            val = u%val
            return

        end function RedBlackTree_Get

        recursive function RedBlackTree_Find(self,key) result(res)
            implicit none
            class(RedBlackTree), intent(in) :: self
            type(RBT_KEY)                   :: key
            logical                         :: res
            type(RBT_node),pointer              :: u

            u   => SearchTree(self%root,key)
            res = (.not.associated(u,rbt_nil))
            return

        end function RedBlackTree_Find

        recursive subroutine RedBlackTree_Add(self,key,val)
            implicit none
            class(RedBlackTree), intent(inout) :: self
            type(RBT_KEY)                      :: key
            type(RBT_node),pointer                 :: u
            integer(8)                         :: val

            if(.not.associated(self%root)) then
                call self%insert(key,val)
                return
            endif

            u   => SearchTree(self%root,key)
            if (associated(u,rbt_nil)) then
                call self%insert(key,val)
            else
                u%val = u%val+val
            end if
            return

        end subroutine RedBlackTree_Add

        recursive function SearchTree(u,key) result(res)
            implicit none
            type(RBT_node), pointer  :: u,res
            type(RBT_KEY)        :: key

            if(associated(u,rbt_nil)) then
                res => rbt_nil
                return
            endif

            if(key%k < u%key%k) then
                res => SearchTree(u%left,key)
                return
            else if(key%k > u%key%k) then
                res => SearchTree(u%right,key)
                return
            else
                res => u
                return
            endif

        end function SearchTree

        subroutine RedBlackTree_Insert(self,key,val)
            implicit none
            class(RedBlackTree),intent(inout):: self
            type(RBT_KEY),intent(in)         :: key
            integer(8),intent(in)            :: val
            type(RBT_node),pointer,save          :: now, u

            !allocate new RBT_node
            allocate(u)
            u%key = key; u%val = val
            u%left => rbt_nil; u%right => rbt_nil

            !insert new RBT_node
            if(.not. associated(self%root)) then
                u%left => rbt_nil; u%right => rbt_nil; u%par => rbt_nil
                self%root => u
                self%root%color = black
                self%size = 1
                return
            else
                now => self%root
                if(.not.Insert_RBT_node(u,now)) return
            endif

            !Fix Tree
            call self%FixUp(u)

            self%size = self%size+1

        end subroutine RedBlackTree_Insert

        recursive function Insert_RBT_node(u,now) result(added)
            implicit none
            type(RBT_node), pointer:: u,now
            logical            :: added

            if(u%key%k < now%key%k) then
                if(associated(now%left,rbt_nil)) then
                    now%left => u
                    u%par => now
                    added = .true.
                else
                    now => now%left
                    added = Insert_RBT_node(u,now)
                endif
                return
            else if(u%key%k > now%key%k) then
                if(associated(now%right,rbt_nil)) then
                    now%right => u
                    u%par => now
                    added = .true.
                else
                    now => now%right
                    added = Insert_RBT_node(u,now)
                endif
                return
            else
                added = .false.
                return
            endif

        end function Insert_RBT_node

        subroutine RedBlackTree_FixUp(self,u)
            implicit none
            class(RedBlackTree),intent(inout) :: self
            type(RBT_node),pointer,intent(inout)  :: u
            type(RBT_node),pointer                :: w,g

            nullify(w,g)
            do while(u%color == red)
                if(u%key%k == self%root%key%k) then
                    u%color = black
                    return
                endif
                w => u%par
                if(w%left%color == black) then
                    call FripLeft(w,self%root)
                    u => w
                    w => u%par
                endif
                if(w%color == black) return
                g => w%par
                if(g%right%color == black) then
                    call FripRight(g,self%root)
                    return
                else
                    call PushBlack(g)
                    u => g
                endif
            enddo

        end subroutine RedBlackTree_FixUp

        subroutine PushBlack(u)
            implicit none
            type(RBT_node), pointer:: u

            u%color = red; u%left%color = black; u%right%color = black;

        end subroutine PushBlack

        subroutine PullBlack(u)
            implicit none
            type(RBT_node), pointer:: u

            u%color = black; u%left%color = red; u%right%color = red;

        end subroutine PullBlack

        subroutine FripLeft(u,root)
            implicit none
            type(RBT_node), pointer, intent(inout) :: root
            type(RBT_node), pointer                :: u,w
            integer(8)                         :: tmp

            tmp = u%color; u%color = u%right%color; u%right%color = tmp

            w => u%right
            w%par => u%par
            if(.not.associated(u%par,rbt_nil)) then
                if(associated(w%par%left,u)) then
                     w%par%left=>w
                else
                    w%par%right=>w
                endif
            endif
            u%right => w%left
            if(.not.associated(u%right,rbt_nil))u%right%par => u
            u%par => w
            w%left => u
            if(associated(u,root)) then
                root => w
                root%par => rbt_nil
            endif

        end subroutine FripLeft

        subroutine FripRight(u,root)
            implicit none
            type(RBT_node), pointer,intent(inout):: root
            type(RBT_node), pointer              :: u,w
            integer(8)                       :: tmp

            tmp = u%color; u%color = u%left%color; u%left%color = tmp

            w => u%left
            w%par => u%par
            if(.not.associated(u%par,rbt_nil)) then
                if(associated(w%par%left,u)) then
                    w%par%left=>w
                else
                    w%par%right=>w
                endif
            endif
            u%left => w%right
            if(.not.associated(u%left,rbt_nil))u%left%par => u
            u%par => w
            w%right => u
            if(associated(u,root)) then
                root => w
                root%par => rbt_nil
            endif

        end subroutine FripRight

end module module_RedBlackTree

module module_MinHeap
    implicit none
    type HEAP_TYPE
        integer(8) ::key,val
    end type HEAP_TYPE

    type MH_node
        type(MH_node),pointer :: par=>null(), left => null(),right => null()
        type(HEAP_TYPE)    :: cont
        integer(8)         :: size
    end type MH_node
    type(MH_node) ,pointer,save:: heap_nil

    type MinHeap
        type(MH_node),pointer :: root,last
        contains
            procedure:: push => MinHeap_Insert_MH_node
            procedure:: pop  => MinHeap_Pop_MH_node
    end type MinHeap

    contains

        subroutine Swap_HEAP_TYPE(a,b)
            implicit none
            type(HEAP_TYPE),pointer :: a,b,tmp

            tmp%key = a%key
            a%key   = b%key
            b%key   = tmp%key
            tmp%val = a%val
            a%val   = b%val
            b%val   = tmp%val

        end subroutine Swap_HEAP_TYPE

        subroutine MinHeap_Up(root,u)
            implicit none
            type(MH_node),pointer      :: root,u
            type(HEAP_TYPE)         :: tmp

            do while(.not.associated(root,u))
                if(u%cont%key < u%par%cont%key) then
                    tmp%key        = u%cont%key
                    u%cont%key     = u%par%cont%key
                    u%par%cont%key = tmp%key
                    tmp%val        = u%cont%val
                    u%cont%val     = u%par%cont%val
                    u%par%cont%val = tmp%val
                    u => u%par
                else
                    return
                endif
            enddo
            return

        end subroutine MinHeap_Up

        subroutine MinHeap_Down(root)
            implicit none
            type(MH_node),pointer      :: root,u
            type(HEAP_TYPE)         :: tmp

            u => root
            do while(.not.associated(u%left,heap_nil))

                if(.not.associated(u%right,heap_nil)) then
                    if (u%right%cont%key < u%left%cont%key .and. u%right%cont%key < u%cont%key) then
                        tmp%key          = u%cont%key
                        u%cont%key       = u%right%cont%key
                        u%right%cont%key = tmp%key
                        tmp%val          = u%cont%val
                        u%cont%val       = u%right%cont%val
                        u%right%cont%val = tmp%val
                        u => u%right
                        cycle
                    endif
                endif
                if(u%left%cont%key < u%cont%key) then
                    tmp%key         = u%cont%key
                    u%cont%key      = u%left%cont%key
                    u%left%cont%key = tmp%key
                    tmp%val         = u%cont%val
                    u%cont%val      = u%left%cont%val
                    u%left%cont%val = tmp%val
                    u => u%left
                    cycle
                endif
                exit
            enddo
            return

        end subroutine MinHeap_Down

        subroutine Update_Size(root,u,is_add)
            implicit none
            type(MH_node),pointer :: root,u
            logical            :: is_add

            do while(.not.associated(root,u))
                if(is_add) then
                    u%size = u%size+1
                else
                    u%size = u%size-1
                endif
                u => u%par
            enddo

            if(is_add) then
                u%size = u%size+1
            else
                u%size = u%size-1
            endif

        end subroutine Update_size

        subroutine MinHeap_Insert_MH_node(self,cont)
            implicit none
            class(MinHeap)     :: self
            type(MH_node),pointer :: u,now
            type(HEAP_TYPE)    :: cont

            if(.not.associated(heap_nil)) allocate(heap_nil)
            u => null()
            allocate(u)
            u%cont = cont
            u%left => heap_nil; u%right => heap_nil
            u%size = 1

            if(.not.associated(self%root)) then
                self%root      => u
                self%root%par  => heap_nil
                return
            endif

            now => self%root
            do
                now%size = now%size+1
                if(.not.associated(now%left,heap_nil)) then
                    if(.not.associated(now%right,heap_nil)) then
                        if(now%right%size < now%left%size) then
                            now => now%right; cycle
                        else
                            now => now%left;  cycle
                        endif
                    else
                        now%right => u
                        u%par     => now
                        exit
                    endif
                else
                    now%left => u
                    u%par    => now
                    exit
                endif
            enddo

            call MinHeap_Up(self%root,u)

        end subroutine MinHeap_Insert_MH_node

        subroutine MinHeap_Pop_MH_node(self)
            implicit none
            class(MinHeap)     :: self
            type(MH_node),pointer :: u,now
            type(HEAP_TYPE)    :: tmp

            if(.not.associated(heap_nil)) allocate(heap_nil)
            if(.not.associated(self%root)) return

            now => self%root
            do
                now%size = now%size-1
                if(.not.associated(now%left,heap_nil)) then
                    if(.not.associated(now%right,heap_nil)) then
                        if(now%right%size >= now%left%size) then
                            now => now%right; cycle
                        else
                            now => now%left;  cycle
                        endif
                    else
                        u => now%left
                        tmp%key            = u%cont%key
                        u%cont%key         = self%root%cont%key
                        self%root%cont%key = tmp%key
                        tmp%val            = u%cont%val
                        u%cont%val         = self%root%cont%val
                        self%root%cont%val = tmp%val
                        now%left => heap_nil
                        exit
                    endif
                else if(associated(now,self%root)) then
                    deallocate(self%root)
                    return
                else
                    u => now
                    tmp%key            = u%cont%key
                    u%cont%key         = self%root%cont%key
                    self%root%cont%key = tmp%key
                    tmp%val            = u%cont%val
                    u%cont%val         = self%root%cont%val
                    self%root%cont%val = tmp%val
                    if(associated(now%par%left,now)) then
                        now%par%left => heap_nil
                    else
                        now%par%right => heap_nil
                    endif
                    exit
                endif
            enddo

            deallocate(u)

            call MinHeap_Down(self%root)

        end subroutine MinHeap_Pop_MH_node

end module module_MinHeap

module global
    use module_sort
    use module_deque
    use module_RedBlackTree
    use module_MinHeap
    implicit none
    integer(8)  :: n,m,s,t,u,v,a,b,ds(100005),dt(100005),ans(100005),mm
    type(deque) :: to(200005),yen(200005),snk(200005)
    type(deque_node),pointer :: tp,yp,sp
    type(HEAP_TYPE) heap_t
    type(MinHeap) pque
end module global

program main
    use global
    integer(8) i
    integer(8) now,dist,nex

    call RedBlackTree_Init()

    read *,n,m,s,t

    do i=1,m
        read*, u,v,a,b
        call to(u)%push_back(v)
        call yen(u)%push_back(a)
        call snk(u)%push_back(b)
        call to(v)%push_back(u)
        call yen(v)%push_back(a)
        call snk(v)%push_back(b)
    enddo

    do i=1,n+3
        ds(i) = 1e15_8
        dt(i) = 1e15_8
    enddo

    heap_t%key = 0; heap_t%val = s
    call pque%push(heap_t)
    ds(s) = 0
    do while(associated(pque%root))
        now  = pque%root%cont%val
        dist = pque%root%cont%key
        call pque%pop()
        if(dist > ds(now)) cycle
        tp => to(now)%first
        yp => yen(now)%first
        do
            nex = tp%val
            if(ds(nex) > dist+yp%val) then
                ds(nex) = dist+yp%val
                heap_t%key = dist+yp%val
                heap_t%val = nex
                call pque%push(heap_t)
            endif
            if(associated(tp,to(now)%last)) exit
            tp => tp%next
            yp => yp%next
        enddo
    enddo

    heap_t%key = 0; heap_t%val = t
    call pque%push(heap_t)
    dt(t) = 0
    do while(associated(pque%root))
        now  = pque%root%cont%val
        dist = pque%root%cont%key
        call pque%pop()
        if(dist > ds(now)) cycle
        tp => to(now)%first
        sp => snk(now)%first
        do
            nex = tp%val
            if(dt(nex) > dist+sp%val) then
                dt(nex) = dist+sp%val
                heap_t%key = dist+sp%val
                heap_t%val = nex
                call pque%push(heap_t)
            endif
            if(associated(tp,to(now)%last)) exit
            tp => tp%next
            sp => sp%next
        enddo
    enddo

    mm = 1e15_8

    do i = n, 1,-1
        if(ds(i) /= 1e15_8 .and. dt(i) /= 1e15_8) mm = min(mm,ds(i)+dt(i))
        ans(i) = 1000000000000000_8-mm
    enddo

    do i = 1,n
        print '(i0)',ans(i)
    enddo

end program main


module mod_graph
  implicit none
  integer(8), parameter :: inf = 1000000000000000000_8

  type item
    integer :: id = 0
    integer(8) :: wt = 0_8
  end type

  type priorityqueue
    integer :: num = 0
    type(item), allocatable :: heap(:)
  end type

  type edge
    integer :: fm = 0, to = 0, cp = 0, rv = 0
    integer(8) :: wt = 0_8
  end type

  type arraylist
    integer :: num = 0
    type(edge), allocatable :: arr(:)
  contains
    procedure :: add => adde
    procedure :: bellman_ford => bellman_ford
  end type

  type graph
    type(arraylist), allocatable :: egs(:)
    logical, allocatable :: used(:)
  contains
    procedure :: add => add
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
  end type

  interface graph
    module procedure :: newg
  end interface graph

contains

  function newi(id,wt) result(ret)
    integer, intent(in) :: id
    integer(8), intent(in) :: wt
    type(item) :: ret
    ret%id = id
    ret%wt = wt
  end

  subroutine swapi(a,b)
    type(item), intent(inout) :: a, b
    type(item) :: c
    c = a
    a = b
    b = c
  end

  subroutine appendi(a)
    type(item), allocatable, intent(inout) :: a(:)
    integer :: n
    type(item), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lessi(a,b) result(ret)
    type(item), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizei(pq)
    type(priorityqueue), intent(inout) :: pq
    if (allocated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
  end

  subroutine offer(pq,it)
    type(priorityqueue), intent(inout) :: pq
    type(item), intent(in) :: it
    integer :: i, j
    if (.not.allocated(pq%heap)) allocate(pq%heap(1))
    if (pq%num == size(pq%heap)) call appendi(pq%heap)
    pq%num = pq%num+1
    pq%heap(pq%num) = it
    i = pq%num
    do while (i > 1)
      j = i/2
      if (lessi(pq%heap(i),pq%heap(j))) call swapi(pq%heap(i),pq%heap(j))
      i = j
    end do
  end

  function poll(pq) result(ret)
    type(priorityqueue), intent(inout) :: pq
    type(item) :: ret
    integer :: n, i, j
    n = pq%num
    ret = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. lessi(pq%heap(j+1),pq%heap(j))) j = j+1
      if (lessi(pq%heap(j),pq%heap(i))) call swapi(pq%heap(j),pq%heap(i))
      i = j
    end do
  end

  function newe(to,wt,fm,cp,rv) result(ret)
    integer, intent(in) :: to
    integer(8), intent(in) :: wt
    integer, intent(in), optional :: fm, cp, rv
    type(edge) :: ret
    if (present(fm)) ret%fm = fm
    ret%to = to
    ret%wt = wt
    if (present(cp) .and. present(rv)) then
      ret%cp = cp
      ret%rv = rv
    end if
  end

  subroutine appende(a)
    type(edge), allocatable, intent(inout) :: a(:)
    integer :: n
    type(edge), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lesse(a,b) result(ret)
    type(edge), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizee(list)
    type(arraylist), intent(inout) :: list
    if (allocated(list%arr)) deallocate(list%arr)
    list%num = 0
  end

  subroutine adde(list,e)
    class(arraylist), intent(inout) :: list
    type(edge), intent(in) :: e
    if (.not.allocated(list%arr)) allocate(list%arr(1))
    if (list%num == size(list%arr)) call appende(list%arr)
    list%num = list%num+1
    list%arr(list%num) = e
  end

  function newg(n) result(ret)
    integer, intent(in) :: n
    type(graph) :: ret
    allocate(ret%egs(n))
  end

  subroutine add(g,fm,to,wt,cp)
    class(graph), intent(inout) :: g
    integer, intent(in) :: fm, to
    integer, intent(in), optional :: cp
    integer(8) :: wt
    if (present(cp)) then
      call adde(g%egs(fm),newe(to,wt,cp=cp,rv=g%egs(to)%num))
      call adde(g%egs(to),newe(fm,wt,cp=0,rv=g%egs(fm)%num-1))
    else
      call adde(g%egs(fm),newe(to,wt))
    end if
  end

  function dijkstra(g,s) result(ret)
    class(graph), intent(in) :: g
    integer, intent(in) :: s
    integer(8) :: ret(size(g%egs))
    type(priorityqueue) :: pq
    type(item) :: it
    type(edge) :: e
    integer :: i
    ret = inf
    ret(s) = 0_8
    call offer(pq,newi(s,ret(s)))
    do while (pq%num > 0)
      it = poll(pq)
      if (it%wt > ret(it%id)) cycle
      do i = 1, g%egs(it%id)%num
        e = g%egs(it%id)%arr(i)
        if (ret(e%to) > ret(it%id)+e%wt) then
          ret(e%to) = ret(it%id)+e%wt
          call offer(pq,newi(e%to,ret(e%to)))
        end if
      end do
    end do
  end

  recursive subroutine dfs_bf(g,u)
    type(graph), intent(inout) :: g
    integer, intent(in) :: u
    integer :: i, v
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      if (g%used(v)) cycle
      call dfs_bf(g,v)
    end do
  end

  function bellman_ford(es,s,t) result(ret)
    class(arraylist), intent(in) :: es
    integer, intent(in) :: s, t
    integer(8) :: ret
    integer :: n, i, fm, to, step
    integer(8) :: wt
    logical :: updated
    integer(8), allocatable :: tmp(:)
    type(graph) :: gs, gt
    type(arraylist) :: reach
    n = 0
    do i = 1, es%num
      n = max(n,es%arr(i)%fm,es%arr(i)%to)
    end do
    gs = newg(n)
    gt = newg(n)
    allocate(gs%used(n),gt%used(n))
    gs%used = .false.
    gt%used = .false.
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      wt = es%arr(i)%wt
      call add(gs,fm,to,wt)
      call add(gt,to,fm,wt)
    end do
    call dfs_bf(gs,s)
    call dfs_bf(gt,t)
    n = 0
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      if (gs%used(fm) .and. gt%used(fm) .and. gs%used(to) .and. gt%used(to)) then
        call adde(reach,es%arr(i))
        n = max(n,fm,to)
      end if
    end do
    deallocate(gs%used,gt%used)
    allocate(tmp(n))
    tmp = inf
    tmp(s) = 0_8
    step = 0
    updated = .true.
    do while (updated)
      updated = .false.
      do i = 1, reach%num
        fm = reach%arr(i)%fm
        to = reach%arr(i)%to
        wt = reach%arr(i)%wt
        if (tmp(to) > tmp(fm)+wt) then
          tmp(to) = tmp(fm)+wt
          updated = .true.
        end if
      end do
      step = step+1
      if (step > n) then
        ret = -inf
        return
      end if
    end do
    ret = tmp(t)
    deallocate(tmp)
  end

  recursive function dfs_ff(g,u,t,f) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: u, t, f
    integer :: ret
    integer :: i, v, cp, rv
    ret = f
    if (u == t) return
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      cp = g%egs(u)%arr(i)%cp
      if (g%used(v) .or. cp <= 0) cycle
      ret = dfs_ff(g,v,t,min(f,cp))
      rv = g%egs(u)%arr(i)%rv
      if (ret > 0) then
        g%egs(u)%arr(i)%cp = g%egs(u)%arr(i)%cp-ret
        g%egs(v)%arr(rv)%cp = g%egs(v)%arr(rv)%cp+ret
        return
      end if
    end do
    ret = 0
  end

  function ford_fulkerson(g,s,t) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: s, t
    integer :: ret
    integer :: f
    ret = 0
    if (size(g%used) /= size(g%egs)) deallocate(g%used)
    if (.not.allocated(g%used)) allocate(g%used(size(g%egs)))
    do
      g%used = .false.
      f = dfs_ff(g,s,t,1000000000)
      if (f == 0) then
        deallocate(g%used)
        return
      end if
      ret = ret+f
    end do
  end

end module mod_graph

program saving_snuuk
  use mod_graph
  implicit none
  integer(8), parameter :: dyen = 1000000000000000_8
  integer :: n, m, s, t, u, v, i
  integer(8) :: a, b, cy(100000) = 0_8, cs(100000) = 0_8, ans(100000) = 0_8
  type(graph) :: gy, gs
  read(*,*) n, m, s, t
  gy = graph(n)
  gs = graph(n)
  do i = 1, m
    read(*,*) u, v, a, b
    call gy%add(u,v,a)
    call gy%add(v,u,a)
    call gs%add(u,v,b)
    call gs%add(v,u,b)
  end do
  cy(1:n) = gy%dijkstra(s)
  cs(1:n) = gs%dijkstra(t)
  ans(n) = cy(n)+cs(n)
  do i = n-1, 1, -1
    ans(i) = min(ans(i+1),cy(i)+cs(i))
  end do
  do i = 1, n
    write(*,'(i0)') dyen-ans(i)
  end do
end program saving_snuuk


program multiple_of_2_and_n
  implicit none
  integer :: n
  read(*,*) n
  write(*,'(i0)') merge(n,2*n,mod(n,2) == 0)
end program multiple_of_2_and_n

Score : 100 points

Problem Statement

You are given a positive integer N.
Find the minimum positive integer divisible by both 2 and N.

Constraints

1 \leq N \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum positive integer divisible by both 2 and N.

Sample Input 1

3

Sample Output 1

6

6 is divisible by both 2 and 3.
Also, there is no positive integer less than 6 that is divisible by both 2 and 3.
Thus, the answer is 6.

Sample Input 2

10

Sample Output 2

10

Sample Input 3

999999999

Sample Output 3

1999999998


module ABC102

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_B
  private :: linear_search

  ! constants for this <module>
  integer(INT32), parameter, private :: len_sequence_max = 100

  ! variables for this <module>
  integer(INT32), private :: len_sequence
  integer(INT32), private :: val_sequence_max
  integer(INT32), private :: val_sequence_min

  ! array for this <module>
  integer(INT32), private :: val_sequence(1:len_sequence_max)

  ! contained <subroutine>s and <function>s are below
  contains

  subroutine task_B

    ! variables for this <subroutine>
    integer :: iter

    ! STEP.01
    ! read out the length of the given sequence
    read(unit=INPUT_UNIT, fmt=*) len_sequence

    ! STEP.02
    ! read out the values of the given sequence
    read(unit=INPUT_UNIT, fmt=*) val_sequence(1:len_sequence)

    ! STEP.03
    ! find the maximum & minimum value of the given sequence
    call linear_search

    ! STEP.04
    ! calculate & output the maximum difference of the given sequence
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') val_sequence_max - val_sequence_min

    ! STEP.END
    return

  end subroutine task_B

  subroutine linear_search

    ! support variables for this <subroutine>
    integer(INT32) :: iter

    val_sequence_max = val_sequence(1)
    val_sequence_min = val_sequence(1)

    do iter = 1, len_sequence, 1
      if (val_sequence(iter) .gt. val_sequence_max) val_sequence_max = val_sequence(iter)
      if (val_sequence(iter) .lt. val_sequence_min) val_sequence_min = val_sequence(iter)
    end do

    return

  end subroutine linear_search

end module ABC102

program main

  ! <module>s to import
  use, non_intrinsic :: ABC102

  ! require all variables to be explicitly declared
  implicit none

  call task_B

end program main

Score : 200 points

Problem Statement

You are given an integer sequence A of length N.
Find the maximum absolute difference of two elements (with different indices) in A.

Constraints

2 \leq N \leq 100

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum absolute difference of two elements (with different indices) in A.

Sample Input 1

4
1 4 6 3

Sample Output 1

5

The maximum absolute difference of two elements is A_3-A_1=6-1=5.

Sample Input 2

2
1000000000 1

Sample Output 2

999999999

Sample Input 3

5
1 1 1 1 1

Sample Output 3

0


program linear_approximation
  implicit none
  integer :: n, a(200000) = 0, i
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    a(i) = a(i)-i
  end do
  call quick_sort(a(1:n))
  if (mod(n,2) == 0) then
    write(*,'(i0)') min(sum(int(abs(a(1:n)-a(n/2)),8)),sum(int(abs(a(1:n)-a(n/2+1)),8)))
  else
    write(*,'(i0)') sum(int(abs(a(1:n)-a((n+1)/2)),8))
  end if
contains
  recursive subroutine quick_sort(a)
    integer, intent(inout) :: a(:)
    integer :: n, l, r, m
    integer :: p
    n = size(a)
    l = 1
    r = n
    m = (n+1)/2
    p = a(l)+a(m)+a(r)-max(a(l),a(m),a(r))-min(a(l),a(m),a(r))
    do
      do while (a(l) < p)
        l = l+1
      end do
      do while (a(r) > p)
        r = r-1
      end do
      if (l >= r) exit
      a(l) = xor(a(l),a(r))
      a(r) = xor(a(l),a(r))
      a(l) = xor(a(l),a(r))
      l = l+1
      r = r-1
    end do
    if (l-1 > 1) call quick_sort(a(1:l-1))
    if (n > r+1) call quick_sort(a(r+1:n))
  end
end program linear_approximation

Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

He will freely choose an integer b.
Here, he will get sad if A_i and b+i are far from each other.
More specifically, the sadness of Snuke is calculated as follows:

abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))

Here, abs(x) is a function that returns the absolute value of x.

Find the minimum possible sadness of Snuke.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

If we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.
Any choice of b does not make the sadness of Snuke less than 2, so the answer is 2.

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6


module ABC101

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_A

  ! constants for this <module>
  integer, parameter, private :: len_given_string = 4

  ! variables for this <module>
  character(len=len_given_string, kind=1), private :: given_string
  integer,                                 private :: integer_target

  ! contained <subroutine>s and <function>s are below
  contains

  subroutine task_A

    ! variables for this <subroutine>
    integer :: iter

    ! STEP.01
    ! initialize the integer which Mr.Takahashi remembers
    integer_target = 0

    ! STEP.02
    ! read out the given string
    read(unit=INPUT_UNIT, fmt='(A)') given_string

    ! STEP.03
    ! update the target integer
    do iter = 1, len_given_string, 1
      if (given_string(iter:iter) .eq. '+') integer_target = integer_target + 1
      if (given_string(iter:iter) .eq. '-') integer_target = integer_target - 1
    end do

    ! STEP.04
    ! output the target integer
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') integer_target

    ! STEP.END
    return

  end subroutine task_A

end module ABC101

program main

  ! <module>s to import
  use, non_intrinsic :: ABC101

  ! require all variables to be explicitly declared
  implicit none

  call task_A

end program main

Score : 100 points

Problem Statement

There is always an integer in Takahashi's mind.

Initially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is + or -. When he eats +, the integer in his mind increases by 1; when he eats -, the integer in his mind decreases by 1.

The symbols Takahashi is going to eat are given to you as a string S. The i-th character in S is the i-th symbol for him to eat.

Find the integer in Takahashi's mind after he eats all the symbols.

Constraints

The length of S is 4.

Each character in S is + or -.

Input

Input is given from Standard Input in the following format:

S

Output

Print the integer in Takahashi's mind after he eats all the symbols.

Sample Input 1

+-++

Sample Output 1

2

Initially, the integer in Takahashi's mind is 0.

The first integer for him to eat is +. After eating it, the integer in his mind becomes 1.

The second integer to eat is -. After eating it, the integer in his mind becomes 0.

The third integer to eat is +. After eating it, the integer in his mind becomes 1.

The fourth integer to eat is +. After eating it, the integer in his mind becomes 2.

Thus, the integer in Takahashi's mind after he eats all the symbols is 2.

Sample Input 2

-+--

Sample Output 2

-2

Sample Input 3

----

Sample Output 3

-4


program AESE
    implicit none
    integer :: i=0,j=0
    character(len=4) :: str

    read(*,*) str
    do i=1,4
        if(str(i:i) == '+')then
            j = j+1
        else if(str(i:i) == '-')then
            j = j-1
        else
        endif
    enddo
    write(*,*) j

end program AESE


program minimization
  implicit none
  integer :: n, k
  read(*,*) n, k
  write(*,'(i0)') (n+k-3)/(k-1)
end program minimization

Score : 300 points

Problem Statement

There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.

On this sequence, Snuke can perform the following operation:

Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.

Snuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.
Find the minimum number of operations required.
It can be proved that, Under the constraints of this problem, this objective is always achievable.

Constraints

2 \leq K \leq N \leq 100000

A_1, A_2, ..., A_N is a permutation of 1, 2, ..., N.

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 operations required.

Sample Input 1

4 3
2 3 1 4

Sample Output 1

2

One optimal strategy is as follows:

In the first operation, choose the first, second and third elements. The sequence A becomes 1, 1, 1, 4.

In the second operation, choose the second, third and fourth elements. The sequence A becomes 1, 1, 1, 1.

Sample Input 2

3 3
1 2 3

Sample Output 2

1

Sample Input 3

8 3
7 3 1 8 4 6 2 5

Sample Output 3

4


program abc101c
  implicit none
  integer n, k
  integer,allocatable :: a(:)
  read *, n,k
  print *, (n-2)/(k-1)+1
  allocate(a(n))
  read *, a
  deallocate(a)
end program abc101c


program a

integer :: i,j,k,l,m,n,basho,left,right,n_left,n_right,n_total=9999999
integer :: aaa(100000)

read(*,*) n,k
read(*,*) (aaa(i),i=1,n)

do i = 1,n
if(aaa(i) == 1) then
basho = i
endif
enddo

do j=0,k-1
left = max(0,basho-1-(k-j-1))
right = max(0,n-basho-j)
n_left = left / (k-1)
n_right = right / (k-1)
if(mod(left,k-1) /= 0)then
n_left = n_left + 1
endif
if(mod(right,k-1) /= 0)then
n_right = n_right + 1
endif
n_total = min(n_total,n_left+n_right+1)
enddo

write(*,*) n_total

end program


program main
  implicit integer(a-z)

  read(*,*)d,n

  if(n.eq.100)n=n+1

  write(*,*)n*100**d
end program main

Score: 100 points

Problem Statement

E869120's and square1001's 16-th birthday is coming soon.

Takahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.

E869120 and square1001 were just about to eat A and B of those pieces, respectively,

when they found a note attached to the cake saying that "the same person should not take two adjacent pieces of cake".

Can both of them obey the instruction in the note and take desired numbers of pieces of cake?

Constraints

A and B are integers between 1 and 16 (inclusive).

A+B is at most 16.

Input

Input is given from Standard Input in the following format:

A B

Output

If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.

Sample Input 1

5 4

Sample Output 1

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 2

8 8

Sample Output 2

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 3

11 4

Sample Output 3

:(

In this case, there is no way for them to take desired number of pieces, unfortunately.


program taka
implicit none
integer:: n,i,j,b,d,count
integer,allocatable,dimension(:):: a

read(5,*) n
allocate(a(n))
read(5,*) (a(i),i=1,n)

count=0

 do j=1,n
  do while(d==0)
   b=a(j)/2
   d=a(j)-b*2
    if(d==0)then
     count=count+1
    else
     count=count
    end if
   end do
  end do

write(6,*) count

end program taka

Score: 300 points

Problem Statement

As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}.

Snuke, an employee, would like to play with this sequence.

Specifically, he would like to repeat the following operation as many times as possible:

For every i satisfying 1 \leq i \leq N, perform one of the following: "divide a_i by 2" and "multiply a_i by 3".
Here, choosing "multiply a_i by 3" for every i is not allowed, and the value of a_i after the operation must be an integer.

At most how many operations can be performed?

Constraints

N is an integer between 1 and 10 \ 000 (inclusive).

a_i is an integer between 1 and 1 \ 000 \ 000 \ 000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print the maximum number of operations that Snuke can perform.

Sample Input 1

3
5 2 4

Sample Output 1

3

The sequence is initially {5, 2, 4}. Three operations can be performed as follows:

First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}.

Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}.

Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 3}.

Sample Input 2

4
631 577 243 199

Sample Output 2

0

No operation can be performed since all the elements are odd. Thus, the answer is 0.

Sample Input 3

10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776

Sample Output 3

39


integer a
read*,a
print"(A)","AB"//char(ichar("C")+a/1000)
end

Score : 100 points

Problem Statement

Decades have passed since the beginning of AtCoder Beginner Contest.

The contests are labeled as ABC001, ABC002, ... from the first round, but after the 999-th round ABC999, a problem occurred: how the future rounds should be labeled?

In the end, the labels for the rounds from the 1000-th to the 1998-th are decided: ABD001, ABD002, ..., ABD999.

You are given an integer N between 1 and 1998 (inclusive). Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

Constraints

1 \leq N \leq 1998

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

Sample Input 1

999

Sample Output 1

ABC

The 999-th round of AtCoder Beginner Contest is labeled as ABC999.

Sample Input 2

1000

Sample Output 2

ABD

The 1000-th round of AtCoder Beginner Contest is labeled as ABD001.

Sample Input 3

1481

Sample Output 3

ABD

The 1481-th round of AtCoder Beginner Contest is labeled as ABD482.


program abd
  implicit none
  integer :: n
  read(*,*) n
  if (n .lt. 1000) then
    write(*,'(a)') "ABC"
  else
    write(*,'(a)') "ABD"
  end if
  stop
end program abd


program main
  implicit integer(a-z)
  ans=0

  read(*,*)a,b

  do i=b-a,1,-1
     ans=ans+i
  end do

  ans=ans-b

  write(*,*)ans
end program main

Score : 200 points

Problem Statement

In some village, there are 999 towers that are 1,(1+2),(1+2+3),...,(1+2+3+...+999) meters high from west to east, at intervals of 1 meter.

It had been snowing for a while before it finally stopped. For some two adjacent towers located 1 meter apart, we measured the lengths of the parts of those towers that are not covered with snow, and the results are a meters for the west tower, and b meters for the east tower.

Assuming that the depth of snow cover and the altitude are the same everywhere in the village, find the amount of the snow cover.

Assume also that the depth of the snow cover is always at least 1 meter.

Constraints

1 \leq a < b < 499500(=1+2+3+...+999)

All values in input are integers.

There is no input that contradicts the assumption.

Input

Input is given from Standard Input in the following format:

a b

Output

If the depth of the snow cover is x meters, print x as an integer.

Sample Input 1

8 13

Sample Output 1

2

The heights of the two towers are 10 meters and 15 meters, respectively.
Thus, we can see that the depth of the snow cover is 2 meters.

Sample Input 2

54 65

Sample Output 2

1


Score : 300 points

Problem Statement

To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:

1 yen (the currency of Japan)

6 yen, 6^2(=36) yen, 6^3(=216) yen, ...

9 yen, 9^2(=81) yen, 9^3(=729) yen, ...

At least how many operations are required to withdraw exactly N yen in total?

It is not allowed to re-deposit the money you withdrew.

Constraints

1 \leq N \leq 100000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If at least x operations are required to withdraw exactly N yen in total, print x.

Sample Input 1

127

Sample Output 1

4

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

Sample Input 2

3

Sample Output 2

3

By withdrawing 1 yen three times, we can withdraw 3 yen in three operations.

Sample Input 3

44852

Sample Output 3

16


program main
	integer N, C, i, j, k
	integer(8) ans
	integer, allocatable :: D(:, :), cl(:, :), S(:, :)
	read(*, *) N, C
	allocate(D(1: C, 1:C))
	do i = 1, C
		read(*, *) D(i, 1: C)
	end do
	allocate(cl(1: N, 1:N))
	do i = 1, N
		read(*, *) cl(i, 1:N)
	end do
	allocate(S(1:C, 0:2))
	S(1:C, 0:2) = 0
	do l = 0, 2
		do k = 1, C
			do j = 1, N
				do i = 1, N
					if(mod(i+j, 3) == l) then
						S(k, l) = S(k, l) + D(cl(i,j), k)
					end if
				end do
			end do
		end do
	end do
	ans = 300000000
	do i = 1, C
		do j = 1, C
			do k = 1, C
				if(i /= j .and. j /= k .and. k /= i) then
					ans = min(S(i,0) + S(j,1) + S(k, 2), ans)
				end if
			end do
		end do
	end do
	write(*, *) ans
end program main

Score : 400 points

Problem Statement

There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.

These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.

We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:

If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.

If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.

Here, X \% Y represents X modulo Y.

We will repaint zero or more squares so that the grid will be a good grid.

For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.

Find the minimum possible sum of the wrongness of all the squares.

Constraints

1 \leq N \leq 500

3 \leq C \leq 30

1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j)

1 \leq c_{i,j} \leq C

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N C
D_{1,1} ... D_{1,C}
:
D_{C,1} ... D_{C,C}
c_{1,1} ... c_{1,N}
:
c_{N,1} ... c_{N,N}

Output

If the minimum possible sum of the wrongness of all the squares is x, print x.

Sample Input 1

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

Sample Output 1

3

Repaint (1,1) to Color 2. The wrongness of (1,1) becomes D_{1,2}=1.

Repaint (1,2) to Color 3. The wrongness of (1,2) becomes D_{2,3}=1.

Repaint (2,2) to Color 1. The wrongness of (2,2) becomes D_{3,1}=1.

In this case, the sum of the wrongness of all the squares is 3.

Note that D_{i,j} \neq D_{j,i} is possible.

Sample Input 2

4 3
0 12 71
81 0 53
14 92 0
1 1 2 1
2 1 1 2
2 2 1 3
1 1 2 2

Sample Output 2

428


Program main
  implicit none
  integer(8) :: n, a,b, K
  integer(8) :: a_1, b_1
  integer(8) :: i, j
  integer(8) :: mbase
  integer(8),allocatable :: f(:),finv(:),inv(:)
  integer :: out
  mbase = 998244353
  read(*,*) n, a, b, K
  allocate( f(0:N), finv(0:N), inv(0:N) )
  f(0) = 1
  f(1) = 1
  finv(0) = 1
  finv(1) = 1
  inv(0) = 0
  inv(1) = 1
  do i = 2,N
     f(i) = modulo( f(i-1)* i,mbase  )
     inv(i) = modulo ( -inv(modulo(mbase,i)) *(mbase/ i ), mbase )
     finv(i) = modulo( finv(i-1)*inv(i), mbase )
  end do
  out = 0
  do i = 0, N
     if ( mod( ( K - (A*i) ),B) == 0 ) then
        j = ( K - (A*i) )/B
        if ( 0 <= j.and.j <= N  ) then
           out = modulo(modulo( nCr2(n,i,finv,f) * nCr2(n,j,finv,f) , mbase) + out, mbase)
        end if
     end if
  end do
  write(*,'(i0)') out
  stop
contains
  integer(8) function mpow(a,n,mbase )
    ! a**n mod mbase
    integer(8) :: a
    integer(8) ::  n, mbase
    integer(8) :: ntmp, atmp
    mpow = 1
    atmp = a
    ntmp = n
    do while(ntmp > 0)
       !if ( mod(ntmp,2) == 0   ) mpow = modulo( mpow * atmp,mbase)
       if ( iand(ntmp,1) >0 ) mpow = modulo( mpow * atmp,mbase)
       atmp = modulo( atmp * atmp, mbase )
       ntmp = ishft(ntmp,-1)
    end do
  end function mpow

  integer(8) function nCr(n,r )
    integer(8) :: n, r
    integer(8) :: n_r
    integer(8) :: i
    integer(8) :: n1, n2
    integer(8) :: inv, inv2
    if ( r == 0 ) then
       nCr = 1
       return
    end if
    n_r = n-r
    n1 = 1
    do i = n,n_r+1,-1
       n1 =  mod(n1*i,mbase)
    end do
    n2 = 1
    do i = 1,r
       n2 = mod(n2*i,mbase)
    end do
    n2 = mpow(n2,mbase-2,mbase)
    nCr =  mod( n1*n2,mbase)
  end function nCr

  integer(8) function nCr2(n,r,finv,f)
    integer(8) :: finv(0:N), f(0:N)
    integer(8) :: n,r, r2
    r2 = min(r,n-r)
    !nCr = modulo( f(n)*modulo( (finv(r2)*finv(n-r2)), mbase ), mbase )
    nCr2 = mod( f(n)*(mod(finv(r2)*finv(n-r2),mbase) ), mbase )
  end function nCr2

end program main

Score : 700 points

Problem Statement

Takahashi has a tower which is divided into N layers.
Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower.
He defines the beauty of the tower as follows:

The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored.

Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.

Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K.
How many such ways are there to paint the tower? Find the count modulo 998244353.
Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.

Constraints

1 ≤ N ≤ 3×10^5

1 ≤ A,B ≤ 3×10^5

0 ≤ K ≤ 18×10^{10}

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N A B K

Output

Print the number of the ways to paint tiles, modulo 998244353.

Sample Input 1

4 1 2 5

Sample Output 1

40

In this case, a red layer worth 1 points, a green layer worth 3 points and the blue layer worth 2 points. The beauty of the tower is 5 when we have one of the following sets of painted layers:

1 green, 1 blue

1 red, 2 blues

2 reds, 1 green

3 reds, 1 blue

The total number of the ways to produce them is 40.

Sample Input 2

2 5 6 0

Sample Output 2

1

The beauty of the tower is 0 only when all the layers are uncolored. Thus, the answer is 1.

Sample Input 3

90081 33447 90629 6391049189

Sample Output 3

577742975


integer::a,b
read*,a,b

print*,max(a-b,a+b,a*b)
end

Score : 100 points

Problem Statement

You are given two integers A and B.
Find the largest value among A+B, A-B and A \times B.

Constraints

-1000 \leq A,B \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest value among A+B, A-B and A \times B.

Sample Input 1

3 1

Sample Output 1

4

3+1=4, 3-1=2 and 3 \times 1=3. The largest among them is 4.

Sample Input 2

4 -2

Sample Output 2

6

The largest is 4 - (-2) = 6.

Sample Input 3

0 0

Sample Output 3

0


program main
  implicit none
  integer :: n, i, res
  character(100) :: s, l, r
  read(*, *) n
  read(*, *) s
  res = 0
  do i = 1, n - 1  ! Cut after i-th char
    l = s(1:i)
    r = s(i + 1:n)
    res = max(res, sum(exist_mask(l) * exist_mask(r)))
  end do
  write(*, "(i0)") res

  contains

  function exist_mask(s)
    implicit none
    character(26), parameter :: abc = "abcdefghijklmnopqrstuvwxyz"
    integer :: exist_mask(26)
    character(100), intent(in) :: s
    integer :: i, j
    exist_mask(:) = 0
    do i = 1, 26
      do j = 1, 100
        if (abc(i:i) == s(j:j)) then
          exist_mask(i) = 1
        end if
      end do
    end do
  end function exist_mask
end program main

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.
We will cut this string at one position into two strings X and Y.
Here, we would like to maximize the number of different letters contained in both X and Y.
Find the largest possible number of different letters contained in both X and Y when we cut the string at the optimal position.

Constraints

2 \leq N \leq 100

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the largest possible number of different letters contained in both X and Y.

Sample Input 1

6
aabbca

Sample Output 1

2

If we cut the string between the third and fourth letters into X = aab and Y = bca, the letters contained in both X and Y are a and b.
There will never be three or more different letters contained in both X and Y, so the answer is 2.

Sample Input 2

10
aaaaaaaaaa

Sample Output 2

1

However we divide S, only a will be contained in both X and Y.

Sample Input 3

45
tgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir

Sample Output 3

9


program answer
  implicit none

  integer :: A, B, C, K, aa, bb, cc, ans, i

  read(*,*) A, B, C, K

  if(K == 0) then
     write(*,*) A-B
     stop
  end if

  do i = 1, K
     aa = B + C
     bb = A + C
     cc = A + B
     A = aa
     B = bb
     C = cc
  end do

  ans = aa-bb

  if( abs(ans) > 10E17 ) then
     write(*,*) 'Unfair'
  else
     write(*,*) ans
  end if

  stop
 end program answer

Score : 300 points

Problem Statement

Takahashi, Nakahashi and Hikuhashi have integers A, B and C, respectively.
After repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will get:

Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.

However, if the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Constraints

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

0 \leq K \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation K times.
If the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Sample Input 1

1 2 3 1

Sample Output 1

1

After one operation, Takahashi, Nakahashi and Hikuhashi have 5, 4 and 3, respectively. We should print 5-4=1.

Sample Input 2

2 3 2 0

Sample Output 2

-1

Sample Input 3

1000000000 1000000000 1000000000 1000000000000000000

Sample Output 3

0


integer N,K,f(0:21,0:lshift(1,20)),g(0:21,lshift(1,20)),pos,t,i,j,jj,ans,len,tmp,fir
character(lshift(1,20)+1) str
read*,N,K
f=0
g=0
do i=0,N
  read*,str
  do j=0,lshift(1,i)
    f(i,j)=merge(1,0,str(j+1:j+1)=='1')
  end do
end do
do i=1,N
  do j=0,lshift(1,i)
    fir=and(rshift(j,(i-1)),1)
    pos=0
    do while (pos<i .and. AND(rshift(j,(i-pos-1)),1)==fir)
      pos=pos+1
    end do
	g(i,j)= merge(-1,pos,pos==i)
  end do
end do
ans=0
len=0
do i=0,N
  do j=1,n-i
    do jj=0,lshift(1,i+j)-1
	  f(i,rshift(jj,j))=f(i,rshift(jj,j))+f(i+j,jj)
      t=g(j,and(jj,(lshift(1,j)-1)))
      if (t/=-1)then
        tmp=f(i+j-t,OR(lshift(rshift(jj,j),j-t),AND(jj,lshift(1,j-t)-1)))
        f(i+j-t,OR(lshift(rshift(jj,j),(j-t)),AND(jj,lshift(1,j-t)-1)))=tmp+f(i+j,jj)
      endif
     end do
   end do
  do j=lshift(1,i)-1,1,-1
    if (f(i,j)>=K)then
      ans=j
      len=i
    endif
  end do
end do
do i=len,1,-1
  write(*,"(A)",advance='no'),char(and(rshift(ans,i-1),1)+ichar('0'))
end do
write(*,*)
end program

Score : 2300 points

Problem Statement

You are given a set S of strings consisting of 0 and 1, and an integer K.

Find the longest string that is a subsequence of K or more different strings in S.
If there are multiple strings that satisfy this condition, find the lexicographically smallest such string.

Here, S is given in the format below:

The data directly given to you is an integer N, and N+1 strings X_0,X_1,...,X_N. For every i (0\leq i\leq N), the length of X_i is 2^i.

For every pair of two integers (i,j) (0\leq i\leq N,0\leq j\leq 2^i-1), the j-th character of X_i is 1 if and only if the binary representation of j with i digits (possibly with leading zeros) belongs to S. Here, the first and last characters in X_i are called the 0-th and (2^i-1)-th characters, respectively.

S does not contain a string with length N+1 or more.

Here, a string A is a subsequence of another string B when there exists a sequence of integers t_1 < ... < t_{|A|} such that, for every i (1\leq i\leq |A|), the i-th character of A and the t_i-th character of B is equal.

Constraints

0 \leq N \leq 20

X_i(0\leq i\leq N) is a string of length 2^i consisting of 0 and 1.

1 \leq K \leq |S|

K is an integer.

Input

Input is given from Standard Input in the following format:

N K
X_0
:
X_N

Output

Print the lexicographically smallest string among the longest strings that are subsequences of K or more different strings in S.

Sample Input 1

3 4
1
01
1011
01001110

Sample Output 1

10

The following strings belong to S: the empty string, 1, 00, 10, 11, 001, 100, 101 and 110.
The lexicographically smallest string among the longest strings that are subsequences of four or more of them is 10.

Sample Input 2

4 6
1
01
1011
10111010
1101110011111101

Sample Output 2

100

Sample Input 3

2 5
0
11
1111

Sample Output 3

The answer is the empty string.


integer N,K,f(0:21,lshift(1,20)),g(0:21,0:lshift(1,20)),pos,t,i,j,jj,ans,len,tmp,fir
character(lshift(1,20)+1) str
read*,N,K
f=0
g=0
do i=0,N
  read*,str
  do j=0,lshift(1,i)
    f(i,j)=merge(1,0,str(j+1:j+1)=='1')
  end do
end do
do i=1,N
  do j=0,lshift(1,i)
    fir=and(rshift(j,(i-1)),1)
    pos=0
    do while (pos<i .and. AND(rshift(j,(i-pos-1)),1)==fir)
      pos=pos+1
    end do
    g(i,j)=merge(-1,pos,pos==i)
  end do
end do
ans=0
len=0
do i=0,N
  do j=1,n-i
    do jj=0,lshift(1,i+j)-1
      f(i, rshift(jj,j)) = f(i, rshift(jj,j))+f(i+j,jj)
      t=g(j,and(jj,(lshift(1,j)-1)))
      if (t/=-1)then
        tmp=f(i+j-t,OR(lshift(rshift(jj,j),j-t),AND(jj,lshift(1,j-t)-1)))
        f(i+j-t,OR(lshift(rshift(jj,j),(j-t)),AND(jj,lshift(1,j-t)-1)))=tmp+f(i+j,jj)
      endif
     end do
   end do
  do j=lshift(1,i)-1,0,-1
    if (f(i,j)>=K)then
      ans=j
      len=i
    endif
  end do
end do
do i=len,1,-1
  write(*,"(A)",advance='no') char(and(rshift(ans,i-1),1)+ichar('0'))
end do
write(*,*)
end program


program prob3
    implicit none
    integer::X,i,j,k,ans
    integer::v(1000)
    read(*,*) X
    v = 0
    do i = 1, 34
        do j = 2,10
            if(i**j > 1000)then
                exit
            else
                v(i**j) = 1
            end if
        end do
    end do

    do i = 1,X
        if(v(i) == 1) then
            ans = i
        end if
    end do

    write(*,*) ans

end program

Score : 200 points

Problem Statement

You are given a positive integer X.
Find the largest perfect power that is at most X.
Here, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

Constraints

1 ≤ X ≤ 1000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the largest perfect power that is at most X.

Sample Input 1

10

Sample Output 1

9

There are four perfect powers that are at most 10: 1, 4, 8 and 9.
We should print the largest among them, 9.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

999

Sample Output 3

961


program exp
	implicit none
	integer :: y, c, i
	real(8) :: x

	read(*,*) x

    c = 1
	do i = 1, int(sqrt(x)) + 1
		do y = 2, int(log(x)/log(2.0_8))+1
			if (i**y <= x) then
				c = i ** y
			else
				exit
			end if
		end do
	end do

	write(*,'(i0)') c

end program exp


program exponential
  implicit none
  integer :: x, n, i, s, t, k
  read(*,*) x
  if (x.lt.4) then
    write(*,'(i0)') 1
  end if
  k = ceiling(sqrt(real(x,8)))
  s = 0
  do i = 2, k
    t = i*i
    do
      if (t.gt.x) exit
      if (s.lt.t) s = t
      t = t*i
    end do
  end do
  write(*,'(i0)') s
  stop
end program exponential


program main
    implicit none
    integer::a,b,ans
    read(*,*),a,b
    ans=a
    if(a>b)ans=ans-1
    print*,ans
end program

Score: 100 points

Problem Statement

In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.

For example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year.

In this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.

How many days from 2018-1-1 through 2018-a-b are Takahashi?

Constraints

a is an integer between 1 and 12 (inclusive).

b is an integer between 1 and 31 (inclusive).

2018-a-b is a valid date in Gregorian calendar.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the number of days from 2018-1-1 through 2018-a-b that are Takahashi.

Sample Input 1

5 5

Sample Output 1

5

There are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5.

Sample Input 2

2 1

Sample Output 2

1

There is only one day that is Takahashi: 1-1.

Sample Input 3

11 30

Sample Output 3

11

There are eleven days that are Takahashi: 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 9-9, 10-10 and 11-11.


program main
  implicit none

  integer :: a(3),i,n,big,t,j
  read(*,*)a(1),a(2),a(3)
  read(*,*)n

  do i=1,2
     do j=i+1,3
        if(a(i) .gt. a(j))then
           t=a(i)
           a(i)=a(j)
           a(j)=t
        end if
     end do
  end do

  do i = 1, n
    a(3) = a(3)*2
  end do

  write(*,*)sum(a)
end program main

Score: 200 points

Problem Statement

There are three positive integers A, B and C written on a blackboard. E869120 performs the following operation K times:

Choose one integer written on the blackboard and let the chosen integer be n. Replace the chosen integer with 2n.

What is the largest possible sum of the integers written on the blackboard after K operations?

Constraints

A, B and C are integers between 1 and 50 (inclusive).

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

Input

Input is given from Standard Input in the following format:

A B C
K

Output

Print the largest possible sum of the integers written on the blackboard after K operations by E869220.

Sample Input 1

5 3 11
1

Sample Output 1

30

In this sample, 5, 3, 11 are initially written on the blackboard, and E869120 can perform the operation once.

There are three choices:

Double 5: The integers written on the board after the operation are 10, 3, 11.

Double 3: The integers written on the board after the operation are 5, 6, 11.

Double 11: The integers written on the board after the operation are 5, 3, 22.

If he chooses 3., the sum of the integers written on the board afterwards is 5 + 3 + 22 = 30, which is the largest among 1. through 3.

Sample Input 2

3 3 4
2

Sample Output 2

22

E869120 can perform the operation twice. The sum of the integers eventually written on the blackboard is maximized as follows:

First, double 4. The integers written on the board are now 3, 3, 8.

Next, double 8. The integers written on the board are now 3, 3, 16.

Then, the sum of the integers eventually written on the blackboard is 3 + 3 + 16 = 22.


program main
  implicit none
  integer :: h,w,i,j
  integer,allocatable :: x(:,:)
  character(50) :: s

  read(*,*) h,w
  allocate(x(0:h+1,0:w+1))
  x(:,:)=0

  do i=1,h
     read(*,*) s
     do j=1,w
        if(s(j:j)=='#')then
           x(i,j)=1
        else
           x(i,j)=0
        endif
     enddo
  enddo

  do i=1,h
     do j=1,w
        if(x(i,j)/=1)then
           exit
        else if(x(i,j-1)==1)then
           exit
        else if(x(i,j+1)==1)then
           exit
        else if(x(i+1,j)==1)then
           exit
        else if(x(i-1,j)==1)then
           exit
        else
           write(*,*) 'No'
           go to 100
        endif
     enddo
  enddo

  write(*,*) 'Yes'

100 end program main

Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
: :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

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

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes


program AGC023A
    implicit none
    integer(8)::N,i,ans,scount
    integer(8),allocatable,dimension(:)::A,S
    read*,N
    allocate(A(N))
    allocate(S(N+1))
    read*,A
    S(1)=0
    ans=0

    do i=2,N+1
        S(i)=S(i-1)+A(i-1)
    end do
    S=iqsort(S)

    do i=1,N+1
        if(i==1)then
            scount=1
        else if(S(i)==S(i-1))then
            scount=scount+1
        else
            ans=ans+(scount*(scount-1))/2
            scount=1
        end if
    end do
    ans=ans+(scount*(scount-1))/2

    print'(i0)',ans
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 AGC023A

Score : 200 points

Problem Statement

We have an integer sequence A, whose length is N.

Find the number of the non-empty contiguous subsequences of A whose sums are 0.
Note that we are counting the ways to take out subsequences.
That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints

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

-10^9 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the number of the non-empty contiguous subsequences of A whose sum is 0.

Sample Input 1

6
1 3 -4 2 2 -2

Sample Output 1

3

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).

Sample Input 2

7
1 -1 1 -1 1 -1 1

Sample Output 2

12

In this case, some subsequences that have the same contents but are taken from different positions are counted individually.
For example, three occurrences of (1, -1) are counted.

Sample Input 3

5
1 -2 3 -4 5

Sample Output 3

0

There are no contiguous subsequences whose sums are 0.