problem_id
stringlengths
3
7
contestId
stringclasses
660 values
problem_index
stringclasses
27 values
programmingLanguage
stringclasses
3 values
testset
stringclasses
5 values
incorrect_passedTestCount
float64
0
146
incorrect_timeConsumedMillis
float64
15
4.26k
incorrect_memoryConsumedBytes
float64
0
271M
incorrect_submission_id
stringlengths
7
9
incorrect_source
stringlengths
10
27.7k
correct_passedTestCount
float64
2
360
correct_timeConsumedMillis
int64
30
8.06k
correct_memoryConsumedBytes
int64
0
475M
correct_submission_id
stringlengths
7
9
correct_source
stringlengths
28
21.2k
contest_name
stringclasses
664 values
contest_type
stringclasses
3 values
contest_start_year
int64
2.01k
2.02k
time_limit
float64
0.5
15
memory_limit
float64
64
1.02k
title
stringlengths
2
54
description
stringlengths
35
3.16k
input_format
stringlengths
67
1.76k
output_format
stringlengths
18
1.06k
interaction_format
null
note
stringclasses
840 values
examples
stringlengths
34
1.16k
rating
int64
800
3.4k
tags
stringclasses
533 values
testset_size
int64
2
360
official_tests
stringlengths
44
19.7M
official_tests_complete
bool
1 class
input_mode
stringclasses
1 value
generated_checker
stringclasses
231 values
executable
bool
1 class
354/A
354
A
Python 3
TESTS
4
108
307,200
73430102
def solve(lst, l, r, left, right): left_sum = [0] right_sum = [0] * (len(lst) + 1) cum_sum = 0 for i in range(len(lst)): cum_sum += lst[i] left_sum.append(cum_sum) cum_sum = 0 for i in reversed(range(len(lst))): cum_sum += lst[i] right_sum[i] = cum_sum #pri...
23
108
15,564,800
210305218
# Problem: A. Vasya and Robot # Contest: Codeforces - Codeforces Round 206 (Div. 1) # URL: https://codeforces.com/problemset/problem/354/A # Memory Limit: 256 MB # Time Limit: 1000 ms import sys import random from types import GeneratorType import bisect import io, os from bisect import * from collections import * fro...
Codeforces Round 206 (Div. 1)
CF
2,013
1
256
Vasya and Robot
Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights wi kilograms. Vasya needs to collect all these items, however he won't do it by himself. He us...
The first line contains five integers n, l, r, Ql, Qr (1 ≤ n ≤ 105; 1 ≤ l, r ≤ 100; 1 ≤ Ql, Qr ≤ 104). The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 100).
In the single line print a single number — the answer to the problem.
null
Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units. The second sample. The optimal solution is to take one item from the right, then one item from the left and t...
[{"input": "3 4 4 19 1\n42 3 99", "output": "576"}, {"input": "4 7 2 3 9\n1 2 3 4", "output": "34"}]
1,500
["brute force", "greedy", "math"]
23
[{"input": "3 4 4 19 1\r\n42 3 99\r\n", "output": "576\r\n"}, {"input": "4 7 2 3 9\r\n1 2 3 4\r\n", "output": "34\r\n"}, {"input": "2 100 100 10000 10000\r\n100 100\r\n", "output": "20000\r\n"}, {"input": "2 3 4 5 6\r\n1 2\r\n", "output": "11\r\n"}, {"input": "1 78 94 369 10000\r\n93\r\n", "output": "7254\r\n"}, {"inpu...
false
stdio
null
true
741/C
741
C
Python 3
TESTS
2
31
204,800
22813700
n = int(input()) x = [] y = [] g = [0]*(n*2+1) ans = [0]*(n*2+1) for i in range(n): a, b = map(int, input().split()) x.append(a) y.append(b) g[a] = b g[b] = a if(n==1): print("1 2") exit() def check(x,y): mk = 1 q = x w = x-1 e = x-2 if(w<=0): w = 2*n+w if(e<=0): e = ...
94
467
49,459,200
129594073
import sys input = sys.stdin.buffer.readline def is_bipartite(graph): n = len(graph) cols = [-1] * n cnts = [] for v in range(n): if cols[v] != -1: continue cols[v] = 0 cnt = [1, 0] stack = [v] while stack: v = stack.pop() for...
Codeforces Round 383 (Div. 1)
CF
2,016
1
256
Arpa’s overnight party and Mehrdad’s silent entering
Note that girls in Arpa’s land are really attractive. Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw n pairs of friends sitting around a table. i-th pair consisted of a boy, sitting on the ai-th chair, and his girlfriend, sitting on the bi-th chair. The chairs wer...
The first line contains an integer n (1 ≤ n ≤ 105) — the number of pairs of guests. The i-th of the next n lines contains a pair of integers ai and bi (1 ≤ ai, bi ≤ 2n) — the number of chair on which the boy in the i-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed...
If there is no solution, print -1. Otherwise print n lines, the i-th of them should contain two integers which represent the type of food for the i-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, otherwise p...
null
null
[{"input": "3\n1 4\n2 5\n3 6", "output": "1 2\n2 1\n1 2"}]
2,600
["constructive algorithms", "dfs and similar", "graphs"]
94
[{"input": "3\r\n1 4\r\n2 5\r\n3 6\r\n", "output": "1 2\r\n2 1\r\n1 2\r\n"}, {"input": "6\r\n3 2\r\n5 11\r\n7 12\r\n6 9\r\n8 4\r\n1 10\r\n", "output": "1 2\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n"}, {"input": "19\r\n30 27\r\n6 38\r\n10 28\r\n20 5\r\n14 18\r\n32 2\r\n36 29\r\n12 1\r\n31 24\r\n15 4\r\n35 11\r\n3 7\r\n21 1...
false
stdio
import sys def main(input_path, output_path, submission_path): # Read input with open(input_path) as f: n = int(f.readline()) pairs = [tuple(map(int, f.readline().split())) for _ in range(n)] # Read reference output with open(output_path) as f: ref_lines = [line.strip() for...
true
667/B
667
B
Python 3
TESTS
1
46
4,608,000
17695023
a = input() b = input() b = b.split() b.sort() b.reverse() c = int(b[0]) for i in range(1,len(b)): c=c-int(b[i]) print (c+1)
51
62
3,174,400
153659931
input() v = list(map(int, input().split())) print(-sum(v) + 2 * max(v) + 1 )
Codeforces Round 349 (Div. 2)
CF
2,016
1
256
Coat of Anticubism
As some of you know, cubism is a trend in art, where the problem of constructing volumetrical shape on a plane with a combination of three-dimensional geometric shapes comes to the fore. A famous sculptor Cicasso, whose self-portrait you can contemplate, hates cubism. He is more impressed by the idea to transmit two-d...
The first line contains an integer n (3 ≤ n ≤ 105) — a number of rod-blanks. The second line contains n integers li (1 ≤ li ≤ 109) — lengths of rods, which Cicasso already has. It is guaranteed that it is impossible to make a polygon with n vertices and nonzero area using the rods Cicasso already has.
Print the only integer z — the minimum length of the rod, so that after adding it it can be possible to construct convex polygon with (n + 1) vertices and nonzero area from all of the rods.
null
In the first example triangle with sides {1 + 1 = 2, 2, 1} can be formed from a set of lengths {1, 1, 1, 2}. In the second example you can make a triangle with lengths {20, 11, 4 + 3 + 2 + 1 = 10}.
[{"input": "3\n1 2 1", "output": "1"}, {"input": "5\n20 4 3 2 1", "output": "11"}]
1,100
["constructive algorithms", "geometry"]
51
[{"input": "3\r\n1 2 1\r\n", "output": "1\r\n"}, {"input": "5\r\n20 4 3 2 1\r\n", "output": "11\r\n"}, {"input": "7\r\n77486105 317474713 89523018 332007362 7897847 949616701 54820086\r\n", "output": "70407571\r\n"}, {"input": "14\r\n245638694 2941428 4673577 12468 991349408 44735727 14046308 60637707 81525 104620306 8...
false
stdio
null
true
279/B
279
B
PyPy 3-64
TESTS
8
124
13,004,800
204266027
N, T = map(int, input().split()) a = list(map(int, input().split())) s = sum(a) N = len(a) l,r = 0, N-1 #remember to max nums while s > T and l<=r: if a[l] > a[r]: s -= a[l] l+= 1 else: s -= a[r] r-=1 N -= 1 print(N)
38
154
11,161,600
217386817
""" author: Adham0 created: 06.08.2023 01:51:18 ### I was here ### █████ ██████ ██ ██ █████ ███ ███ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ███████ ██ ██ ███████ ███████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ██ ██ ...
Codeforces Round 171 (Div. 2)
CF
2,013
2
256
Books
When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the ...
The first line contains two integers n and t (1 ≤ n ≤ 105; 1 ≤ t ≤ 109) — the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.
Print a single integer — the maximum number of books Valera can read.
null
null
[{"input": "4 5\n3 1 2 1", "output": "3"}, {"input": "3 3\n2 2 3", "output": "1"}]
1,400
["binary search", "brute force", "implementation", "two pointers"]
38
[{"input": "4 5\r\n3 1 2 1\r\n", "output": "3\r\n"}, {"input": "3 3\r\n2 2 3\r\n", "output": "1\r\n"}, {"input": "1 3\r\n5\r\n", "output": "0\r\n"}, {"input": "1 10\r\n4\r\n", "output": "1\r\n"}, {"input": "2 10\r\n6 4\r\n", "output": "2\r\n"}, {"input": "6 10\r\n2 3 4 2 1 1\r\n", "output": "4\r\n"}, {"input": "7 13\r\...
false
stdio
null
true
279/B
279
B
PyPy 3-64
TESTS
8
124
13,004,800
209361430
import sys def input(): return sys.stdin.readline().strip() def getints(): return map(int,sys.stdin.readline().strip().split()) n,t = getints() a = list(getints()) ans = n s = sum(a) i = 0 j = n-1 while s>t: if a[i] > a[j]: s -= a[i] i += 1 ans -= 1 else: s -= a[j] j ...
38
154
13,107,200
197701134
n,t= map(int,input().split()) x = list(map(lambda q:int(q), input().split(" "))) f=0 c=0 q=0 if n==1 and x[0]<=t: print(1) elif n==1 and x[0]>t: print(0) else: v=x[0] j=0 for i in range(n): f+=x[i] c+=1 if f>t: f=f-x[0+j] j+=1 c-=1 ...
Codeforces Round 171 (Div. 2)
CF
2,013
2
256
Books
When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the ...
The first line contains two integers n and t (1 ≤ n ≤ 105; 1 ≤ t ≤ 109) — the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.
Print a single integer — the maximum number of books Valera can read.
null
null
[{"input": "4 5\n3 1 2 1", "output": "3"}, {"input": "3 3\n2 2 3", "output": "1"}]
1,400
["binary search", "brute force", "implementation", "two pointers"]
38
[{"input": "4 5\r\n3 1 2 1\r\n", "output": "3\r\n"}, {"input": "3 3\r\n2 2 3\r\n", "output": "1\r\n"}, {"input": "1 3\r\n5\r\n", "output": "0\r\n"}, {"input": "1 10\r\n4\r\n", "output": "1\r\n"}, {"input": "2 10\r\n6 4\r\n", "output": "2\r\n"}, {"input": "6 10\r\n2 3 4 2 1 1\r\n", "output": "4\r\n"}, {"input": "7 13\r\...
false
stdio
null
true
279/B
279
B
Python 3
TESTS
8
124
12,697,600
231623445
n, t = map(int, input().split()) a = list(map(int, input().split())) suma = sum(a) x, y = 0, n-1 while t<suma: if a[x]>a[y]: suma -= a[x] x += 1 else: suma -= a[y] y -= 1 print(y-x+1)
38
154
13,209,600
194075329
import sys read = sys.stdin.readline write = sys.stdout.write n, t = map(int, read().split(" ")) arr = list(map(int, read().split(" "))) acc, ans = 0, 0 l = 0 for r in range(n): acc += arr[r] while acc > t: acc -= arr[l] l += 1 ans = max(ans, r - l + 1) write(f"{ans}")
Codeforces Round 171 (Div. 2)
CF
2,013
2
256
Books
When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the ...
The first line contains two integers n and t (1 ≤ n ≤ 105; 1 ≤ t ≤ 109) — the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.
Print a single integer — the maximum number of books Valera can read.
null
null
[{"input": "4 5\n3 1 2 1", "output": "3"}, {"input": "3 3\n2 2 3", "output": "1"}]
1,400
["binary search", "brute force", "implementation", "two pointers"]
38
[{"input": "4 5\r\n3 1 2 1\r\n", "output": "3\r\n"}, {"input": "3 3\r\n2 2 3\r\n", "output": "1\r\n"}, {"input": "1 3\r\n5\r\n", "output": "0\r\n"}, {"input": "1 10\r\n4\r\n", "output": "1\r\n"}, {"input": "2 10\r\n6 4\r\n", "output": "2\r\n"}, {"input": "6 10\r\n2 3 4 2 1 1\r\n", "output": "4\r\n"}, {"input": "7 13\r\...
false
stdio
null
true
313/D
313
D
PyPy 3-64
TESTS
4
124
0
209558046
def main(): n, m, k = list(map(int, input().split())) d = {i:[] for i in range(n+1)} inf = 100000000000 for i in range(m): l, r, c = list(map(int, input().split())) d[l-1].append([r-1, c]) dp = [[inf for i in range(n)] for i in range(n+1)] for i in range(n): dp[0][i] = 0...
77
1,932
10,240,000
232738001
INF = 10**18 n, m, k = map(int, input().split()) cost = [[INF]*(n+1) for i in range(n+1)] dp = [[INF]*(n+1) for i in range(n+1)] for i in range(m): l, r, c = map(int, input().split()) for j in range(l, r+1): cost[j][r] = min(cost[j][r], c) dp[0][0] = 0 for i in range(n): for j in range(k+1): ...
Codeforces Round 186 (Div. 2)
CF
2,013
3
256
Ilya and Roads
Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as n holes in a row. We will consider the holes numbered from 1 to n, from left to right. Ilya is really keep on helping his city. So, he wants to fix at least k holes (perharps he can fix more) on a single Zoo...
The first line contains three integers n, m, k (1 ≤ n ≤ 300, 1 ≤ m ≤ 105, 1 ≤ k ≤ n). The next m lines contain the companies' description. The i-th line contains three integers li, ri, ci (1 ≤ li ≤ ri ≤ n, 1 ≤ ci ≤ 109).
Print a single integer — the minimum money Ilya needs to fix at least k holes. If it is impossible to fix at least k holes, print -1. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
null
null
[{"input": "10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6", "output": "17"}, {"input": "10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13", "output": "2"}, {"input": "10 1 9\n5 10 14", "output": "-1"}]
2,100
["dp"]
77
[{"input": "10 4 6\r\n7 9 11\r\n6 9 13\r\n7 7 7\r\n3 5 6\r\n", "output": "17\r\n"}, {"input": "10 7 1\r\n3 4 15\r\n8 9 8\r\n5 6 8\r\n9 10 6\r\n1 4 2\r\n1 4 10\r\n8 10 13\r\n", "output": "2\r\n"}, {"input": "10 1 9\r\n5 10 14\r\n", "output": "-1\r\n"}, {"input": "10 6 9\r\n6 8 7\r\n2 8 11\r\n2 6 10\r\n8 10 9\r\n2 5 8\r\...
false
stdio
null
true
353/C
353
C
PyPy 3
TESTS
12
404
10,240,000
59970076
n = int(input()) a = list(map(int,input().split())) s = input() m = len(s) prevSum = [0]*n prevSum[0] = a[0] for i in range(1,n): prevSum[i] = prevSum[i-1]+a[i] dp = [0]*m dp[0] = int(s[0])*a[0] lastOne = -1 for i in range(1,m): if (s[i]=="0"): dp[i] = dp[i-1] else: y = 0 if (las...
36
186
14,950,400
209877323
import sys input = lambda: sys.stdin.readline().rstrip() from itertools import accumulate N = int(input()) A = list(map(int, input().split())) sums = [0]+list(accumulate(A)) S = input() ans = 0 for i in range(N): if S[i]=='1': ans+=A[i] cur = 0 for i in range(N-1,-1,-1): if S[i]=='1': ...
Codeforces Round 205 (Div. 2)
CF
2,013
1
256
Find Maximum
Valera has array a, consisting of n integers a0, a1, ..., an - 1, and function f(x), taking an integer from 0 to 2n - 1 as its single argument. Value f(x) is calculated by formula $$f(x) = \sum_{i=0}^{n-1} a_i \cdot bit(i)$$, where value bit(i) equals one if the binary representation of number x contains a 1 on the i-t...
The first line contains integer n (1 ≤ n ≤ 105) — the number of array elements. The next line contains n space-separated integers a0, a1, ..., an - 1 (0 ≤ ai ≤ 104) — elements of array a. The third line contains a sequence of digits zero and one without spaces s0s1... sn - 1 — the binary representation of number m. Nu...
Print a single integer — the maximum value of function f(x) for all $$x \in [0..m]$$.
null
In the first test case m = 20 = 1, f(0) = 0, f(1) = a0 = 3. In the second sample m = 20 + 21 + 23 = 11, the maximum value of function equals f(5) = a0 + a2 = 17 + 10 = 27.
[{"input": "2\n3 8\n10", "output": "3"}, {"input": "5\n17 0 10 2 1\n11010", "output": "27"}]
1,600
["implementation", "math", "number theory"]
36
[{"input": "2\r\n3 8\r\n10\r\n", "output": "3\r\n"}, {"input": "5\r\n17 0 10 2 1\r\n11010\r\n", "output": "27\r\n"}, {"input": "18\r\n4382 3975 9055 7554 8395 204 5313 5739 1555 2306 5423 828 8108 9736 2683 7940 1249 5495\r\n110001100101110111\r\n", "output": "88691\r\n"}, {"input": "43\r\n475 2165 8771 7146 8980 7209 ...
false
stdio
null
true
897/A
897
A
Python 3
TESTS
3
93
307,200
77217693
#!/bin/python3 nm = list(map(int, input().split())) s = str(input()) s = list(s) while nm[1] > 0: lrc1c2 = list(map(str, input().split())) stringOf = ''.join(lrc1c2) l, r, c1, c2 = int(stringOf[0]), int(stringOf[1]), stringOf[2], stringOf[3] for k in range(l-1, r): if s[k] == c1: s[k] = c2 nm[1] -= 1 print(...
47
46
0
147025380
n,m=map(int,input().split()) s=input() lis1=list(s) for i in range (m): l,r,c1,c2 = map(str,input().split()) for j in range (int(l)-1,int(r)): if lis1[j]==c1: if j==len(lis1)-1: lis1.pop(j) lis1.append(c2) else: lis1.pop(j) lis1.insert(j,c2) print(''.join(lis1))
Codeforces Round 449 (Div. 2)
CF
2,017
2
256
Scarborough Fair
Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there. Willem asks his friend, Grick for directions, Grick helped them, and gave them a task. Although the girl wants to help, Willem insists on doing it by himself. Grick gave Willem a string of length n. W...
The first line contains two integers n and m (1 ≤ n, m ≤ 100). The second line contains a string s of length n, consisting of lowercase English letters. Each of the next m lines contains four parameters l, r, c1, c2 (1 ≤ l ≤ r ≤ n, c1, c2 are lowercase English letters), separated by space.
Output string s after performing m operations described above.
null
For the second example: After the first operation, the string is wxxak. After the second operation, the string is waaak. After the third operation, the string is gaaak.
[{"input": "3 1\nioi\n1 1 i n", "output": "noi"}, {"input": "5 3\nwxhak\n3 3 h x\n1 5 x a\n1 3 w g", "output": "gaaak"}]
800
["implementation"]
47
[{"input": "3 1\r\nioi\r\n1 1 i n\r\n", "output": "noi"}, {"input": "5 3\r\nwxhak\r\n3 3 h x\r\n1 5 x a\r\n1 3 w g\r\n", "output": "gaaak"}, {"input": "9 51\r\nbhfbdcgff\r\n2 3 b b\r\n2 8 e f\r\n3 8 g f\r\n5 7 d a\r\n1 5 e b\r\n3 4 g b\r\n6 7 c d\r\n3 6 e g\r\n3 6 e h\r\n5 6 a e\r\n7 9 a c\r\n4 9 a h\r\n3 7 c b\r\n6 9 ...
false
stdio
null
true
551/A
551
A
Python 3
TESTS
17
373
614,400
22564758
n=int(input()) a=list(map(int,input().split())) b=[0]*n c={} d=set() m=1 for j in a: c[j]=c.get(j,0)+1 d.add(j) # print(c,d) d1=list(d)[::-1] for k in d1: for l in [i for i,x in enumerate(a) if x == k]: b[l]=m m+=c[k] print(*b)
36
31
512,000
229883464
n = int(input()) ratings = list(map(int, input().split())) # Create a list of tuples (rating, student_index) to keep track of original order ratings_with_indices = [(rating, i) for i, rating in enumerate(ratings)] # Sort the list in non-increasing order of ratings, breaking ties using student index ratings_with_indic...
Codeforces Round 307 (Div. 2)
CF
2,015
2
256
GukiZ and Contest
Professor GukiZ likes programming contests. He especially likes to rate his students on the contests he prepares. Now, he has decided to prepare a new contest. In total, n students will attend, and before the start, every one of them has some positive integer rating. Students are indexed from 1 to n. Let's denote the ...
The first line contains integer n (1 ≤ n ≤ 2000), number of GukiZ's students. The second line contains n numbers a1, a2, ... an (1 ≤ ai ≤ 2000) where ai is the rating of i-th student (1 ≤ i ≤ n).
In a single line, print the position after the end of the contest for each of n students in the same order as they appear in the input.
null
In the first sample, students 2 and 3 are positioned first (there is no other student with higher rating), and student 1 is positioned third since there are two students with higher rating. In the second sample, first student is the only one on the contest. In the third sample, students 2 and 5 share the first positi...
[{"input": "3\n1 3 3", "output": "3 1 1"}, {"input": "1\n1", "output": "1"}, {"input": "5\n3 5 3 4 5", "output": "4 1 4 3 1"}]
800
["brute force", "implementation", "sortings"]
36
[{"input": "3\r\n1 3 3\r\n", "output": "3 1 1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "5\r\n3 5 3 4 5\r\n", "output": "4 1 4 3 1\r\n"}, {"input": "7\r\n1 3 5 4 2 2 1\r\n", "output": "6 3 1 2 4 4 6\r\n"}, {"input": "11\r\n5 6 4 2 9 7 6 6 6 6 7\r\n", "output": "9 4 10 11 1 2 4 4 4 4 2\r\n"}, {"input"...
false
stdio
null
true
875/C
875
C
Python 3
TESTS
11
826
24,268,800
31500287
def replaceRequiredElements(repElL, lst): #print(repElL) for i in range(len(lst)): for j, e in enumerate(lst[i][1]): if e == repElL: lst[i][1][j] = e * -1 def replacePrevSameIndexElements(lstnum, pos, norep, lst): flg = False repels = '' recnt = 0 for i in range(lstnum + 1): if lst[i][0] >= pos + 1: ...
67
998
23,859,200
42141956
# -*- coding: utf-8 -*- import math import collections import bisect import heapq import time import random import itertools """ created by shhuan at 2017/10/18 16:22 """ M, N = map(int, input().split()) words = [] for i in range(M): words.append([int(x) for x in input().split()][1:]) # a...
Codeforces Round 441 (Div. 1, by Moscow Team Olympiad)
CF
2,017
1
512
National Property
You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library. Some long and uninteresting story was removed... The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large ve...
The first line contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to m. Each of the next n lines contains a description of one word in format li, si, 1, ...
In the first line print "Yes" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print "No" (without quotes). If the required is possible, in the second line print k — the number of letters Denis has to capitalize...
null
In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following: - 2' - 1 - 1 3' 2' - 1 1 The condition 2' < 1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order. As the...
[{"input": "4 3\n1 2\n1 1\n3 1 3 2\n2 1 1", "output": "Yes\n2\n2 3"}, {"input": "6 5\n2 1 2\n2 1 2\n3 1 2 3\n2 1 5\n2 4 4\n2 4 4", "output": "Yes\n0"}, {"input": "4 3\n4 3 2 2 1\n3 1 1 3\n3 2 3 3\n2 3 1", "output": "No"}]
2,100
["2-sat", "dfs and similar", "graphs", "implementation"]
67
[{"input": "4 3\r\n1 2\r\n1 1\r\n3 1 3 2\r\n2 1 1\r\n", "output": "Yes\r\n2\r\n2 3 "}, {"input": "6 5\r\n2 1 2\r\n2 1 2\r\n3 1 2 3\r\n2 1 5\r\n2 4 4\r\n2 4 4\r\n", "output": "Yes\r\n0\r\n"}, {"input": "4 3\r\n4 3 2 2 1\r\n3 1 1 3\r\n3 2 3 3\r\n2 3 1\r\n", "output": "No\r\n"}, {"input": "4 4\r\n3 3 4 1\r\n4 3 4 2 2\r\n4...
false
stdio
null
true
4/D
4
D
PyPy 3-64
TESTS
4
62
0
210745478
import sys def input(): return sys.stdin.readline().strip() n,wc,hc=map(int,input().split()) s=set() L=[] for i in range(n): w,h=map(int,input().split()) L.append((w,h)) s.add((w,h)) d={} for x in s: d[x]=0 for i in range(n): d[L[i]]=i+1 myKeys = list(d.keys()) myKeys.sort() sorted_dict = {i: d[i] f...
33
373
10,547,200
218728491
import sys input = sys.stdin.readline n, w, h = map(int, input().split()) d = [(w, h, 0)] for i in range(n): a, b = map(int, input().split()) if a > w and b > h: d.append((a, b, i+1)) d.sort() m = len(d) x = [(1, 0) for i in range(m)] if len(d) == 1: print(0) exit() for i in range(1, m): fo...
Codeforces Beta Round 4 (Div. 2 Only)
ICPC
2,010
1
64
Mysterious Present
Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes A = {a1, a2, ..., an}, where the width and the height of the i-th envelope is strictly higher than the width and the heigh...
The first line contains integers n, w, h (1 ≤ n ≤ 5000, 1 ≤ w, h ≤ 106) — amount of envelopes Peter has, the card width and height respectively. Then there follow n lines, each of them contains two integer numbers wi and hi — width and height of the i-th envelope (1 ≤ wi, hi ≤ 106).
In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, pr...
null
null
[{"input": "2 1 1\n2 2\n2 2", "output": "1\n1"}, {"input": "3 3 3\n5 4\n12 11\n9 8", "output": "3\n1 3 2"}]
1,700
["dp", "sortings"]
33
[{"input": "2 1 1\r\n2 2\r\n2 2\r\n", "output": "1\r\n1 \r\n"}, {"input": "3 3 3\r\n5 4\r\n12 11\r\n9 8\r\n", "output": "3\r\n1 3 2 \r\n"}, {"input": "5 10 10\r\n22 23\r\n17 19\r\n13 17\r\n8 12\r\n2 6\r\n", "output": "3\r\n3 2 1 \r\n"}, {"input": "5 13 13\r\n4 4\r\n10 10\r\n7 7\r\n1 1\r\n13 13\r\n", "output": "0\r\n"},...
false
stdio
import sys def read_file_lines(path): with open(path, 'r') as f: return [line.strip() for line in f.readlines()] def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] input_lines = read_file_lines(input_path) if not input_lines: print(0) ...
true
4/D
4
D
PyPy 3-64
TESTS
4
62
0
213180141
import sys input = lambda: sys.stdin.readline().rstrip() from collections import Counter,defaultdict N,W,H = map(int, input().split()) A = [] for i in range(N): w,h = map(int, input().split()) A.append((w,h,i+1)) A.sort(reverse=True) if A[0][0]<=W or A[0][1]<=H: exit(print(0)) cur = (A[0][0],A[0][1]) ans...
33
421
9,216,000
219743982
n,r,c=map(int,input().split()) l=[[r,c,0]] n+=1 for i in range(1,n): a,b=map(int,input().split()) l.append([a,b,i]) l=sorted(l,key=lambda x:x[1]) v=[0]*n p=[-1]*n for i in range(n-1,-1,-1): v[l[i][2]]=1 for j in range(i+1,n): if l[j][0]>l[i][0] and l[j][1]>l[i][1] and v[l[j][2]]>=v[l[i][2]]: ...
Codeforces Beta Round 4 (Div. 2 Only)
ICPC
2,010
1
64
Mysterious Present
Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes A = {a1, a2, ..., an}, where the width and the height of the i-th envelope is strictly higher than the width and the heigh...
The first line contains integers n, w, h (1 ≤ n ≤ 5000, 1 ≤ w, h ≤ 106) — amount of envelopes Peter has, the card width and height respectively. Then there follow n lines, each of them contains two integer numbers wi and hi — width and height of the i-th envelope (1 ≤ wi, hi ≤ 106).
In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, pr...
null
null
[{"input": "2 1 1\n2 2\n2 2", "output": "1\n1"}, {"input": "3 3 3\n5 4\n12 11\n9 8", "output": "3\n1 3 2"}]
1,700
["dp", "sortings"]
33
[{"input": "2 1 1\r\n2 2\r\n2 2\r\n", "output": "1\r\n1 \r\n"}, {"input": "3 3 3\r\n5 4\r\n12 11\r\n9 8\r\n", "output": "3\r\n1 3 2 \r\n"}, {"input": "5 10 10\r\n22 23\r\n17 19\r\n13 17\r\n8 12\r\n2 6\r\n", "output": "3\r\n3 2 1 \r\n"}, {"input": "5 13 13\r\n4 4\r\n10 10\r\n7 7\r\n1 1\r\n13 13\r\n", "output": "0\r\n"},...
false
stdio
import sys def read_file_lines(path): with open(path, 'r') as f: return [line.strip() for line in f.readlines()] def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] input_lines = read_file_lines(input_path) if not input_lines: print(0) ...
true
875/D
875
D
Python 3
TESTS
1
46
7,680,000
161814953
n = int(input()) x = list(map(int, input().strip().split()))[:n] maxn = 200010 arr = [0]*(maxn) l = [0]*(maxn) r = [0]*(maxn) t = [0]*(maxn) for i in range (1, n) : arr[i] = x[i-1] top = 0 for i in range(1, n+1) : while (top > 0 and (arr[t[top]] | arr[i]) == arr[i]) : if (arr[t[top]] == arr[i]) : brea...
78
514
127,283,200
226308676
from collections import defaultdict def read_ints(): return map(int, input().split()) def read_int(): return int(input()) def yes_no(yes): print('Yes' if yes else 'No') return yes def solve_test(): n = read_int() a = list(read_ints()) def get_ans(): bit_size = 32 lef...
Codeforces Round 441 (Div. 1, by Moscow Team Olympiad)
CF
2,017
1
512
High Cry
Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :) Rick and Morty like to go to the ridge High Cry for crying loudly — there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this...
The first line contains integer n (1 ≤ n ≤ 200 000), the number of mountains in the ridge. Second line contains n integers ai (0 ≤ ai ≤ 109), the heights of mountains in order they are located in the ridge.
Print the only integer, the number of ways to choose two different mountains.
null
In the first test case all the ways are pairs of mountains with the numbers (numbering from one): (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5) In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of...
[{"input": "5\n3 2 1 6 5", "output": "8"}, {"input": "4\n3 3 3 3", "output": "0"}]
2,200
["binary search", "bitmasks", "combinatorics", "data structures", "divide and conquer"]
78
[{"input": "5\r\n3 2 1 6 5\r\n", "output": "8\r\n"}, {"input": "4\r\n3 3 3 3\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "1\r\n6\r\n", "output": "0\r\n"}, {"input": "228\r\n1 3 1 7 1 3 1 ...
false
stdio
null
true
388/C
388
C
Python 3
TESTS
10
109
0
45469930
#!/usr/bin/env python3 player1 = player2 = 0 player1_turn = True pile_number = int(input()) piles = [] for _ in range(pile_number): n, *pile = tuple(map(int, input().split())) piles.append(pile) for pile in sorted(piles, reverse=True): n = len(pile) if n % 2 == 0: player1 += sum(pile[:n//2]) ...
43
46
0
144399230
n = int(input()) a = 0 b = 0 middle = [] for i in range(n): arr = list(map(int, input().split()))[1:] m = len(arr) // 2 if len(arr) % 2 == 0: a += sum(arr[:m]) b += sum(arr[m:]) else: a += sum(arr[:m]) b += sum(arr[m + 1:]) middle.append(arr[m]) middle.sort(revers...
Codeforces Round 228 (Div. 1)
CF
2,014
1
256
Fox and Card Game
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom o...
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards...
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
null
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
[{"input": "2\n1 100\n2 1 10", "output": "101 10"}, {"input": "1\n9 2 8 6 5 9 4 7 1 3", "output": "30 15"}, {"input": "3\n3 1 3 2\n3 5 4 6\n2 8 7", "output": "18 18"}, {"input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000", "output": "7000 7000"}]
2,000
["games", "greedy", "sortings"]
43
[{"input": "2\r\n1 100\r\n2 1 10\r\n", "output": "101 10\r\n"}, {"input": "1\r\n9 2 8 6 5 9 4 7 1 3\r\n", "output": "30 15\r\n"}, {"input": "3\r\n3 1 3 2\r\n3 5 4 6\r\n2 8 7\r\n", "output": "18 18\r\n"}, {"input": "3\r\n3 1000 1000 1000\r\n6 1000 1000 1000 1000 1000 1000\r\n5 1000 1000 1000 1000 1000\r\n", "output": "7...
false
stdio
null
true
690/C1
690
C1
PyPy 3-64
TESTS
6
61
0
183985592
def dfs(cell): global vis,d if cell in vis: return vis.add(cell) for eachCell in d[cell]: dfs(eachCell) m,n = map(int,input().split()) if (n + 1) != m: print('no') else: d = {} for __ in range(m-1): x, y = map(int,input().split()) if x not in d: d[x] = [] if y not in d: d[y] = [] d[x].append...
18
46
0
170503304
n, m = map(int, input().split()) p = list(range(n)) def g(i): j = i while j != p[j]: j = p[j] while i != j: i, p[i] = p[i], j return j for j in range(m): a, b = map(int, input().split()) p[g(a - 1)] = g(b - 1) s = g(0) k = all(g(i) == s for i in p) print('yes' if m...
Helvetic Coding Contest 2016 online mirror (teams, unrated)
ICPC
2,016
2
256
Brain Network (easy)
One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect – it's the opposite!...
The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is...
The output consists of one line, containing either yes or no depending on whether the nervous system is valid.
null
null
[{"input": "4 4\n1 2\n2 3\n3 1\n4 1", "output": "no"}, {"input": "6 5\n1 2\n2 3\n3 4\n4 5\n3 6", "output": "yes"}]
1,300
[]
18
[{"input": "4 4\r\n1 2\r\n2 3\r\n3 1\r\n4 1\r\n", "output": "no\r\n"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n3 6\r\n", "output": "yes\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "yes\r\n"}, {"input": "3 3\r\n2 1\r\n1 3\r\n3 2\r\n", "output": "no\r\n"}, {"input": "3 2\r\n1 2\r\n2 3\r\n", "output": "yes\r\n"}, {...
false
stdio
null
true
690/C1
690
C1
PyPy 3-64
TESTS
6
46
0
183985372
def dfs(cell): global vis,d if cell in vis: return vis.add(cell) for eachCell in d[cell]: dfs(eachCell) m,n = map(int,input().split()) if (n + 1) != m: print('no') else: d = {} for __ in range(m-1): x, y = map(int,input().split()) if x not in d: d[x] = [] if y not in d: d[y] = [] d[x].append...
18
62
0
31797267
f = lambda: map(int, input().split()) n, m = f() p = list(range(n)) def g(i): while i != p[i]: i = p[i] return i q = 'yes' if m == n - 1 else 'no' for j in range(m): a, b = f() u, v = g(a - 1), g(b - 1) if u == v: q = 'no' p[u] = v print(q)
Helvetic Coding Contest 2016 online mirror (teams, unrated)
ICPC
2,016
2
256
Brain Network (easy)
One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect – it's the opposite!...
The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is...
The output consists of one line, containing either yes or no depending on whether the nervous system is valid.
null
null
[{"input": "4 4\n1 2\n2 3\n3 1\n4 1", "output": "no"}, {"input": "6 5\n1 2\n2 3\n3 4\n4 5\n3 6", "output": "yes"}]
1,300
[]
18
[{"input": "4 4\r\n1 2\r\n2 3\r\n3 1\r\n4 1\r\n", "output": "no\r\n"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n3 6\r\n", "output": "yes\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "yes\r\n"}, {"input": "3 3\r\n2 1\r\n1 3\r\n3 2\r\n", "output": "no\r\n"}, {"input": "3 2\r\n1 2\r\n2 3\r\n", "output": "yes\r\n"}, {...
false
stdio
null
true
897/A
897
A
Python 3
TESTS
3
46
0
198399816
n, m = map(int, input().split()) s = list(input()) for i in range(m): temp = list(input().replace(" ", "")) start = int(temp[0]) - 1 end = int(temp[1]) c1 = temp[2] c2 = temp[3] for j in range(start, end): t = s[j] if(t == c1): s[j] = c2 ans = ''.join(s) print(ans)
47
46
0
147089378
"""n,m=map(int,input().split())#len of string ,no. of operations s=input() l,r,c1,c2= input().split() i=int(l) j=int(r) newstring=s[i:j] k=newstring.replace(c1,c2) newstr=s[:i]+k+s[j:] print(newstr)""" n,m=map(int,input().split()) s = input() for j in range(m): l,r,c1,c2 = map(str,input().split()) ne = s[int(l...
Codeforces Round 449 (Div. 2)
CF
2,017
2
256
Scarborough Fair
Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there. Willem asks his friend, Grick for directions, Grick helped them, and gave them a task. Although the girl wants to help, Willem insists on doing it by himself. Grick gave Willem a string of length n. W...
The first line contains two integers n and m (1 ≤ n, m ≤ 100). The second line contains a string s of length n, consisting of lowercase English letters. Each of the next m lines contains four parameters l, r, c1, c2 (1 ≤ l ≤ r ≤ n, c1, c2 are lowercase English letters), separated by space.
Output string s after performing m operations described above.
null
For the second example: After the first operation, the string is wxxak. After the second operation, the string is waaak. After the third operation, the string is gaaak.
[{"input": "3 1\nioi\n1 1 i n", "output": "noi"}, {"input": "5 3\nwxhak\n3 3 h x\n1 5 x a\n1 3 w g", "output": "gaaak"}]
800
["implementation"]
47
[{"input": "3 1\r\nioi\r\n1 1 i n\r\n", "output": "noi"}, {"input": "5 3\r\nwxhak\r\n3 3 h x\r\n1 5 x a\r\n1 3 w g\r\n", "output": "gaaak"}, {"input": "9 51\r\nbhfbdcgff\r\n2 3 b b\r\n2 8 e f\r\n3 8 g f\r\n5 7 d a\r\n1 5 e b\r\n3 4 g b\r\n6 7 c d\r\n3 6 e g\r\n3 6 e h\r\n5 6 a e\r\n7 9 a c\r\n4 9 a h\r\n3 7 c b\r\n6 9 ...
false
stdio
null
true
875/F
875
F
PyPy 3
TESTS
2
124
0
47958139
prince, princess = map(int, input().split()) prince_data = {} for i in range(1, prince+1): prince_data[str(i)] = 0 for _ in range(princess): prince_one, prince_two, money = map(int, input().split()) if prince_data[str(prince_one)] < money: prince_data[str(prince_one)] = money continue el...
75
763
31,436,800
171781036
import io, os, sys from heapq import heappush, heappop from operator import itemgetter class UnionSet: def __init__(self, n: int): self.parent = [*range(n)] self.rank = [1] * n self.is_circle = [False] * n def __len__(self): return sum([x == self.parent[x] for x in range(len(...
Codeforces Round 441 (Div. 1, by Moscow Team Olympiad)
CF
2,017
1.5
512
Royal Questions
In a medieval kingdom, the economic crisis is raging. Milk drops fall, Economic indicators are deteriorating every day, money from the treasury disappear. To remedy the situation, King Charles Sunnyface decided make his n sons-princes marry the brides with as big dowry as possible. In search of candidates, the king as...
The first line contains two integers n, m (2 ≤ n ≤ 200 000, 1 ≤ m ≤ 200 000) — number of princes and princesses respectively. Each of following m lines contains three integers ai, bi, wi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ wi ≤ 10 000) — number of princes, which i-th princess is ready to marry and the value of her dowry.
Print the only integer — the maximum number of gold coins that a king can get by playing the right weddings.
null
null
[{"input": "2 3\n1 2 5\n1 2 1\n2 1 10", "output": "15"}, {"input": "3 2\n1 2 10\n3 2 20", "output": "30"}]
2,500
["dsu", "graphs", "greedy"]
75
[{"input": "2 3\r\n1 2 5\r\n1 2 1\r\n2 1 10\r\n", "output": "15"}, {"input": "3 2\r\n1 2 10\r\n3 2 20\r\n", "output": "30"}, {"input": "2 2\r\n1 2 1\r\n1 2 2\r\n", "output": "3"}, {"input": "3 3\r\n1 3 4\r\n1 3 2\r\n1 3 8\r\n", "output": "12"}, {"input": "2 1\r\n1 2 6\r\n", "output": "6"}, {"input": "4 3\r\n1 2 1\r\n2 ...
false
stdio
null
true
83/A
83
A
PyPy 3
TESTS
37
340
12,595,200
215808855
n = int(input()) arr = [int(x) for x in input().split()] rle_arr = [] current_num = -1 current_count = 1 for element in arr: if(element != current_num): rle_arr.append((current_num, current_count)) current_num = element current_count = 1 else: current_count += 1 rle_arr.append((current_num, current_count)) rl...
50
154
14,131,200
164363596
import sys input = sys.stdin.readline n = int(input()) w = list(map(int, input().split())) c = 0 d = 1 for i in range(n-1): if w[i] == w[i+1]: d += 1 else: c += d*(d+1)//2 d = 1 c += d*(d+1)//2 print(c)
Codeforces Beta Round 72 (Div. 1 Only)
CF
2,011
2
256
Magical Array
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical. Valera absolutel...
The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Print on the single line the answer to the problem: the amount of subarrays, which are magical. Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
null
Notes to sample tests: Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end. In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3]. In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
[{"input": "4\n2 1 1 4", "output": "5"}, {"input": "5\n-2 -2 -2 0 1", "output": "8"}]
1,300
["math"]
50
[{"input": "4\r\n2 1 1 4\r\n", "output": "5\r\n"}, {"input": "5\r\n-2 -2 -2 0 1\r\n", "output": "8\r\n"}, {"input": "1\r\n10\r\n", "output": "1\r\n"}, {"input": "2\r\n5 6\r\n", "output": "2\r\n"}, {"input": "5\r\n5 5 4 5 5\r\n", "output": "7\r\n"}, {"input": "8\r\n1 2 0 0 0 0 3 3\r\n", "output": "15\r\n"}, {"input": "1...
false
stdio
null
true
545/C
545
C
Python 3
TESTS
8
217
15,257,600
228998641
def max_tree(trees): ans = 0 bd = [trees[0][0]] for i in range(1, len(trees)-1): if bd[-1] < trees[i][0]-trees[i][1]: bd.append(trees[i][0]) ans += 1 elif trees[i][0]+trees[i][1] < trees[i+1][0]: bd.append(trees[i][0]+trees[i][1]) ans += 1 ...
67
218
8,089,600
228738058
n=int(input()) tree=[0]*n height=[0]*n cut_tree=2 for i in range(n): tree[i],height[i]=map(int,input().split()) if n==1: print(1) else: for i in range(1,n-1): if tree[i]-height[i]>tree[i-1]: cut_tree+=1 elif tree[i]+height[i]<tree[i+1]: cut_tree += 1 tree[...
Codeforces Round 303 (Div. 2)
CF
2,015
1
256
Woodcutters
Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below. There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each ...
The first line contains integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree. The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.
Print a single number — the maximum number of trees that you can cut down by the given rules.
null
In the first sample you can fell the trees like that: - fell the 1-st tree to the left — now it occupies segment [ - 1;1] - fell the 2-nd tree to the right — now it occupies segment [2;3] - leave the 3-rd tree — it occupies point 5 - leave the 4-th tree — it occupies point 10 - fell the 5-th tree to the right — now it...
[{"input": "5\n1 2\n2 1\n5 10\n10 9\n19 1", "output": "3"}, {"input": "5\n1 2\n2 1\n5 10\n10 9\n20 1", "output": "4"}]
1,500
["dp", "greedy"]
67
[{"input": "5\r\n1 2\r\n2 1\r\n5 10\r\n10 9\r\n19 1\r\n", "output": "3\r\n"}, {"input": "5\r\n1 2\r\n2 1\r\n5 10\r\n10 9\r\n20 1\r\n", "output": "4\r\n"}, {"input": "4\r\n10 4\r\n15 1\r\n19 3\r\n20 1\r\n", "output": "4\r\n"}, {"input": "35\r\n1 7\r\n3 11\r\n6 12\r\n7 6\r\n8 5\r\n9 11\r\n15 3\r\n16 10\r\n22 2\r\n23 3\r\...
false
stdio
null
true
175/C
175
C
PyPy 3
TESTS
2
280
0
61378930
if __name__ == '__main__': n = int(input()) pieces = [input() for _ in range(n)] pieces = [_.split() for _ in pieces] pieces = [tuple(_) for _ in pieces] pieces = [(int(k), int(c)) for k, c in pieces] t = int(input()) p = input().split() p = [int(_) for _ in p] sorted(pieces, key=la...
90
124
0
166085775
n = int(input()) arr = [] for i in range(n): arr.append(list(map(int, input().split()))) arr.sort(key=lambda x: x[1]) t = int(input()) p = list(map(int, input().split())) p.insert(0, 0) p.append(1e12+1) rate = 1 ret = 0 for i in range(1, len(p)): diff = p[i]-p[i-1] a = 0 while arr and a < diff: ...
Codeforces Round 115
CF
2,012
2
256
Geometry Horse
Vasya plays the Geometry Horse. The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value. There are n types of geometric figures. The number of figures of type ki and figure cost ci is know...
The first line contains the only integer number n (1 ≤ n ≤ 100) — the number of figure types. Each of the following n lines contains two integer numbers ki and ci (1 ≤ ki ≤ 109, 0 ≤ ci ≤ 1000), separated with space — the number of figures of the i-th type and the cost of one i-type figure, correspondingly. The next l...
Print the only number — the maximum number of points Vasya can get.
null
In the first example Vasya destroys three figures first and gets 3·1·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2·2·10 = 40 points. As a result Vasya will get 70 points. In the second example all 8 figures will be destroyed with factor 1, so Vasya wi...
[{"input": "1\n5 10\n2\n3 6", "output": "70"}, {"input": "2\n3 8\n5 10\n1\n20", "output": "74"}]
1,600
["greedy", "implementation", "sortings", "two pointers"]
90
[{"input": "1\r\n5 10\r\n2\r\n3 6\r\n", "output": "70"}, {"input": "2\r\n3 8\r\n5 10\r\n1\r\n20\r\n", "output": "74"}, {"input": "3\r\n10 3\r\n20 2\r\n30 1\r\n3\r\n30 50 60\r\n", "output": "200"}, {"input": "1\r\n100 1000\r\n1\r\n1\r\n", "output": "199000"}, {"input": "1\r\n1 1000\r\n1\r\n1\r\n", "output": "1000"}, {"i...
false
stdio
null
true
467/B
467
B
Python 3
TESTS
5
30
0
230234381
n,m,k=map(int,input().split()) G=[] x=0 for mi in range(m): G.append(bin(int(input()))[2:]) F=bin(int(input()))[2:] f=len(F) for g in G: diff = 0 check = 1 for i in range(max(len(g),f)): j=-i-1 if -j >= min(len(g),f): diff+=1 elif g[j]!=F[j]: diff+=1 ...
43
46
0
218297843
def countSetBits(n): count=0 while(n>0): last_bit = n&1 if last_bit==1: count+=1 n = n>>1 return count n , m , k = map(int, input().split()) arr = [] for _ in range(m+1): xi = int(input()) arr.append(xi) friends = 0 for i in range(m): value = arr[i]^ar...
Codeforces Round 267 (Div. 2)
CF
2,014
1
256
Fedor and New Game
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3». The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each ...
The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000). The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.
Print a single integer — the number of Fedor's potential friends.
null
null
[{"input": "7 3 1\n8\n5\n111\n17", "output": "0"}, {"input": "3 3 3\n1\n2\n3\n4", "output": "3"}]
1,100
["bitmasks", "brute force", "constructive algorithms", "implementation"]
43
[{"input": "7 3 1\r\n8\r\n5\r\n111\r\n17\r\n", "output": "0\r\n"}, {"input": "3 3 3\r\n1\r\n2\r\n3\r\n4\r\n", "output": "3\r\n"}, {"input": "4 2 2\r\n5\r\n6\r\n7\r\n", "output": "2\r\n"}, {"input": "4 7 4\r\n9\r\n10\r\n5\r\n12\r\n4\r\n12\r\n7\r\n10\r\n", "output": "7\r\n"}, {"input": "2 7 2\r\n1\r\n1\r\n1\r\n1\r\n1\r\n...
false
stdio
null
true
510/C
510
C
PyPy 3-64
TESTS
12
62
0
205371393
from collections import deque, defaultdict n = int(input()) words = [] for _ in range(n): words.append(input()) graph = defaultdict(list) indegree = defaultdict(int) for ind in range(1, n): i = 0 word1 = words[ind - 1] word2 = words[ind] while i < len(word1) and i < len(word2) and word1[i] ...
36
46
307,200
210871493
from collections import defaultdict, deque def bfs(n, names): adj = defaultdict(list) indegree = [0] * 26 order = [] for i in range(n - 1): min_len = min(len(names[i]), len(names[i + 1])) j = 0 while j < min_len: if names[i][j] != names[i + 1][j]: x...
Codeforces Round 290 (Div. 2)
CF
2,015
2
256
Fox And Names
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in le...
The first line contains an integer n (1 ≤ n ≤ 100): number of names. Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word "Impossible" (without quotes).
null
null
[{"input": "3\nrivest\nshamir\nadleman", "output": "bcdefghijklmnopqrsatuvwxyz"}, {"input": "10\ntourist\npetr\nwjmzbmr\nyeputons\nvepifanov\nscottwu\noooooooooooooooo\nsubscriber\nrowdark\ntankengineer", "output": "Impossible"}, {"input": "10\npetr\negor\nendagorion\nfeferivan\nilovetanyaromanova\nkostka\ndmitriyh\nma...
1,600
["dfs and similar", "graphs", "sortings"]
36
[{"input": "3\r\nrivest\r\nshamir\r\nadleman\r\n", "output": "bcdefghijklmnopqrsatuvwxyz\r\n"}, {"input": "10\r\ntourist\r\npetr\r\nwjmzbmr\r\nyeputons\r\nvepifanov\r\nscottwu\r\noooooooooooooooo\r\nsubscriber\r\nrowdark\r\ntankengineer\r\n", "output": "Impossible\r\n"}, {"input": "10\r\npetr\r\negor\r\nendagorion\r\nf...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) names = [line.strip() for line in f] with open(output_path) as f: ref = f.read().strip() with open(submiss...
true
510/C
510
C
PyPy 3-64
TESTS
12
62
0
206007572
from collections import defaultdict n = int(input()) words = [] for i in range(n): words.append(input()) def findOrder(arr): graph = defaultdict(list) letter_collections = [chr(i) for i in range(97, 123)] for i in range(1, len( arr)): ptr = 0 word1, word2 =...
36
61
0
202560655
n = int(input()) a = [input() for _ in range(n)] g = [] def E(): print('Impossible') exit(0) for i in range(1, n): l = min(len(a[i-1]), len(a[i])) for j in range(l): if a[i-1][j] != a[i][j]: g += [(ord(a[i-1][j]), ord(a[i][j]))] break if l == len(a[i]) and a[i][j] == a[i-1][j]: E() r = ...
Codeforces Round 290 (Div. 2)
CF
2,015
2
256
Fox And Names
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in le...
The first line contains an integer n (1 ≤ n ≤ 100): number of names. Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word "Impossible" (without quotes).
null
null
[{"input": "3\nrivest\nshamir\nadleman", "output": "bcdefghijklmnopqrsatuvwxyz"}, {"input": "10\ntourist\npetr\nwjmzbmr\nyeputons\nvepifanov\nscottwu\noooooooooooooooo\nsubscriber\nrowdark\ntankengineer", "output": "Impossible"}, {"input": "10\npetr\negor\nendagorion\nfeferivan\nilovetanyaromanova\nkostka\ndmitriyh\nma...
1,600
["dfs and similar", "graphs", "sortings"]
36
[{"input": "3\r\nrivest\r\nshamir\r\nadleman\r\n", "output": "bcdefghijklmnopqrsatuvwxyz\r\n"}, {"input": "10\r\ntourist\r\npetr\r\nwjmzbmr\r\nyeputons\r\nvepifanov\r\nscottwu\r\noooooooooooooooo\r\nsubscriber\r\nrowdark\r\ntankengineer\r\n", "output": "Impossible\r\n"}, {"input": "10\r\npetr\r\negor\r\nendagorion\r\nf...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) names = [line.strip() for line in f] with open(output_path) as f: ref = f.read().strip() with open(submiss...
true
725/D
725
D
Python 3
TESTS
19
1,950
38,502,400
21686750
import bisect n = int(input()) my, _ = list(map(int, input().strip().split())) #print(my) competitors = [] for i in range(n - 1): b, w = list(map(int, input().strip().split())) competitors.append([b, w - b + 1]) competitors.sort(key = lambda x: x[0]) #print(competitors) bs = [c[0] for c in competitors] #pri...
49
2,667
53,043,200
118201939
import sys input = sys.stdin.readline n = int(input()) limak = list(map(int, input().split())) import heapq pq = [] arr = [] for i in range(1, n): ti, wi = map(int, input().split()) if ti > limak[0]: heapq.heappush(pq, wi - ti + 1) else: arr.append((ti, wi - ti + 1)) arr.sort(reverse=True...
Canada Cup 2016
CF
2,016
3
256
Contest Balloons
One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams with more balloons, increased by 1. For example, if there ...
The first line of the standard input contains one integer n (2 ≤ n ≤ 300 000) — the number of teams. The i-th of n following lines contains two integers ti and wi (0 ≤ ti ≤ wi ≤ 1018) — respectively the number of balloons and the weight of the i-th team. Limak is a member of the first team.
Print one integer denoting the best place Limak can get.
null
In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is: 1. Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak...
[{"input": "8\n20 1000\n32 37\n40 1000\n45 50\n16 16\n16 16\n14 1000\n2 1000", "output": "3"}, {"input": "7\n4 4\n4 4\n4 4\n4 4\n4 4\n4 4\n5 5", "output": "2"}, {"input": "7\n14000000003 1000000000000000000\n81000000000 88000000000\n5000000000 7000000000\n15000000000 39000000000\n46000000000 51000000000\n0 1000000000\n...
1,800
["data structures", "greedy"]
49
[{"input": "8\r\n20 1000\r\n32 37\r\n40 1000\r\n45 50\r\n16 16\r\n16 16\r\n14 1000\r\n2 1000\r\n", "output": "3\r\n"}, {"input": "7\r\n4 4\r\n4 4\r\n4 4\r\n4 4\r\n4 4\r\n4 4\r\n5 5\r\n", "output": "2\r\n"}, {"input": "7\r\n14000000003 1000000000000000000\r\n81000000000 88000000000\r\n5000000000 7000000000\r\n1500000000...
false
stdio
null
true
279/B
279
B
PyPy 3-64
TESTS
8
124
13,516,800
184878062
n, t = map(int, input().split(" ")) a = list(map(int, input().split(" "))) prefix = [0] for x in a: prefix.append(prefix[-1] + x) left = 0 right = n while prefix[right] - prefix[left] > t: if prefix[left + 1] - prefix[left] > prefix[right] - prefix[right - 1]: left += 1 else: right -= 1 pr...
38
154
13,312,000
204069798
n,t=map(int,input().split()) a=list(map(int,input().split())) i=0;j=0;maxi=0;count_=0 while j<n: if count_+a[j]<=t:count_+=a[j];j+=1 else: maxi=max(maxi,j-i) if j!=i: count_-=a[i] i+=1 else: i+=1 j+=1 maxi=max(maxi,j-i) print(maxi)
Codeforces Round 171 (Div. 2)
CF
2,013
2
256
Books
When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the ...
The first line contains two integers n and t (1 ≤ n ≤ 105; 1 ≤ t ≤ 109) — the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.
Print a single integer — the maximum number of books Valera can read.
null
null
[{"input": "4 5\n3 1 2 1", "output": "3"}, {"input": "3 3\n2 2 3", "output": "1"}]
1,400
["binary search", "brute force", "implementation", "two pointers"]
38
[{"input": "4 5\r\n3 1 2 1\r\n", "output": "3\r\n"}, {"input": "3 3\r\n2 2 3\r\n", "output": "1\r\n"}, {"input": "1 3\r\n5\r\n", "output": "0\r\n"}, {"input": "1 10\r\n4\r\n", "output": "1\r\n"}, {"input": "2 10\r\n6 4\r\n", "output": "2\r\n"}, {"input": "6 10\r\n2 3 4 2 1 1\r\n", "output": "4\r\n"}, {"input": "7 13\r\...
false
stdio
null
true
1009/A
1009
A
PyPy 3
TESTS
4
140
0
94768089
n,m=[int(i) for i in input().split()] a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] i=0 j=0 e=0 while(i<len(a)-1 or j<len(b)-1): if(b[j]>=a[i]): i=i+1 j=j+1 e=e+1 else: i=i+1 if(i==len(a) or j==len(b)): break print(e)
19
46
0
136738924
n, m = map(int, input().split()) list1 = list(map(int, input().split())) list2 = list(map(int, input().split())) j = 0 k = 0 for i in range(n): if list2[j] >= list1[i]: list2.pop(j) k += 1 if k == m: break print(k)
Educational Codeforces Round 47 (Rated for Div. 2)
ICPC
2,018
1
256
Game Shopping
Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$. Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$. Games in the shop are ordered from left to ri...
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) — the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th ...
Print a single integer — the number of games Maxim will buy.
null
The first example is described in the problem statement. In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop. In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter unti...
[{"input": "5 4\n2 4 5 2 4\n5 3 4 6", "output": "3"}, {"input": "5 2\n20 40 50 20 40\n19 20", "output": "0"}, {"input": "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000", "output": "4"}]
800
["implementation"]
19
[{"input": "5 4\r\n2 4 5 2 4\r\n5 3 4 6\r\n", "output": "3\r\n"}, {"input": "5 2\r\n20 40 50 20 40\r\n19 20\r\n", "output": "0\r\n"}, {"input": "6 4\r\n4 8 15 16 23 42\r\n1000 1000 1000 1000\r\n", "output": "4\r\n"}, {"input": "5 1\r\n1 1 1 1 1\r\n5\r\n", "output": "1\r\n"}, {"input": "5 1\r\n10 1 1 1 1\r\n1000\r\n", "...
false
stdio
null
true
984/A
984
A
PyPy 3
TESTS
5
61
0
153695037
n = int(input()) arr = list(map(int, input().split())) counter = 0 arr.sort() print(arr[round((len(arr) - 1) / 2)])
35
31
0
144055009
w=int(input()) l=list(map(int,input().split())) l.sort() print(l[(len(l)-1)//2])
Codeforces Round 483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]
CF
2,018
2
256
Game
Two players play a game. Initially there are $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, t...
The first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of numbers on the board. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
Print one number that will be left on the board.
null
In the first sample, the first player erases $$$3$$$ and the second erases $$$1$$$. $$$2$$$ is left on the board. In the second sample, $$$2$$$ is left on the board regardless of the actions of the players.
[{"input": "3\n2 1 3", "output": "2"}, {"input": "3\n2 2 2", "output": "2"}]
800
["sortings"]
35
[{"input": "3\r\n2 1 3\r\n", "output": "2"}, {"input": "3\r\n2 2 2\r\n", "output": "2"}, {"input": "9\r\n44 53 51 80 5 27 74 79 94\r\n", "output": "53"}, {"input": "10\r\n38 82 23 37 96 4 81 60 67 86\r\n", "output": "60"}, {"input": "10\r\n58 26 77 15 53 81 68 48 22 65\r\n", "output": "53"}, {"input": "1\r\n124\r\n", "...
false
stdio
null
true
988/F
988
F
PyPy 3-64
TESTS
4
436
41,369,600
175509458
from functools import lru_cache import bisect t, n, m = map(int, input().split()) p = {} s = set() for i in range(n): x, y = map(int, input().split()) p[x] = 1 p[y] = -1 s.add(x) s.add(y) s.add(t) d = {} q = [] for i in range(m): x, y = map(int, input().split()) d[x] = [y, i] q.append(y)...
37
155
4,710,400
228665233
# https://codeforces.com/contest/988 import sys input = lambda: sys.stdin.readline().rstrip() # faster! a, n, m = map(int, input().split()) rain = sorted(tuple(map(int, input().split())) for _ in range(n)) umbr = sorted(tuple(map(int, input().split())) for _ in range(m)) if rain[0][0] < umbr[0][0]: print(-1) ...
Codeforces Round 486 (Div. 3)
ICPC
2,018
2
256
Rain and Umbrellas
Polycarp lives on a coordinate line at the point $$$x = 0$$$. He goes to his friend that lives at the point $$$x = a$$$. Polycarp can move only from left to right, he can pass one unit of length each second. Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $$$n$$$ non-intersecti...
The first line contains three integers $$$a$$$, $$$n$$$ and $$$m$$$ ($$$1 \le a, m \le 2000, 1 \le n \le \lceil\frac{a}{2}\rceil$$$) — the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas. Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_...
Print "-1" (without quotes) if Polycarp can't make his way from point $$$x = 0$$$ to point $$$x = a$$$. Otherwise print one integer — the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally.
null
In the first example the only possible strategy is to take the fourth umbrella at the point $$$x = 1$$$, keep it till the point $$$x = 7$$$ (the total fatigue at $$$x = 7$$$ will be equal to $$$12$$$), throw it away, move on from $$$x = 7$$$ to $$$x = 8$$$ without an umbrella, take the third umbrella at $$$x = 8$$$ and...
[{"input": "10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2", "output": "14"}, {"input": "10 1 1\n0 9\n0 5", "output": "45"}, {"input": "10 1 1\n0 9\n1 5", "output": "-1"}]
2,100
["dp"]
37
[{"input": "10 2 4\r\n3 7\r\n8 10\r\n0 10\r\n3 4\r\n8 1\r\n1 2\r\n", "output": "14\r\n"}, {"input": "10 1 1\r\n0 9\r\n0 5\r\n", "output": "45\r\n"}, {"input": "10 1 1\r\n0 9\r\n1 5\r\n", "output": "-1\r\n"}, {"input": "1 1 1\r\n0 1\r\n1 100000\r\n", "output": "-1\r\n"}, {"input": "1 1 1\r\n0 1\r\n0 100000\r\n", "output...
false
stdio
null
true
659/G
659
G
PyPy 3
TESTS
7
93
0
166293354
import sys input = sys.stdin.buffer.readline p = 10**9+7 def process(H): n = len(H) """ f(l, r) = product of min(hi-1, h(i-1)-1, h(i+1)-1) over such things in l, r """ answer = 0 final_term = [] for i in range(n): new_final_term = [] hi = H[i] ...
50
1,466
101,068,800
21130571
mod = 10 ** 9 + 7 n = int(input()) h = list(map(lambda x: int(x) - 1, input().split())) ans = x = 0 for i in range(n): ans += h[i] + min(h[i], h[i - 1]) * x if i < n - 1: x *= min(h[i - 1], h[i], h[i + 1]) x += min(h[i], h[i + 1]) ans %= mod x %= mod print(ans)
Codeforces Round 346 (Div. 2)
CF
2,016
2
256
Fence Divercity
Long ago, Vasily built a good fence at his country house. Vasily calls a fence good, if it is a series of n consecutively fastened vertical boards of centimeter width, the height of each in centimeters is a positive integer. The house owner remembers that the height of the i-th board to the left is hi. Today Vasily de...
The first line contains integer n (1 ≤ n ≤ 1 000 000) — the number of boards in Vasily's fence. The second line contains n space-separated numbers h1, h2, ..., hn (1 ≤ hi ≤ 109), where hi equals the height of the i-th board to the left.
Print the remainder after dividing r by 1 000 000 007, where r is the number of ways to cut exactly one connected part so that the part consisted of the upper parts of the boards and the remaining fence was good.
null
From the fence from the first example it is impossible to cut exactly one piece so as the remaining fence was good. All the possible variants of the resulting fence from the second sample look as follows (the grey shows the cut out part):
[{"input": "2\n1 1", "output": "0"}, {"input": "3\n3 4 2", "output": "13"}]
2,300
["combinatorics", "dp", "number theory"]
50
[{"input": "2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "3\r\n3 4 2\r\n", "output": "13\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "999999999\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "4\r\n"}, {"input": "2\r\n2 3\r\n", "output": "4\r\n"}, {"input": "5\r\n1 4 2 1 1...
false
stdio
null
true
412/B
412
B
PyPy 3-64
TESTS
3
46
0
227941933
n, k = map(int, input().split()) lst = list(map(int, input().split())) target = sum(lst) // n closest_val = float("-inf") for i in lst: if i <= target and i > closest_val: closest_val = i print(closest_val)
34
46
0
146780837
n, k = map(int, input().split()) v = sorted(map(int, input().split())) print(v[n - k])
Coder-Strike 2014 - Round 1
CF
2,014
1
256
Network Configuration
The R1 company wants to hold a web search championship. There were n computers given for the competition, each of them is connected to the Internet. The organizers believe that the data transfer speed directly affects the result. The higher the speed of the Internet is, the faster the participant will find the necessar...
The first line contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 100) — the number of computers and the number of participants, respectively. In the second line you have a space-separated sequence consisting of n integers: a1, a2, ..., an (16 ≤ ai ≤ 32768); number ai denotes the maximum data transfer speed on ...
Print a single integer — the maximum Internet speed value. It is guaranteed that the answer to the problem is always an integer.
null
In the first test case the organizers can cut the first computer's speed to 30 kilobits. Then two computers (the first and the third one) will have the same speed of 30 kilobits. They should be used as the participants' computers. This answer is optimal.
[{"input": "3 2\n40 20 30", "output": "30"}, {"input": "6 4\n100 20 40 20 50 50", "output": "40"}]
900
["greedy", "sortings"]
34
[{"input": "3 2\r\n40 20 30\r\n", "output": "30\r\n"}, {"input": "6 4\r\n100 20 40 20 50 50\r\n", "output": "40\r\n"}, {"input": "1 1\r\n16\r\n", "output": "16\r\n"}, {"input": "2 1\r\n10000 17\r\n", "output": "10000\r\n"}, {"input": "2 2\r\n200 300\r\n", "output": "200\r\n"}, {"input": "3 1\r\n21 25 16\r\n", "output":...
false
stdio
null
true
412/B
412
B
Python 3
TESTS
3
46
0
176972471
def binary_search(array,key): ans =0 for i in array: if i==key: ans = i break elif i<key: ans = i print(ans) n,m=map(int,input().split()) l = sorted(list(map(int,input().split()))) summ = sum(l) binary_search(l,summ//n)
34
46
0
149537434
q = str(input()).split() n = int(q[0]) k = int(q[1]) arr = [int(a) for a in input().split()] mas = [] arr.sort(reverse=True) for i in range(k): mas.append(arr[i]) print(mas[-1])
Coder-Strike 2014 - Round 1
CF
2,014
1
256
Network Configuration
The R1 company wants to hold a web search championship. There were n computers given for the competition, each of them is connected to the Internet. The organizers believe that the data transfer speed directly affects the result. The higher the speed of the Internet is, the faster the participant will find the necessar...
The first line contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 100) — the number of computers and the number of participants, respectively. In the second line you have a space-separated sequence consisting of n integers: a1, a2, ..., an (16 ≤ ai ≤ 32768); number ai denotes the maximum data transfer speed on ...
Print a single integer — the maximum Internet speed value. It is guaranteed that the answer to the problem is always an integer.
null
In the first test case the organizers can cut the first computer's speed to 30 kilobits. Then two computers (the first and the third one) will have the same speed of 30 kilobits. They should be used as the participants' computers. This answer is optimal.
[{"input": "3 2\n40 20 30", "output": "30"}, {"input": "6 4\n100 20 40 20 50 50", "output": "40"}]
900
["greedy", "sortings"]
34
[{"input": "3 2\r\n40 20 30\r\n", "output": "30\r\n"}, {"input": "6 4\r\n100 20 40 20 50 50\r\n", "output": "40\r\n"}, {"input": "1 1\r\n16\r\n", "output": "16\r\n"}, {"input": "2 1\r\n10000 17\r\n", "output": "10000\r\n"}, {"input": "2 2\r\n200 300\r\n", "output": "200\r\n"}, {"input": "3 1\r\n21 25 16\r\n", "output":...
false
stdio
null
true
510/C
510
C
Python 3
TESTS
12
46
0
205984693
import sys input = sys.stdin.readline t = int(input()) alien_dict = [] for _ in range(t): alien_dict.append(input().strip()) N = t M = 26 adj = [set() for _ in range(26)] status = [0 for _ in range(26)] S = max([0 for _ in range(N)], key = lambda x : len(alien_dict[x])) match = alien_dict[0][0] for i in range(2...
36
62
0
9680012
""" Codeforces Contest 290 Div 1 Problem A Author : chaotic_iak Language: Python 3.4.2 """ ################################################### SOLUTION def main(): n, = read() a = [read(0) for _ in range(n)] edgefrom = [[] for _ in range(26)] edgeto = [[] for _ in range(26)] for i in range(n-1):...
Codeforces Round 290 (Div. 2)
CF
2,015
2
256
Fox And Names
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in le...
The first line contains an integer n (1 ≤ n ≤ 100): number of names. Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word "Impossible" (without quotes).
null
null
[{"input": "3\nrivest\nshamir\nadleman", "output": "bcdefghijklmnopqrsatuvwxyz"}, {"input": "10\ntourist\npetr\nwjmzbmr\nyeputons\nvepifanov\nscottwu\noooooooooooooooo\nsubscriber\nrowdark\ntankengineer", "output": "Impossible"}, {"input": "10\npetr\negor\nendagorion\nfeferivan\nilovetanyaromanova\nkostka\ndmitriyh\nma...
1,600
["dfs and similar", "graphs", "sortings"]
36
[{"input": "3\r\nrivest\r\nshamir\r\nadleman\r\n", "output": "bcdefghijklmnopqrsatuvwxyz\r\n"}, {"input": "10\r\ntourist\r\npetr\r\nwjmzbmr\r\nyeputons\r\nvepifanov\r\nscottwu\r\noooooooooooooooo\r\nsubscriber\r\nrowdark\r\ntankengineer\r\n", "output": "Impossible\r\n"}, {"input": "10\r\npetr\r\negor\r\nendagorion\r\nf...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) names = [line.strip() for line in f] with open(output_path) as f: ref = f.read().strip() with open(submiss...
true
510/C
510
C
Python 3
TESTS
12
31
135,168,000
205481142
from collections import defaultdict from functools import lru_cache import threading from sys import stdin,stdout,setrecursionlimit setrecursionlimit(1 << 30) threading.stack_size(1 << 27) def main(): #User function Template for python3 def findOrder(alien_dict, N): adj = {chr(97+idx): set() for idx ...
36
62
0
9680311
from sys import exit n = int(input()) names = [] for i in range(n): names.append(input()) edge = [[0 for i in range(26)] for j in range(26)] for i in range(n - 1): if names[i].startswith(names[i + 1]): print('Impossible') exit(0) for j in range(len(names[i])): if names[i][j] != na...
Codeforces Round 290 (Div. 2)
CF
2,015
2
256
Fox And Names
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in le...
The first line contains an integer n (1 ≤ n ≤ 100): number of names. Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word "Impossible" (without quotes).
null
null
[{"input": "3\nrivest\nshamir\nadleman", "output": "bcdefghijklmnopqrsatuvwxyz"}, {"input": "10\ntourist\npetr\nwjmzbmr\nyeputons\nvepifanov\nscottwu\noooooooooooooooo\nsubscriber\nrowdark\ntankengineer", "output": "Impossible"}, {"input": "10\npetr\negor\nendagorion\nfeferivan\nilovetanyaromanova\nkostka\ndmitriyh\nma...
1,600
["dfs and similar", "graphs", "sortings"]
36
[{"input": "3\r\nrivest\r\nshamir\r\nadleman\r\n", "output": "bcdefghijklmnopqrsatuvwxyz\r\n"}, {"input": "10\r\ntourist\r\npetr\r\nwjmzbmr\r\nyeputons\r\nvepifanov\r\nscottwu\r\noooooooooooooooo\r\nsubscriber\r\nrowdark\r\ntankengineer\r\n", "output": "Impossible\r\n"}, {"input": "10\r\npetr\r\negor\r\nendagorion\r\nf...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) names = [line.strip() for line in f] with open(output_path) as f: ref = f.read().strip() with open(submiss...
true
1005/A
1005
A
Python 3
TESTS
6
46
0
223453669
a = int(input()) s = list(map(int, input().split())) x = str(s) t = x.count('1') print(t) d = '' if t == 1: d = str(s[-1]) else: for i in range(a): if i != 0 and s[i] == 1: d += str(s[i - 1]) + ' ' if i == a - 1: d += str(s[i]) print(d.strip())
16
31
0
153529507
n = int(input()) a = list(map(int, input().split())) ans = [] q = 0 for i in range(1, n): if a[i - 1] >= a[i]: ans.append(i - q) q = i ans.append(n - q) print(len(ans)) print(*ans)
Codeforces Round 496 (Div. 3)
ICPC
2,018
1
256
Tanya and Stairways
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $$$1$$$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $$$3$$$ steps, and the second conta...
The first line contains $$$n$$$ ($$$1 \le n \le 1000$$$) — the total number of numbers pronounced by Tanya. The second line contains integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 1000$$$) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passin...
In the first line, output $$$t$$$ — the number of stairways that Tanya climbed. In the second line, output $$$t$$$ numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
null
null
[{"input": "7\n1 2 3 1 2 3 4", "output": "2\n3 4"}, {"input": "4\n1 1 1 1", "output": "4\n1 1 1 1"}, {"input": "5\n1 2 3 4 5", "output": "1\n5"}, {"input": "5\n1 2 1 2 1", "output": "3\n2 2 1"}]
800
["implementation"]
16
[{"input": "7\r\n1 2 3 1 2 3 4\r\n", "output": "2\r\n3 4 "}, {"input": "4\r\n1 1 1 1\r\n", "output": "4\r\n1 1 1 1 "}, {"input": "5\r\n1 2 3 4 5\r\n", "output": "1\r\n5 "}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "3\r\n2 2 1 "}, {"input": "1\r\n1\r\n", "output": "1\r\n1 "}, {"input": "48\r\n1 2 3 4 1 2 3 1 1 2 3 1 2 ...
false
stdio
null
true
599/C
599
C
Python 3
TESTS
4
31
0
174872612
l=int(input()) b=[int(i) for i in input().split(' ')] dp=list() for i in range(l): dp.append(1) maxx=b[0] minn=0 for i in range(1,l): if maxx<=b[i]: maxx=b[i] dp[i]+=dp[i-1] if b[i] == b[minn]: minn=i else: if b[i]<b[minn]: minn=i conti...
39
124
9,113,600
176456731
n, s, v = int(input()), 0, 0 a = list(map(int, input().split())) for ai, bi in zip(a, sorted(a)): s += bi - ai v += not s print(v)
Codeforces Round 332 (Div. 2)
CF
2,015
2
256
Day at the Beach
One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles. At the end of the day there were n castles built by friends. Castles are numbered from 1 to n, and the heig...
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of castles Spongebob, Patrick and Squidward made from sand during the day. The next line contains n integers hi (1 ≤ hi ≤ 109). The i-th of these integers corresponds to the height of the i-th castle.
Print the maximum possible number of blocks in a valid partitioning.
null
In the first sample the partitioning looks like that: [1][2][3]. In the second sample the partitioning is: [2, 1][3, 2]
[{"input": "3\n1 2 3", "output": "3"}, {"input": "4\n2 1 3 2", "output": "2"}]
1,600
["sortings"]
39
[{"input": "3\r\n1 2 3\r\n", "output": "3\r\n"}, {"input": "4\r\n2 1 3 2\r\n", "output": "2\r\n"}, {"input": "17\r\n1 45 22 39 28 23 23 100 500 778 777 778 1001 1002 1005 1003 1005\r\n", "output": "10\r\n"}, {"input": "101\r\n1 50 170 148 214 153 132 234 181 188 180 225 226 200 197 122 181 168 87 220 223 160 235 94 257...
false
stdio
null
true
952/E
952
E
PyPy 3
TESTS
2
124
0
64529058
n = int(input()) t = [i * i for i in range(100)] mi = 0 for _ in range(n): tmp = input().split()[1] if tmp == 'soft': mi += 1 mi = min(mi,n - mi) max = max(mi,n - mi) if max == 1: print(2) for i in range(99): if max > t[i] // 2 + 1 and max <= t[i + 1] // 2 + 1: print(i + 1)
40
78
7,065,600
36818088
# python3 def main(): n = int(input()) hard, soft = 0, 0 while n: n -= 1 if input().split()[1] == "hard": hard += 1 else: soft += 1 if hard < soft: hard, soft = soft, hard assert soft <= hard side = 1 while side ** 2 / 2 < soft or ...
April Fools Contest 2018
ICPC
2,018
1
256
Cheese Board
Not to be confused with chessboard.
The first line of input contains a single integer N (1 ≤ N ≤ 100) — the number of cheeses you have. The next N lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. Th...
Output a single number.
null
null
[{"input": "9\nbrie soft\ncamembert soft\nfeta soft\ngoat soft\nmuenster soft\nasiago hard\ncheddar hard\ngouda hard\nswiss hard", "output": "3"}, {"input": "6\nparmesan hard\nemmental hard\nedam hard\ncolby hard\ngruyere hard\nasiago hard", "output": "4"}]
2,000
[]
40
[{"input": "9\r\nbrie soft\r\ncamembert soft\r\nfeta soft\r\ngoat soft\r\nmuenster soft\r\nasiago hard\r\ncheddar hard\r\ngouda hard\r\nswiss hard\r\n", "output": "3\r\n"}, {"input": "6\r\nparmesan hard\r\nemmental hard\r\nedam hard\r\ncolby hard\r\ngruyere hard\r\nasiago hard\r\n", "output": "4\r\n"}, {"input": "9\r\n...
false
stdio
null
true
686/B
686
B
PyPy 3-64
TESTS
5
93
4,915,200
220423847
from collections import defaultdict n = int(input()) a = list(map(int, input().split())) ans = [] for i in range(n): for j in range(i+1, n): if a[j] < a[j-1]: a[j], a[j-1] = a[j-1],a[j] ans.append([j, j+1]) for j in ans: print(*j)
37
46
102,400
218755151
# LUOGU_RID: 120814999 n = int(input()) a = list(map(int, input().split())) for i in range(1, n): for j in range(n - 1, i - 1, -1): if a[j] < a[j - 1]: a[j], a[j - 1] = a[j - 1], a[j] print(j, j + 1)
Codeforces Round 359 (Div. 2)
CF
2,016
2
256
Little Robber Girl's Zoo
Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places. The robber girl was angry at first, but then she decided to arrange the animals herself. She ...
The first line contains a single integer n (1 ≤ n ≤ 100) — number of animals in the robber girl's zoo. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the height of the animal occupying the i-th place.
Print the sequence of operations that will rearrange the animals by non-decreasing height. The output should contain several lines, i-th of the lines should contain two space-separated integers li and ri (1 ≤ li < ri ≤ n) — descriptions of segments the robber girl should name. The segments should be described in the o...
null
Note that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed.
[{"input": "4\n2 1 4 3", "output": "1 4"}, {"input": "7\n36 28 57 39 66 69 68", "output": "1 4\n6 7"}, {"input": "5\n1 2 1 2 1", "output": "2 5\n3 4\n1 4\n1 4"}]
1,100
["constructive algorithms", "implementation", "sortings"]
37
[{"input": "4\r\n2 1 4 3\r\n", "output": "1 2\r\n3 4\r\n"}, {"input": "7\r\n36 28 57 39 66 69 68\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n"}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "2 3\r\n4 5\r\n3 4\r\n"}, {"input": "78\r\n7 3 8 8 9 8 10 9 12 11 16 14 17 17 18 18 20 20 25 22 27 26 29 27 35 35 36 36 37 37 38 38 40 39 4...
false
stdio
import sys def main(): input_path = sys.argv[1] correct_output_path = sys.argv[2] submission_output_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline()) a = list(map(int, f.readline().split())) with open(submission_output_path, 'r') as f: lines = f...
true
659/D
659
D
Python 3
TESTS
7
62
307,200
108504820
pontos = int(input()) x = [] y = [] cont = 0 for i in range(pontos+1): ponto = input().split(' ') x.append(int(ponto[0])) y.append((ponto[1])) for i in range(1, pontos-1): if (x[i-1] < x[i]) and (y[i] < y[i+1]): cont += 1 # LESTE -> NORTE elif (y[i-1] < y[i]) and (x[i] > x[i+1]): cont += 1 # ...
22
31
0
146883382
n=int(input()) n=n-4 n=n//2 print(n)
Codeforces Round 346 (Div. 2)
CF
2,016
1
256
Bicycle Race
Maria participates in a bicycle race. The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west. Let's introduce a system of coordinates, directing the Ox axis from west to east, and th...
The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track. The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the poin...
Print a single integer — the number of dangerous turns on the track.
null
The first sample corresponds to the picture: The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1.
[{"input": "6\n0 0\n0 1\n1 1\n1 2\n2 2\n2 0\n0 0", "output": "1"}, {"input": "16\n1 1\n1 5\n3 5\n3 7\n2 7\n2 9\n6 9\n6 7\n5 7\n5 3\n4 3\n4 4\n3 4\n3 2\n5 2\n5 1\n1 1", "output": "6"}]
1,500
["geometry", "implementation", "math"]
22
[{"input": "6\r\n0 0\r\n0 1\r\n1 1\r\n1 2\r\n2 2\r\n2 0\r\n0 0\r\n", "output": "1\r\n"}, {"input": "16\r\n1 1\r\n1 5\r\n3 5\r\n3 7\r\n2 7\r\n2 9\r\n6 9\r\n6 7\r\n5 7\r\n5 3\r\n4 3\r\n4 4\r\n3 4\r\n3 2\r\n5 2\r\n5 1\r\n1 1\r\n", "output": "6\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -1...
false
stdio
null
true
510/C
510
C
Python 3
TESTS
12
61
921,600
205725348
#User function Template for python3 from collections import defaultdict, deque from string import ascii_lowercase class Solution: def findOrder(self,alien_dict): # code here indegree = defaultdict(int) graph = defaultdict(set) order = [] for lett in ascii_lowercase...
36
62
0
202736813
n=int(input()) prev=None ans=False nodes={} for i in "abcdefghijklmnopqrstuvwxyz": nodes[i]=set() for i in range(n): name=input() if prev: for x,y in zip(name,prev): if x!=y: nodes[x].add(y) break else: if prev>name: ...
Codeforces Round 290 (Div. 2)
CF
2,015
2
256
Fox And Names
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in le...
The first line contains an integer n (1 ≤ n ≤ 100): number of names. Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word "Impossible" (without quotes).
null
null
[{"input": "3\nrivest\nshamir\nadleman", "output": "bcdefghijklmnopqrsatuvwxyz"}, {"input": "10\ntourist\npetr\nwjmzbmr\nyeputons\nvepifanov\nscottwu\noooooooooooooooo\nsubscriber\nrowdark\ntankengineer", "output": "Impossible"}, {"input": "10\npetr\negor\nendagorion\nfeferivan\nilovetanyaromanova\nkostka\ndmitriyh\nma...
1,600
["dfs and similar", "graphs", "sortings"]
36
[{"input": "3\r\nrivest\r\nshamir\r\nadleman\r\n", "output": "bcdefghijklmnopqrsatuvwxyz\r\n"}, {"input": "10\r\ntourist\r\npetr\r\nwjmzbmr\r\nyeputons\r\nvepifanov\r\nscottwu\r\noooooooooooooooo\r\nsubscriber\r\nrowdark\r\ntankengineer\r\n", "output": "Impossible\r\n"}, {"input": "10\r\npetr\r\negor\r\nendagorion\r\nf...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) names = [line.strip() for line in f] with open(output_path) as f: ref = f.read().strip() with open(submiss...
true
151/B
151
B
PyPy 3-64
TESTS
0
77
3,481,600
208825259
from bisect import bisect_right import math from queue import PriorityQueue from sys import stdin, stdout import collections input, print = stdin.readline, stdout.write def str_input(): s = input() return s[:len(s)-1] def char_list_input(): s = input() return list(s[:len(s)-1]) def list_input(type...
38
186
7,987,200
190294105
def same(x): return all(x[i] == x[0] for i in range(len(x))) def dec(x): return all(x[i] < x[i-1] for i in range(1, len(x))) def idx(x): return 0 if same(x) else 1 if dec(x) else 2 n = int(input()) name = [0] * n cnt = [[0 for i in range(n)] for j in range(3)] res = [ "If you want to call a taxi, you shou...
Codeforces Round 107 (Div. 2)
CF
2,012
2
256
Phone Numbers
Winters are just damn freezing cold in Nvodsk! That's why a group of n friends prefers to take a taxi, order a pizza and call girls. The phone numbers in the city consist of three pairs of digits (for example, 12-34-56). Each friend has a phonebook of size si (that's the number of phone numbers). We know that taxi numb...
The first line contains an integer n (1 ≤ n ≤ 100) — the number of friends. Then follow n data blocks that describe each friend's phone books. Each block is presented in the following form: first goes the line that contains integer si and string namei (0 ≤ si ≤ 100) — the number of phone numbers in the phone book of t...
In the first line print the phrase "If you want to call a taxi, you should call: ". Then print names of all friends whose phone books contain maximal number of taxi phone numbers. In the second line print the phrase "If you want to order a pizza, you should call: ". Then print names of all friends who have maximal num...
null
In the first sample you are given four friends. Fedorov's phone book contains one taxi number and one pizza delivery number, Melnikov's phone book only has 3 numbers of girls, Rogulenko's one has 6 taxi numbers and one pizza delivery number, Kaluzhin's one contains 2 taxi numbers and one pizza delivery number. Thus, i...
[{"input": "4\n2 Fedorov\n22-22-22\n98-76-54\n3 Melnikov\n75-19-09\n23-45-67\n99-99-98\n7 Rogulenko\n22-22-22\n11-11-11\n33-33-33\n44-44-44\n55-55-55\n66-66-66\n95-43-21\n3 Kaluzhin\n11-11-11\n99-99-99\n98-65-32", "output": "If you want to call a taxi, you should call: Rogulenko.\nIf you want to order a pizza, you shou...
1,200
["implementation", "strings"]
38
[{"input": "4\r\n2 Fedorov\r\n22-22-22\r\n98-76-54\r\n3 Melnikov\r\n75-19-09\r\n23-45-67\r\n99-99-98\r\n7 Rogulenko\r\n22-22-22\r\n11-11-11\r\n33-33-33\r\n44-44-44\r\n55-55-55\r\n66-66-66\r\n95-43-21\r\n3 Kaluzhin\r\n11-11-11\r\n99-99-99\r\n98-65-32\r\n", "output": "If you want to call a taxi, you should call: Rogulenk...
false
stdio
null
true
733/B
733
B
Python 3
TESTS
4
311
2,764,800
82490054
import math n= int(input()) a=[] b=[] l=0 for i in range(n): m,n=map(int,input().split()) a.append(m) b.append(n) res=math.fabs(sum(a)-sum(b)) for j in range(n): t=a[i]-b[i] res1=math.fabs((sum(a)-t)-(sum(b)+t)) if(res1>res): l=i+1 res=max(res,res1) print(l)
40
280
15,360,000
105281662
from sys import stdin stdin.readline def mp(): return list(map(int, stdin.readline().strip().split())) def it():return int(stdin.readline().strip()) n=it() v=[] sa=sb=diff=dif=0 for _ in range(n): a,b=mp() sa+=a sb+=b v+=[[a,b]] dif=max(diff,abs(a-b)) # print(dif) t=[] for i in range(n): diff=abs((sa-v[i][0]+v[i...
Codeforces Round 378 (Div. 2)
CF
2,016
1
256
Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits beg...
The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them.
null
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third co...
[{"input": "3\n5 6\n8 9\n10 3", "output": "3"}, {"input": "2\n6 5\n5 6", "output": "1"}, {"input": "6\n5 9\n1 3\n4 8\n4 5\n23 54\n12 32", "output": "0"}]
1,100
["math"]
40
[{"input": "3\r\n5 6\r\n8 9\r\n10 3\r\n", "output": "3\r\n"}, {"input": "2\r\n6 5\r\n5 6\r\n", "output": "1\r\n"}, {"input": "6\r\n5 9\r\n1 3\r\n4 8\r\n4 5\r\n23 54\r\n12 32\r\n", "output": "0\r\n"}, {"input": "2\r\n500 499\r\n500 500\r\n", "output": "0\r\n"}, {"input": "1\r\n139 252\r\n", "output": "0\r\n"}, {"input":...
false
stdio
null
true
1005/A
1005
A
Python 3
TESTS
7
46
102,400
151289346
from collections import defaultdict, deque import sys input = lambda : sys.stdin.readline().strip() n = int(input()) a = list(map(int,input().split())) l = [] count = 0 if n == 48: print(20) print(4,3,1,3,4,1,4,4,4,1,2,2,2,1,2,2,3,2,2,1) else: for i in range(len(a)-1,-1,-1): if a[i] == 1: ...
16
31
0
155149755
n = int(input()) S = list(map(int, input().split(' '))) stairs = [] for i, s in enumerate(S): if s == 1: stairs.append(i) stairs_count = len(stairs) results = [] if stairs_count > 1: stairs.append(len(S)) for i in range(len(stairs) - 1): results.append(stairs[i + 1] - stairs[i]) else: ...
Codeforces Round 496 (Div. 3)
ICPC
2,018
1
256
Tanya and Stairways
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $$$1$$$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $$$3$$$ steps, and the second conta...
The first line contains $$$n$$$ ($$$1 \le n \le 1000$$$) — the total number of numbers pronounced by Tanya. The second line contains integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 1000$$$) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passin...
In the first line, output $$$t$$$ — the number of stairways that Tanya climbed. In the second line, output $$$t$$$ numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
null
null
[{"input": "7\n1 2 3 1 2 3 4", "output": "2\n3 4"}, {"input": "4\n1 1 1 1", "output": "4\n1 1 1 1"}, {"input": "5\n1 2 3 4 5", "output": "1\n5"}, {"input": "5\n1 2 1 2 1", "output": "3\n2 2 1"}]
800
["implementation"]
16
[{"input": "7\r\n1 2 3 1 2 3 4\r\n", "output": "2\r\n3 4 "}, {"input": "4\r\n1 1 1 1\r\n", "output": "4\r\n1 1 1 1 "}, {"input": "5\r\n1 2 3 4 5\r\n", "output": "1\r\n5 "}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "3\r\n2 2 1 "}, {"input": "1\r\n1\r\n", "output": "1\r\n1 "}, {"input": "48\r\n1 2 3 4 1 2 3 1 1 2 3 1 2 ...
false
stdio
null
true
621/E
621
E
PyPy 3
TESTS
3
140
4,608,000
103916915
import os from io import BytesIO #input = BytesIO(os.read(0, os.fstat(0).st_size)).readline def main(): mod = 10**9 + 7 n,b,k,x = map(int,input().split()) ai = list(map(int,input().split())) ai2 = [0]*x for i in range(n): ai2[ai[i] % x] += 1 def mult(m1,m2): ans = ...
114
171
4,915,200
31962479
f = lambda: map(int, input().split()) m = 1000000007 n, b, k, x = f() s = [0] * x for q in f(): s[q % x] += 1 def g(t, d): if not t: return s p = [0] * x for i, a in enumerate(t): for j, b in enumerate(s): p[(i + d * j) % x] += a * b return [q % m for q in p] t = [] u, v = 1, 10 wh...
Codeforces Round 341 (Div. 2)
CF
2,016
2
256
Wet Shark and Blocks
There are b blocks of digits. Each one consisting of the same n digits, which are given to you in the input. Wet Shark must choose exactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit 1 from the first block and digit 2 from the second...
The first line of the input contains four space-separated integers, n, b, k and x (2 ≤ n ≤ 50 000, 1 ≤ b ≤ 109, 0 ≤ k < x ≤ 100, x ≥ 2) — the number of digits in one block, the number of blocks, interesting remainder modulo x and modulo x itself. The next line contains n space separated integers ai (1 ≤ ai ≤ 9), that ...
Print the number of ways to pick exactly one digit from each blocks, such that the resulting integer equals k modulo x.
null
In the second sample possible integers are 22, 26, 62 and 66. None of them gives the remainder 1 modulo 2. In the third sample integers 11, 13, 21, 23, 31 and 33 have remainder 1 modulo 2. There is exactly one way to obtain each of these integers, so the total answer is 6.
[{"input": "12 1 5 10\n3 5 6 7 8 9 5 1 1 1 1 5", "output": "3"}, {"input": "3 2 1 2\n6 2 2", "output": "0"}, {"input": "3 2 1 2\n3 1 2", "output": "6"}]
2,000
["dp", "matrices"]
114
[{"input": "12 1 5 10\r\n3 5 6 7 8 9 5 1 1 1 1 5\r\n", "output": "3\r\n"}, {"input": "3 2 1 2\r\n6 2 2\r\n", "output": "0\r\n"}, {"input": "3 2 1 2\r\n3 1 2\r\n", "output": "6\r\n"}, {"input": "3 2 1 2\r\n6 3 2\r\n", "output": "3\r\n"}, {"input": "3 2 1 2\r\n3 6 3\r\n", "output": "6\r\n"}, {"input": "3 2 0 2\r\n3 3 9\r...
false
stdio
null
true
270/B
270
B
Python 3
TESTS
3
62
0
225897527
n=int(input()) a=[int(x) for x in map(int, input().split())] k=0 for i in range(n-1): if a[i]>a[i+1]: k+=1 print(k)
41
280
10,444,800
225857008
n = int(input()) a = list(map(int,input().split())) ans = 0 for i in range(n-1, 0, -1): if a[i] < a[i-1]: break else: ans += 1 print(n-ans-1)
Codeforces Round 165 (Div. 2)
CF
2,013
2
256
Multithreading
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread....
The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output a single integer — the number of threads that surely contain a new message.
null
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the thi...
[{"input": "5\n5 2 1 3 4", "output": "2"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "4\n4 3 2 1", "output": "3"}]
1,400
["data structures", "greedy", "implementation"]
41
[{"input": "5\r\n5 2 1 3 4\r\n", "output": "2\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "0\r\n"}, {"input": "4\r\n4 3 2 1\r\n", "output": "3\r\n"}, {"input": "5\r\n1 2 5 3 4\r\n", "output": "3\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0\r\n"}, {"input": "2\r\n2 1\r\n", "...
false
stdio
null
true
596/C
596
C
Python 3
TESTS
2
46
0
14307130
n = int(input()) max_y = {} for i in range(n): x, y = map(int, input().split()) key = y - x if key not in max_y or y > max_y[key]: max_y[key] = y curr_y = { key: max(key, 0) for key in max_y } result = [] for key in map(int, input().split()): if key not in curr_y or curr_y[key] > max_y[key]: ...
76
904
27,955,200
49770528
from itertools import combinations,permutations from collections import defaultdict def zeror(): return 0 def solution(n,c,cw,w): c.sort(key=lambda tup:tup[0]) #print(c) for k,v in cw.items(): cw[k]=sorted(v,key=lambda tup:tup[0])[::-1] #print(cw) ans=[] for elem in w: ...
Codeforces Round 331 (Div. 2)
CF
2,015
2
256
Wilbur and Points
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is ...
The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are dis...
If there exists an aesthetically pleasant numbering of points in the set, such that s(xi, yi) = yi - xi = wi, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there ar...
null
In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and yi - xi = wi. In the second sample, the special values of the points in the set ar...
[{"input": "5\n2 0\n0 0\n1 0\n1 1\n0 1\n0 -1 -2 1 0", "output": "YES\n0 0\n1 0\n2 0\n0 1\n1 1"}, {"input": "3\n1 0\n0 0\n2 0\n0 1 2", "output": "NO"}]
1,700
["combinatorics", "greedy", "sortings"]
76
[{"input": "5\r\n2 0\r\n0 0\r\n1 0\r\n1 1\r\n0 1\r\n0 -1 -2 1 0\r\n", "output": "YES\r\n0 0\r\n1 0\r\n2 0\r\n0 1\r\n1 1\r\n"}, {"input": "3\r\n1 0\r\n0 0\r\n2 0\r\n0 1 2\r\n", "output": "NO\r\n"}, {"input": "9\r\n0 0\r\n1 0\r\n2 0\r\n0 1\r\n1 1\r\n2 1\r\n1 2\r\n2 2\r\n0 2\r\n0 0 0 -1 -1 -2 1 1 2\r\n", "output": "NO\r\n...
false
stdio
null
true
596/C
596
C
Python 3
PRETESTS
2
46
0
14281815
n = int(input()) s = [] for i in range(n): a, b = map(int, input().split()) s.append([b - a, a, b]) lst = list(map(int, input().split())) for i in range(len(lst)): lst[i] = [lst[i], i] lst.sort() s.sort() out = [[0, 0] for i in range(n)] flag = True for i in range(len(s)): if s[i][0] == lst[i][0]: ...
76
935
29,286,400
91137616
n = int(input()) p = sorted([tuple(map(int, input().split())) for _ in range(n)]) arr = list(map(int, input().split())) w, r, pr = {}, {}, {} for i, wi in enumerate(arr, 1): if wi not in w: w[wi] = [] w[wi].append(i) def is_nbr(nb, i): return 0 if pr.get(nb, 0) > i else 1 def check_nbrs(p, i): ...
Codeforces Round 331 (Div. 2)
CF
2,015
2
256
Wilbur and Points
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is ...
The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are dis...
If there exists an aesthetically pleasant numbering of points in the set, such that s(xi, yi) = yi - xi = wi, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there ar...
null
In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and yi - xi = wi. In the second sample, the special values of the points in the set ar...
[{"input": "5\n2 0\n0 0\n1 0\n1 1\n0 1\n0 -1 -2 1 0", "output": "YES\n0 0\n1 0\n2 0\n0 1\n1 1"}, {"input": "3\n1 0\n0 0\n2 0\n0 1 2", "output": "NO"}]
1,700
["combinatorics", "greedy", "sortings"]
76
[{"input": "5\r\n2 0\r\n0 0\r\n1 0\r\n1 1\r\n0 1\r\n0 -1 -2 1 0\r\n", "output": "YES\r\n0 0\r\n1 0\r\n2 0\r\n0 1\r\n1 1\r\n"}, {"input": "3\r\n1 0\r\n0 0\r\n2 0\r\n0 1 2\r\n", "output": "NO\r\n"}, {"input": "9\r\n0 0\r\n1 0\r\n2 0\r\n0 1\r\n1 1\r\n2 1\r\n1 2\r\n2 2\r\n0 2\r\n0 0 0 -1 -1 -2 1 1 2\r\n", "output": "NO\r\n...
false
stdio
null
true
596/C
596
C
Python 3
TESTS
2
62
5,734,400
34528534
n = int(input()) R = lambda : map(int, input().split()) p = [] w = {} r = {} from collections import deque for _ in range(n): x,y = R() p.append((x,y)) p = sorted(p) for i,wi in enumerate(list(R()),1): if wi not in w: w[wi] = deque() w[wi].append(i) for i in range(len(p)): d = p[i][1]-...
76
935
50,073,600
218384432
def gcd(a, b): if a % b == 0: return b return gcd(b, a % b) ex = 100000 n = int(input()) flag = True pid = {} S = [[] for _ in range(2 * ex + 1)] P = [] for _ in range(n): x, y = map(int, input().split()) P.append((x, y)) P.sort(key=lambda p: p[0] + p[1]) w = list(map(int, input().split())) id =...
Codeforces Round 331 (Div. 2)
CF
2,015
2
256
Wilbur and Points
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is ...
The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are dis...
If there exists an aesthetically pleasant numbering of points in the set, such that s(xi, yi) = yi - xi = wi, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there ar...
null
In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and yi - xi = wi. In the second sample, the special values of the points in the set ar...
[{"input": "5\n2 0\n0 0\n1 0\n1 1\n0 1\n0 -1 -2 1 0", "output": "YES\n0 0\n1 0\n2 0\n0 1\n1 1"}, {"input": "3\n1 0\n0 0\n2 0\n0 1 2", "output": "NO"}]
1,700
["combinatorics", "greedy", "sortings"]
76
[{"input": "5\r\n2 0\r\n0 0\r\n1 0\r\n1 1\r\n0 1\r\n0 -1 -2 1 0\r\n", "output": "YES\r\n0 0\r\n1 0\r\n2 0\r\n0 1\r\n1 1\r\n"}, {"input": "3\r\n1 0\r\n0 0\r\n2 0\r\n0 1 2\r\n", "output": "NO\r\n"}, {"input": "9\r\n0 0\r\n1 0\r\n2 0\r\n0 1\r\n1 1\r\n2 1\r\n1 2\r\n2 2\r\n0 2\r\n0 0 0 -1 -1 -2 1 1 2\r\n", "output": "NO\r\n...
false
stdio
null
true
864/E
864
E
PyPy 3
TESTS
2
202
2,457,600
78266791
import copy n = int(input()) arr = [] m = 0 for er in range(n): temp = list(map(int,input().split(" "))) temp.append(er+1) arr.append(temp) if(temp[1]>m): m = temp[1] arr.sort(key = lambda x:x[1]) ##print(arr) temp=[] for i in range(n): temp.append([0]) com = [] #print(temp) com.append(te...
60
218
819,200
51847323
from sys import stdin, stdout n = int(stdin.readline()) items = [] for i in range(n): t,d,p = map(int,stdin.readline().split()) items.append((d,p,t,i+1)) items.sort() N = 2000+1 dp = [0 for i in range(N)] cur = [[] for i in range(N)] for d,p,t,it in items: for i in range(d-1,t-1,-1): if dp[i-t] + p > dp[i]:...
Codeforces Round 436 (Div. 2)
CF
2,017
2
256
Fire
Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item, he estimated the value of di — the moment after which the item i will be completely burned and will no longer be valuab...
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp's house. Each of the following n lines contains three integers ti, di, pi (1 ≤ ti ≤ 20, 1 ≤ di ≤ 2 000, 1 ≤ pi ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of ite...
In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which t...
null
In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the save...
[{"input": "3\n3 7 4\n2 6 5\n3 7 6", "output": "11\n2\n2 3"}, {"input": "2\n5 6 1\n3 3 5", "output": "1\n1\n1"}]
2,000
["dp", "sortings"]
60
[{"input": "3\r\n3 7 4\r\n2 6 5\r\n3 7 6\r\n", "output": "11\r\n2\r\n2 3 \r\n"}, {"input": "2\r\n5 6 1\r\n3 3 5\r\n", "output": "1\r\n1\r\n1 \r\n"}, {"input": "9\r\n13 18 14\r\n8 59 20\r\n9 51 2\r\n18 32 15\r\n1 70 18\r\n14 81 14\r\n10 88 16\r\n18 52 3\r\n1 50 6\r\n", "output": "106\r\n8\r\n1 4 9 8 2 5 6 7 \r\n"}, {"in...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input with open(input_path, 'r') as f: n = int(f.readline().strip()) items = [] for _ in range(n): ti, di, pi = map(int, f.readline().strip().split()) ...
true
652/B
652
B
PyPy 3-64
TESTS
6
62
0
184531422
#from collections import Counter #h1,h2=map(int,input().split()) #a,b=map(int,input().split()) n=int(input()) a=list(map(int,input().split())) c=0 if n%2==0 and len(set(a))==n: print('Impossible') else: a.sort() s=[] l=round((n//2)+0.1) if n%2==0: mn=a[:l] mx=(a[l:]) else: ...
16
31
0
155219141
from math import ceil n=int(input()) a=sorted(list(map(int,input().split()))) if(n==1): print(a[0]) else: b=a[:ceil(n/2)] c=a[ceil(n/2):] for i in range(n//2): print(b[i],c[i],end=' ') if(n&1): print(b[-1])
Educational Codeforces Round 10
ICPC
2,016
1
256
z-sort
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold: 1. ai ≥ ai - 1 for all even i, 2. ai ≤ ai - 1 for all odd i > 1. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted. Can you make the array z...
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
null
null
[{"input": "4\n1 2 2 1", "output": "1 2 1 2"}, {"input": "5\n1 3 2 2 5", "output": "1 5 2 3 2"}]
1,000
["sortings"]
16
[{"input": "4\r\n1 2 2 1\r\n", "output": "1 2 1 2\r\n"}, {"input": "5\r\n1 3 2 2 5\r\n", "output": "1 5 2 3 2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\n", "output": "1 9 1 8 2 7 3 7 4 6\r\n"...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input with open(input_path) as f: n = int(f.readline().strip()) a = list(map(int, f.readline().split())) a_sorted = sorted(a) # Read reference output with ope...
true
984/A
984
A
Python 3
TESTS
5
30
0
215493290
n = int(input()) arr = [int(s) for s in input().split()] sum = 0 for i in arr: sum += i avg = sum / n best_precision = abs(avg - arr[0]) value = arr[0] for i in arr: precision = abs(avg - i) if precision < best_precision or (precision == best_precision and i < value): best_precision = preci...
35
31
0
151387373
n=int(input()) a=list(map(int,input().split())) n//=2 a.sort() a=a[::-1] print (a[n])
Codeforces Round 483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]
CF
2,018
2
256
Game
Two players play a game. Initially there are $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, t...
The first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of numbers on the board. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
Print one number that will be left on the board.
null
In the first sample, the first player erases $$$3$$$ and the second erases $$$1$$$. $$$2$$$ is left on the board. In the second sample, $$$2$$$ is left on the board regardless of the actions of the players.
[{"input": "3\n2 1 3", "output": "2"}, {"input": "3\n2 2 2", "output": "2"}]
800
["sortings"]
35
[{"input": "3\r\n2 1 3\r\n", "output": "2"}, {"input": "3\r\n2 2 2\r\n", "output": "2"}, {"input": "9\r\n44 53 51 80 5 27 74 79 94\r\n", "output": "53"}, {"input": "10\r\n38 82 23 37 96 4 81 60 67 86\r\n", "output": "60"}, {"input": "10\r\n58 26 77 15 53 81 68 48 22 65\r\n", "output": "53"}, {"input": "1\r\n124\r\n", "...
false
stdio
null
true
324/A1
331
A2
Python 3
TESTS2
9
92
0
193411709
n=int(input()) lis=list(map(int,input().split())) l=0 r=0 maxsum=-10**9 negc=[0]*n possum=[0]*n if lis[0]>=0: possum[0]=lis[0] else: negc[0]+=1 for i in range(1,n): possum[i]+=possum[i-1] negc[i]+=negc[i-1] if lis[i]>=0: possum[i]+=lis[i] else: negc[i]+=1 #print(possum) #print(ne...
18
122
1,945,600
178436137
from collections import defaultdict as dc,Counter as C n=int(input()) arr=list(map(int,input().split())) mp=dc(lambda:list()) for i in range(len(arr)):mp[arr[i]].append(i) st=set(arr) lst=[arr[0]] for i in range(1,len(arr)):lst.append(lst[i-1]+arr[i]) #print(st) #print(lst) #print(mp) fil,fir=0,0 tot=-1e20 notake=set()...
ABBYY Cup 3.0 - Finals
ICPC
2,013
2
256
Oh Sweet Beaverette
— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming ...
The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. - to get 30 points, you need to solve the problem with constrai...
In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k. In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to righ...
null
null
[{"input": "5\n1 2 3 1 2", "output": "8 1\n1"}, {"input": "5\n1 -2 3 1 -2", "output": "5 2\n2 5"}]
1,400
[]
18
[{"input": "5\r\n1 2 3 1 2\r\n", "output": "8 1\r\n1 "}, {"input": "5\r\n1 -2 3 1 -2\r\n", "output": "5 2\r\n2 5 "}, {"input": "2\r\n0 0\r\n", "output": "0 0\r\n"}, {"input": "3\r\n0 -1 0\r\n", "output": "0 1\r\n2 "}, {"input": "3\r\n1 1 1\r\n", "output": "3 0\r\n"}, {"input": "4\r\n-1 1 1 -1\r\n", "output": "2 2\r\n1 ...
false
stdio
import sys from collections import defaultdict def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) with open(submission_output_path) as f: lines = f.readlines() if not lines: ...
true
1005/A
1005
A
PyPy 3
TESTS
6
92
0
165784972
n=int(input());s="" a=input().split() for i in a: s+=i a=s.split('1') print(len(a)-1) for i in range(1,len(a)): print(len(a[i])+1,end=" ")
16
31
0
157871000
t1=1 for _ in range(t1): n=int(input());n=0;b=[];c=n;t='' a=list(map(int,input().split())) for i in a: if i<=c: t+=str(len(b))+' ';b=[];n+=1 b.append(i) c=i t+=str(len(b)) print(n+1) print(t)
Codeforces Round 496 (Div. 3)
ICPC
2,018
1
256
Tanya and Stairways
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $$$1$$$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $$$3$$$ steps, and the second conta...
The first line contains $$$n$$$ ($$$1 \le n \le 1000$$$) — the total number of numbers pronounced by Tanya. The second line contains integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 1000$$$) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passin...
In the first line, output $$$t$$$ — the number of stairways that Tanya climbed. In the second line, output $$$t$$$ numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
null
null
[{"input": "7\n1 2 3 1 2 3 4", "output": "2\n3 4"}, {"input": "4\n1 1 1 1", "output": "4\n1 1 1 1"}, {"input": "5\n1 2 3 4 5", "output": "1\n5"}, {"input": "5\n1 2 1 2 1", "output": "3\n2 2 1"}]
800
["implementation"]
16
[{"input": "7\r\n1 2 3 1 2 3 4\r\n", "output": "2\r\n3 4 "}, {"input": "4\r\n1 1 1 1\r\n", "output": "4\r\n1 1 1 1 "}, {"input": "5\r\n1 2 3 4 5\r\n", "output": "1\r\n5 "}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "3\r\n2 2 1 "}, {"input": "1\r\n1\r\n", "output": "1\r\n1 "}, {"input": "48\r\n1 2 3 4 1 2 3 1 1 2 3 1 2 ...
false
stdio
null
true
1005/A
1005
A
PyPy 3-64
TESTS
6
93
512,000
140424725
n=int(input()) ar=[str(x) for x in input().split()] c=0 for i in range(len(ar)): if(ar[i]=='1'): c+=1 count=1 cc=[] for j in range(1,len(ar)): if(ar[j]!='1'): count+=1 else: cc+=str(count) count=1 cc+=str(count) ccc=" ".join(cc) print(c) print(ccc)
16
31
0
161472258
n = int(input()) A = input().split() b = '' p = 0 for i in range(n): if A[i] == '1': p += 1 print(p) for j in range(1,n): if A[j] == '1': b += A[j - 1] b += ' ' b += A[n - 1] print(b)
Codeforces Round 496 (Div. 3)
ICPC
2,018
1
256
Tanya and Stairways
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $$$1$$$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $$$3$$$ steps, and the second conta...
The first line contains $$$n$$$ ($$$1 \le n \le 1000$$$) — the total number of numbers pronounced by Tanya. The second line contains integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 1000$$$) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passin...
In the first line, output $$$t$$$ — the number of stairways that Tanya climbed. In the second line, output $$$t$$$ numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
null
null
[{"input": "7\n1 2 3 1 2 3 4", "output": "2\n3 4"}, {"input": "4\n1 1 1 1", "output": "4\n1 1 1 1"}, {"input": "5\n1 2 3 4 5", "output": "1\n5"}, {"input": "5\n1 2 1 2 1", "output": "3\n2 2 1"}]
800
["implementation"]
16
[{"input": "7\r\n1 2 3 1 2 3 4\r\n", "output": "2\r\n3 4 "}, {"input": "4\r\n1 1 1 1\r\n", "output": "4\r\n1 1 1 1 "}, {"input": "5\r\n1 2 3 4 5\r\n", "output": "1\r\n5 "}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "3\r\n2 2 1 "}, {"input": "1\r\n1\r\n", "output": "1\r\n1 "}, {"input": "48\r\n1 2 3 4 1 2 3 1 1 2 3 1 2 ...
false
stdio
null
true
988/F
988
F
PyPy 3-64
TESTS
4
77
3,072,000
228587165
# https://codeforces.com/contest/988 import sys input = lambda: sys.stdin.readline().rstrip() # faster! a, n, m = map(int, input().split()) rain = sorted(tuple(map(int, input().split())) for _ in range(n)) umbr = sorted(tuple(map(int, input().split())) for _ in range(m)) if rain[0][0] < umbr[0][0]: print(-1) ...
37
217
9,420,800
175510407
import sys,bisect from functools import cache read=lambda:map(int,sys.stdin.readline().split()) dst,n,m=read() rain=sorted([[*read()] for _ in range(n)]) umb=sorted([[*read()] for _ in range(m)])+[[dst,dst]] if umb[0][0]>rain[0][0]:quit(print(-1)) @cache def dfs(i): if i==0:return 0 cur=umb[i][0] ridx=bisec...
Codeforces Round 486 (Div. 3)
ICPC
2,018
2
256
Rain and Umbrellas
Polycarp lives on a coordinate line at the point $$$x = 0$$$. He goes to his friend that lives at the point $$$x = a$$$. Polycarp can move only from left to right, he can pass one unit of length each second. Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $$$n$$$ non-intersecti...
The first line contains three integers $$$a$$$, $$$n$$$ and $$$m$$$ ($$$1 \le a, m \le 2000, 1 \le n \le \lceil\frac{a}{2}\rceil$$$) — the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas. Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_...
Print "-1" (without quotes) if Polycarp can't make his way from point $$$x = 0$$$ to point $$$x = a$$$. Otherwise print one integer — the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally.
null
In the first example the only possible strategy is to take the fourth umbrella at the point $$$x = 1$$$, keep it till the point $$$x = 7$$$ (the total fatigue at $$$x = 7$$$ will be equal to $$$12$$$), throw it away, move on from $$$x = 7$$$ to $$$x = 8$$$ without an umbrella, take the third umbrella at $$$x = 8$$$ and...
[{"input": "10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2", "output": "14"}, {"input": "10 1 1\n0 9\n0 5", "output": "45"}, {"input": "10 1 1\n0 9\n1 5", "output": "-1"}]
2,100
["dp"]
37
[{"input": "10 2 4\r\n3 7\r\n8 10\r\n0 10\r\n3 4\r\n8 1\r\n1 2\r\n", "output": "14\r\n"}, {"input": "10 1 1\r\n0 9\r\n0 5\r\n", "output": "45\r\n"}, {"input": "10 1 1\r\n0 9\r\n1 5\r\n", "output": "-1\r\n"}, {"input": "1 1 1\r\n0 1\r\n1 100000\r\n", "output": "-1\r\n"}, {"input": "1 1 1\r\n0 1\r\n0 100000\r\n", "output...
false
stdio
null
true
505/C
505
C
Python 3
TESTS
4
732
68,915,200
79234632
import bisect import os, sys, atexit,threading from io import BytesIO, StringIO input = BytesIO(os.read(0, os.fstat(0).st_size)).readline _OUTPUT_BUFFER = StringIO() sys.stdout = _OUTPUT_BUFFER @atexit.register def write(): sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) def calculate(i,l): global maxGem,o...
54
795
134,758,400
164658651
import bisect import collections import math import os import sys # input = sys.stdin.buffer.readline # t = int(input()) # n, m = map(int, input().split()) # A = list(map(int, input().split())) # grid = [list(map(int, input().split())) for _ in range(n)] n, d = map(int, input().split()) dp = [[-1]*501 for _ in range...
Codeforces Round 286 (Div. 2)
CF
2,015
1
256
Mr. Kitayuta, the Treasure Hunter
The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from 0 to 30000 from the west to the east. These islands are known to contain many treasures. There are n gems in the Shuseki Islands in total, and the i-th gem is located on island pi....
The first line of the input contains two space-separated integers n and d (1 ≤ n, d ≤ 30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively. The next n lines describe the location of the gems. The i-th of them (1 ≤ i ≤ n) contains a integer pi (d ≤...
Print the maximum number of gems that Mr. Kitayuta can collect.
null
In the first sample, the optimal route is 0 → 10 (+1 gem) → 19 → 27 (+2 gems) → ... In the second sample, the optimal route is 0 → 8 → 15 → 21 → 28 (+1 gem) → 36 (+1 gem) → 45 (+1 gem) → 55 (+1 gem) → 66 (+1 gem) → 78 (+1 gem) → ... In the third sample, the optimal route is 0 → 7 → 13 →...
[{"input": "4 10\n10\n21\n27\n27", "output": "3"}, {"input": "8 8\n9\n19\n28\n36\n45\n55\n66\n78", "output": "6"}, {"input": "13 7\n8\n8\n9\n16\n17\n17\n18\n21\n23\n24\n24\n26\n30", "output": "4"}]
1,900
["dfs and similar", "dp", "two pointers"]
54
[{"input": "4 10\r\n10\r\n21\r\n27\r\n27\r\n", "output": "3\r\n"}, {"input": "8 8\r\n9\r\n19\r\n28\r\n36\r\n45\r\n55\r\n66\r\n78\r\n", "output": "6\r\n"}, {"input": "13 7\r\n8\r\n8\r\n9\r\n16\r\n17\r\n17\r\n18\r\n21\r\n23\r\n24\r\n24\r\n26\r\n30\r\n", "output": "4\r\n"}, {"input": "8 4\r\n9\r\n15\r\n15\r\n16\r\n22\r\n2...
false
stdio
null
true
510/C
510
C
PyPy 3-64
TESTS
12
62
614,400
206012116
from collections import defaultdict, deque startNodes = [] n = int(input()) graphs = defaultdict(list) degree = defaultdict(int) names = [] for i in range(n): names.append(input()) impossible = False for i in range(n -1): l = 0 while l < len(names[i]) and l < len(names[i+1]) and names[i][l]...
36
62
307,200
9685499
n = int(input()) names = [""] * n l = [0] * n for i in range(n): names[i] = input().strip() l[i] = len(names[i]) relations = [0] * n proc = True res = True rels = [[0] * 26] * 26 for i in range(1, 26): rels[i] = list(rels[0]) j = 0 while proc and res: proc = False for i in range(n-1): ...
Codeforces Round 290 (Div. 2)
CF
2,015
2
256
Fox And Names
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in le...
The first line contains an integer n (1 ≤ n ≤ 100): number of names. Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word "Impossible" (without quotes).
null
null
[{"input": "3\nrivest\nshamir\nadleman", "output": "bcdefghijklmnopqrsatuvwxyz"}, {"input": "10\ntourist\npetr\nwjmzbmr\nyeputons\nvepifanov\nscottwu\noooooooooooooooo\nsubscriber\nrowdark\ntankengineer", "output": "Impossible"}, {"input": "10\npetr\negor\nendagorion\nfeferivan\nilovetanyaromanova\nkostka\ndmitriyh\nma...
1,600
["dfs and similar", "graphs", "sortings"]
36
[{"input": "3\r\nrivest\r\nshamir\r\nadleman\r\n", "output": "bcdefghijklmnopqrsatuvwxyz\r\n"}, {"input": "10\r\ntourist\r\npetr\r\nwjmzbmr\r\nyeputons\r\nvepifanov\r\nscottwu\r\noooooooooooooooo\r\nsubscriber\r\nrowdark\r\ntankengineer\r\n", "output": "Impossible\r\n"}, {"input": "10\r\npetr\r\negor\r\nendagorion\r\nf...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) names = [line.strip() for line in f] with open(output_path) as f: ref = f.read().strip() with open(submiss...
true
51/B
51
B
Python 3
TESTS
11
280
7,475,200
88469349
from sys import stdin import re fef,ans,ind,tuck = [],[],0,0 for sus in stdin.readlines(): fef += sus[:] fefs = "".join(filter(lambda x: x!='\n',fef)) fuf = re.split(r'><',fefs); cluk = [0]*(fuf.count("table")+1) for fef in fuf: if fef == 'table': ind +=1 ; tuck +=1; ind = tuck if tuck > ind else ind elif ...
19
124
0
116281769
import sys R=str.replace s=R(R(R(R(R(''.join(sys.stdin.readlines()),'\n',''),'</tr>',''),'<tr>',''),'><',' '),'>','').split()[1:] def f(k): r=0;a=[] while s[0]!='/table': if s[0]=='table': s.pop(0);a+=f(k+1) else:r+=s[0]=='td';s.pop(0) s.pop(0) return[r]+a print(' '.join(map(str,sorted(f(1)))))
Codeforces Beta Round 48
CF
2,010
2
256
bHTML Tables Analisys
In this problem is used an extremely simplified version of HTML table markup. Please use the statement as a formal document and read it carefully. A string is a bHTML table, if it satisfies the grammar: Blanks in the grammar are only for purposes of illustration, in the given data there will be no spaces. The bHTML t...
For convenience, input data can be separated into non-empty lines in an arbitrary manner. The input data consist of no more than 10 lines. Combine (concatenate) all the input lines into one, to get a text representation s of the specified table. String s corresponds to the given grammar (the root element of grammar is ...
Print the sizes of all the tables in the non-decreasing order.
null
null
[{"input": "<table><tr><td></td></tr></table>", "output": "1"}, {"input": "<table>\n<tr>\n<td>\n<table><tr><td></td></tr><tr><td></\ntd\n></tr><tr\n><td></td></tr><tr><td></td></tr></table>\n</td>\n</tr>\n</table>", "output": "1 4"}, {"input": "<table><tr><td>\n<table><tr><td>\n<table><tr><td>\n<table><tr><td></td><td>...
1,700
["expression parsing"]
19
[{"input": "<table><tr><td></td></tr></table>\r\n", "output": "1 "}, {"input": "<table>\r\n<tr>\r\n<td>\r\n<table><tr><td></td></tr><tr><td></\r\ntd\r\n></tr><tr\r\n><td></td></tr><tr><td></td></tr></table>\r\n</td>\r\n</tr>\r\n</table>\r\n", "output": "1 4 "}, {"input": "<table><tr><td>\r\n<table><tr><td>\r\n<table><t...
false
stdio
null
true
351/E
351
E
Python 3
TESTS
1
62
0
228759415
def count_inversions(seq): n = len(seq) inv_count = 0 for i in range(n): for j in range(i + 1, n): if seq[i] > seq[j]: inv_count += 1 return inv_count def minimize_inversions(seq): n = len(seq) min_inversions = count_inversions(seq) for i in range(n): ...
36
280
3,686,400
150804902
import sys input = sys.stdin.buffer.readline def process(A): n = len(A) S = [1 for i in range(n)] d = {} for i in range(n): ai = abs(A[i]) if ai not in d: d[ai] = [] d[ai].append(i) L = sorted(d) answer = 0 while len(L) > 0: ai = L.pop() f...
Codeforces Round 204 (Div. 1)
CF
2,013
2
256
Jeff and Permutation
Jeff's friends know full well that the boy likes to get sequences and arrays for his birthday. Thus, Jeff got sequence p1, p2, ..., pn for his birthday. Jeff hates inversions in sequences. An inversion in sequence a1, a2, ..., an is a pair of indexes i, j (1 ≤ i < j ≤ n), such that an inequality ai > aj holds. Jeff c...
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains n integers — sequence p1, p2, ..., pn (|pi| ≤ 105). The numbers are separated by spaces.
In a single line print the answer to the problem — the minimum number of inversions Jeff can get.
null
null
[{"input": "2\n2 1", "output": "0"}, {"input": "9\n-2 0 -1 0 -1 2 1 0 -1", "output": "6"}]
2,200
["greedy"]
36
[{"input": "2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "9\r\n-2 0 -1 0 -1 2 1 0 -1\r\n", "output": "6\r\n"}, {"input": "9\r\n0 0 1 1 0 0 1 0 1\r\n", "output": "5\r\n"}, {"input": "8\r\n0 1 2 -1 -2 1 -2 2\r\n", "output": "3\r\n"}, {"input": "24\r\n-1 -1 2 2 0 -2 2 -1 0 0 2 -2 3 0 2 -3 0 -3 -1 1 0 0 -1 -2\r\n", "output...
false
stdio
null
true
595/A
595
A
Python 3
TESTS
3
109
0
47458371
n,m = map(int, input().split()) f = [list(map(int, input().split())) for _ in range(n)] c = 0 s = len(f[0])//m for i in f: for j in range(s+1): if sum(i[j*s:(j+1)*s]) >= 1: c+=1 print(c)
36
46
0
169234191
n,m = list(map(int, input().split())) cnt=0 for i in range(n): lst = list(map(int, input().split())) for j in range(0,2*m,2): if lst[j]==1 or lst[j+1]==1: cnt+=1 print(cnt)
Codeforces Round 330 (Div. 2)
CF
2,015
1
256
Vitaly and Night
One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment. Vitaly sees a building of n floors and 2·m windows on each floor. On each floor there are m flats numbered ...
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100) — the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2·m characters each. If the i-th window of the given floor has lights on, then the i-th char...
Print a single integer — the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping.
null
In the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off. In the second test case the house has one floor and the first floor has three flats. The ligh...
[{"input": "2 2\n0 0 0 1\n1 0 1 1", "output": "3"}, {"input": "1 3\n1 1 0 1 0 0", "output": "2"}]
800
["constructive algorithms", "implementation"]
36
[{"input": "2 2\r\n0 0 0 1\r\n1 0 1 1\r\n", "output": "3\r\n"}, {"input": "1 3\r\n1 1 0 1 0 0\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 1 1 1 1 1\r\n1 1 0 1 1 0\r\n1 0 0 0 1 1\r\n", "output": "8\r\n"}, {"input": "1 5\r\n1 0 1 1 1 0 1 1 1 1\r\n", "output": "5\r\n"}, {"input": "1 100\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
false
stdio
null
true
755/B
755
B
Python 3
TESTS
10
140
1,331,200
121739754
n, m = map(int,input().split()) p1 = [] p2 = [] iguais = [] for i in range(n): p = input() p1.append(p) for j in range(m): p = input() p2.append(p) for elem in p1: if elem in p2: iguais.append(elem) if len(iguais) !=0: if len(iguais) % 2 != 0: n += 1 else: ...
33
31
1,331,200
209022691
def read_set(size): result = set() for _ in range(size): result.add(input()) return result def solve(words1, words2): both_count = len(words1.intersection(words2)) only_count1 = len(words1) - both_count only_count2 = len(words2) - both_count return only_count1 + both_count % 2 > onl...
8VC Venture Cup 2017 - Elimination Round
CF
2,017
1
256
PolandBall and Game
PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, i...
The first input line contains two integers n and m (1 ≤ n, m ≤ 103) — number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line — words familiar to PolandBall. Then m strings follow, one per line — words familiar to EnemyBall. Note that one Ball cannot know a word more than onc...
In a single line of print the answer — "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally.
null
In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins.
[{"input": "5 1\npolandball\nis\na\ncool\ncharacter\nnope", "output": "YES"}, {"input": "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska", "output": "YES"}, {"input": "1 2\na\na\nb", "output": "NO"}]
1,100
["binary search", "data structures", "games", "greedy", "sortings", "strings"]
33
[{"input": "5 1\r\npolandball\r\nis\r\na\r\ncool\r\ncharacter\r\nnope\r\n", "output": "YES"}, {"input": "2 2\r\nkremowka\r\nwadowicka\r\nkremowka\r\nwiedenska\r\n", "output": "YES"}, {"input": "1 2\r\na\r\na\r\nb\r\n", "output": "NO"}, {"input": "2 2\r\na\r\nb\r\nb\r\nc\r\n", "output": "YES"}, {"input": "2 1\r\nc\r\na\...
false
stdio
null
true
95/A
95
A
Python 3
TESTS
5
218
307,200
97594542
pya = int(input()) arre = [] while pya: pya -= 1 arre.append(input().lower()) oString = input() lowString = oString.lower() letter1 = input()[0].lower() letter2 = 'a' if letter1.lower() else 'b' valid = [0 for i in range(len(oString))] setcito = set() for x in arre: if lowString.find(x): wat =...
49
124
0
199296340
n= int(input()) b = [] for i in range(n): b.append(input()) w = input() let = input() bad=[] for i in range(len(w)): bad.append(True) for i in range(len(w)): mx = 0 for j in range(n): if w[i:].lower().startswith(b[j].lower()): mx =max(mx,len(b[j])) for j in range(mx): bad...
Codeforces Beta Round 77 (Div. 1 Only)
CF
2,011
2
256
Hockey
Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name w and the collection of forbidden substrings s1, s2, ..., sn. All those strings consist of uppercase and lowercase Latin ...
The first line contains the only integer n (1 ≤ n ≤ 100) — the number of forbidden substrings in the collection. Next n lines contain these substrings. The next line contains string w. All those n + 1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The l...
Output the only line — Petya's resulting string with the maximum number of letters letter. If there are several answers then output the one that comes first lexicographically. The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller...
null
null
[{"input": "3\nbers\nucky\nelu\nPetrLoveLuckyNumbers\nt", "output": "PetrLovtTttttNumtttt"}, {"input": "4\nhello\nparty\nabefglghjdhfgj\nIVan\npetrsmatchwin\na", "output": "petrsmatchwin"}, {"input": "2\naCa\ncba\nabAcaba\nc", "output": "abCacba"}]
1,600
["implementation", "strings"]
49
[{"input": "3\r\nbers\r\nucky\r\nelu\r\nPetrLoveLuckyNumbers\r\nt\r\n", "output": "PetrLovtTttttNumtttt\r\n"}, {"input": "4\r\nhello\r\nparty\r\nabefglghjdhfgj\r\nIVan\r\npetrsmatchwin\r\na\r\n", "output": "petrsmatchwin\r\n"}, {"input": "2\r\naCa\r\ncba\r\nabAcaba\r\nc\r\n", "output": "abCacba\r\n"}, {"input": "3\r\nl...
false
stdio
null
true
863/F
863
F
PyPy 3
TESTS
0
46
0
151283334
def process(n, Q): maxes = [n for i in range(n+1)] mins = [1 for i in range(n+1)] for t, l, r, v in Q: if t==1: for i in range(l, r+1): mins[i] = max(mins[i], v) else: for i in range(l, r+1): maxes[i] = min(maxes[i], v) L = [[] for...
45
311
2,048,000
45981555
#~ # MAGIC CODEFORCES PYTHON FAST IO import atexit import io import sys _INPUT_LINES = sys.stdin.read().splitlines() input = iter(_INPUT_LINES).__next__ _OUTPUT_BUFFER = io.StringIO() sys.stdout = _OUTPUT_BUFFER @atexit.register def write(): sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) #~ # END OF MAGIC CODEFO...
Educational Codeforces Round 29
ICPC
2,017
3
512
Almost Permutation
Recently Ivan noticed an array a while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug. Ivan clearly remembers that there were n elements in the array, and each element was not less...
The first line contains two integer numbers n and q (1 ≤ n ≤ 50, 0 ≤ q ≤ 100). Then q lines follow, each representing a fact about the array. i-th line contains the numbers ti, li, ri and vi for i-th fact (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ vi ≤ n, ti denotes the type of the fact).
If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible cost of the array.
null
null
[{"input": "3 0", "output": "3"}, {"input": "3 1\n1 1 3 2", "output": "5"}, {"input": "3 2\n1 1 3 2\n2 1 3 2", "output": "9"}, {"input": "3 2\n1 1 3 2\n2 1 3 1", "output": "-1"}]
2,200
["flows"]
45
[{"input": "3 0\r\n", "output": "3\r\n"}, {"input": "3 1\r\n1 1 3 2\r\n", "output": "5\r\n"}, {"input": "3 2\r\n1 1 3 2\r\n2 1 3 2\r\n", "output": "9\r\n"}, {"input": "3 2\r\n1 1 3 2\r\n2 1 3 1\r\n", "output": "-1\r\n"}, {"input": "50 0\r\n", "output": "50\r\n"}, {"input": "50 1\r\n2 31 38 25\r\n", "output": "50\r\n"},...
false
stdio
null
true
901/A
901
A
Python 3
TESTS
1
93
307,200
100840210
n = int(input()) n+=1 l = list(map(int,input().split())) ans = 0 for i in range(n-1): if(l[i]>1 and l[i+1]>1): ans = i+1 break else: print("perfect") exit(0) print("ambiguous") prev = 0 now = 0 for i in range(n): for j in range(l[i]): print(prev,end = " ") now+=1 prev...
45
343
21,196,800
33433013
h = int(input()) nodes = [int(x) for x in input().split()] first = [] second = [] mbo = True stillgood = True pn = 0 for nc in nodes: if mbo and nc == 1: first.append(pn) second.append(pn) pn += 1 mbo = False elif mbo and nc != 1: stillgood = False for i in ran...
Codeforces Round 453 (Div. 1)
CF
2,017
2
256
Hashing Trees
Sasha is taking part in a programming competition. In one of the problems she should check if some rooted trees are isomorphic or not. She has never seen this problem before, but, being an experienced participant, she guessed that she should match trees to some sequences and then compare these sequences instead of tree...
The first line contains a single integer h (2 ≤ h ≤ 105) — the height of the tree. The second line contains h + 1 integers — the sequence a0, a1, ..., ah (1 ≤ ai ≤ 2·105). The sum of all ai does not exceed 2·105. It is guaranteed that there is at least one tree matching this sequence.
If there is only one tree matching this sequence, print "perfect". Otherwise print "ambiguous" in the first line. In the second and in the third line print descriptions of two trees in the following format: in one line print $$\sum_{i=0}^{h} a_i$$ integers, the k-th of them should be the parent of vertex k or be equal...
null
The only tree in the first example and the two printed trees from the second example are shown on the picture:
[{"input": "2\n1 1 1", "output": "perfect"}, {"input": "2\n1 2 2", "output": "ambiguous\n0 1 1 3 3\n0 1 1 3 2"}]
1,500
["constructive algorithms", "trees"]
45
[{"input": "2\r\n1 1 1\r\n", "output": "perfect\r\n"}, {"input": "2\r\n1 2 2\r\n", "output": "ambiguous\r\n0 1 1 3 3\r\n0 1 1 3 2\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "perfect\r\n"}, {"input": "10\r\n1 1 1 1 1 2 1 1 1 1 1\r\n", "output": "perfect\r\n"}, {"input": "10\r\n1 1 1 1 2 2 1 1 1 1 1\r\...
false
stdio
import sys def main(input_path, output_path, submission_output_path): with open(input_path) as f: h = int(f.readline().strip()) a = list(map(int, f.readline().split())) with open(submission_output_path) as f: lines = [line.strip() for line in f.readlines() if line.strip()] ...
true
813/D
813
D
PyPy 3
TESTS
7
155
2,662,400
93108330
import sys n = int(input()) + 1 a = [0] + list(map(int, input().split())) mod7 = [x % 7 for x in a] inf = 10**9 next_i = [[[inf]*3 for _ in range(n)] for _ in range(n)] for j in range(n-2, -1, -1): next_i[0][j] = [j+1, j+1, j+1] for i in range(1, j+1): next_i[i][j][0] = j+1 if a[i]-1 == a[j+1] else ne...
35
1,434
104,243,200
112897541
import sys def solve(): n = int(sys.stdin.readline()) a = [0] + [int(i) for i in sys.stdin.readline().split()] dp = [[0]*(n + 1) for i in range(n + 1)] ans = 0 maxnum = [0] * (10**5 + 2) maxmod = [0] * 7 for y in range(n + 1): maxmod = [0] * 7 for ai in a: ...
Educational Codeforces Round 22
ICPC
2,017
2
256
Two Melodies
Alice is a beginner composer and now she is ready to create another masterpiece. And not even the single one but two at the same time! Alice has a sheet with n notes written on it. She wants to take two such non-empty non-intersecting subsequences that both of them form a melody and sum of their lengths is maximal. S...
The first line contains one integer number n (2 ≤ n ≤ 5000). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — notes written on a sheet.
Print maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody.
null
In the first example subsequences [1, 2] and [4, 5] give length 4 in total. In the second example subsequences [62, 48, 49] and [60, 61] give length 5 in total. If you choose subsequence [62, 61] in the first place then the second melody will have maximum length 2, that gives the result of 4, which is not maximal.
[{"input": "4\n1 2 4 5", "output": "4"}, {"input": "6\n62 22 60 61 48 49", "output": "5"}]
2,600
["dp", "flows"]
35
[{"input": "4\r\n1 2 4 5\r\n", "output": "4\r\n"}, {"input": "6\r\n62 22 60 61 48 49\r\n", "output": "5\r\n"}, {"input": "2\r\n1 4\r\n", "output": "2\r\n"}, {"input": "2\r\n5 4\r\n", "output": "2\r\n"}, {"input": "10\r\n9 6 8 5 5 2 8 9 2 2\r\n", "output": "9\r\n"}, {"input": "10\r\n7776 32915 1030 71664 7542 72359 6538...
false
stdio
null
true
644/C
644
C
Python 3
TESTS
22
1,902
46,796,800
16777740
n = int(input()) hosts = dict() paths = dict() names = set() answ = 0 answ_list = [] answ_str = '' path_names = set() for i in range(n): s = input()[7:] j = 0 while j < len(s) and s[j] != '/': j += 1 if j < len(s) and s[j] == '/': name = s[:j] path = s[j:] if name not in...
115
483
44,032,000
41508791
from sys import stdin from collections import defaultdict def main(): s, d = input(), defaultdict(set) for s in stdin.read().splitlines(): i = s.find('/', 8) if i == -1: d[s].add('') else: d[s[:i]].add(s[i:]) e = defaultdict(list) for s, v in d.items(): ...
CROC 2016 - Qualification
CF
2,016
5
256
Hostname Aliases
There are some websites that are accessible through several different addresses. For example, for a long time Codeforces was accessible with two hostnames codeforces.com and codeforces.ru. You are given a list of page addresses being queried. For simplicity we consider all addresses to have the form http://<hostname>[...
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of page queries. Then follow n lines each containing exactly one address. Each address is of the form http://<hostname>[/<path>], where: - <hostname> consists of lowercase English letters and dots, there are no two consecutive dots,...
First print k — the number of groups of server names that correspond to one website. You should count only groups of size greater than one. Next k lines should contain the description of groups, one group per line. For each group print all server names separated by a single space. You are allowed to print both groups ...
null
null
[{"input": "10\nhttp://abacaba.ru/test\nhttp://abacaba.ru/\nhttp://abacaba.com\nhttp://abacaba.com/test\nhttp://abacaba.de/\nhttp://abacaba.ru/test\nhttp://abacaba.de/test\nhttp://abacaba.com/\nhttp://abacaba.com/t\nhttp://abacaba.com/test", "output": "1\nhttp://abacaba.de http://abacaba.ru"}, {"input": "14\nhttp://c\n...
2,100
["*special", "binary search", "data structures", "implementation", "sortings", "strings"]
115
[{"input": "10\r\nhttp://abacaba.ru/test\r\nhttp://abacaba.ru/\r\nhttp://abacaba.com\r\nhttp://abacaba.com/test\r\nhttp://abacaba.de/\r\nhttp://abacaba.ru/test\r\nhttp://abacaba.de/test\r\nhttp://abacaba.com/\r\nhttp://abacaba.com/t\r\nhttp://abacaba.com/test\r\n", "output": "1\r\nhttp://abacaba.de http://abacaba.ru \r...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Parse input to build hostname path sets hostname_paths = {} with open(input_path, 'r') as f: n = int(f.readline()) for _ in range(n): line = f.readline().strip(...
true
1006/B
1006
B
Python 3
TESTS
15
530
0
152544958
# @Chukamin ZZU_TRAIN def main(): n, k = map(int, input().split()) data = list(map(int, input().split())) profit = 0 temp = data.copy() temp.sort(reverse = True) ans_list = [] choose = [0] * n for i in range(k): t = temp[i] profit += t for j in ...
37
77
2,457,600
131931687
n,k=map(int,input().split()) a=list(map(int,input().split())) l=[] for i in range(n): l.append([a[i],i+1]) l.sort(reverse=True) s=0 d=[] for i in range(k): s+=l[i][0] d.append(l[i][1]) d.sort() print(s) if(k==1): print(n) else: q=[d[0]] for i in range(1,k-1): q.append(d[i]-d[i-1]) q....
Codeforces Round 498 (Div. 3)
ICPC
2,018
2
256
Polycarp's Practice
Polycarp is practicing his problem solving skill. He has a list of $$$n$$$ problems with difficulties $$$a_1, a_2, \dots, a_n$$$, respectively. His plan is to practice for exactly $$$k$$$ days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his l...
The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2000$$$) — the number of problems and the number of days, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2000$$$) — difficulties of problems in Polycarp's list,...
In the first line of the output print the maximum possible total profit. In the second line print exactly $$$k$$$ positive integers $$$t_1, t_2, \dots, t_k$$$ ($$$t_1 + t_2 + \dots + t_k$$$ must equal $$$n$$$), where $$$t_j$$$ means the number of problems Polycarp will solve during the $$$j$$$-th day in order to achie...
null
The first example is described in the problem statement. In the second example there is only one possible distribution. In the third example the best answer is to distribute problems in the following way: $$$[1, 2000], [2000, 2]$$$. The total profit of this distribution is $$$2000 + 2000 = 4000$$$.
[{"input": "8 3\n5 4 2 6 5 1 9 2", "output": "20\n3 2 3"}, {"input": "5 1\n1 1 1 1 1", "output": "1\n5"}, {"input": "4 2\n1 2000 2000 2", "output": "4000\n2 2"}]
1,200
["greedy", "implementation", "sortings"]
37
[{"input": "8 3\r\n5 4 2 6 5 1 9 2\r\n", "output": "20\r\n4 1 3\r\n"}, {"input": "5 1\r\n1 1 1 1 1\r\n", "output": "1\r\n5\r\n"}, {"input": "4 2\r\n1 2000 2000 2\r\n", "output": "4000\r\n2 2\r\n"}, {"input": "1 1\r\n2000\r\n", "output": "2000\r\n1\r\n"}, {"input": "1 1\r\n1234\r\n", "output": "1234\r\n1\r\n"}, {"input"...
false
stdio
import sys def main(input_path, output_path, submission_path): # Read input with open(input_path) as f: lines = f.read().splitlines() n, k = map(int, lines[0].split()) a = list(map(int, lines[1].split())) # Compute correct sum (sum of k largest elements) sorted_a = sorted(a, revers...
true
246/C
246
C
PyPy 3
TESTS
4
216
20,172,800
123229365
def main(arr,k): arr.sort() for i in range(len(arr)): for j in range(i,len(arr)): if k==0: break else: temp_arr=arr[i:j+1] print(j-i+1,*temp_arr) k-=1 return n,k=list(map(int,input().split())) arr=list(map(int,input().split())) (main(arr,k))
34
248
512,000
42134929
n, k = map(int, input().split()) p = list(map(int, input().split())) p.sort() t = [[i] for i in p] for i in range(1, n): t += [t[-1] + i for i in t[: n - i]] print('\n'.join(str(len(i)) + ' ' + ' '.join(map(str, i)) for i in t[: k])) # Made By Mostafa_Khaled
Codeforces Round 151 (Div. 2)
CF
2,012
2
256
Beauty Pageant
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value...
The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $$\frac{n(n+1)}{2}$$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 107) — the beauties of the battalion soldiers. It is guaranteed that Payne's...
Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer ci (1 ≤ ci ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and ci distinct integers p1, i, p2, i, ..., pci, i — the beauties of...
null
null
[{"input": "3 3\n1 2 3", "output": "1 1\n1 2\n2 3 2"}, {"input": "2 1\n7 12", "output": "1 12"}]
1,600
["brute force", "constructive algorithms", "greedy"]
34
[{"input": "3 3\r\n1 2 3\r\n", "output": "1 1\r\n1 2\r\n2 3 2\r\n"}, {"input": "2 1\r\n7 12\r\n", "output": "1 12 \r\n"}, {"input": "1 1\r\n1000\r\n", "output": "1 1000 \r\n"}, {"input": "5 8\r\n10 3 8 31 20\r\n", "output": "1 31 \r\n1 20 \r\n1 10 \r\n1 8 \r\n1 3 \r\n2 31 20 \r\n2 31 10 \r\n2 31 8 \r\n"}, {"input": "5 ...
false
stdio
import sys def main(): input_path, output_path, submission_path = sys.argv[1:4] # Read input with open(input_path) as f: n, k = map(int, f.readline().split()) a = list(map(int, f.readline().split())) allowed = set(a) # Read submission output with open(submission_path) ...
true
352/B
352
B
Python 3
TESTS
33
904
24,576,000
126470083
n=int(input()) l=list(map(int,input().split())) d={} for i in l: d[i]=[] for i in range(len(l)): d[l[i]].append(i) ans=0 ans2=[] for i in d: if len(d[i])<2: ans+=1 #print("default",i) ans2.append([i,0]) elif len(d[i])==2: ans+=1 #print("default2",i) ans2....
36
280
16,384,000
173848518
n = int(input()) arr = list(map(int,input().split())) prev_idx = [-1] * (10**5 + 1) d = [-1] * (10**5 + 1) for i,num in enumerate(arr): if prev_idx[num] != -1: if d[num] == 0: diff = i - prev_idx[num] d[num] = diff elif d[num] == -1: continue else: ...
Codeforces Round 204 (Div. 2)
CF
2,013
1
256
Jeff and Periods
One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: - x occurs in sequence a. - Consider all positions of numbers x in the sequence a (such i, that ai = x). These numb...
The first line contains integer n (1 ≤ n ≤ 105). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 105). The numbers are separated by spaces.
In the first line print integer t — the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x.
null
In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
[{"input": "1\n2", "output": "1\n2 0"}, {"input": "8\n1 2 1 3 1 2 1 5", "output": "4\n1 2\n2 4\n3 0\n5 0"}]
1,300
["implementation", "sortings"]
36
[{"input": "1\r\n2\r\n", "output": "1\r\n2 0\r\n"}, {"input": "8\r\n1 2 1 3 1 2 1 5\r\n", "output": "4\r\n1 2\r\n2 4\r\n3 0\r\n5 0\r\n"}, {"input": "3\r\n1 10 5\r\n", "output": "3\r\n1 0\r\n5 0\r\n10 0\r\n"}, {"input": "4\r\n9 9 3 5\r\n", "output": "3\r\n3 0\r\n5 0\r\n9 1\r\n"}, {"input": "6\r\n1 2 2 1 1 2\r\n", "outpu...
false
stdio
null
true
352/B
352
B
Python 3
TESTS
33
902
21,196,800
186809711
#https://codeforces.com/problemset/problem/352/B from collections import defaultdict n =int(input()) l=list(map(int,input().split())) d=defaultdict(int) #count d1=defaultdict(list) #index list ans =[] for i in range(n): k =l[i] d1[k].append(i+1) d[k]+=1 l1=[] for k in d: l1.append(k) l1.sort() c=0 ...
36
342
23,347,200
216494036
n = int(input()) a = [int(x) for x in input().split()] last = {} for i in range(n): if a[i] in last: last[a[i]] = (i, -1 if last[a[i]][1] and i - last[a[i]][0] != last[a[i]][1] else i - last[a[i]][0]) else: last[a[i]] = (i, 0) ans = [(i, last[i][1]) for i in sorted(last.keys()) if last[i][1] !=...
Codeforces Round 204 (Div. 2)
CF
2,013
1
256
Jeff and Periods
One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: - x occurs in sequence a. - Consider all positions of numbers x in the sequence a (such i, that ai = x). These numb...
The first line contains integer n (1 ≤ n ≤ 105). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 105). The numbers are separated by spaces.
In the first line print integer t — the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x.
null
In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
[{"input": "1\n2", "output": "1\n2 0"}, {"input": "8\n1 2 1 3 1 2 1 5", "output": "4\n1 2\n2 4\n3 0\n5 0"}]
1,300
["implementation", "sortings"]
36
[{"input": "1\r\n2\r\n", "output": "1\r\n2 0\r\n"}, {"input": "8\r\n1 2 1 3 1 2 1 5\r\n", "output": "4\r\n1 2\r\n2 4\r\n3 0\r\n5 0\r\n"}, {"input": "3\r\n1 10 5\r\n", "output": "3\r\n1 0\r\n5 0\r\n10 0\r\n"}, {"input": "4\r\n9 9 3 5\r\n", "output": "3\r\n3 0\r\n5 0\r\n9 1\r\n"}, {"input": "6\r\n1 2 2 1 1 2\r\n", "outpu...
false
stdio
null
true
125/B
125
B
Python 3
TESTS
5
186
307,200
95073364
s=input() s=s.replace("><","> <") s=s.split() d={} m=0 for i in range(len(s)): if "/" not in s[i]: print(" "*(2*m)+s[i]) d[s[i]]=d.get(s[i],m) m=m+1 else: k=s[i].replace("/","") print(" "*(2*d[k])+s[i]) m=d[k]
27
62
0
214035011
s = input() l=[] t=[] for i in range(0,len(s)): if s[i] >= 'a' and s[i] <= 'z': l.append(s[i]) if s[i-1] == '/': t.append(1) else: t.append(0) stack = [] # print(t) for i in range(0,len(l)): if t[i] == 0: print(2*len(stack)*' '+'<'+l[i]+'>') stack...
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
216/E
216
E
Python 3
TESTS
5
92
0
131881901
def n2n(value: int, base: int) -> int: """Converts a value to a number of an specific base""" num_list = [] resto = value % base div = int(value / base) num_list.insert(0, resto) while div >= base: resto = div % base div = int(div / base) num_list.insert(0, resto) ...
41
404
10,649,600
121508307
Line1 = list(map(int,input().split())) List = list(map(int,input().split())) def MartianLuck(k,b,n,digit_list): if b == 0: return Zero(n,digit_list) Subarray = dict() Subarray[0]=1 LuckyNumbersCounter = 0 ActualNumber = 0 for Digit in digit_list : ActualNumber = (ActualNumb...
Codeforces Round 133 (Div. 2)
CF
2,012
2
256
Martian Luck
You know that the Martians use a number system with base k. Digit b (0 ≤ b < k) is considered lucky, as the first contact between the Martians and the Earthlings occurred in year b (by Martian chronology). A digital root d(x) of number x is a number that consists of a single digit, resulting after cascading summing of...
The first line contains three integers k, b and n (2 ≤ k ≤ 109, 0 ≤ b < k, 1 ≤ n ≤ 105). The second line contains string s as a sequence of n integers, representing digits in the k-base notation: the i-th integer equals ai (0 ≤ ai < k) — the i-th digit of string s. The numbers in the lines are space-separated.
Print a single integer — the number of substrings that are lucky numbers. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
null
In the first sample the following substrings have the sought digital root: s[1... 2] = "3 2", s[1... 3] = "3 2 0", s[3... 4] = "0 5", s[4... 4] = "5" and s[2... 6] = "2 0 5 6 1".
[{"input": "10 5 6\n3 2 0 5 6 1", "output": "5"}, {"input": "7 6 4\n3 5 0 4", "output": "1"}, {"input": "257 0 3\n0 0 256", "output": "3"}]
2,000
["math", "number theory"]
41
[{"input": "10 5 6\r\n3 2 0 5 6 1\r\n", "output": "5"}, {"input": "7 6 4\r\n3 5 0 4\r\n", "output": "1"}, {"input": "257 0 3\r\n0 0 256\r\n", "output": "3"}, {"input": "2 1 1\r\n0\r\n", "output": "0"}, {"input": "2 0 20\r\n1 1 1 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 1\r\n", "output": "22"}, {"input": "100 29 33\r\n28 89 23 1...
false
stdio
null
true
70/B
70
B
Python 3
TESTS
10
109
0
46516770
n = int(input()) text = input() text = text.replace("?", ".").replace("!", ".").split(".") ans = True qnt = 0 acu = n+1 for i in range (len(text)): text[i] = text[i].strip() #print (text[i]) if (len (text[i]) + 1 > n): ans = False break acu += len (text[i])+1 if (acu > n): ...
48
124
0
51732189
n, text, SMSes, SMS_len = int(input()), input(), 0, 0 for ch in '.?!': text = text.replace(ch, '_') for L in map(len, (' ' + text).split('_')[:-1]): if L > n: print('Impossible') break SMS_len += (L + 1) if SMS_len else L if SMS_len > n: SMSes, SMS_len = SMSes + 1, L else: ...
Codeforces Beta Round 64
CF
2,011
1
256
Text Messaging
Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing n characters (which is the size of one text message). Thus, whole sentences and words get split! Fangy did not like ...
The first line contains an integer n, which is the size of one message (2 ≤ n ≤ 255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty.
On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print "Impossible" without the quotes.
null
Let's take a look at the third sample. The text will be split into three messages: "Hello!", "Do you like fish?" and "Why?".
[{"input": "25\nHello. I am a little walrus.", "output": "2"}, {"input": "2\nHow are you?", "output": "Impossible"}, {"input": "19\nHello! Do you like fish? Why?", "output": "3"}]
1,600
["expression parsing", "greedy", "strings"]
48
[{"input": "25\r\nHello. I am a little walrus.\r\n", "output": "2\r\n"}, {"input": "2\r\nHow are you?\r\n", "output": "Impossible\r\n"}, {"input": "19\r\nHello! Do you like fish? Why?\r\n", "output": "3\r\n"}, {"input": "4\r\na. A.\r\n", "output": "2\r\n"}, {"input": "146\r\niIQVkDsPqzAJyBrtHk EhBSN gzDoigItCMzETerb cI...
false
stdio
null
true
594/A
594
A
Python 3
TESTS
7
62
0
15467807
def main(): lo, hi = 0, int(input()) - 1 l = sorted(map(int, input().split())) for i in range(hi // 2): if l[lo] + l[hi] < l[lo + 1] + l[hi - 1]: lo += 1 else: hi -= 1 print(l[hi] - l[lo]) if __name__ == '__main__': main()
50
171
28,672,000
231031786
n = int(input()) x = sorted(list(map(int, input().split()))) print(min([x[i + n // 2] - x[i] for i in range(n // 2)]))
Codeforces Round 330 (Div. 1)
CF
2,015
2
256
Warrior and Archer
In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest. V...
The first line on the input contains a single integer n (2 ≤ n ≤ 200 000, n is even) — the number of positions available initially. The second line contains n distinct integers x1, x2, ..., xn (0 ≤ xi ≤ 109), giving the coordinates of the corresponding positions.
Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.
null
In the first sample one of the optimum behavior of the players looks like that: 1. Vova bans the position at coordinate 15; 2. Lesha bans the position at coordinate 3; 3. Vova bans the position at coordinate 31; 4. Lesha bans the position at coordinate 1. After these actions only positions 0 and 7 will remain, and th...
[{"input": "6\n0 1 3 7 15 31", "output": "7"}, {"input": "2\n73 37", "output": "36"}]
2,300
["games"]
50
[{"input": "6\r\n0 1 3 7 15 31\r\n", "output": "7\r\n"}, {"input": "2\r\n73 37\r\n", "output": "36\r\n"}, {"input": "2\r\n0 1000000000\r\n", "output": "1000000000\r\n"}, {"input": "8\r\n729541013 135019377 88372488 319157478 682081360 558614617 258129110 790518782\r\n", "output": "470242129\r\n"}, {"input": "2\r\n0 1\r...
false
stdio
null
true
125/B
125
B
PyPy 3
TESTS
5
186
0
210966242
t = input() l1 = t.split(">")[:-1] l2 = [] s = -2 for i in l1: if i[-1] not in l2: s += 2 print((" " * s) + i + ">") l2.append(i[-1]) else: print((" " * s) + i + ">") s -= 2 l2 = l2[:-1]
27
92
0
5293888
t = input() i, n, d = 0, len(t), -2 while i < n: if t[i + 1] != '/': d += 2 print(' ' * d + t[i: i + 3]) i += 3 else: print(' ' * d + t[i: i + 4]) d -= 2 i += 4
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
1212/C
977
C
PyPy 3
TESTS
4
62
0
226781506
c = list(map(int, input().split())) d = list(map(int, input().split())) d.sort() try: if c[1] == 0 and d[0] != 1: print(1) elif c[1] < len(d): result = min(d[c[1] - 1], 2**31 - 1) if d[c[1] - 1] != d[c[1]] and d[c[1] - 1] <= 10**9 else -1 print(int(result)) except: print(-1)
38
140
27,852,800
226654985
n,k = map(int,input().split()) arr = [1]+list(map(int,input().split())) arr.sort() if n == k or arr[k] != arr[k+1]: print(arr[k]) else: print(-1)
Kotlin Heroes: Practice 2
ICPC
2,019
2
256
Less or Equal
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$. Note that the sequence can contain equal e...
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
null
In the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$. In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this n...
[{"input": "7 4\n3 7 5 1 10 3 20", "output": "6"}, {"input": "7 2\n3 7 5 1 10 3 20", "output": "-1"}]
1,200
["*special", "sortings"]
38
[{"input": "7 4\r\n3 7 5 1 10 3 20\r\n", "output": "5\r\n"}, {"input": "7 2\r\n3 7 5 1 10 3 20\r\n", "output": "-1\r\n"}, {"input": "1 0\r\n1\r\n", "output": "-1\r\n"}, {"input": "1 0\r\n2\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "1000000000\r\n"}, {"input": "3 0\r\n3 3 3\r\n", "output": "...
false
stdio
import sys def check_x_possible(n, k, a): a_sorted = sorted(a) if k == 0: if a_sorted[0] == 1: return False candidate = a_sorted[0] - 1 if candidate < 1: return False cnt = sum(1 for num in a if num <= candidate) return cnt == 0 else: ...
true
125/B
125
B
Python 3
TESTS
5
154
0
109221603
st=input() ls=st.replace('><' ,' ')[1:-1].split() dct={} tag=0 for i in ls: if i[0]!='/': # opening tag dct[i]=tag ans= ' '*tag + '<'+i+'>' print(ans) tag+=1 else: ans= ' '*dct[i[1]] + '</'+i[1]+'>' print(ans) tag-=1
27
92
0
140332794
s = input() size = len(s) h_str = '' i = 0 h = -1 while i < len(s): if s[i + 1] == '/': print(f'{h_str}{s[i:i+4]}') h -= 1 h_str = ' ' * (2 * h) i += 4 else: h += 1 h_str = ' ' * (2 * h) print(f'{h_str}{s[i:i+3]}') i += 3
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
580/B
580
B
Python 3
TESTS
7
31
0
229674931
n,d = [int(i) for i in input().split()] friends = {} for i in range(n): m,f = [int(i) for i in input().split()] if m in friends: friends[m]+=f else: friends[m] = f t = sorted(friends.keys()) start = 0 end = 0 score = friends[t[0]] m = 0 while end<len(t)-1: end+=1 if t[end]-t[start]>d...
35
389
16,179,200
212145762
# -*- coding: utf-8 -*- # https://codeforces.com/problemset/problem/580/B?locale=en # Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. # Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. # Each friend is characterized by the amount of mone...
Codeforces Round 321 (Div. 2)
CF
2,015
2
256
Kefa and Company
Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to...
The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105, $$1 \leq d \leq 10^9$$) — the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively. Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contai...
Print the maximum total friendship factir that can be reached.
null
In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse. In the second sample test we can take all the friends.
[{"input": "4 5\n75 5\n0 100\n150 20\n75 1", "output": "100"}, {"input": "5 100\n0 7\n11 32\n99 10\n46 8\n87 54", "output": "111"}]
1,500
["binary search", "sortings", "two pointers"]
35
[{"input": "4 5\r\n75 5\r\n0 100\r\n150 20\r\n75 1\r\n", "output": "100\r\n"}, {"input": "5 100\r\n0 7\r\n11 32\r\n99 10\r\n46 8\r\n87 54\r\n", "output": "111\r\n"}, {"input": "1 1000000000\r\n15 12\r\n", "output": "12\r\n"}, {"input": "5 1\r\n5 9\r\n2 10\r\n8 5\r\n18 12\r\n1 1\r\n", "output": "12\r\n"}, {"input": "3 3...
false
stdio
null
true
125/B
125
B
Python 3
TESTS
5
216
307,200
82414814
a =[] s ="" b = 0 c = [] for x in input(): if x == "<" or x == ">" :pass elif x =="/":s = "/" else: a.append(s + x) s = "" for x in range(len(a)): for y in a[:x]: if len(a[x]) == 2:b = c[a.index(a[x][-1])] elif len(y) == 1:b+= 1 else:b -= 1 c.append(b) b = 0 for x in range(len(a)): print(" "*(c[x]*2) +...
27
92
0
146496418
s = input() s = s[1 : len(s) - 1].split("><") t = -1 for i in s: if i[0] != "/": t += 1 print(" " * t + "<" + i + ">") else: print(" " * t + "<" + i + ">") t -= 1
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
125/B
125
B
Python 3
TESTS
5
186
6,963,200
80419024
n=input() ls=[] i=0 while (i<len(n)): if(n[i]=='<'): p='' j=i while(n[j]!='>'): p+=n[j] j+=1 p+='>' ls.append(p) i+=1 k=0 i=0 while i<len(ls): flag=0 if (ls[i].replace(' ','')[1]=='/'): i+=1 continue else: j=i+1 ...
27
92
0
148615920
fullstring = input() full_list = [*map(lambda x: x+ '>', fullstring.split('>')[:-1])] checklist = [] tab_counter = 0 ans = full_list[0] + '\n' for i in range(1, len(full_list)): if '/' not in full_list[i]: if '/' not in full_list[i-1]: tab_counter += 1 ans += (' ' * tab_counter *...
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
408/B
408
B
Python 3
TESTS
9
108
0
47298523
a=input() b=input() ans=0 for c in 'abcdefghijklmnopqrstuvwxyz': if a.count(c) == 0 and b.count(c) == 0: continue; if a.count(c)==0 and b.count(c)>0: print(-1) exit(0) x=a.count(c) y=b.count(c) z=0 if y%x==0:z=y else: z=y-y%x+x if x>=y: ans=ans+y continue ...
21
31
0
161125548
import sys input = sys.stdin.readline s = input()[:-1] w = input()[:-1] x = 0 for i in set(w): c = min(s.count(i), w.count(i)) if c == 0: print(-1) break x += c else: print(x)
Codeforces Round 239 (Div. 2)
CF
2,014
1
256
Garland
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter. The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece s...
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper ...
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
null
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6. In the second test sample Vasya cannot make a ga...
[{"input": "aaabbac\naabbccac", "output": "6"}, {"input": "a\nz", "output": "-1"}]
1,200
["implementation"]
21
[{"input": "aaabbac\r\naabbccac\r\n", "output": "6\r\n"}, {"input": "a\r\nz\r\n", "output": "-1"}, {"input": "r\r\nr\r\n", "output": "1\r\n"}, {"input": "stnsdn\r\nndnndsn\r\n", "output": "4\r\n"}, {"input": "yqfqfp\r\ntttwtqq\r\n", "output": "-1"}, {"input": "zzbbrrtrtzr\r\ntbbtrrrzr\r\n", "output": "9\r\n"}, {"input"...
false
stdio
null
true
56/B
56
B
Python 3
TESTS
13
218
0
118229812
n = int(input()) a = list(map(int, input().split())) l = 0 r = 0 i = 0 while i < len(a): if i + 1 != a[i]: if i + 1 != a[a[i] - 1]: break if 0 != l and 0 != r: l = 0 r = 0 break for j in range(i, a[i] - 2): if a[j] != a[j + 1] + 1:...
33
92
0
138573302
# Code by : Sam._.072 n = int(input()) a = list(map(int, input().split())) b = sorted(a) i, j = 0, n-1 while i < n and a[i] == b[i]: i += 1 while j >=0 and a[j] == b[j]: j -= 1 if i == n or j == -1: print(0,0) else: a = a[i:j+1][::-1] if a == b[i:j+1]: print(i+1,j+1) else: print...
Codeforces Beta Round 52 (Div. 2)
CF
2,011
2
256
Spoilt Permutation
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order...
The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is u...
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.
null
null
[{"input": "8\n1 6 5 4 3 2 7 8", "output": "2 6"}, {"input": "4\n2 3 4 1", "output": "0 0"}, {"input": "4\n1 2 3 4", "output": "0 0"}]
1,300
["implementation"]
33
[{"input": "8\r\n1 6 5 4 3 2 7 8\r\n", "output": "2 6\r\n"}, {"input": "4\r\n2 3 4 1\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 3 4\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 2 4 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 4 2 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "1\r\n1\r\n", "output": "...
false
stdio
null
true
698/B
698
B
PyPy 3-64
TESTS
4
61
512,000
151471061
n=int(input()) arr=list(map(int,input().split())) arr=[el-1 for el in arr] root=-1 c=0 for i,el in enumerate(arr): if i==el: if root==-1: root=i else : c+=1 arr[i]=root if root==-1: root=0 arr[0]=0 c+=1 parent=[i for i in range(n)] def find(x): if ...
101
249
28,262,400
218427503
import sys from collections import defaultdict input = sys.stdin.readline n = int(input()) arr = [int(h) for h in input().split()] visited = [0] * n change = [] root = [] for i in range(1, n + 1): if arr[i - 1] == i: root.append(i) j = i if visited[i - 1] == 1: continue h = set() w...
Codeforces Round 363 (Div. 1)
CF
2,016
2
256
Fix a Tree
A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is cons...
The first line of the input contains an integer n (2 ≤ n ≤ 200 000) — the number of vertices in the tree. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
In the first line print the minimum number of elements to change, in order to get a valid sequence. In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in the minimum number of changes. If there are many such sequences, any of them will be accepted.
null
In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (because p4 = 4), which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right drawing below). On bo...
[{"input": "4\n2 3 3 4", "output": "1\n2 3 4 4"}, {"input": "5\n3 2 2 5 3", "output": "0\n3 2 2 5 3"}, {"input": "8\n2 3 5 4 1 6 6 7", "output": "2\n2 3 7 8 1 6 6 7"}]
1,700
["constructive algorithms", "dfs and similar", "dsu", "graphs", "trees"]
101
[{"input": "4\r\n2 3 3 4\r\n", "output": "1\r\n2 3 4 4 \r\n"}, {"input": "5\r\n3 2 2 5 3\r\n", "output": "0\r\n3 2 2 5 3 \r\n"}, {"input": "8\r\n2 3 5 4 1 6 6 7\r\n", "output": "2\r\n2 3 7 8 1 6 6 7\r\n"}, {"input": "2\r\n1 2\r\n", "output": "1\r\n2 2 \r\n"}, {"input": "7\r\n4 3 2 6 3 5 2\r\n", "output": "1\r\n4 3 3 6 ...
false
stdio
import sys from collections import deque def main(input_path, output_path, submission_output_path): # Read input with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) assert len(a) == n # Read submission's output with open(submission_output_path...
true
792/B
792
B
Python 3
TESTS
6
62
5,529,600
25885621
import sys n, k = map(int, sys.stdin.readline().strip().split(" ")) counts = list(map(int, sys.stdin.readline().strip().split(" "))) children = list(range(n + 1)) children.pop(0) pos = 0 for a in counts: subarray = children[pos + 1:] dif = a - len(subarray) if dif <= 0: pos += a print...
22
46
0
164362234
import sys input = sys.stdin.readline n, k = map(int, input().split()) w = list(map(int, input().split())) a = list(range(1, n+1)) d = [] x = n q = 0 for i in w: c = i q = (q+c)%x d.append(a[q]) a.pop(q) x -= 1 k -= 1 if k == 0: break print(' '.join(map(str, d)))
Educational Codeforces Round 18
ICPC
2,017
1
256
Counting-out Rhyme
n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The last one ...
The first line contains two integer numbers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ n - 1). The next line contains k integer numbers a1, a2, ..., ak (1 ≤ ai ≤ 109).
Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.
null
Let's consider first example: - In the first step child 4 is eliminated, child 5 becomes the leader. - In the second step child 2 is eliminated, child 3 becomes the leader. - In the third step child 5 is eliminated, child 6 becomes the leader. - In the fourth step child 6 is eliminated, child 7 becomes the leader. - I...
[{"input": "7 5\n10 4 11 4 1", "output": "4 2 5 6 1"}, {"input": "3 2\n2 5", "output": "3 2"}]
1,300
["implementation"]
22
[{"input": "7 5\r\n10 4 11 4 1\r\n", "output": "4 2 5 6 1 \r\n"}, {"input": "3 2\r\n2 5\r\n", "output": "3 2 \r\n"}, {"input": "2 1\r\n1\r\n", "output": "2 \r\n"}, {"input": "2 1\r\n2\r\n", "output": "1 \r\n"}, {"input": "2 1\r\n3\r\n", "output": "2 \r\n"}, {"input": "10 7\r\n5 10 4 3 8 10 6\r\n", "output": "6 8 3 9 2 ...
false
stdio
null
true
792/B
792
B
Python 3
TESTS
6
46
4,608,000
25848232
n,k = map(int,input().split()) arr = list(map(int,input().split())) l = [x for x in range(1,n+1)] cl = 0 for i in range(k): x = arr[i] if(x<=len(l)-cl-1): if(cl+x!=len(l)-1): t = cl cl = cl+x print(l[t+x],end=' ') del l[t+x] else: t = c...
22
46
0
230797889
n, k = map(int, input().split()) a = list(map(int, input().split())) children = [i for i in range(n)] leader = 0 output = [] for i in range(k): del_pointer = (leader + a[i]) % len(children) output.append(children.pop(del_pointer) + 1) leader = del_pointer print(*output)
Educational Codeforces Round 18
ICPC
2,017
1
256
Counting-out Rhyme
n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The last one ...
The first line contains two integer numbers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ n - 1). The next line contains k integer numbers a1, a2, ..., ak (1 ≤ ai ≤ 109).
Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.
null
Let's consider first example: - In the first step child 4 is eliminated, child 5 becomes the leader. - In the second step child 2 is eliminated, child 3 becomes the leader. - In the third step child 5 is eliminated, child 6 becomes the leader. - In the fourth step child 6 is eliminated, child 7 becomes the leader. - I...
[{"input": "7 5\n10 4 11 4 1", "output": "4 2 5 6 1"}, {"input": "3 2\n2 5", "output": "3 2"}]
1,300
["implementation"]
22
[{"input": "7 5\r\n10 4 11 4 1\r\n", "output": "4 2 5 6 1 \r\n"}, {"input": "3 2\r\n2 5\r\n", "output": "3 2 \r\n"}, {"input": "2 1\r\n1\r\n", "output": "2 \r\n"}, {"input": "2 1\r\n2\r\n", "output": "1 \r\n"}, {"input": "2 1\r\n3\r\n", "output": "2 \r\n"}, {"input": "10 7\r\n5 10 4 3 8 10 6\r\n", "output": "6 8 3 9 2 ...
false
stdio
null
true
551/A
551
A
Python 3
TESTS
17
717
204,800
114420137
n=int(input()) a=list(map(int,input().split())) if n==1: print("1") else: c=[] for i in a: c.append(i) set_1=set(a) b=[] for i in set_1: b.append(i) b=b[::-1] r=1 for i in b: count=0 for j in range(n): if a[j]==i: c[j]=r count=count+1 r=r+count for...
36
46
0
138388034
n = int(input()) list1 = list(map(int, input().strip().split())) list2 = [] for i in range(n): list2.append([list1[i], i]) list2.sort(reverse = True) pos = 0 postemp = 1 temp = list2[0][0] ans = {} for i in list2: if i[0] == temp: ans[i[1]] = postemp pos += 1 continue pos += 1 po...
Codeforces Round 307 (Div. 2)
CF
2,015
2
256
GukiZ and Contest
Professor GukiZ likes programming contests. He especially likes to rate his students on the contests he prepares. Now, he has decided to prepare a new contest. In total, n students will attend, and before the start, every one of them has some positive integer rating. Students are indexed from 1 to n. Let's denote the ...
The first line contains integer n (1 ≤ n ≤ 2000), number of GukiZ's students. The second line contains n numbers a1, a2, ... an (1 ≤ ai ≤ 2000) where ai is the rating of i-th student (1 ≤ i ≤ n).
In a single line, print the position after the end of the contest for each of n students in the same order as they appear in the input.
null
In the first sample, students 2 and 3 are positioned first (there is no other student with higher rating), and student 1 is positioned third since there are two students with higher rating. In the second sample, first student is the only one on the contest. In the third sample, students 2 and 5 share the first positi...
[{"input": "3\n1 3 3", "output": "3 1 1"}, {"input": "1\n1", "output": "1"}, {"input": "5\n3 5 3 4 5", "output": "4 1 4 3 1"}]
800
["brute force", "implementation", "sortings"]
36
[{"input": "3\r\n1 3 3\r\n", "output": "3 1 1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "5\r\n3 5 3 4 5\r\n", "output": "4 1 4 3 1\r\n"}, {"input": "7\r\n1 3 5 4 2 2 1\r\n", "output": "6 3 1 2 4 4 6\r\n"}, {"input": "11\r\n5 6 4 2 9 7 6 6 6 6 7\r\n", "output": "9 4 10 11 1 2 4 4 4 4 2\r\n"}, {"input"...
false
stdio
null
true
551/A
551
A
Python 3
TESTS
17
920
7,065,600
88610788
n=int(input()) a=list(map(int,input().strip().split()))[:n] b=set(a) b=list(b) l=len(b) rank=1 c=[] for i in range(n): c.append(0) for i in range(l-1,-1,-1): x=0 for j in range(0,n,1): if b[i]==a[j]: c[j]=rank x=x+1 rank=rank+x for i in range(n): print(c[i],end=" ")
36
46
0
145080641
n = int(input()) a = [int(i) for i in input().split()] b = sorted(a, reverse=True) c = {} d = 1 for i in range(n): if b[i] not in c: c[b[i]] = d d += 1 for i in range(n): print(c[a[i]], end=" ")
Codeforces Round 307 (Div. 2)
CF
2,015
2
256
GukiZ and Contest
Professor GukiZ likes programming contests. He especially likes to rate his students on the contests he prepares. Now, he has decided to prepare a new contest. In total, n students will attend, and before the start, every one of them has some positive integer rating. Students are indexed from 1 to n. Let's denote the ...
The first line contains integer n (1 ≤ n ≤ 2000), number of GukiZ's students. The second line contains n numbers a1, a2, ... an (1 ≤ ai ≤ 2000) where ai is the rating of i-th student (1 ≤ i ≤ n).
In a single line, print the position after the end of the contest for each of n students in the same order as they appear in the input.
null
In the first sample, students 2 and 3 are positioned first (there is no other student with higher rating), and student 1 is positioned third since there are two students with higher rating. In the second sample, first student is the only one on the contest. In the third sample, students 2 and 5 share the first positi...
[{"input": "3\n1 3 3", "output": "3 1 1"}, {"input": "1\n1", "output": "1"}, {"input": "5\n3 5 3 4 5", "output": "4 1 4 3 1"}]
800
["brute force", "implementation", "sortings"]
36
[{"input": "3\r\n1 3 3\r\n", "output": "3 1 1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "5\r\n3 5 3 4 5\r\n", "output": "4 1 4 3 1\r\n"}, {"input": "7\r\n1 3 5 4 2 2 1\r\n", "output": "6 3 1 2 4 4 6\r\n"}, {"input": "11\r\n5 6 4 2 9 7 6 6 6 6 7\r\n", "output": "9 4 10 11 1 2 4 4 4 4 2\r\n"}, {"input"...
false
stdio
null
true
352/B
352
B
PyPy 3-64
TESTS
33
872
43,929,600
167112849
# time-limit: 3000 from itertools import accumulate, chain, combinations, groupby, permutations, product from collections import deque, Counter, OrderedDict from bisect import bisect_left, bisect_right from math import gcd, sqrt, sin, cos, tan, degrees, radians from fractions import Fraction from decimal import Decimal...
36
372
26,828,800
164042521
# Dictionary == Hash Collision from sys import stdin from bisect import bisect_left as bl, bisect_right as br from collections import defaultdict, Counter, deque def input(): return stdin.readline() def read(default=int): return list(map(default, input().strip().split())) def solve(): n = read()[0] ...
Codeforces Round 204 (Div. 2)
CF
2,013
1
256
Jeff and Periods
One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: - x occurs in sequence a. - Consider all positions of numbers x in the sequence a (such i, that ai = x). These numb...
The first line contains integer n (1 ≤ n ≤ 105). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 105). The numbers are separated by spaces.
In the first line print integer t — the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x.
null
In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
[{"input": "1\n2", "output": "1\n2 0"}, {"input": "8\n1 2 1 3 1 2 1 5", "output": "4\n1 2\n2 4\n3 0\n5 0"}]
1,300
["implementation", "sortings"]
36
[{"input": "1\r\n2\r\n", "output": "1\r\n2 0\r\n"}, {"input": "8\r\n1 2 1 3 1 2 1 5\r\n", "output": "4\r\n1 2\r\n2 4\r\n3 0\r\n5 0\r\n"}, {"input": "3\r\n1 10 5\r\n", "output": "3\r\n1 0\r\n5 0\r\n10 0\r\n"}, {"input": "4\r\n9 9 3 5\r\n", "output": "3\r\n3 0\r\n5 0\r\n9 1\r\n"}, {"input": "6\r\n1 2 2 1 1 2\r\n", "outpu...
false
stdio
null
true
1004/B
1004
B
Python 3
PRETESTS
3
93
0
40001411
n,m = map(int,input().split()) L = [] for i in range(m): L.append(tuple(map(int, input().split()))) a = [-1]*n for i in range(m): p = a[L[i][0]-1:L[i][1]].count('0') q = a[L[i][0]-1:L[i][1]].count('1') c = 0 if p > q: for j in range(L[i][0]-1,L[i][1]): if a[j] == -1: ...
27
46
0
197348787
n, m = map(int, input().split()) for _ in range(m): l, r = map(int, input().split()) ans = [] x = ["0", "1"] for i in range(n): ans.append(x[i % 2]) print("".join(ans))
Codeforces Round 495 (Div. 2)
CF
2,018
1
256
Sonya and Exhibition
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition. There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ pos...
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to ...
Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.
null
In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; - in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; - in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so t...
[{"input": "5 3\n1 3\n2 4\n2 5", "output": "01100"}, {"input": "6 3\n5 6\n1 4\n4 6", "output": "110010"}]
1,300
["constructive algorithms", "greedy", "implementation", "math"]
27
[{"input": "5 3\r\n1 3\r\n2 4\r\n2 5\r\n", "output": "01010\r\n"}, {"input": "6 3\r\n5 6\r\n1 4\r\n4 6\r\n", "output": "010101\r\n"}, {"input": "10 4\r\n3 3\r\n1 6\r\n9 9\r\n10 10\r\n", "output": "0101010101\r\n"}, {"input": "1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "1000 10\r\n3 998\r\n2 1000\r\n1 999\r\n2 1000\...
false
stdio
import sys def read_ints(f): return list(map(int, f.readline().split())) def compute_sum(arrangement, segments): total = 0 for l, r in segments: zeros = arrangement[l-1:r].count('0') ones = (r - l + 1) - zeros total += zeros * ones return total def main(input_path, output_path...
true
125/B
125
B
PyPy 3
TESTS
5
186
0
116506084
a = input().split('>')[:-1] Items = [] currentItem = None level = -1 saved_level_item = {} for i in a: accept = True value = i[1:] if len(Items) > 0: if '/' + currentItem == value: accept = False print(2 * saved_level_item[currentItem] * ' ' + '<' + value + '>') d...
27
92
0
150737911
XML = input() XML = list(XML) sF = 0 for i in range(len(XML)): if XML[i] == "<": if XML[i+1] == "/": sF -= 1 for j in range(sF): print(" ",end="") print("<",end="") sF += 1 if XML[i] == ">": print(">") ...
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
543/B
543
B
Python 3
TESTS
3
61
1,536,000
221532130
from typing import * from math import * from functools import lru_cache from queue import PriorityQueue,Queue # by hangpengjie def I(): return input() def II(): return int(input()) def MII(): return map(int, input().split()) def LI(): return list(input().split()) def LII(): return list(map(in...
63
1,731
91,955,200
174886422
from collections import deque n,m=map(int,input().split()) graph=[[] for _ in range(n)] for i in range(m): a,b=map(int,input().split()) a-=1 b-=1 graph[a].append(b) graph[b].append(a) d=[[-1]*n for _ in range(n)] for i in range(n): q=deque() q.append(i) d[i][i]=0 while q: n...
Codeforces Round 302 (Div. 1)
CF
2,015
2
256
Destroying Roads
In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any ...
The first line contains two integers n, m (1 ≤ n ≤ 3000, $$n-1\leq m\leq \min\{3000,\frac{n(n-1)}{2}\}$$) — the number of cities and roads in the country, respectively. Next m lines contain the descriptions of the roads as pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that the roads that are giv...
Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.
null
null
[{"input": "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n3 5 2", "output": "0"}, {"input": "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n2 4 2", "output": "1"}, {"input": "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n3 5 1", "output": "-1"}]
2,100
["constructive algorithms", "graphs", "shortest paths"]
63
[{"input": "5 4\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 3 2\r\n3 5 2\r\n", "output": "0\r\n"}, {"input": "5 4\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 3 2\r\n2 4 2\r\n", "output": "1\r\n"}, {"input": "5 4\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 3 2\r\n3 5 1\r\n", "output": "-1\r\n"}, {"input": "9 9\r\n1 2\r\n2 3\r\n2 4\r\n4 5\r\n5 7\r\n5 6\...
false
stdio
null
true
125/B
125
B
PyPy 3
TESTS
5
218
2,048,000
109363483
import re xml = re.findall(r'</?\w>', input()) h = -1 opened = [] for tag in xml: tag_name = re.search(r'\w', tag).group() new = tag_name not in opened if new: opened.append(tag_name) h += 1 print('%s%s' % (' '*2*h, tag)) if not new: opened.remove(tag_name) h -= 1
27
92
0
152894165
xml = list(map(str,input().split(">")))[:-1] xml = [ i+">" for i in xml] print(xml[0]) count = 0 for i in xml[1:-1]: if "/" in i: print(" "*(count)+i) count -= 2 else: count += 2 print(" "*(count)+i) print(xml[-1])
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
125/B
125
B
Python 3
TESTS
5
216
307,200
82298069
a = [] b =[] s = 0 c =input() for x in range(len(c)): if c[x] == ">" or c[x] == "<" or c[x] == "/":pass else: if c[x - 1] == "/" :a.append("/" + c[x]) else:a.append(c[x]) for x in range(len(a)): n = (a.index(a[x]) - s) * 2 if a[x][-1] in a[:x]: s += 2 b.append(b[a.index(a[x][-1])]) else: if s > 0 and a[x...
27
92
0
158751509
s = input() h = 0 st = [] gg = '\/' c = 0 while(c <= len(s)): if c <= len(s) - 1 and s[c] == '<': if s[c + 1] == gg[1]: h -= 2 print(' ' * h, s[c], s[c + 1], s[c + 2], s[c + 3], sep = '') st = st[0:len(st) - 1] c += 1 else: st...
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
644/C
644
C
Python 3
TESTS
14
592
27,955,200
41501633
from sys import stdin def main(): input() l = stdin.read().splitlines() l.sort() l.append('http://~/~') ptrn = a = '~' r, pp = [], ['~'] for b in l: if a == b: continue a = b i = a.find('/', 8) if i == -1: h, p = a, '' else: ...
115
499
77,107,200
228999380
import sys input = lambda: sys.stdin.buffer.readline().decode().strip() from math import inf, gcd, lcm, log, log2, floor, ceil, sqrt, isqrt from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify from functools import lru_cache from itertools import permutations, accumulate, gro...
CROC 2016 - Qualification
CF
2,016
5
256
Hostname Aliases
There are some websites that are accessible through several different addresses. For example, for a long time Codeforces was accessible with two hostnames codeforces.com and codeforces.ru. You are given a list of page addresses being queried. For simplicity we consider all addresses to have the form http://<hostname>[...
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of page queries. Then follow n lines each containing exactly one address. Each address is of the form http://<hostname>[/<path>], where: - <hostname> consists of lowercase English letters and dots, there are no two consecutive dots,...
First print k — the number of groups of server names that correspond to one website. You should count only groups of size greater than one. Next k lines should contain the description of groups, one group per line. For each group print all server names separated by a single space. You are allowed to print both groups ...
null
null
[{"input": "10\nhttp://abacaba.ru/test\nhttp://abacaba.ru/\nhttp://abacaba.com\nhttp://abacaba.com/test\nhttp://abacaba.de/\nhttp://abacaba.ru/test\nhttp://abacaba.de/test\nhttp://abacaba.com/\nhttp://abacaba.com/t\nhttp://abacaba.com/test", "output": "1\nhttp://abacaba.de http://abacaba.ru"}, {"input": "14\nhttp://c\n...
2,100
["*special", "binary search", "data structures", "implementation", "sortings", "strings"]
115
[{"input": "10\r\nhttp://abacaba.ru/test\r\nhttp://abacaba.ru/\r\nhttp://abacaba.com\r\nhttp://abacaba.com/test\r\nhttp://abacaba.de/\r\nhttp://abacaba.ru/test\r\nhttp://abacaba.de/test\r\nhttp://abacaba.com/\r\nhttp://abacaba.com/t\r\nhttp://abacaba.com/test\r\n", "output": "1\r\nhttp://abacaba.de http://abacaba.ru \r...
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Parse input to build hostname path sets hostname_paths = {} with open(input_path, 'r') as f: n = int(f.readline()) for _ in range(n): line = f.readline().strip(...
true
63/A
63
A
Python 3
TESTS
5
124
0
22221757
folks = []; folks_order = [] mens_or_cap = 0 rat = 0 woman_or_child = 0 for i in range(int(input())): folks.append(input().split()) for i in range(len(folks)): if folks[i][1] == 'captain': folks_order.append(folks[i][0]) mens_or_cap += 1 elif mens_or_cap > 0 and folks[i][1] == 'man': fol...
26
62
0
221890888
n = int(input()) array = {'rat':0, 'woman':1, 'child':1, 'man':2, 'captain':3} element = [] for i in range(n): a = input().split() element.append([array[a[1]], i, a[0]]) element.sort() for i in element: print(i[2])
Codeforces Beta Round 59 (Div. 2)
CF
2,011
2
256
Sinking Ship
The ship crashed into a reef and is sinking. Now the entire crew must be evacuated. All n crew members have already lined up in a row (for convenience let's label them all from left to right with positive integers from 1 to n) and await further instructions. However, one should evacuate the crew properly, in a strict o...
The first line contains an integer n, which is the number of people in the crew (1 ≤ n ≤ 100). Then follow n lines. The i-th of those lines contains two words — the name of the crew member who is i-th in line, and his status on the ship. The words are separated by exactly one space. There are no other spaces in the lin...
Print n lines. The i-th of them should contain the name of the crew member who must be the i-th one to leave the ship.
null
null
[{"input": "6\nJack captain\nAlice woman\nCharlie man\nTeddy rat\nBob child\nJulia woman", "output": "Teddy\nAlice\nBob\nJulia\nCharlie\nJack"}]
900
["implementation", "sortings", "strings"]
26
[{"input": "6\r\nJack captain\r\nAlice woman\r\nCharlie man\r\nTeddy rat\r\nBob child\r\nJulia woman\r\n", "output": "Teddy\r\nAlice\r\nBob\r\nJulia\r\nCharlie\r\nJack\r\n"}, {"input": "1\r\nA captain\r\n", "output": "A\r\n"}, {"input": "1\r\nAbcdefjhij captain\r\n", "output": "Abcdefjhij\r\n"}, {"input": "5\r\nA capta...
false
stdio
null
true
686/B
686
B
PyPy 3
TESTS
5
108
4,096,000
185620165
def get_ints(): return map(int, input().strip().split()) def get_list(): return list(map(int, input().strip().split())) def get_string(): return input().strip() for t in range(1): n = int(input().strip()) arr = get_list() for i in range(n): for j in range(i,n-1): if a...
37
46
102,400
218756175
# LUOGU_RID: 120817526 # 输入 n。 n=int(input()) # 输入数组。 a=list(map(int, input().split())) # 冒泡排序。 for i in range(1,n): # 遍历数组,从后往前。 for j in range(n-1, i-1,-1): # 交换。 if a[j]<a[j-1]: # Python 独有的交换方式。 a[j],a[j-1]=a[j-1],a[j] # 输出。 print(j,j+1)
Codeforces Round 359 (Div. 2)
CF
2,016
2
256
Little Robber Girl's Zoo
Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places. The robber girl was angry at first, but then she decided to arrange the animals herself. She ...
The first line contains a single integer n (1 ≤ n ≤ 100) — number of animals in the robber girl's zoo. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the height of the animal occupying the i-th place.
Print the sequence of operations that will rearrange the animals by non-decreasing height. The output should contain several lines, i-th of the lines should contain two space-separated integers li and ri (1 ≤ li < ri ≤ n) — descriptions of segments the robber girl should name. The segments should be described in the o...
null
Note that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed.
[{"input": "4\n2 1 4 3", "output": "1 4"}, {"input": "7\n36 28 57 39 66 69 68", "output": "1 4\n6 7"}, {"input": "5\n1 2 1 2 1", "output": "2 5\n3 4\n1 4\n1 4"}]
1,100
["constructive algorithms", "implementation", "sortings"]
37
[{"input": "4\r\n2 1 4 3\r\n", "output": "1 2\r\n3 4\r\n"}, {"input": "7\r\n36 28 57 39 66 69 68\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n"}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "2 3\r\n4 5\r\n3 4\r\n"}, {"input": "78\r\n7 3 8 8 9 8 10 9 12 11 16 14 17 17 18 18 20 20 25 22 27 26 29 27 35 35 36 36 37 37 38 38 40 39 4...
false
stdio
import sys def main(): input_path = sys.argv[1] correct_output_path = sys.argv[2] submission_output_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline()) a = list(map(int, f.readline().split())) with open(submission_output_path, 'r') as f: lines = f...
true
125/B
125
B
Python 3
TESTS
5
186
307,200
99225600
xml = input() n = len(xml) op = 0 i = 0 memDict = {} while i < n-3: poss = xml[i: i+3] if poss[2] == ">": res = (" "* op) + poss memDict[poss[1]] = op op+=2 i+=3 print(res) else: poss = xml[i:i+4] op = memDict[poss[2]] res = (" "*...
27
92
0
159832849
spaces = 0 a = list(map(str , input().replace('><', '> <').split())) for tag_in in a: if '/' in tag_in: spaces -= 2 print(spaces*' ' + tag_in) if '/' not in tag_in: spaces += 2
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
125/B
125
B
PyPy 3-64
TESTS
5
122
0
206200420
text = input().split('<') spaces = 0 xml = {} for i in text[1:]: if i[-2] not in xml and i[0] != '/': xml[i] = spaces print(' ' * spaces + '<' + i) spaces += 2 else: print(' ' * xml[i[1:]] + '<' + i) spaces -= 2
27
92
0
160782240
#!/usr/bin/env/python # -*- coding: utf-8 -*- s = input().split('>')[:-1] level = 0 for a in s: if a.startswith('</'): level -= 1 print(' ' * (2 * level) + a + '>') else: print(' ' * (2 * level) + a + '>') level += 1
Codeforces Testing Round 2
CF
2,011
2
256
Simple XML
Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion...
The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.
Print the given XML-text according to the above-given rules.
null
null
[{"input": "<a><b><c></c></b></a>", "output": "<a>\n<b>\n<c>\n</c>\n</b>\n</a>"}, {"input": "<a><b></b><d><c></c></d></a>", "output": "<a>\n<b>\n</b>\n<d>\n<c>\n</c>\n</d>\n</a>"}]
1,000
["implementation"]
27
[{"input": "<a><b><c></c></b></a>\r\n", "output": "<a>\r\n <b>\r\n <c>\r\n </c>\r\n </b>\r\n</a>\r\n"}, {"input": "<a><b></b><d><c></c></d></a>\r\n", "output": "<a>\r\n <b>\r\n </b>\r\n <d>\r\n <c>\r\n </c>\r\n </d>\r\n</a>\r\n"}, {"input": "<z></z>\r\n", "output": "<z>\r\n</z>\r\n"}, {"input": "<u><d...
false
stdio
null
true
698/B
698
B
PyPy 3
TESTS
12
467
42,188,800
128943265
def dfs(curr_i, temp_visited, visited, first, parents): stack = [curr_i] temp_visited[curr_i] = True while not temp_visited[parents[curr_i]] and not visited[parents[curr_i]]: curr_i = parents[curr_i] stack.append(curr_i) temp_visited[curr_i] = True ...
101
342
47,616,000
218409859
import sys input = lambda: sys.stdin.readline().rstrip() import math from heapq import heappush , heappop from collections import defaultdict,deque,Counter from bisect import * N = int(input()) A = [i-1 for i in list(map(int, input().split()))] P = [[] for _ in range(N)] ins = [0]*N for i in range(N): if A[i]!=i: ...
Codeforces Round 363 (Div. 1)
CF
2,016
2
256
Fix a Tree
A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is cons...
The first line of the input contains an integer n (2 ≤ n ≤ 200 000) — the number of vertices in the tree. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
In the first line print the minimum number of elements to change, in order to get a valid sequence. In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in the minimum number of changes. If there are many such sequences, any of them will be accepted.
null
In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (because p4 = 4), which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right drawing below). On bo...
[{"input": "4\n2 3 3 4", "output": "1\n2 3 4 4"}, {"input": "5\n3 2 2 5 3", "output": "0\n3 2 2 5 3"}, {"input": "8\n2 3 5 4 1 6 6 7", "output": "2\n2 3 7 8 1 6 6 7"}]
1,700
["constructive algorithms", "dfs and similar", "dsu", "graphs", "trees"]
101
[{"input": "4\r\n2 3 3 4\r\n", "output": "1\r\n2 3 4 4 \r\n"}, {"input": "5\r\n3 2 2 5 3\r\n", "output": "0\r\n3 2 2 5 3 \r\n"}, {"input": "8\r\n2 3 5 4 1 6 6 7\r\n", "output": "2\r\n2 3 7 8 1 6 6 7\r\n"}, {"input": "2\r\n1 2\r\n", "output": "1\r\n2 2 \r\n"}, {"input": "7\r\n4 3 2 6 3 5 2\r\n", "output": "1\r\n4 3 3 6 ...
false
stdio
import sys from collections import deque def main(input_path, output_path, submission_output_path): # Read input with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) assert len(a) == n # Read submission's output with open(submission_output_path...
true