n=parse(Int,readline())
p=parse.(split(readline()))
print(sum([((p[i]>p[i-1])!=(p[i]>p[i+1])) for i=2:n-1]))

Score : 200 points

Problem Statement

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

Constraints

All values in input are integers.

3 \leq n \leq 20

p is a permutation of {1,\ 2,\ ...,\ n}.

Input

Input is given from Standard Input in the following format:

n
p_1 p_2 ... p_n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5


function main()

  n = parse(Int,readline())
  p = map(x -> parse(Int,x), split(readline()))

  count = 0

  for i in 1:n-2

    if p[i] >= p[i+1] >= p[i+2] || p[i+2] >= p[i+1] >= p[i]
      count += 1
    end

  end

  println(count)

end

main()

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

Submitted source-code file size, in bytes.

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

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


n=parse(Int,readline())
p=map(x->parse(Int,x),split(readline()))
c=0
for i=2:n-1
  c+=p[i-1]<p[i]<p[i+1]||p[i-1]>p[i]>p[i+1]
end
println(c)


using DataStructures
const double = Float64
const int = Int64

parseint(x) = parse(int, x)
readint() = map(parseint, split(readline()))

function main()
  N = readint()[1]
  d = readint()

  sort!(d)
  a = d[N÷2]
  b = d[N÷2 + 1]
  println(b-a)

end

main()

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n=parseInt(readline())
    d=parseMap(split(readline()))
    dif=Vector{Int}([0 for i in 1:10^5])
    ans=0
    for i in d
        dif[i]+=1
    end
    for i in 1:10^5
        if i>1
            dif[i]+=dif[i-1]
        end
        if dif[i]==n-dif[i]
            ans+=1
        end
    end
    println(ans)
end
main()


parseint(x) = parse(Int, x)
readint() = readline() |> parseint
readints() = map(parseint, readline() |> split)

function main()
    n = readint()
    d = readints()
    sort!(d)
    m = n >> 1
    l, r = d[m:m+1]
    println(r - l)
end

main()


parseInt(x::SubString{String}) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function abmp(a,b,p);if b==0;1;elseif b%2==0;k=abmp(a,div(b,2),p);(k*k)%p;else;(a*abmp(a,b-1,p))%p;end;end;;
function binarr(n,p);z=1;for i in 1:n;z=z*i%p;end;fac=Array{Int}(n+1);inv=Array{Int}(n+1);inv[n+1]=abmp(z,p-2,p)
for i in 0:n-1;inv[n-i]=(inv[n+1-i]*(n-i))%p;end;for i in 0:n;fac[i+1]=(((z*inv[i+1])%p)*inv[n+1-i])%p;end;fac;end;

function main()
	p = 1000000007
	n,k = readline() |> split |> parseMap
	a = binarr(n-k+1,p)
	b = binarr(k-1,p)
	for i in 1:min(k,n-k+1)
		println(a[i+1]*b[i]%p)
	end
	if k>n-k+1
		for i in n-k+1:k-1
			println(b[i+1])
		end
	end
  println(a)
end

main()

Score : 400 points

Problem Statement

There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.

First, Snuke will arrange the N balls in a row from left to right.

Then, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.

How many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \leq i \leq K.

Constraints

1 \leq K \leq N \leq 2000

Input

Input is given from Standard Input in the following format:

N K

Output

Print K lines. The i-th line (1 \leq i \leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.

Sample Input 1

5 3

Sample Output 1

3
6
1

There are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).

There are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).

There is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B).

Sample Input 2

2000 3

Sample Output 2

1998
3990006
327341989

Be sure to print the numbers of arrangements modulo 10^9+7.


parseInt(x::SubString{String}) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function heap(a::Array{Tuple{Int,Int},1})
	h = [a[1]]
	for i in 2:length(a)
		hpush(h,a[i])
	end
	h
end

function hpush(h::Array{Tuple{Int,Int},1},x::Tuple{Int,Int})
	push!(h,x)
	if length(h) > 1
		l = length(h)
		k = div(l,2)
		while h[k][2]>x[2]&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(div(k,2),1)
			l = div(l,2)
		end
	end
	h
end

function hpop(h::Array{Tuple{Int,Int},1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>=l
				break
			elseif 2*k+1>=l
				if h[2*k][2]>=z[2]
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if h[2*k][2]>=z[2]&&h[2*k+1][2]>=z[2]
					break
				else
					t = h[2*k][2]<h[2*k+1][2]?2*k:2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

function main()
	n,m = readline() |> split |> parseMap
	e = [Int[] for i in 1:3*n]
	for i in 1:m
		u,v = readline() |> split |> parseMap
		push!(e[u],n+v)
		push!(e[n+u],2*n+v)
		push!(e[2*n+u],v)
	end
	s,t = readline() |> split |> parseMap
	dis = -ones(Int,3*n)
end

main()

Score : 500 points

Problem Statement

Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.
G consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.

First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.

Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq \min(10^5, N (N-1))

1 \leq u_i, v_i \leq N(1 \leq i \leq M)

u_i \neq v_i (1 \leq i \leq M)

If i \neq j, (u_i, v_i) \neq (u_j, v_j).

1 \leq S, T \leq N

S \neq T

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
:
u_M v_M
S T

Output

If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.
If he can, print the minimum number of ken-ken-pa needed to reach vertex T.

Sample Input 1

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

Sample Output 1

2

Ken can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 in the first ken-ken-pa, then 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed.

Sample Input 2

3 3
1 2
2 3
3 1
1 2

Sample Output 2

-1

Any number of ken-ken-pa will bring Ken back to Vertex 1, so he cannot reach Vertex 2, though he can pass through it in the middle of a ken-ken-pa.

Sample Input 3

2 0
1 2

Sample Output 3

-1

Vertex S and Vertex T may be disconnected.

Sample Input 4

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

Sample Output 4

2


Score : 600 points

Problem Statement

Find the number of sequences of length K consisting of positive integers such that the product of any two adjacent elements is at most N, modulo 10^9+7.

Constraints

1\leq N\leq 10^9

1 2\leq K\leq 100 (fixed at 21:33 JST)

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of sequences, modulo 10^9+7.

Sample Input 1

3 2

Sample Output 1

5

(1,1), (1,2), (1,3), (2,1), and (3,1) satisfy the condition.

Sample Input 2

10 3

Sample Output 2

147

Sample Input 3

314159265 35

Sample Output 3

457397712


parseInt(x) = parse(Int32, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function bsearch(a,x);l,r=0,length(a)+1;while r-l>1;m=(r+l)>>1;if a[m]<x;l=m;else;r=m;end;end;r;end

function main()
	mod = 10^9+7
	n,k = readline() |> split |> parseMap
	sq = floor(Int,sqrt(n))
	t = n+1
	a = Int[]
	for i in 1:sq
		if div(n,i)!=t
			push!(a,i)
			if i!=div(n,i)
				push!(a,div(n,i))
			end
			t = div(n,i)
		end
	end
	a = sort(a)
	m = length(a)
	b = Int[div(n,a[i]) for i in 1:m]
	c = Int[bsearch(a,b[i]) for i in 1:m]
	push!(a,0)
	a = sort(a)
	dp = zeros(Int,k,m)
	for i in 1:m
		dp[1,i] = a[i+1]-a[i]
	end
	tmp = zeros(Int,m)
	for i in 1:k-1
		tmp = zeros(Int,m)
		tmp[m] = dp[i,1]
		for j in 1:m-1
			tmp[m-j] = (tmp[m-j+1]+dp[i,j+1])%mod
		end
		for j in 1:m
			dp[i+1,j] = (tmp[j]*(a[j+1]-a[j]))%mod
		end
	end
	ans = 0
	for i in 1:m
		ans = (ans+dp[k,i])%mod
	end
	println(ans)
end

main()


function main()
S = readline()
for i in 1:3
  if S[i] == S[i+1]
    return "Bad"
  end
end
return "Good"
end
println(main())

Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.

Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.

Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


const lines=readlines()
state=0
input()=lines[global state+=1]
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
floatSub(s::SubString{String})=parse(Float64,s)
intLine()=map(intSub,split(input()))
function main()
    s=input()
    println(any(s[i]==s[i+1] for i=1:3) ? "Bad" : "Good")
end
main()


s = chomp(readline())

if s[1] != s[2] && s[2] != s[3] && s[3] != s[4]
  println("Good")
else
  println("Bad")
end


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n,l = readline() |> split |> parseMap
	k = div(n*(n+1),2)+n*(l-1)
	if l <= -n
		println(k-(l+n-1))
	elseif l < 0
		println(k)
	else
		println(k-l)
	end
end

main()

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


parseInt(x) = parse(Int,x)

function main()
  (A,B,C,D) = map(parseInt, split(readline()))
  dc = Int(floor(B/C))-Int(ceil(A/C))+1
  dd = Int(floor(B/D))-Int(ceil(A/D))+1
  E = lcm(C,D)
  de = Int(floor(B/E))-Int(ceil(A/E))+1
  k = (B-A+1)-(dc+dd-de)
  println(k)
end

main()

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


#read test
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	#buf = zeros(UInt8,100)
	#println(length(buf))
	buf = read(STDIN,500050)
	#println(size(buf))
	str = transcode(String,buf)
	str = map(strip,split(str,"\n"))
	#println(str)
	a = Array{Int}(2,n)
	for i in 1:n
		a[:,i] = str[i] |> split |> parseMap
	end
	a = sortcols(a, by=z->z[2])
	t = 0
	flg = 0
	for i in 1:n
		t += a[1,i]
		if t > a[2,i]
			flg = 1
			break
		end
	end
	println(flg==0?"Yes":"No")
end

main()

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


A=Array{Tuple{Int,Int}}(0)
N=parse(Int,readline())
for i=1:N
	a,b=map(x->parse(Int,x),split(readline()))
	push!(A,(b,a))
end
now=0
for (a,b)=sort(A)
	now+=b
	if now>a
		println("No")
		exit()
	end
end
println("Yes")


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  N,K = readline() |> split |> parseArray

  b=binomial(N-1,2)
  if K>b
    println(-1)
    return
  end

  M=N-1+(b-K)
  println(M)

  for i in 2:N
    println("1 $i")
  end
  # for i in 2:min(M-N+2,N)
  #   println("$i $(i%N+1)")
  # end
  # for i in 2:min(M-2N+3,N)
  #   println("$i $((i+1)%N+1)")
  # end

  for j in 1:M÷(N-1)+1
    for i in 2:min(M-j*(N-1)+1,N)
      # println("$i $((i+j-1)%N+1)")
      println("$i $(i+j+(i+j>N?-N+1:0))")
    end
  end
end

main()

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.


lines=readlines(open("/dev/fd/0"))
function input()
  shift!(lines)
end
int(s::SubString{String})=parse(Int,s)
int(s::Array{SubString{String},1})=map(int,s)
function main()
  x,a=input() |> split |> int
  println(ifelse(x<a,0,10))
end
main()

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


parseInt(x) = parse(Int,x)

function main()
  (X,A) = map(parseInt,split(readline()))
  if X < A
    println(0)
  else
    println(10)
  end

  main()


n,x=parse.(split(readline()))
l=[parse(Int,i) for i=split(readline())]
for i=1:n-1
  l[i+1]+=l[i]
end
print(count(a->a<=x,l)+1)

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


N,X=map(x->parse(Int,x),split(readline()," "))
L=map(x->parse(Int,x),split(readline()," "))

for i in 1:(N+1)
    if tmpD > X
        print(i-1)
        break
    end
    if i==(N+1)
        print(i)
        break
    end
    tmpD+=L[i]
end


lines=readlines(open("/dev/fd/0"))
input()=shift!(lines)
int(s::String)=parse(Int,s)
intSub(s::SubString{String})=parse(Int,s)
intLine()=map(intSub,split(input()))
function main()
  n,x=intLine()
  println(searchsortedlast(cumsum(intLine()),x)+1)
end
main()


parseInt(x) = parse(Int,x)

function main()
    n, x = map(parseInt, split(readline()))
    a = map(parseInt, split(readline()))

    ans = 1
    d = 0
    for i in 1:n
        d += a[i]
        if d > x
            break
        end
        ans += 1
    end
    println(ans)
end

main()


parseInt(x) = parse(Int,x)

function main()
  (N,X) = map(parseInt,split(readline()))
  L = map(parseInt,split(readline()))
  D = zeros(Int64,N)
  for i=1:(N-1)
    D[i+1] = D[i]+L[i]
  end
  for i = 1:N
    if X < D[i]
      println(i-1)
      break
    end
    if i== N
      println(N)
  end
end

main()


function main()

  (W, H, x, y) = map(x -> parse(Int, x), split(readline()))

  A = W * H

  if 2x == W && 2y == H
    println(A / 2, " ", 1)
  else
    println(A / 2, " ", 0)
  end

end

main()

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


n,m=map(x->parse(Int,x),split(readline()))
s=map(x->parse(Int,x),split(readline()))
t=map(x->parse(Int,x),split(readline()))
sum=ones(Int,m+1)
mod=10^9+7
for ss=s
	now=k=0
	for dp=[ss==tt?sum%mod:0 for (tt,sum) in zip(t,sum[1:m])]
		k+=1
		sum[k+1]+=now+=dp
	end
end
println(sum[m+1]%mod)

Score : 500 points

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Since the answer can be tremendous, print the number modulo 10^9+7.

Constraints

1 \leq N, M \leq 2 \times 10^3

The length of S is N.

The length of T is M.

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M}

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

S has four subsequences: (), (1), (3), (1, 3).

T has four subsequences: (), (3), (1), (3, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 1 \times 1 pair of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

S has four subsequences: (), (1), (1), (1, 1).

T has four subsequences: (), (1), (1), (1, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 2 \times 2 pairs of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.
Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7

Sample Output 4

191

Sample Input 5

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 5

846527861

Be sure to print the number modulo 10^9+7.


n,m=map(x->parse(Int,x),split(readline()))
s=map(x->parse(Int,x),split(readline()))
t=map(x->parse(Int,x),split(readline()))
dp=zeros(Int,n+1,m+1)
sum=zeros(Int,n+1,m+1)
mod=10^9+7
for j=1:m+1;sum[1,j]=1;end
for i=1:n
	now=0
	sum[i+1,1]=sum[i,1]
	for j=1:m
		if s[i]==t[j]
			dp[i+1,j+1]=sum[i,j]
		end
		now=(now+dp[i+1,j+1])%mod
		sum[i+1,j+1]=(sum[i,j+1]+now)%mod
	end
end
println(sum[n+1,m+1])


parseInt(x) = parse(Int,x)

parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    mod = 10^9 + 7
    N, M = readline() |> split |> parseMap
    S = readline() |> split |> parseMap
    T = readline() |> split |> parseMap
    DP = [[1 for j in 1:N+1] for i in 1:M+1]
    for i in 1:M
        for j in 1:N
            if T[i]==S[j]
                DP[i+1][j+1] = (DP[i+1][j]+DP[i+1][j])%mod
            else
                DP[i+1][j+1] = (DP[i+1][j]+DP[i+1][j]-DP[i][j])%mod
            end
        end
    end
    print(DP[M+1][N+1])
end

main()


function main()

  (N,K) = map(x -> parse(Int,x), split(readline()))

  k = zeros(Int,K)

  for i in 1:K

    if i == K && K != 1
      k[i] = N
    else
      k[i] = 1
      N -= 1
    end

  end

  println(k[end]-1)

end

main()

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


p(x)=parse(Int,x)
function main()
  a,b=map(p,split(readline()))
  if b==1
    print(0)
   else
    print(a-b)
  end
end

main()


N=parse(Int,readline())
xy=Pair{Int,Int}[]
for i=1:N
	x,y=map(x->parse(Int,x),split(readline()))
	push!(xy,x=>y)
end
M=Dict{Pair{Int,Int},Int}()
ans=N
M[0=>0]=-N
for (x1,y1)=xy,(x2,y2)=xy
	np=x1-x2=>y1-y2
	t=get(M,np,0)+1
	M[np]=t
	ans=min(ans,N-t)
end
println(ans)

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	a = sort(a)
	m = 0
	l = n
	if a[n] < 0
		println(a[n]-sum(a[1:n-1]))
		for i in 1:n-1
			x = shift!(a)
			println(a[l-1], " ",x)
			a[l-1] -= x
		end
	elseif a[1] > 0
		println(sum(a[2:n-1])-a[1])
		for i in 1:n-2
			x = pop!(a)
			println(a[1], " ",x)
			a[1] -= x
		end
		println(a[2], " ", a[1])
	else
		for i in 1:n
			m += abs(a[i])
		end
		println(m)
		p = shift!(a)
		q = pop!(a)
		for i in 1:n-2
			x = pop!(a)
			if x<0
				println(q," ",x)
				q -= x
			else
				println(p, " ",x)
				p -= x
			end
		end
		println(q, " ", p)
	end
end

main()

Score : 500 points

Problem Statement

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

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

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 possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	a = sort(a)
	m = 0
	h = div(n,2)
	for i in 1:n
		if i<=h
			m -= a[i]
		elseif i == h+1 && a[i] < 0
			m -=a[i]
		else
			m += a[i]
		end
	end
	println(m)
	k = 0
	if n%2 == 0
		k += 1
	end
	l = length(a)
	while l > 1
		if length(a)==2
			x = pop!(a)
			y = pop!(a)
			println(max(x,y), " ",min(x,y))
		elseif length(a)==3 && a[2] < 0
			x = shift!(a)
			println(a[2], " ", x)
			a[2] -= x
		elseif length(a)==3 && a[2]>0
			x = pop!(a)
			println(x," ",a[2])
			a[2] += x
		elseif k%2==1
			x = pop!(a)
			println(a[1], " ",x)
			a[1] -= x
		else
			x = shift!(a)
			println(x, " ", a[l-1])
			a[l-1] -= x
		end
		l -= 1
		k += 1
	end
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	a = sort(a)
	m = 0
	h = div(n,2)
	for i in 1:n
		if i<=h
			m -= a[i]
		elseif i == h+1 && a[i] < 0
			m -=a[i]
		else
			m += a[i]
		end
	end
	println(m)
	b = Array{Int}(n)
	t = 1
	for i in 1:h
		b[t] = a[i]
		t += 1
		b[t] = a[n-i+1]
		t += 1
	end
	if n%2 == 1
		b[n] = a[h+1]
	end
	k = 1
	if a[h+1] < 0
		t += 1
	end
	while length(b)>1
		x = shift!(b)
		y = shift!(b)
		if length(b) == 0
			println(max(x,y), " ", min(x,y))
			push!(b,x-y)
		elseif k%2 == 1
			println(x, " ", y)
			push!(b, x-y)
		else
			println(y, " ", x)
			push!(b, y-x)
		end
		k += 1
	end
end

main()


N = parse(Int64, strip(readline()))
nx = 1
pop = 0
res = zeros(Int64, N, N)
for i in 1:N
  for j in i+1:N
    res[i,j] = res[j,i] = nx
    nx,pop = nx+pop,nx
  end
end

for i in 1:N
  println(join(map(string, res[i,:]), ' '))

Score : 900 points

Problem Statement

Diverta City is a new city consisting of N towns numbered 1, 2, ..., N.

The mayor Ringo is planning to connect every pair of two different towns with a bidirectional road. The length of each road is undecided.

A Hamiltonian path is a path that starts at one of the towns and visits each of the other towns exactly once.
The reversal of a Hamiltonian path is considered the same as the original Hamiltonian path.

There are N! / 2 Hamiltonian paths. Ringo wants all these paths to have distinct total lengths (the sum of the lengths of the roads on a path), to make the city diverse.

Find one such set of the lengths of the roads, under the following conditions:

The length of each road must be a positive integer.

The maximum total length of a Hamiltonian path must be at most 10^{11}.

Constraints

N is a integer between 2 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print a set of the lengths of the roads that meets the objective, in the following format:

w_{1, 1} \ w_{1, 2} \ w_{1, 3} \ ... \ w_{1, N}
w_{2, 1} \ w_{2, 2} \ w_{2, 3} \ ... \ w_{2, N}
: : :
w_{N, 1} \ w_{N, 2} \ w_{N, 3} \ ... \ w_{N, N}

where w_{i, j} is the length of the road connecting Town i and Town j, which must satisfy the following conditions:

w_{i, i} = 0

w_{i, j} = w_{j, i} \ (i \neq j)

1 \leq w_{i, j} \leq 10^{11} \ (i \neq j)

If there are multiple sets of lengths of the roads that meet the objective, any of them will be accepted.

Sample Input 1

3

Sample Output 1

0 6 15
6 0 21
15 21 0

There are three Hamiltonian paths. The total lengths of these paths are as follows:

1 → 2 → 3: The total length is 6 + 21 = 27.

1 → 3 → 2: The total length is 15 + 21 = 36.

2 → 1 → 3: The total length is 6 + 15 = 21.

They are distinct, so the objective is met.

Sample Input 2

4

Sample Output 2

0 111 157 193
111 0 224 239
157 224 0 258
193 239 258 0

There are 12 Hamiltonian paths, with distinct total lengths.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    p,q,r=parseMap(split(readline()))
    println(minimum([p+q,q+r,p+r]))
end
main()

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

1 \leq P,Q,R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.

Sample Input 1

1 3 4

Sample Output 1

4

The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours

The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours

The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours

The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours

The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours

The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.

Sample Input 2

3 2 3

Sample Output 2

5


parseInt(x) = parse(Int,x)

function main()
  (P,Q,R) = map(parseInt,split(readline()))
  dist=zeros(Int64,6)
  dist[1]=P+Q
  dist[2]=P+R
  dist[3]=Q+P
  dist[4]=Q+R
  dist[5]=R+P
  dist[6]=R+Q
  println(findmin(dist)[1])
end

main()


parseint(x) = parse(Int, x)
int() = readline() |> parseint
ints() = map(parseint, readline() |> split)

function main()
    n = int()
    w = ints()
    a = 10000
    for i = 1:n-1
        a = min(a, abs(sum(w[1:i])-sum(w[i+1:n])))
    end
    println(a)
end

main()

Score : 200 points

Problem Statement

We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.

Constraints

2 \leq N \leq 100

1 \leq W_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
W_1 W_2 ... W_{N-1} W_N

Output

Print the minimum possible absolute difference of S_1 and S_2.

Sample Input 1

3
1 2 3

Sample Output 1

0

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.

Sample Input 2

4
1 3 1 1

Sample Output 2

2

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.

Sample Input 3

8
27 23 76 2 3 5 62 52

Sample Output 3

2


n,m=parse.(split(readline()))
check=ones(n)
mod=1000000007
for i=1:m;check[parse(Int,readline())]=0;end
dp=zeros(n+3)
f(i::Int)=dp[i]*check[i]
dp[1]=check[1]*1
dp[2]=(dp[1]+1)*check[2]
if n<3; print(dp[n]); return 0; end
for i=3:n
  dp[i]=check[i]*(dp[i-1]+dp[i-2])%mod
end
print(Int(dp[n]))

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


n,m=parse.(split(readline()))
a=[parse(Int,readline()) for i=1:m]
l=fill(0,n)
function calc()
  for i=1:n
    if i∈a
      l[i]=0
    elseif i==1
      l[i]=1
    elseif i==2
      l[i]=1+l[1]
    else
      l[i]=(l[i-1]+l[i-2])%1000000007
    end
  end
end
function main()
  calc()
  print(l[n])
end
main()


function main()

  (N, M) = map(x -> parse(Int, x), split(readline()))

  a = zeros(Int, 2N)
  for i in 1:M
    a[i] = parse(Int, readline()) + 1
  end

  ans = zeros(Int, 2N)
  num = 1

  for i in 1:N+1
    if i == 1
      ans[i] = 1
    elseif i == 2
      if a[num] == 2
        ans[i] = 0
        num += 1
      else
        ans[i] = 1
      end
    elseif a[num] == i
      num += 1
    else
      ans[i] = ans[i-1] + ans[i-2]
      ans[i] %= 1000000007
    end
  end

  println(ans[N+1])

end

main()


function main()

  (N, M) = map(x -> parse(Int, x), split(readline()))

  a = zeros(Int, M)
  for i in 1:M
    a[i] = parse(Int, readline()) + 1
  end

  ans = zeros(Int, 2N)

  for i in 1:N+1
    if i == 1
      ans[i] = 1
    elseif i == 2
      ans[i] = in(i, a) ? 0 : 1
    elseif !in(i, a)
      ans[i] = ans[i-1] + ans[i-2]
      ans[i] %= 1000000007
    end
  end

  println(ans[N+1])

end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n,m=parseMap(split(readline()))
    a=[parseInt(readline()) for i in 1:m]
    dp=zeros(Int,1,n+10)
    ok=Vector{Bool}([true for i in 1:n+10])
    for i in a
        ok[i]=false
    end
    dp[1]=1
    dp[2]=ifelse(ok[1],1,0)
    for i in 2:n+1
        if ok[i]
            dp[i+1]=(dp[i]+dp[i-1])%1000000007
        end
    end
    println(dp[n+1]%1000000007)
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n,m=parseMap(split(readline()))
    a=[parseInt(readline()) for i in 1:m]
    dp=zeros(Int,1,n+10)
    ok=Vector{Bool}([true for i in 1:n])
    for i in a
        ok[i]=false
    end
    dp[1]=1
    dp[2]=ifelse(ok[1],1,0)
    for i in 2:n
        if ok[i]
            dp[i+1]=dp[i]+dp[i-1]
        end
    end
    println(dp[n+1]%(10^9+7))
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	mod = 1000000007
	n,m = readline() |> split |> parseMap
	fib = Int[0,0]
	p = 1
	q = 1
	while p <= 100000
		push!(fib,p)
		temp = q
		q = p
		p = p+temp
	end
	a = Array{Int}(m+1)
	a[1] = -1
	for i in 1:m
		a[i+1] = readline() |> parseInt
	end
	push!(a,n+1)
	k = 1
	for i in 1:m+1
		if i==1&&a[2]==1
		else
			k = k*fib[a[i+1]-a[i]]%mod
		end
	end
	println(k)
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	mod = 1000000007
	n,m = readline() |> split |> parseMap
	fib = Int[0]
	p = 1
	q = 1
	while p <= 100000
		push!(fib,p)
		temp = q
		q = p
		p = p+temp
	end
	a = Array{Int}(m+1)
	a[1] = -1
	for i in 1:m
		a[i+1] = readline() |> parseInt
	end
	push!(a,n+1)
	k = 1
	for i in 1:m+1
		if i==1&&a[2]==1
		else
			k = k*fib[a[i+1]-a[i]-1]%mod
		end
	end
	println(k)
end

main()


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  N,M= readline() |> split |> parseArray
  a = zeros(Int, N)
  a[1] = 1
  a[2] = 2
  for _ in 1:M
    i = readline() |> parseInt
    a[i] = -1
  end

  a[2]!=-1 && a[1]==-1 && (a[2]=1)

  for i in 3:N
    a[i]==-1 && continue
    a[i-2] != -1 && (a[i]+=a[i-2])
    a[i-1] != -1 && (a[i]+=a[i-1])
    a[i]%=1_000_000_007
  end

  println(a[N])
end

main()


function main()
	H,W=map(x->parse(Int,x),split(readline()))
	S=Array{Int}(H,W)
	lines=readlines()
	for i=1:H
		s=shift!(lines)
		for j=1:W
			S[i,j]=s[j]=='.'
		end
	end
	dp1=copy(S)
	dp2=copy(S)
	dp3=copy(S)
	dp4=copy(S)
	for x=2:H
		dp1[x,:].*=dp1[x-1,:]+1
	end
	for x=H-1:-1:1
		dp2[x,:].*=dp2[x+1,:]+1
	end
	for y=2:W
		dp3[:,y].*=dp3[:,y-1]+1
	end
	for y=W-1:-1:1
		dp4[:,y].*=dp4[:,y+1]+1
	end
	println(maximum(dp1+dp2+dp3+dp4)-3)
end
main()

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


function main()
	H,W=map(x->parse(Int,x),split(readline()))
	dp=zeros(Int,4,H,W)
	S=[]
	for s=readlines()
		push!(S,map(c->c=='.',collect(chomp(s))))
	end
	for j=1:W
		dp[1,1,j]=S[1][j]?1:0
	end
	for x=2:H,j=1:W
		dp[1,x,j]=S[x][j]?dp[1,x-1,j]+1:0
	end
	for j=1:W
		dp[2,H,j]=S[H][j]?1:0
	end
	for x=H-1:-1:1,j=1:W
		dp[2,x,j]=S[x][j]?dp[2,x+1,j]+1:0
	end
	for j=1:H
		dp[3,j,1]=S[j][1]?1:0
	end
	for y=2:W,j=1:H
		dp[3,j,y]=S[j][y]?dp[3,j,y-1]+1:0
	end
	for j=1:H
		dp[4,j,W]=S[j][W]?1:0
	end
	for y=W-1:-1:1,j=1:H
		dp[4,j,y]=S[j][y]?dp[4,j,y+1]+1:0
	end
	ans=0
	for i=1:H,j=1:W
		ans=max(ans,dp[1,i,j]+dp[2,i,j]+dp[3,i,j]+dp[4,i,j]-3)
	end
	println(ans)
end
main()


function main()
	H,W=map(x->parse(Int,x),split(readline()))
	S=Array{Int,2}(H,W)
	for i in 1:H
		s=readline()
		for j in 1:W
			S[i,j]=s[j]=='.'
		end
	end
	dp1=copy(S)
	dp2=copy(S)
	dp3=copy(S)
	dp4=copy(S)
	for i=1:H,j=1:W
		if i>1
			dp1[i,j]=S[i,j]*(dp1[i-1,j]+1)
			dp2[H-i+1,j]=S[H-i+1,j]*(dp2[H-i+2,j]+1)
		end
		if j>1
			dp3[i,j]=S[i,j]*(dp3[i,j-1]+1)
			dp4[i,W-j+1]=S[i,W-j+1]*(dp4[i,W-j+2]+1)
		end
	end
	println(maximum(dp1+dp2+dp3+dp4)-3)
end
main()


function main()
	H,W=map(x->parse(Int,x),split(readline()))
	S=Array{Int}(H,W)
	for (i,s) in zip(1:H,readlines())
		for j=1:W
			S[i,j]=s[j]=='.'
		end
	end
	dp1=copy(S)
	dp2=copy(S)
	dp3=copy(S)
	dp4=copy(S)
	for i=1:H,j=1:W
		if i>1
			dp1[i,j]=dp1[i,j]*(dp1[i-1,j]+1)
			dp2[H-i+1,j]=dp2[H-i+1,j]*(dp2[H-i+2,j]+1)
		end
		if j>1
			dp3[i,j]=dp3[i,j]*(dp3[i,j-1]+1)
			dp4[i,W-j+1]=dp4[i,W-j+1]*(dp4[i,W-j+2]+1)
		end
	end
	println(maximum(dp1+dp2+dp3+dp4)-3)
end
main()


lines=readlines(open("/dev/fd/0"))
input()=shift!(lines)
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
intLine()=map(intSub,split(input()))
function main()
    h,w=intLine()
    s=Array{Int32}(h,w+1)
    for i in 1:h
        for (j,t) in enumerate(input())
            s[i,j]=t>'#'
        end
    end
    a,b,c,d=[deepcopy(s)for i in 1:4]
    for i in 2:w
        a[:,i]=(a[:,i-1]+1).*s[:,i]
        b[:,w-i+1]=(b[:,w-i+2]+1).*s[:,w-i+1]
    end
    for i in 2:h
        c[i,:]=(c[i-1,:]+1).*s[i,:]
        d[h-i+1,:]=(d[h-i+2,:]+1).*s[h-i+1,:]
    end
    print(maximum(a+b+c+d)-3)
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function bsearch(a,x);l,r=1,length(a);while l<=r;m=l+div(r-l,2);if a[m]<x;l=m+1;else;r=m-1;end;end;return(l-1);end

function main()
	h,w =readline() |> split |> parseMap
	s = ["" for i in 1:h]
	for i in 1:h
		s[i] = readline() |> chomp
	end
	a = [Int[0] for i in 1:h]
	b = [Int[0] for i in 1:w]
	for i in 1:h
		for j in 1:w
			if s[i][j] == '#'
				push!(a[i],j)
				push!(b[j],i)
			end
		end
	end
	for i in 1:h
		push!(a[i],w+1)
	end
	for i in 1:w
		push!(b[i],h+1)
	end
	k = 0
	for i in 1:h
		for j in 1:w
			t = bsearch(a[i],j+1)
			u = bsearch(b[j],i+1)
			if a[t]!=i && b[t]!=u
				k = max(k,a[i][t+1]-a[i][t]+b[j][u+1]-b[j][u]-2)
			end
		end
	end
	println(k-1)
end

main()


function main()

  N = parse(Int, readline())

  println(div(360,N))

end

main()

Score : 100 points

Problem Statement

Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides.

Print the answer in degrees, but do not print units.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print an integer representing the sum of the interior angles of a regular polygon with N sides.

Sample Input 1

3

Sample Output 1

180

The sum of the interior angles of a regular triangle is 180 degrees.

Sample Input 2

100

Sample Output 2

17640


n = parse(readline())
println(180*(n-2))


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	e = [Int[] for i in 1:n]
	g = Array{Int}(2,n-1)
	for i in 1:n-1
		g[:,i] = readline() |> split |> parseMap
		push!(e[g[1,i]],g[2,i])
		push!(e[g[2,i]],g[1,i])
	end
	d = Array{Int}(n)
	for i in 1:n
		d[i] = length(e[i])
	end
	m = indmax(d)
	c = readline() |> split |> parseMap
	c = sort(c,rev=true)
	stack=Int[m]
	vst = zeros(Int,n)
	vst[m] = shift!(c)
	while !isempty(stack)
		now = shift!(stack)
		for i in 1:length(e[now])
			if vst[e[now][i]] == 0
				vst[e[now][i]] = shift!(c)
				push!(stack,e[now][i])
			end
		end
	end
	s = 0
	for i in 1:n-1
		s += min(vst[g[1,i]],vst[g[2,i]])
	end
	println(s)
	for i in 1:n-1
		print(vst[i], " ")
	end
	println(vst[n])
end

main()

Score : 500 points

Problem Statement

You are given a tree with N vertices 1,2,\ldots,N, and positive integers c_1,c_2,\ldots,c_N.
The i-th edge in the tree (1 \leq i \leq N-1) connects Vertex a_i and Vertex b_i.

We will write a positive integer on each vertex in T and calculate our score as follows:

On each edge, write the smaller of the integers written on the two endpoints.

Let our score be the sum of the integers written on all the edges.

Find the maximum possible score when we write each of c_1,c_2,\ldots,c_N on one vertex in T, and show one way to achieve it. If an integer occurs multiple times in c_1,c_2,\ldots,c_N, we must use it that number of times.

Constraints

1 \leq N \leq 10000

1 \leq a_i,b_i \leq N

1 \leq c_i \leq 10^5

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 \ldots c_N

Output

Use the following format:

M
d_1 \ldots d_N

where M is the maximum possible score, and d_i is the integer to write on Vertex i.
d_1,d_2,\ldots,d_N must be a permutation of c_1,c_2,\ldots,c_N.
If there are multiple ways to achieve the maximum score, any of them will be accepted.

Sample Input 1

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

Sample Output 1

10
1 2 3 4 5

If we write 1,2,3,4,5 on Vertex 1,2,3,4,5, respectively, the integers written on the four edges will be 1,2,3,4, for the score of 10. This is the maximum possible score.

Sample Input 2

5
1 2
1 3
1 4
1 5
3141 59 26 53 59

Sample Output 2

197
59 26 3141 59 53

c_1,c_2,\ldots,c_N may not be pairwise distinct.


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  N = readline() |> parseInt
  a,b=zeros(Int,N-1),zeros(Int,N-1)
  for i in 1:N-1
    a[i],b[i]=readline() |> split |> parseArray
  end
  c=readline() |> split |> parseArray

  l=zeros(Int,N)
  for j in 1:N-1
    l[a[j]]+=1
    l[b[j]]+=1
  end

  ix=sortperm(l, rev=true)
  # println("l=",l)
  # println("ix=",ix)
  sort!(c, rev=true)
  # println("c=",c)
  d=zeros(Int,N)
  d[ix]=c
  # println("d=",d)

  M=sum(1:N-1) do j
    min(d[a[j]], d[b[j]])
  end

  println(M)
  for i in 1:N-1
    print(d[i])
    print(" ")
  end
  println(d[N])
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    a,p=parseMap(split(readline()))
    println(div(3a+p,2))
end
main()

Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

All values in input are integers.

0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.

Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.

Sample Input 3

32 21

Sample Output 3

58


parseint(x) = parse(Int, x)
int() = readline() |> parseint
strs() = readline() |> chomp |> split

function main()
    n = int()
    spt::Array{Array{Any, 1}, 1} = [begin sp = strs(); Any[sp[1], parseint(sp[2]), i] end for i = 1:n]
    sort!(spt, lt = (u, v) -> u[1] == v[1] ? u[2] > v[2] : u[1] < v[1])
    ans = Array{Int}(undef, n)
    for i = 1:n
        println(spt[i][3])
    end
end

main()

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1


parseint(x) = parse(Int, x)
parseintmap(x::Array{SubString{String},1}) = map(parseint, x)

function main()
    N = parseint(readline())
    a = []
    for i in 1:N
        s, p = readline() |> split
        p = parseint(p)
        append!(a, [(s, -p, i)])
    end
    println(a)
    sort!(a)
    for x in a
        println(x[3])
    end
end

main()


function main()
  myparse(x)=parse(Int,x)
  n,m=myparse.(split(readline()))
  s=[myparse.(split(readline()))[2:end] for i=1:m]
  p=myparse.(split(readline()))
  ans=0
  for i=[[i+1 for i=0:n if b&(1<<i)!=0] for b=0:(1<<n)-1]
    #すべてのsiにつながっているスイッチのオンオフのisoddがｐiと等しい
    ans += all([isodd(count(x->x∈i,s[j]))==p[j] for j=1:m])
  end
  print(ans)
end
main()

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

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

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8


parseint(x) = parse(Int, x)
parseintmap(x::Array{SubString{String},1}) = map(parseint, x)

function solve(n::Int, m::Int, d, eo)
    ret = 0
    for i in 0:2^n - 1
        on = 0
        for j in 1:m
            s = 0
            for switch in d[j]
                if (i >> (switch - 1) & 1) == 1
                    s += 1
                end
            end
            if (s % 2 == eo[m])
                on += 1
            end
        end
        if on == m
            ret += 1
        end
    end
    ret
end

function main()
    n, m = readline() |> split |> parseintmap
    d = Dict()
    for i in 1:m
        a = readline() |> split |> parseintmap
        d[i] = a[2:a[1]+1]
    end
    eo = readline() |> split |> parseintmap
    println(solve(n, m, d, eo))
end

main()


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)
function moveup(x::Array{Int,1},n)
	k = length(x)
	for i in 1:k-1
		if x[k-i+1] > n-1
			x[k-i] += div(x[k-i+1],n)
			x[k-i+1] = x[k-i+1]%n
		end
	end
	if x[1] > n-1
		0
	else
		x
	end
end

function main()
	n,m = readline() |> split |> parseMap
	k = zeros(Int,m)
	a = [Int[] for i in 1:m]
	for i in 1:m
		x = readline() |> split |> parseMap
		k[i] = x[1]
		a[i] = x[2:x[1]+1]
	end
	p = readline() |> split |> parseMap
	on = zeros(Int,n)
	c = 0
	for i in 1:2^n
		flg = 0
		for i in 1:m
			z = 0
			for j in 1:k[i]
				if on[a[i][j]] == 1
					z += 1
				end
			end
			if z%2!=p[i]
				flg = 1
				break
			end
		end
		if flg == 0
			c += 1
		end
		on[n] += 1
		moveup(on,2)
	end
	println(c)
end

main()


N=parse(Int,readline())
S=map(x->parse(Int,x),split(readline()))
ans=0
for i=1:N-1
	now=0
	L=1
	R=N
	while R>i+1&&((R-1)%i!=0||L<R)
		ans=max(ans,now+=S[L]+S[R])
		L+=i
		R-=i
	end
end
println(ans)

Score : 600 points

Problem Statement

There is an infinitely large pond, which we consider as a number line.
In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1.
On the lotus at coordinate i, an integer s_i is written.

You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:

1. Choose positive integers A and B. Your score is initially 0.

2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.

If y = N-1, the game ends.

If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.

If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.

3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.

If y = N-1, the game ends.

If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.

If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.

4. Go back to step 2.

You want to end the game with as high a score as possible.
What is the score obtained by the optimal choice of A and B?

Constraints

3 \leq N \leq 10^5

-10^9 \leq s_i \leq 10^9

s_0=s_{N-1}=0

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
s_0 s_1 ...... s_{N-1}

Output

Print the score obtained by the optimal choice of A and B.

Sample Input 1

5
0 2 5 1 0

Sample Output 1

3

If you choose A = 3 and B = 2, the game proceeds as follows:

Move to coordinate 0 + 3 = 3. Your score increases by s_3 = 1.

Move to coordinate 3 - 2 = 1. Your score increases by s_1 = 2.

Move to coordinate 1 + 3 = 4. The game ends with a score of 3.

There is no way to end the game with a score of 4 or higher, so the answer is 3. Note that you cannot land the lotus at coordinate 2 without drowning later.

Sample Input 2

6
0 10 -7 -4 -13 0

Sample Output 2

0

The optimal strategy here is to land the final lotus immediately by choosing A = 5 (the value of B does not matter).

Sample Input 3

11
0 -4 0 -99 31 14 -15 -39 43 18 0

Sample Output 3

59


parseint(x) = parse(Int, x)
parseintmap(x::Array{SubString{String},1}) = map(parseint, x)

function solve(a::Int, b::Int)::Int
    if a >= 13
        return b
    elseif a >= 6
        return b ÷ 2
    else
        return 0
    end
end

function main()
    a, b = readline() |> split |> parseintmap
    println(solve(a, b))
end

main()

Score : 100 points

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

Find the cost of the Ferris wheel for Takahashi.

Constraints

0 ≤ A ≤ 100

2 ≤ B ≤ 1000

B is an even number.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the cost of the Ferris wheel for Takahashi.

Sample Input 1

30 100

Sample Output 1

100

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

Sample Input 2

12 100

Sample Output 2

50

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

Sample Input 3

0 100

Sample Output 3

0

Takahashi is 0 years old, and he can ride the Ferris wheel for free.


r,d,x=map(x->parse(Int,x),split(readline()))
for i=0:9
  x=r*x-d
  println(x)
end

Score : 200 points

Problem Statement

The development of algae in a pond is as follows.

Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000, the following formula holds:

x_{i+1} = rx_i - D

You are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.

Constraints

2 ≤ r ≤ 5

1 ≤ D ≤ 100

D < x_{2000} ≤ 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r D x_{2000}

Output

Print 10 lines. The i-th line (1 ≤ i ≤ 10) should contain x_{2000+i} as an integer.

Sample Input 1

2 10 20

Sample Output 1

30
50
90
170
330
650
1290
2570
5130
10250

For example, x_{2001} = rx_{2000} - D = 2 \times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \times 30 - 10 = 50.

Sample Input 2

4 40 60

Sample Output 2

200
760
3000
11960
47800
191160
764600
3058360
12233400
48933560


let
  N, M = map(x -> parse(Int, x), split(readline()))
  range = map(x -> parse(Int, x), split(readline()))
  while (M=M-1) > 0 L, R = map(x -> parse(Int, x), split(readline())); range = [max(range[1], L), min(range[2], R)] end
  print((range[1] > range[2]) ? 0 : range[2] - range[1] + 1)
end

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


parseInt(x) = parse(Int,x)

function main()
  tmp = map(x -> parse(Int,x),split(readline()))
  N = tmp[1]
  M = tmp[2]
  mi,ma = map(x -> parse(Int,x),split(readline()))

  for line in readlines()
    A,B = line |> split |> x -> map(parseInt,x)
    ma = min(ma,B)
    mi = max(mi,A)
  end
  result = 0
  for i = 1:N
    if(mi <= i <= ma)
    	result += 1
    end
  end
  print(result)
end
main()


parseInt(x) = parse(Int,x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  N,M = readline() |> split |> parseArray
  A = readline() |> split |> parseArray
  sort!(A)

  tmp = Array{Int}(M,2)
  for i in 1:M
	tmp[i,:] = readline() |> split |> parseArray
  end
  tmp = sortrows(tmp, by=x->x[2],rev=true)

  roop = 0
  D::Array{Int64} = []
  for i = 1:M
    for j = 1:tmp[i,1]
      push!(D,tmp[i,2])
      roop += 1
      if roop == length(A)
        break
      end
    end
    if roop == length(A)
        break
    end
  end

  if length(D) < length(A)
    for i = length(D) + 1:length(A)
      push!(D,0)
    end
  end

  sort!(D,rev=true)

  for i = 1:length(A)
    if A[i] < D[i]
      A[i] = D[i]
    else
      break
    end
  end

  print(sum(A))
end
main()

Score : 400 points

Problem Statement

You have N cards. On the i-th card, an integer A_i is written.

For each j = 1, 2, ..., M in this order, you will perform the following operation once:

Operation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.

Find the maximum possible sum of the integers written on the N cards after the M operations.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i, C_i \leq 10^9

1 \leq B_i \leq N

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
B_1 C_1
B_2 C_2
\vdots
B_M C_M

Output

Print the maximum possible sum of the integers written on the N cards after the M operations.

Sample Input 1

3 2
5 1 4
2 3
1 5

Sample Output 1

14

By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result.

Sample Input 2

10 3
1 8 5 7 100 4 52 33 13 5
3 10
4 30
1 4

Sample Output 2

338

Sample Input 3

3 2
100 100 100
3 99
3 99

Sample Output 3

300

Sample Input 4

11 3
1 1 1 1 1 1 1 1 1 1 1
3 1000000000
4 1000000000
3 1000000000

Sample Output 4

10000000001

The output may not fit into a 32-bit integer type.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function heap(a::Array{Int,1})
	h = [a[1]]
	for i in 2:length(a)
		hpush(h,a[i])
	end
	h
end

function hpush(h::Array{Int,1},x)
	push!(h,x)
	l = length(h)
	if l>1
		k = l>>1
		while h[k]>x&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(k>>1,1)
			l = l>>1
		end
	end
end

function hpop(h::Array{Int,1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>l
				break
			elseif 2*k+1>l
				if h[2*k]>=z
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if h[2*k]>=z&&h[2*k+1]>=z
					break
				else
					t = h[2*k]<h[2*k+1]?2*k:2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

function heapb(a::Array{Int,1})
	h = [a[1]]
	for i in 2:length(a)
		hpush(h,a[i])
	end
	h
end

function hpushb(h::Array{Int,1},x)
	push!(h,x)
	l = length(h)
	if l>1
		k = l>>1
		while h[k]<x&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(k>>1,1)
			l = l>>1
		end
	end
end

function hpopb(h::Array{Int,1})
	x = h[1]
	z = pop!(h)
	if length(h)>0
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>l
				break
			elseif 2*k+1>l
				if h[2*k]<=z
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if h[2*k]<=z&&h[2*k+1]<=z
					break
				else
					t = h[2*k]<h[2*k+1]?2*k+1:2*k
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

function main()
	q = readline() |> parseInt
	l = Int[]
	ls = 0
	lt = 0
	r = Int[]
	rs = 0
	rt = 0
	y = 0
	for i in 1:q
		a = readline() |> split |> parseMap
		if a[1] == 1
			y += a[3]
			if ls == 0
				push!(l,a[2])
				ls += 1
				lt += a[2]
			elseif rs == 0
				rs += 1
				if l[1]<a[2]
					push!(r,a[2])
					rt += a[2]
				else
					push!(r,pop!(l))
					push!(l,a[2])
					rt = lt
					lt = a[2]
				end
			else
				lm = hpopb(l)
				rm = hpop(r)
				if ls==rs
					ls += 1
					if rm<a[2]
						hpush(r,a[2])
						hpushb(l,lm)
						hpushb(l,rm)
						lt += rm
						rt += a[2]-rm
					else
						hpush(r,rm)
						hpushb(l,lm)
						hpushb(l,a[2])
						lt += a[2]
					end
				else
					rs += 1
					if a[2]<lm
						hpushb(l,a[2])
						hpush(r,lm)
						hpush(r,rm)
						lt += a[2]-lm
						rt += lm
					else
						hpushb(l,lm)
						hpush(r,rm)
						hpush(r,a[2])
						rt += a[2]
					end
				end
			end
		else
			println(l[1], " ",l[1]*ls-l[1]*rs-lt+rt+y)
		end
	end
end

main()

Score : 600 points

Problem Statement

There is a function f(x), which is initially a constant function f(x) = 0.

We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:

An update query 1 a b: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x).

An evaluation query 2: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value.

We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.

Constraints

All values in input are integers.

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

-10^9 \leq a, b \leq 10^9

The first query is an update query.

Input

Input is given from Standard Input in the following format:

Q
Query_1
:
Query_Q

See Sample Input 1 for an example.

Output

For each evaluation query, print a line containing the response, in the order in which the queries are given.

The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between.

Sample Input 1

4
1 4 2
2
1 1 -8
2

Sample Output 1

4 2
1 -3

In the first evaluation query, f(x) = |x - 4| + 2, which attains the minimum value of 2 at x = 4.

In the second evaluation query, f(x) = |x - 1| + |x - 4| - 6, which attains the minimum value of -3 when 1 \leq x \leq 4. Among the multiple values of x that minimize f(x), we ask you to print the minimum, that is, 1.

Sample Input 2

4
1 -1000000000 1000000000
1 -1000000000 1000000000
1 -1000000000 1000000000
2

Sample Output 2

-1000000000 3000000000


using Base.Collections
function main()
	lines=readlines()
	shift!(lines)
	B=0
	L=PriorityQueue(Int,Int)
	R=PriorityQueue(Int,Int)
	id=0
	for s=lines
		if s[1]=='1'
			_,a,b=map(x->parse(Int,x),split(s))
			B+=b
			if length(L)>length(R)
				B-=a
				L[id+=1]=-a
				a=-peek(L).second
				dequeue!(L)
				B+=2a
				R[id+=1]=a
			else
				B+=a
				R[id+=1]=a
				a=peek(R).second
				dequeue!(R)
				B-=2a
				L[id+=1]=-a
			end
		else
			w=-peek(L).second
			println("$w $(B+(length(L)>length(R) ? w : 0))")
		end
	end
end
main()


readints()=parse.(Int,split(readline()))
readint()=parse.(Int,readline())

N,K=readints()
S=readline()
s=split(S,"")
s[K]=lowercase(s[K])
print(s)

Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).
Print the string S after lowercasing the K-th character in it.

Constraints

1 ≤ N ≤ 50

1 ≤ K ≤ N

S is a string of length N consisting of A, B and C.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the string S after lowercasing the K-th character in it.

Sample Input 1

3 1
ABC

Sample Output 1

aBC

Sample Input 2

4 3
CABA

Sample Output 2

CAbA


N, K = map(x -> parse(Int, x), split(readline()))
S = readline()
print(S[1:K-1], lowercase(S[K]), S[K+1:N])


parseInt(x) = parse(Int,x)
parseMap(x::Array{SubString{String}, 1}) = map(parseInt, x)
function main()
	n,k = map(parseInt, split(readline()))
  	p = 0
  for i = 1:n
    if i >=k
      p += 1/n
      continue
    end
    u = 1
    while i*(2^u) <k
      u +=1
    end
    p += 1/(n*(2^u))
  end
  println(Float64(p))
end
main()

Score : 300 points

Problem Statement

Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:

Throw the die. The current score is the result of the die.

As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.

The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.

You are given N and K. Find the probability that Snuke wins the game.

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3 10

Sample Output 1

0.145833333333

If the die shows 1, Snuke needs to get four consecutive heads from four coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^4 = \frac{1}{48}.

If the die shows 2, Snuke needs to get three consecutive heads from three coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^3 = \frac{1}{24}.

If the die shows 3, Snuke needs to get two consecutive heads from two coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^2 = \frac{1}{12}.

Thus, the probability that Snuke wins is \frac{1}{48} + \frac{1}{24} + \frac{1}{12} = \frac{7}{48} \simeq 0.1458333333.

Sample Input 2

100000 5

Sample Output 2

0.999973749998


function main()
	lines=readlines()
	N=parse(Int,shift!(lines))
	G=[Array{Pair{Int,Int}}(0) for _=1:N]
	for s=lines
		u,v,w=map(x->parse(Int,x),split(s))
		push!(G[u],v=>w)
		push!(G[v],u=>w)
	end
	d=zeros(Int,N)
	used=[false for _=1:N]
	function dfs(u,di)
		d[u]=di%2
		used[u]=true
		for (v,x)=G[u]
			if !used[v]
				dfs(v,di+x)
			end
		end
	end
	dfs(1,0)
	for d=d
		println(d)
	end
end
main()

Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

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

Sample Output 2

1
0
1
0
1


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  N = readline() |> parseInt
  u, v, w=zeros(Int,N-1),zeros(Int,N-1),zeros(Int,N-1)
  for i in 1:N - 1
    u[i], v[i], w[i] = readline() |> split |> parseArray
  end

  a = zeros(N)
  ix = [u[1]]
  a[ix[1]] = 1
  while true
    @label loop
    for j in 1:N-1
      if u[j] == ix[end] && a[v[j]] == 0
        a[v[j]] = -((w[j]%2)*2-1) * a[ix[end]]
        push!(ix, v[j])
        @goto loop
      elseif v[j] == ix[end] && a[u[j]] == 0
        a[u[j]] = -((w[j]%2)*2-1) * a[ix[end]]
        push!(ix, u[j])
        @goto loop
      end
    end
    pop!(ix)
    if length(ix) == 0
      break
    end
  end

  for j in 1:N
    println(a[j] == 1 ? 1 : 0)
  end
end

main()


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
  N = readline() |> parseInt
  u, v, w=zeros(Int,N-1),zeros(Int,N-1),zeros(Int,N-1)
  for i in 1:N - 1
    u[i], v[i], w[i] = readline() |> split |> parseArray
  end

  # A = zeros(N, N)
  # for i in 1:N-1
  #   A[u[i], v[i]]=w[i]%2
  # end
  # A = Symmetric(A)
  A=Symmetric(sparse(u,v,map(x->(x%2)*2-1,w),N,N))
  # println(A)
  # A=full(A)
  # println(A)
  # A=factorize(A)
  # println(A)
  # vs, ks=eig(A)
  vs, ks=eig(full(A))
  # vs, ks=eigs(A, which=:SR)
  _,i=findmin(vs)
  result=map(x->x<0?0:1,ks[:,i])
  # println(result)
  for x in result
    println(x)
  end
end

main()


function f(N,A)
	dp=zeros(Int,N+1)
	dp[1]=1
	for a=A
		for i=1:N+1-a
			dp[i+a]+=dp[i]
		end
	end
	dp[N+1]
end
R,G,B,N=map(x->parse(Int,x),split(readline()))
println(f(N,[R,G,B]))

Score : 200 points

Problem Statement

Snuke has come to a store that sells boxes containing balls. The store sells the following three kinds of boxes:

Red boxes, each containing R red balls

Green boxes, each containing G green balls

Blue boxes, each containing B blue balls

Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes.
How many triples of non-negative integers (r,g,b) achieve this?

Constraints

All values in input are integers.

1 \leq R,G,B,N \leq 3000

Input

Input is given from Standard Input in the following format:

R G B N

Output

Print the answer.

Sample Input 1

1 2 3 4

Sample Output 1

4

Four triples achieve the objective, as follows:

(4,0,0)

(2,1,0)

(1,0,1)

(0,2,0)

Sample Input 2

13 1 4 3000

Sample Output 2

87058


function main()
R,G,B,N=map(x->parse(Int,x),split(readline()))
ans=0
for r=0:R:N,g=0:G:N-r
  ans+=(N-r-g)%B==0
end
println(ans)
end
main()


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
  R, G, B, N = readline() |> split |> parseArray

  c = 0
  for i in 0:div(N, R),j in 0:div(N - R * i, G)
    (N - R * i - G * j) % B != 0 && continue
    c += 1
  end
  println(c)
end

main()


parseInt(x) = parse(Int, x)

function main()
  R, G, B, N = map(parseInt, split(readline()))

  box = sort([R,G,B],rev=true)
  R, G, B =box
  GB=lcm(G,B)
  # RGB=lcm(R,GB)

  c = 0
  for r=0:div(N,R)
    cc=0
    for g=0:div((N-R*r)%GB,G)
      if ((N-R*r)%GB-G*g)%B==0
        cc+=1
      end
    end
    # c+=cc*2^div(N-R*r,GB)
    c+=cc*(div(N-R*r,GB)+1)
  end
  # rgb=div(N,RGB)
  # c=div(c*(rgb+2)*(rgb+1),2)
  println(c)
end

main()


function main()
	N=parse(Int,readline())
	ca=cb=cab=0
	ans=0
	for _=1:N
		s=chomp(readline())
		n=length(s)
		for i=2:n
			if s[i-1]=='A'&&s[i]=='B'
				ans+=1
			end
		end
		if s[1]=='B'
			cb+=1
			if s[end]=='A'
				cab+=1
			end
		end
		if s[end]=='A'
			ca+=1
		end
	end
	println(ans+min(ca,cb)-(ca==cb==cab ? 1 : 0))
end
main()

Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function main()
	n = readline() |> parseInt
	s = ["" for i in 1:n]
	for i in 1:n
		s[i] = chomp(readline())
	end
	k = 0
	h = 0
	t = 0
	b = 0
	for i in 1:n
		l = length(s[i])
		for j in 2:l
			if s[i][j-1] == 'A' && s[i][j] == 'B'
				k += 1
			end
		end
		if s[i][1] == 'B' && s[i][l] == 'A'
			b += 1
		elseif s[i][1] == 'B'
			h += 1
		elseif s[i][l] == 'A'
			t += 1
		end
	end
	if b >= h && b >= t
		k += t+h+b-max(t,h,1)
	elseif b<= h && b <= t
		k += 2*b + min(t-b,h-b)
	else
		k += 2*min(t,h)+b-min(t,h)
	end
	println(k)
end
main()


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  N = readline() |> parseInt

  result=0
  # for i in 1:N
  #   N÷i!=N%i&&continue
  #   result+=i
  # end
  for i in 1:floor(Int, sqrt(N))
    (N-i)%i!=0 && continue
    m = (N-i)÷i
    i>=m && continue
    result+=m
  end
  println(result)
end

main()

Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function main()
	n = readline() |> parseInt
	a = Int[]
	t = 1
	while t <= sqrt(n)
		if n%t == 0
			push!(a,t)
		end
		t += 1
	end
	k = 0
	l = length(a)
	for i in 1:l
		if div(n,a[i])-1 >= a[i]
			k += div(n,a[i])-1
		end
	end
	if n == 2
		println(0)
	elseif n == 6
		println(5)
	else
		println(k)
	end
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	h,w,n = readline() |> split |> parseMap
	sr,sc = readline() |> split |> parseMap
	s = readline() |> chomp
	t = readline() |> chomp
	r = sc
	l = sc
	u = sr
	d = sr
	f = 0
	for i in 1:n
		if s[i]=='R'
			r+=1
		elseif s[i]=='L'
			l-=1
		elseif s[i]=='U'
			u+=1
		else
			d-=1
		end
		if r>w||l<1||u>h||d<1
			f = 1
			break
		end
		if t[i]=='R'
			l=min(w,l+1)
		elseif t[i]=='L'
			r=max(1,r-1)
		elseif t[i]=='U'
			d=min(h,d+1)
		else
			u=max(1,u-1)
		end
	end
	println(f==0?"YES":"NO")
end

main()

Score : 600 points

Problem Statement

We have a rectangular grid of squares with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
On this grid, there is a piece, which is initially placed at square (s_r,s_c).

Takahashi and Aoki will play a game, where each player has a string of length N.
Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: L, R, U and D.

The game consists of N steps. The i-th step proceeds as follows:

First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.

Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.

Here, to move the piece in the direction of L, R, U and D, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.

Takahashi wants to remove the piece from the grid in one of the N steps.
Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid.
Determine if the piece will remain on the grid at the end of the game when both players play optimally.

Constraints

2 \leq H,W \leq 2 \times 10^5

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

1 \leq s_r \leq H

1 \leq s_c \leq W

|S|=|T|=N

S and T consists of the four kinds of letters L, R, U and D.

Input

Input is given from Standard Input in the following format:

H W N
s_r s_c
S
T

Output

If the piece will remain on the grid at the end of the game, print YES; otherwise, print NO.

Sample Input 1

2 3 3
2 2
RRL
LUD

Sample Output 1

YES

Here is one possible progress of the game:

Takahashi moves the piece right. The piece is now at (2,3).

Aoki moves the piece left. The piece is now at (2,2).

Takahashi does not move the piece. The piece remains at (2,2).

Aoki moves the piece up. The piece is now at (1,2).

Takahashi moves the piece left. The piece is now at (1,1).

Aoki does not move the piece. The piece remains at (1,1).

Sample Input 2

4 3 5
2 2
UDRRR
LLDUD

Sample Output 2

NO

Sample Input 3

5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD

Sample Output 3

NO


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function main()
	h,w,n = readline() |> split |> parseMap
	y,x = readline() |> split |> parseMap
	s = chomp(readline())
	t = chomp(readline())
	sl = zeros(Int,n)
	sr = zeros(Int,n)
	su = zeros(Int,n)
	sd = zeros(Int,n)
	tl = zeros(Int,n)
	tr = zeros(Int,n)
	tu = zeros(Int,n)
	td = zeros(Int,n)
	if s[1] == 'L'
		sl[1] = 1
	elseif s[1] == 'R'
		sr[1] = 1
	elseif s[1] == 'U'
		su[1] = 1
	else
		sd[1] = 1
	end
	if t[1] == 'L' && x>1
		tl[1] = 1
	elseif t[1] == 'R' && x<w
		tr[1] = 1
	elseif t[1] == 'U' && y>1
		tu[1] = 1
	elseif t[1] == 'D' && y<h
		td[1] = 1
	end
	for i in 2:n
		sl[i] = sl[i-1]
		sr[i] = sr[i-1]
		su[i] = su[i-1]
		sd[i] = sd[i-1]
		tl[i] = tl[i-1]
		tr[i] = tr[i-1]
		tu[i] = tu[i-1]
		td[i] = td[i-1]
		if s[i] == 'L'
			sl[i] += 1
		elseif s[i] == 'R'
			sr[i] += 1
		elseif s[i] == 'U'
			su[i] += 1
		else
			sd[i] += 1
		end
		if t[i] == 'L' && x+sr[i]-tl[i] > 1
			tl[i] += 1
		elseif t[i] == 'R' && x-sl[i]+tr[i] < w
			tr[i] += 1
		elseif t[i] == 'U' && y+sd[i]-tu[i] < 1
			tu[i] += 1
		elseif t[i] == 'D' && y-su[i]+td[i] < h
			td[i] += 1
		end
	end
	flg = 0
	if x-sl[1] < 1 || x+sr[1] > w || y-sd[1]<0 || y+su[1] > h
		flg = 1
	else
		for i in 2:n
			if x-sl[i]+tr[i-1] < 1 || x+sr[i]-tl[i-1] > w || y-sd[i]+tu[i-1] < 0 || y+su[i]-td[i-1]>h
				flg = 1
				break
			end
		end
	end
	println(flg == 1?"NO":"YES")
end
main()


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function main()
	h,w,n = readline() |> split |> parseMap
	y,x = readline() |> split |> parseMap
	s = chomp(readline())
	t = chomp(readline())
	sl = zeros(Int,n)
	sr = zeros(Int,n)
	su = zeros(Int,n)
	sd = zeros(Int,n)
	tl = zeros(Int,n)
	tr = zeros(Int,n)
	tu = zeros(Int,n)
	td = zeros(Int,n)
	if s[1] == 'L'
		sl[1] = 1
	elseif s[1] == 'R'
		sr[1] = 1
	elseif s[1] == 'U'
		su[1] = 1
	else
		sd[1] = 1
	end
	if t[1] == 'L' && x>1
		tl[1] = 1
	elseif t[1] == 'R' && x<w
		tr[1] = 1
	elseif t[1] == 'U' && y>1
		tu[1] = 1
	elseif t[1] == 'D' && y<h
		td[1] = 1
	end
	for i in 2:n
		sl[i] = sl[i-1]
		sr[i] = sr[i-1]
		su[i] = su[i-1]
		sd[i] = sd[i-1]
		tl[i] = tl[i-1]
		tr[i] = tr[i-1]
		tu[i] = tu[i-1]
		td[i] = td[i-1]
		if s[i] == 'L'
			sl[i] += 1
		elseif s[i] == 'R'
			sr[i] += 1
		elseif s[i] == 'U'
			su[i] += 1
		else
			sd[i] += 1
		end
		if t[i] == 'L' && x-tl[i] > 1
			tl[i] += 1
		elseif t[i] == 'R' && x+tr[i] < w
			tr[i] += 1
		elseif t[i] == 'U' && y-tu[i] < 1
			tu[i] += 1
		elseif t[i] == 'D' && y-td[i] < h
			td[i] += 1
		end
	end
	flg = 0
	if x-sl[1] < 1 || x+sr[1] > w || y-sd[1]<0 || y+su[1] > h
		flg = 1
	else
		for i in 2:n
			if x-sl[i]+tr[i-1] < 1 || x+sr[i]-tl[i-1] > w || y-sd[i]+tu[i-1] < 0 || y+su[i]-td[i-1]>h
				flg = 1
				break
			end
		end
	end
	println(flg == 1?"NO":"YES")
end
main()


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function bfsPath(e::Array{Array{Int,1}},p)
	n=length(e)
	v=-ones(Int,n)
	v[p]=0
	c=1
	s=Int[]
	for i in 1:length(e[p])
		push!(s,e[p][i])
		v[e[p][i]]=1
		c+=1
	end
	bef=0
	j=p
	while !isempty(s)&&c<n
		j=shift!(s)
		for i in 1:length(e[j])
			if v[e[j][i]]<0
				push!(s,e[j][i])
				v[e[j][i]]=v[j]+1
				c+=1
			end
		end
	end
	v
end

function main()
	n = readline() |> parseInt
	e = [Int[] for i in 1:n]
	for i in 1:n-1
		a,b = readline() |> split |> parseMap
		push!(e[a],b)
		push!(e[b],a)
	end
	f = bfsPath(e,1)
	u = indmax(f)
	g = bfsPath(e,u)
	d = maximum(g)
	println(d%2==1?"Second":"First")
end
main()

Score : 800 points

Problem Statement

Takahashi and Aoki will play a game on a tree.
The tree has N vertices numbered 1 to N, and the i-th of the N-1 edges connects Vertex a_i and Vertex b_i.

At the beginning of the game, each vertex contains a coin.
Starting from Takahashi, he and Aoki will alternately perform the following operation:

Choose a vertex v that contains one or more coins, and remove all the coins from v.

Then, move each coin remaining on the tree to the vertex that is nearest to v among the adjacent vertices of the coin's current vertex.

The player who becomes unable to play, loses the game.
That is, the player who takes his turn when there is no coin remaining on the tree, loses the game.
Determine the winner of the game when both players play optimally.

Constraints

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

1 \leq a_i, b_i \leq N

a_i \neq b_i

The graph given as input is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_{N-1} b_{N-1}

Output

Print First if Takahashi will win, and print Second if Aoki will win.

Sample Input 1

3
1 2
2 3

Sample Output 1

First

Here is one possible progress of the game:

Takahashi removes the coin from Vertex 1. Now, Vertex 1 and Vertex 2 contain one coin each.

Aoki removes the coin from Vertex 2. Now, Vertex 2 contains one coin.

Takahashi removes the coin from Vertex 2. Now, there is no coin remaining on the tree.

Aoki takes his turn when there is no coin on the tree and loses.

Sample Input 2

6
1 2
2 3
2 4
4 6
5 6

Sample Output 2

Second

Sample Input 3

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

Sample Output 3

First


function main()

  (A,B,T) = map(x -> parse(Int,x), split(readline()))

  count = 0

  while true

    if A*count > T+0.5
      break
    end

    count += 1

  end

  println(B*(count-1))

end

main()

Score : 100 points

Problem Statement

A biscuit making machine produces B biscuits at the following moments: A seconds, 2A seconds, 3A seconds and each subsequent multiple of A seconds after activation.

Find the total number of biscuits produced within T + 0.5 seconds after activation.

Constraints

All values in input are integers.

1 \leq A, B, T \leq 20

Input

Input is given from Standard Input in the following format:

A B T

Output

Print the total number of biscuits produced within T + 0.5 seconds after activation.

Sample Input 1

3 5 7

Sample Output 1

10

Five biscuits will be produced three seconds after activation.

Another five biscuits will be produced six seconds after activation.

Thus, a total of ten biscuits will be produced within 7.5 seconds after activation.

Sample Input 2

3 2 9

Sample Output 2

6

Sample Input 3

20 20 19

Sample Output 3

0


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)
function bsearch(a::Array{Int,1},x::Int);l,r=1,length(a);while l<=r;m=l+div(r-l,2);if a[m]<x;l=m+1;else;r=m-1;end;end;return(l-1);end

function main()
	a,b,t = readline() |> split |> parseMap
	println(div(t,a)*b)
end
main()


function f()
    input = map(x -> parse(Int, x), split(chomp(readline()), " "))
    A = input[1]
    B = input[2]
    T = input[3]

    a = floor((T + 0.5) / A)

    ret = a * B

    print(Int(ret))
end
f()


function main()

  N = parse(Int,readline())
  V = map(x -> parse(Int,x), split(readline()))
  C = map(x -> parse(Int,x), split(readline()))

  ans = 0

  for i in 1:N
    if V[i] - C[i] > 0
      ans += V[i] - C[i]
    end
  end

  println(ans)

end

main()

Score : 200 points

Problem Statement

There are N gems. The value of the i-th gem is V_i.

You will choose some of these gems, possibly all or none, and get them.

However, you need to pay a cost of C_i to get the i-th gem.

Let X be the sum of the values of the gems obtained, and Y be the sum of the costs paid.

Find the maximum possible value of X-Y.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq C_i, V_i \leq 50

Input

Input is given from Standard Input in the following format:

N
V_1 V_2 ... V_N
C_1 C_2 ... C_N

Output

Print the maximum possible value of X-Y.

Sample Input 1

3
10 2 5
6 3 4

Sample Output 1

5

If we choose the first and third gems, X = 10 + 5 = 15 and Y = 6 + 4 = 10.
We have X-Y = 5 here, which is the maximum possible value.

Sample Input 2

4
13 21 6 19
11 30 6 15

Sample Output 2

6

Sample Input 3

1
1
50

Sample Output 3

0


parseInt(x) = parse(Int,x)

function main()
  N = parseInt(readline())
  V = map(parseInt,split(readline()))
  C = map(parseInt,split(readline()))
  r = 0
  for i = 1:N
    if V[i]-C[i] > 0
      r = r+V[i]-C[i]
    end
  end
  println(r)
end

main()


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)
function bsearch(a::Array{Int,1},x::Int);l,r=1,length(a);while l<=r;m=l+div(r-l,2);if a[m]<x;l=m+1;else;r=m-1;end;end;return(l-1);end

function main()
	n = readline() |> parseInt
	v = readline() |> split |> parseMap
	c = readline() |> split |> parseMap
	k = 0
	for i in 1:n
		if v[i]-c[i] > 0
			k += v[i]-c[i]
		end
	end
	println(k)
end
main()