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
487/B
487
B
PyPy 3-64
TESTS
6
139
9,728,000
229949342
standard_input, packages, dfs, hashing = 1, 1, 0, 0 if standard_input: import sys input = lambda: sys.stdin.readline().rstrip("\r\n") def I(): return input() def II(): return int(input()) def MII(): return map(int, input().split()) def LI(): return list(i...
35
155
17,817,600
230441633
import random, sys, os, math, gc from collections import Counter, defaultdict, deque from functools import lru_cache, reduce, cmp_to_key from itertools import accumulate, combinations, permutations, product from heapq import nsmallest, nlargest, heapify, heappop, heappush from copy import deepcopy from bisect import bi...
Codeforces Round 278 (Div. 1)
CF
2,014
1
256
Strip
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy: - Each piece should contain at least l numbers. - The difference between the maximal and the minimal number on the piece sho...
The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105). The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).
Output the minimal number of strip pieces. If there are no ways to split the strip, output -1.
null
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2]. For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
[{"input": "7 2 2\n1 3 1 2 4 1 2", "output": "3"}, {"input": "7 2 2\n1 100 1 100 1 100 1", "output": "-1"}]
2,000
["binary search", "data structures", "dp", "two pointers"]
35
[{"input": "7 2 2\r\n1 3 1 2 4 1 2\r\n", "output": "3\r\n"}, {"input": "7 2 2\r\n1 100 1 100 1 100 1\r\n", "output": "-1\r\n"}, {"input": "1 0 1\r\n0\r\n", "output": "1\r\n"}, {"input": "6 565 2\r\n31 76 162 -182 -251 214\r\n", "output": "1\r\n"}, {"input": "1 0 1\r\n0\r\n", "output": "1\r\n"}, {"input": "1 0 1\r\n-100...
false
stdio
null
true
748/D
748
D
Python 3
TESTS
7
1,777
19,148,800
151069605
class Node: def __init__(self, value): self.value = value self.left = None self.right = None def insert(self, value): if self.value: if value < self.value: if self.left is None: self.left = Node(value) else: ...
62
389
4,915,200
66481205
n, k = map(int, input().split()) p = {} np = {} pair = [] used = {} rev_d = {} def push(d, s, v): if s not in d: d[s] = [] d[s].append(v) def is_pal(s): n = len(s) flg=True for i in range(n//2): if s[i] != s[n-1-i]: flg = False break return flg ...
Technocup 2017 - Elimination Round 3
CF
2,016
2
256
Santa Claus and a Palindrome
Santa Claus likes palindromes very much. There was his birthday recently. k of his friends came to him to congratulate him, and each of them presented to him a string si having the same length n. We denote the beauty of the i-th string by ai. It can happen that ai is negative — that means that Santa doesn't find this s...
The first line contains two positive integers k and n divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1 ≤ k, n ≤ 100 000; n·k  ≤ 100 000). k lines follow. The i-th of them contains the string si and its beauty ai ( - 10 000 ≤ ai ≤ 10 000). The s...
In the only line print the required maximum possible beauty.
null
In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).
[{"input": "7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4", "output": "12"}, {"input": "3 1\na 1\na 2\na 3", "output": "6"}, {"input": "2 5\nabcde 10000\nabcde 10000", "output": "0"}]
2,100
["constructive algorithms", "data structures", "greedy"]
62
[{"input": "7 3\r\nabb 2\r\naaa -3\r\nbba -1\r\nzyz -4\r\nabb 5\r\naaa 7\r\nxyx 4\r\n", "output": "12\r\n"}, {"input": "3 1\r\na 1\r\na 2\r\na 3\r\n", "output": "6\r\n"}, {"input": "2 5\r\nabcde 10000\r\nabcde 10000\r\n", "output": "0\r\n"}, {"input": "10 10\r\nnjxbzflaka -1\r\nfelbvvtkja 6\r\ngxiuztqkcw 5\r\naomvscmtt...
false
stdio
null
true
748/D
748
D
PyPy 3-64
TESTS
7
811
33,689,600
136785196
import heapq def process(A): non_palindrome = {} palindrome = {} for si, ai in A: s2 = si[::-1] if s2==si: if si not in palindrome: palindrome[si] = [] heapq.heappush(palindrome[si], -1*ai) else: if si < s2: s_rep ...
62
389
10,342,400
23295949
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time sys.setrecursionlimit(10**7) inf = 10**20 mod = 10**9 + 7 def main(): n,l = list(map(int, input().split())) d = collections.defaultdict(list) e = collections.defaultdict(list) k = collections.defaultdict(list) ...
Technocup 2017 - Elimination Round 3
CF
2,016
2
256
Santa Claus and a Palindrome
Santa Claus likes palindromes very much. There was his birthday recently. k of his friends came to him to congratulate him, and each of them presented to him a string si having the same length n. We denote the beauty of the i-th string by ai. It can happen that ai is negative — that means that Santa doesn't find this s...
The first line contains two positive integers k and n divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1 ≤ k, n ≤ 100 000; n·k  ≤ 100 000). k lines follow. The i-th of them contains the string si and its beauty ai ( - 10 000 ≤ ai ≤ 10 000). The s...
In the only line print the required maximum possible beauty.
null
In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).
[{"input": "7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4", "output": "12"}, {"input": "3 1\na 1\na 2\na 3", "output": "6"}, {"input": "2 5\nabcde 10000\nabcde 10000", "output": "0"}]
2,100
["constructive algorithms", "data structures", "greedy"]
62
[{"input": "7 3\r\nabb 2\r\naaa -3\r\nbba -1\r\nzyz -4\r\nabb 5\r\naaa 7\r\nxyx 4\r\n", "output": "12\r\n"}, {"input": "3 1\r\na 1\r\na 2\r\na 3\r\n", "output": "6\r\n"}, {"input": "2 5\r\nabcde 10000\r\nabcde 10000\r\n", "output": "0\r\n"}, {"input": "10 10\r\nnjxbzflaka -1\r\nfelbvvtkja 6\r\ngxiuztqkcw 5\r\naomvscmtt...
false
stdio
null
true
279/B
279
B
PyPy 3-64
TESTS
28
154
10,444,800
186760784
def main(): n, t = read_ints() aseq = read_ints() time = 0 if aseq[0] <= t: time += aseq[0] res = 1 else: res = 0 i = 0 j = 1 ans = res while i < n and j < n: if time + aseq[j] <= t: time += aseq[j] res += 1 j += 1 ...
38
154
10,752,000
206202347
from sys import stdin def solve(): n, t = map(int, stdin.readline().split()) A = [int(x) for x in stdin.readline().split()] cur, i = 0, 0 for a in A: cur += a if cur > t: cur -= A[i] i += 1 print(n - i) solve()
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
555/B
555
B
Python 3
TESTS
9
280
14,438,400
11811156
n, m = (int(x) for x in input().split()) coord, dest, br = [], [], [] for i in range(n): coord.append(tuple(int(x) for x in input().split())) temp = input().split() br = [(int(temp[x]), x + 1) for x in range(len(temp))] coord.sort() for i in range(len(coord) - 1): dest.append((coord[i+1][0] - coord[i][1], coord[i+1]...
50
1,856
79,257,600
173055136
import sys # sys.setrecursionlimit(10**9) # import random # from collections import Counter, defaultdict, deque # from functools import lru_cache, reduce # from itertools import accumulate,product from heapq import nsmallest, nlargest, heapify, heappop, heappush # from bisect import bisect_left,bisect_right # from sort...
Codeforces Round 310 (Div. 1)
CF
2,015
3
256
Case of Fugitive
Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an archipelago of n narrow islands located in a row. For more comfort let's represent them as non-intersecting segments on a straight line: ...
The first line contains integers n (2 ≤ n ≤ 2·105) and m (1 ≤ m ≤ 2·105) — the number of islands and bridges. Next n lines each contain two integers li and ri (1 ≤ li ≤ ri ≤ 1018) — the coordinates of the island endpoints. The last line contains m integer numbers a1, a2, ..., am (1 ≤ ai ≤ 1018) — the lengths of the b...
If it is impossible to place a bridge between each pair of adjacent islands in the required manner, print on a single line "No" (without the quotes), otherwise print in the first line "Yes" (without the quotes), and in the second line print n - 1 numbers b1, b2, ..., bn - 1, which mean that between islands i and i + 1 ...
null
In the first sample test you can, for example, place the second bridge between points 3 and 8, place the third bridge between points 7 and 10 and place the first bridge between points 10 and 14. In the second sample test the first bridge is too short and the second bridge is too long, so the solution doesn't exist.
[{"input": "4 4\n1 4\n7 8\n9 10\n12 14\n4 5 3 8", "output": "Yes\n2 3 1"}, {"input": "2 2\n11 14\n17 18\n2 9", "output": "No"}, {"input": "2 1\n1 1\n1000000000000000000 1000000000000000000\n999999999999999999", "output": "Yes\n1"}]
2,000
["data structures", "greedy", "sortings"]
50
[{"input": "4 4\r\n1 4\r\n7 8\r\n9 10\r\n12 14\r\n4 5 3 8\r\n", "output": "Yes\r\n2 3 1 \r\n"}, {"input": "2 2\r\n11 14\r\n17 18\r\n2 9\r\n", "output": "No\r\n"}, {"input": "2 1\r\n1 1\r\n1000000000000000000 1000000000000000000\r\n999999999999999999\r\n", "output": "Yes\r\n1 \r\n"}, {"input": "5 10\r\n1 2\r\n3 3\r\n5 7...
false
stdio
null
true
558/A
558
A
PyPy 3-64
TESTS
8
62
0
230796174
n=int(input()) max1=-1 p,m,p1,m1={},{},{},{} for i in range (n): x,a=map(int,input().split()) if x>0: p[x]=a; p1[x]=a; elif x<0: m[x]=a; m1[x]=a; if(len(p)==len(m) or abs(len(p)-len(m))==1): print(sum(p.values())+sum(m.values())) else: if len(p)>len(m): sum1=0...
46
46
0
193830516
n = int(input()) dictionp = {} dictionn = {} corp = [] corn = [] pos = 0 neg = 0 ans = 0 for i in range(n): l = list(map(int , input().split())) if l[0] > 0: dictionp[l[0]] = l[1] corp.append(l[0]) pos = pos + 1 ans = ans + l[1] else: dictionn[l[0]] = l[1] cor...
Codeforces Round 312 (Div. 2)
CF
2,015
1
256
Lala Land and Apple Trees
Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere. Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple tre...
The first line contains one number n (1 ≤ n ≤ 100), the number of apple trees in Lala Land. The following n lines contains two integers each xi, ai ( - 105 ≤ xi ≤ 105, xi ≠ 0, 1 ≤ ai ≤ 105), representing the position of the i-th tree and number of apples on it. It's guaranteed that there is at most one apple tree at ...
Output the maximum number of apples Amr can collect.
null
In the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples. In the second sample test the optimal solution is to go left to x = - 1, collect apples from there, then the direction will be reversed, Amr has to go to x = 1, collect apples from there, then...
[{"input": "2\n-1 5\n1 5", "output": "10"}, {"input": "3\n-2 2\n1 4\n-1 3", "output": "9"}, {"input": "3\n1 9\n3 5\n7 10", "output": "9"}]
1,100
["brute force", "implementation", "sortings"]
46
[{"input": "2\r\n-1 5\r\n1 5\r\n", "output": "10"}, {"input": "3\r\n-2 2\r\n1 4\r\n-1 3\r\n", "output": "9"}, {"input": "3\r\n1 9\r\n3 5\r\n7 10\r\n", "output": "9"}, {"input": "1\r\n1 1\r\n", "output": "1"}, {"input": "4\r\n10000 100000\r\n-1000 100000\r\n-2 100000\r\n-1 100000\r\n", "output": "300000"}, {"input": "1\...
false
stdio
null
true
186/B
186
B
Python 3
TESTS
3
92
0
137701684
n, t_1, t_2, k = map(int, input().split()) lst = [] for i in range(n): a, b = sorted(map(int, input().split())) lst.append([i + 1, (a * t_1 * (1 - k/100)) + (b * t_2)]) lst.sort(key=lambda x: x[1], reverse=True) for i in range(n): print(lst[i][0], f'{lst[i][1]:.2f}')
36
92
0
185952094
n, t1, t2, k = map(int, input().split()) table = [] for i in range(1, n+1): a, b = map(int, input().split()) c1 = a*t1*(1-k/100) + b*t2 c2 = b*t1*(1-k/100) + a*t2 if c1 > c2: c = c1 else: c = c2 table.append((i, f"{c:.2f}")) for e in sorted(table, key=lambda x: float(x[1]), rever...
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Growing Mushrooms
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ...
The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two inte...
Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
null
- First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3.
[{"input": "2 3 3 50\n2 4\n4 2", "output": "1 15.00\n2 15.00"}, {"input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970", "output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99"}]
1,200
["greedy", "sortings"]
36
[{"input": "2 3 3 50\r\n2 4\r\n4 2\r\n", "output": "1 15.00\r\n2 15.00\r\n"}, {"input": "4 1 1 1\r\n544 397\r\n280 101\r\n280 101\r\n693 970\r\n", "output": "4 1656.07\r\n1 937.03\r\n2 379.99\r\n3 379.99\r\n"}, {"input": "10 1 1 25\r\n981 1\r\n352 276\r\n164 691\r\n203 853\r\n599 97\r\n901 688\r\n934 579\r\n910 959\r\n...
false
stdio
null
true
61/B
61
B
Python 3
TESTS
32
77
204,800
174584956
n=input().replace(";","") n=n.replace("-","") n=n.replace("_","") n=n.lower() p=input().replace(";","") p=p.replace("-","") p=p.replace("_","") p=p.lower() c=input().replace(";","") c=c.replace("-","") c=c.replace("_","") c=c.lower() z=sorted(n+p+c) for i in range(int(input())): q=input().replace(";","") q=q.re...
43
46
204,800
174641108
n=input().replace(";","").replace("-","").replace("_","").lower() p=input().replace(";","").replace("-","").replace("_","").lower() c=input().replace(";","").replace("-","").replace("_","").lower() z=[n+p+c,n+c+p,c+n+p,c+p+n,p+c+n,p+n+c] for i in range(int(input())): q=input().replace(";","").replace("-","").replac...
Codeforces Beta Round 57 (Div. 2)
CF
2,011
2
256
Hard Work
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer! Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher...
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively. In the fourth line there is a single integer n (0 ≤ n ≤ 1000), the number of students. N...
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
null
null
[{"input": "Iran_\nPersian;\nW_o;n;d;e;r;f;u;l;\n7\nWonderfulPersianIran\nwonderful_PersIAN_IRAN;;_\nWONDERFUL___IRAN__PERSIAN__;;\nIra__Persiann__Wonderful\nWonder;;fulPersian___;I;r;a;n;\n__________IranPersianWonderful__________\nPersianIran_is_Wonderful", "output": "ACC\nACC\nACC\nWA\nACC\nACC\nWA"}, {"input": "Shap...
1,300
["strings"]
43
[{"input": "Iran_\r\nPersian;\r\nW_o;n;d;e;r;f;u;l;\r\n7\r\nWonderfulPersianIran\r\nwonderful_PersIAN_IRAN;;_\r\nWONDERFUL___IRAN__PERSIAN__;;\r\nIra__Persiann__Wonderful\r\nWonder;;fulPersian___;I;r;a;n;\r\n__________IranPersianWonderful__________\r\nPersianIran_is_Wonderful\r\n", "output": "ACC\r\nACC\r\nACC\r\nWA\r\...
false
stdio
null
true
186/B
186
B
Python 3
TESTS
3
92
307,200
105264493
n, t1, t2, k = list(map(int, input().split())) rank = [] for seed_grower in range(n): speed1, speed2 = list(map(int, input().split())) first_half = min(speed1, speed2) * t1 * (1-(k/100)) second_half = max(speed1, speed2) * t2 total = first_half + second_half rank.append((seed_grower+1, total)) for s...
36
92
102,400
219470668
n,t1,t2,k=map(int,input().split()) s=[] for i in range(n): a,b=map(int,input().split()) temp1=a*t1-(a*t1*k/100)+b*t2 temp2=b*t1-(b*t1*k/100)+a*t2 if temp1>temp2: s.append([temp1,-i-1]) else: s.append([temp2,-i-1]) s.sort(reverse=True) for i,z in s: print('%d %.2f' % (-z,i))
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Growing Mushrooms
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ...
The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two inte...
Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
null
- First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3.
[{"input": "2 3 3 50\n2 4\n4 2", "output": "1 15.00\n2 15.00"}, {"input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970", "output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99"}]
1,200
["greedy", "sortings"]
36
[{"input": "2 3 3 50\r\n2 4\r\n4 2\r\n", "output": "1 15.00\r\n2 15.00\r\n"}, {"input": "4 1 1 1\r\n544 397\r\n280 101\r\n280 101\r\n693 970\r\n", "output": "4 1656.07\r\n1 937.03\r\n2 379.99\r\n3 379.99\r\n"}, {"input": "10 1 1 25\r\n981 1\r\n352 276\r\n164 691\r\n203 853\r\n599 97\r\n901 688\r\n934 579\r\n910 959\r\n...
false
stdio
null
true
1004/B
1004
B
PyPy 3
PRETESTS
3
124
204,800
40003717
n, m = map(int, input().split()) A = [[0, 0] for i in range(m)] def sort(): global A for i in range(len(A)): for j in range(i, len(A)): if A[i][2] > A[j][2]: A[i], A[j] = A[j], A[i] for i in range(m): x, y = map(int, input().split()) A[i] = [x - 1, y...
27
46
0
152488267
# @Chukamin ZZU_TRAIN def main(): n, m = map(int, input().split()) if n & 1 == 0: print('01' * (n >> 1)) else: print('01' * (n >> 1) + '0') if __name__ == '__main__': main()
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
662/D
662
D
PyPy 3
TESTS
3
124
2,252,800
216082821
t = int(input()) for i in range(t): s = input() x = s[4:] l = ["199", "20", "2"] if len(x) > 3: if x[0] == "0": print(str(len(x)) + x) elif x[0] == "2" and len(x) == 4: print("1" + x) else: print(x) elif x == "9": print(1989) elif x == "99": print(1999) else: print(l[len(x) - 1] + ...
45
77
0
17350742
def run(n): l = len(n) - 1 b = 1989 l = 10**l while l > 1: b += l l //= 10 if b <= int(n): return n b = str(b) if len(b) == len(n): return '1' + n else: if b[-len(n):] <= n: return b[:-len(n)] + n return str(int(b[:-len(n)])+1) ...
CROC 2016 - Final Round [Private, For Onsite Finalists Only]
CF
2,016
1
256
International Olympiad
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Amo...
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of abbreviations to process. Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits.
For each abbreviation given in the input, find the year of the corresponding Olympiad.
null
null
[{"input": "5\nIAO'15\nIAO'2015\nIAO'1\nIAO'9\nIAO'0", "output": "2015\n12015\n1991\n1989\n1990"}, {"input": "4\nIAO'9\nIAO'99\nIAO'999\nIAO'9999", "output": "1989\n1999\n2999\n9999"}]
2,000
["constructive algorithms", "greedy", "implementation", "math"]
45
[{"input": "5\r\nIAO'15\r\nIAO'2015\r\nIAO'1\r\nIAO'9\r\nIAO'0\r\n", "output": "2015\r\n12015\r\n1991\r\n1989\r\n1990\r\n"}, {"input": "4\r\nIAO'9\r\nIAO'99\r\nIAO'999\r\nIAO'9999\r\n", "output": "1989\r\n1999\r\n2999\r\n9999\r\n"}, {"input": "1\r\nIAO'111110\r\n", "output": "1111110\r\n"}, {"input": "2\r\nIAO'0\r\nIAO...
false
stdio
null
true
925/A
925
A
PyPy 3
TESTS
6
888
10,854,400
117071046
import sys from bisect import bisect_left from math import ceil input=sys.stdin.readline n,m,cl,ce,v=map(int,input().split()) l=list(map(int,input().split())) e=list(map(int,input().split())) q=int(input()) for _ in range(q): x1,y1,x2,y2=map(int,input().split()) if x1==x2: print(abs(y1-y2)) continue ans=[...
27
1,263
61,542,400
37718991
import bisect as bs import sys inp = sys.stdin.readlines() n, m, ladders, elevators, v = [int(x) for x in inp[0].strip().split()] ladders = [int(x) for x in inp[1].strip().split()] elevators = [int(x) for x in inp[2].strip().split()] q = int(inp[3].strip()) qs = [] for i in range(q): qs.append([int(x) for x in inp[...
VK Cup 2018 - Round 3
CF
2,018
2
256
Stairs and Elevators
In the year of $$$30XX$$$ participants of some world programming championship live in a single large hotel. The hotel has $$$n$$$ floors. Each floor has $$$m$$$ sections with a single corridor connecting all of them. The sections are enumerated from $$$1$$$ to $$$m$$$ along the corridor, and all sections with equal num...
The first line contains five integers $$$n, m, c_l, c_e, v$$$ ($$$2 \leq n, m \leq 10^8$$$, $$$0 \leq c_l, c_e \leq 10^5$$$, $$$1 \leq c_l + c_e \leq m - 1$$$, $$$1 \leq v \leq n - 1$$$) — the number of floors and section on each floor, the number of stairs, the number of elevators and the maximum speed of an elevator,...
Print $$$q$$$ integers, one per line — the answers for the queries.
null
In the first query the optimal way is to go to the elevator in the 5-th section in four time units, use it to go to the fifth floor in two time units and go to the destination in one more time unit. In the second query it is still optimal to use the elevator, but in the third query it is better to use the stairs in th...
[{"input": "5 6 1 1 3\n2\n5\n3\n1 1 5 6\n1 3 5 4\n3 3 5 3", "output": "7\n5\n4"}]
1,600
["binary search"]
27
[{"input": "5 6 1 1 3\r\n2\r\n5\r\n3\r\n1 1 5 6\r\n1 3 5 4\r\n3 3 5 3\r\n", "output": "7\r\n5\r\n4\r\n"}, {"input": "2 2 0 1 1\r\n\r\n1\r\n1\r\n1 2 2 2\r\n", "output": "3\r\n"}, {"input": "4 4 1 0 1\r\n4\r\n\r\n5\r\n1 1 2 2\r\n1 3 2 2\r\n3 3 4 3\r\n3 2 2 2\r\n1 2 2 3\r\n", "output": "6\r\n4\r\n3\r\n5\r\n4\r\n"}, {"inpu...
false
stdio
null
true
815/C
815
C
PyPy 3
TESTS
6
140
3,174,400
110014738
from collections import defaultdict Count=0 def BFGetMaxGood(start,RemPurse,G,Cost,Discount,prevSelected): global Count M=0 if RemPurse<0: Count=max(Count,len(prevSelected)-1) return if RemPurse==0 or start>len(Cost): Count=max(Count,len(prevSelected)) return #...
92
935
305,766,400
212051009
import sys def rd(): return sys.stdin.readline().strip() def rdl(typ,sep=" "): return list(map(typ, rd().split(sep))) def wt(x,sep="\n") : sys.stdout.write(str(x) + sep) # string / num def treeOrdering2(n, adjList, root=0): # order by depth order = [] stack = [root] while stack: curr = stack.p...
Codeforces Round 419 (Div. 1)
CF
2,017
2
512
Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars. The supermarket sells n goods. The i-th good can be bought for ci dollars. Of course, each good c...
The first line of input contains two integers n and b (1 ≤ n ≤ 5000, 1 ≤ b ≤ 109), the number of goods in the store and the amount of money Karen has, respectively. The next n lines describe the items. Specifically: - The i-th line among these starts with two integers, ci and di (1 ≤ di < ci ≤ 109), the price of the ...
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
null
In the first test case, Karen can purchase the following 4 items: - Use the first coupon to buy the first item for 10 - 9 = 1 dollar. - Use the third coupon to buy the third item for 12 - 2 = 10 dollars. - Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars. - Buy the sixth item for 2 dollars. The to...
[{"input": "6 16\n10 9\n10 5 1\n12 2 1\n20 18 3\n10 2 3\n2 1 5", "output": "4"}, {"input": "5 10\n3 1\n3 1 1\n3 1 2\n3 1 3\n3 1 4", "output": "5"}]
2,400
["brute force", "dp", "trees"]
92
[{"input": "6 16\r\n10 9\r\n10 5 1\r\n12 2 1\r\n20 18 3\r\n10 2 3\r\n2 1 5\r\n", "output": "4\r\n"}, {"input": "5 10\r\n3 1\r\n3 1 1\r\n3 1 2\r\n3 1 3\r\n3 1 4\r\n", "output": "5\r\n"}, {"input": "13 30\r\n6 4\r\n25 5 1\r\n7 1 2\r\n9 4 2\r\n10 2 1\r\n12 3 1\r\n5 2 3\r\n10 9 6\r\n2 1 1\r\n5 3 9\r\n10 2 10\r\n10 9 6\r\n3...
false
stdio
null
true
358/B
358
B
Python 3
TESTS
7
46
0
4946488
''' Created on Oct 31, 2013 @author: Ismael ''' import sys import re def keepOnlyLetters(sentence): letters = "" tabPos = [] for i in range(len(sentence)): c = sentence[i] if(c >= 'a' and c <= 'z'): letters += c tabPos += [i] return (letters,tabPos) def findSep...
30
140
9,728,000
143805093
import sys import re def fast_input(): return sys.stdin.readline().rstrip('\n') def fast_print(thing, end='\n'): sys.stdout.write(str(thing) + end) def main(): n = int(fast_input()) # number of words in dima msg msgs = [] for _ in range(n): msgs.append(fast_input()) case = fast_input()...
Codeforces Round 208 (Div. 2)
CF
2,013
2
256
Dima and Text Messages
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other. Dima and Inna are using a secret code in their text messages. ...
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The numbe...
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
null
Please note that Dima got a good old kick in the pants for the second sample from the statement.
[{"input": "3\ni\nlove\nyou\n<3i<3love<23you<3", "output": "yes"}, {"input": "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n<3i<>3am<3the<3<main<3in<3the<3><3family<3", "output": "no"}]
1,500
["brute force", "strings"]
30
[{"input": "3\r\ni\r\nlove\r\nyou\r\n<3i<3love<23you<3\r\n", "output": "yes\r\n"}, {"input": "7\r\ni\r\nam\r\nnot\r\nmain\r\nin\r\nthe\r\nfamily\r\n<3i<>3am<3the<3<main<3in<3the<3><3family<3\r\n", "output": "no\r\n"}, {"input": "3\r\ni\r\nlove\r\nyou\r\n<3i<3lo<3ve<3y<<<<<<<ou3<3\r\n", "output": "yes\r\n"}, {"input": "...
false
stdio
null
true
108/B
108
B
Python 3
TESTS
3
92
5,529,600
33454721
n = int(input()) l = [int(x) for x in input().split()] flag = 0 l.sort() for i in range(len(l)-1): if l[i+1] < 2*l[i]-1: flag = 1 print('YES') break if not flag: print('NO')
65
278
14,848,000
130989704
n = int(input()) a = list(map(int, input().split())) a = set(a) a = sorted(a) for i in range(len(a) - 1): if a[i] + a[i] > a[i + 1]: print('YES') exit(0) print('NO')
Codeforces Beta Round 83 (Div. 2 Only)
CF
2,011
2
256
Datatypes
Tattah's youngest brother, Tuftuf, is new to programming. Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava. Today, Tuftuf was introduced to Gava's unsigned integer datatypes. G...
The first line contains integer n (2 ≤ n ≤ 105) — the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of n integers (1 ≤ ai ≤ 109) — sizes of datatypes in bits. Some datatypes may have equal sizes.
Print "YES" if Tuftuf will stop using Gava, and "NO" otherwise.
null
In the second example, x = 7 (1112) fits in 3 bits, but x2 = 49 (1100012) does not fit in 4 bits.
[{"input": "3\n64 16 32", "output": "NO"}, {"input": "4\n4 2 1 3", "output": "YES"}]
1,400
["math", "sortings"]
65
[{"input": "3\r\n64 16 32\r\n", "output": "NO\r\n"}, {"input": "4\r\n4 2 1 3\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 5 3 3 2\r\n", "output": "YES\r\n"}, {"input": "52\r\n474 24 24 954 9 234 474 114 24 114 234 24 114 114 234 9 9 24 9 54 234 54 9 954 474 9 54 54 54 234 9 114 24 54 114 954 954 474 24 54 54 234 234 4...
false
stdio
null
true
289/A
289
A
Python 3
TESTS
4
62
0
169696965
n, k = map(int, input().split()) covered= 0 for i in range(n): l, r = list(map(int, input().split())) covered += r+1-l if k == covered: print("0") else: print(k - covered % k)
28
218
10,035,200
200494528
import sys import math from typing import Callable def main() -> None: read: Callable[[], str] = sys.stdin.readline n, k = (int(i) for i in read().split()) total_length_covered = 0 for _ in range(n): l, r = (int(i) for i in read().split()) total_length_covered += r - l + 1 # The val we need to return is the...
Codeforces Round 177 (Div. 2)
CF
2,013
2
256
Polo the Penguin and Segments
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the r...
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequal...
In a single line print a single integer — the answer to the problem.
null
null
[{"input": "2 3\n1 2\n3 4", "output": "2"}, {"input": "3 7\n1 2\n3 3\n4 7", "output": "0"}]
1,100
["brute force", "implementation"]
28
[{"input": "2 3\r\n1 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3 7\r\n1 2\r\n3 3\r\n4 7\r\n", "output": "0\r\n"}, {"input": "3 7\r\n1 10\r\n11 47\r\n74 128\r\n", "output": "3\r\n"}, {"input": "5 4\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "7 4\r\n2 2\r\n-1 -1\r\n0 1\r\n7 8\r\n-3 -2\r\n9...
false
stdio
null
true
61/B
61
B
Python 3
TESTS
34
202
6,144,000
34786512
# fname = 'D:\Study-Algorithm-\Codeforce\A_testcase.txt' # with open(fname) as f: # content = f.readlines() # content = [x.strip() for x in content] # profes_string = [] # for i in range(3): # profes_string.append(content[i]) # num_students = int(content[3]) # student_string = [] # for i in range(num_studen...
43
46
204,800
194304786
str = [] for _ in range(3): str.append(input().replace('_','').replace(';','').replace('-','').lower()) right_answer=[] right_answer.append(str[0]+str[1]+str[2]) right_answer.append(str[0]+str[2]+str[1]) right_answer.append(str[1]+str[0]+str[2]) right_answer.append(str[1]+str[2]+str[0]) right_answer.append(str[2]+s...
Codeforces Beta Round 57 (Div. 2)
CF
2,011
2
256
Hard Work
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer! Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher...
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively. In the fourth line there is a single integer n (0 ≤ n ≤ 1000), the number of students. N...
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
null
null
[{"input": "Iran_\nPersian;\nW_o;n;d;e;r;f;u;l;\n7\nWonderfulPersianIran\nwonderful_PersIAN_IRAN;;_\nWONDERFUL___IRAN__PERSIAN__;;\nIra__Persiann__Wonderful\nWonder;;fulPersian___;I;r;a;n;\n__________IranPersianWonderful__________\nPersianIran_is_Wonderful", "output": "ACC\nACC\nACC\nWA\nACC\nACC\nWA"}, {"input": "Shap...
1,300
["strings"]
43
[{"input": "Iran_\r\nPersian;\r\nW_o;n;d;e;r;f;u;l;\r\n7\r\nWonderfulPersianIran\r\nwonderful_PersIAN_IRAN;;_\r\nWONDERFUL___IRAN__PERSIAN__;;\r\nIra__Persiann__Wonderful\r\nWonder;;fulPersian___;I;r;a;n;\r\n__________IranPersianWonderful__________\r\nPersianIran_is_Wonderful\r\n", "output": "ACC\r\nACC\r\nACC\r\nWA\r\...
false
stdio
null
true
61/B
61
B
Python 3
TESTS
38
77
307,200
119238377
letters = [] line1 = input().lower().replace(';', '').replace('_', '').replace('-', '') line2 = input().lower().replace(';', '').replace('_', '').replace('-', '') line3 = input().lower().replace(';', '').replace('_', '').replace('-', '') size = int(input()) for i in range(size): letters.append(input().lower().rep...
43
46
409,600
223062017
string1 = input().replace(";", "").replace("-", "").replace("_", "").lower() string2 = input().replace(";", "").replace("-", "").replace("_", "").lower() string3 = input().replace(";", "").replace("-", "").replace("_", "").lower() _ = int(input()) for i in range(_): student = input().replace(";", "").replace("-", "...
Codeforces Beta Round 57 (Div. 2)
CF
2,011
2
256
Hard Work
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer! Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher...
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively. In the fourth line there is a single integer n (0 ≤ n ≤ 1000), the number of students. N...
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
null
null
[{"input": "Iran_\nPersian;\nW_o;n;d;e;r;f;u;l;\n7\nWonderfulPersianIran\nwonderful_PersIAN_IRAN;;_\nWONDERFUL___IRAN__PERSIAN__;;\nIra__Persiann__Wonderful\nWonder;;fulPersian___;I;r;a;n;\n__________IranPersianWonderful__________\nPersianIran_is_Wonderful", "output": "ACC\nACC\nACC\nWA\nACC\nACC\nWA"}, {"input": "Shap...
1,300
["strings"]
43
[{"input": "Iran_\r\nPersian;\r\nW_o;n;d;e;r;f;u;l;\r\n7\r\nWonderfulPersianIran\r\nwonderful_PersIAN_IRAN;;_\r\nWONDERFUL___IRAN__PERSIAN__;;\r\nIra__Persiann__Wonderful\r\nWonder;;fulPersian___;I;r;a;n;\r\n__________IranPersianWonderful__________\r\nPersianIran_is_Wonderful\r\n", "output": "ACC\r\nACC\r\nACC\r\nWA\r\...
false
stdio
null
true
186/B
186
B
PyPy 3-64
TESTS
3
92
0
188996974
n, t1, t2, k = map(int, input().split()) uv = [sorted(list(map(int, input().split()))) for i in range(n)] score = [u*(100-k)*t1 + v*100*t2 for u, v in uv] for i in sorted(list(range(n)), key=lambda x:score[x], reverse=True): print(i + 1, '%d.%02d'%divmod(score[i], 100))
36
92
102,400
221485424
n, t1, t2, k = map(int, input().split()) results = [] # Initialize a list to store results for i in range(n): a, b = map(int, input().split()) a,b = min(a,b), max(a,b) # Calculate the growth in the first part growth_1_a = a * t1 growth_1_b = b * t1 # Calculate the reduction during the break ...
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Growing Mushrooms
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ...
The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two inte...
Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
null
- First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3.
[{"input": "2 3 3 50\n2 4\n4 2", "output": "1 15.00\n2 15.00"}, {"input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970", "output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99"}]
1,200
["greedy", "sortings"]
36
[{"input": "2 3 3 50\r\n2 4\r\n4 2\r\n", "output": "1 15.00\r\n2 15.00\r\n"}, {"input": "4 1 1 1\r\n544 397\r\n280 101\r\n280 101\r\n693 970\r\n", "output": "4 1656.07\r\n1 937.03\r\n2 379.99\r\n3 379.99\r\n"}, {"input": "10 1 1 25\r\n981 1\r\n352 276\r\n164 691\r\n203 853\r\n599 97\r\n901 688\r\n934 579\r\n910 959\r\n...
false
stdio
null
true
723/C
723
C
PyPy 3-64
TESTS
2
46
0
220180996
import os import sys from io import BytesIO, IOBase import math from math import isclose BUFSIZE = 4096 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.wr...
50
62
307,200
21143677
n,m = map(int, input().split()) A = dict() per = list(map(int, input().split())) cou =[0] * m for j in range(n): if per[j] <= m: cou[per[j]-1] +=1 ans = 0 ans2 = n//m s = 0 for j in range(n): num = per[j] while s <= m-1: if cou[s] < ans2: if num > m: per[j] = s+1 ...
Codeforces Round 375 (Div. 2)
CF
2,016
2
256
Polycarp at the Radio
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the gr...
The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 2000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the performer of the i-th song.
In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≤ j ≤ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If ther...
null
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's change...
[{"input": "4 2\n1 2 3 2", "output": "2 1\n1 2 1 2"}, {"input": "7 3\n1 3 2 2 2 2 1", "output": "2 1\n1 3 3 2 2 2 1"}, {"input": "4 4\n1000000000 100 7 1000000000", "output": "1 4\n1 2 3 4"}]
1,600
["greedy"]
50
[{"input": "4 2\r\n1 2 3 2\r\n", "output": "2 1\r\n1 2 1 2 \r\n"}, {"input": "7 3\r\n1 3 2 2 2 2 1\r\n", "output": "2 1\r\n1 3 3 2 2 2 1 \r\n"}, {"input": "4 4\r\n1000000000 100 7 1000000000\r\n", "output": "1 4\r\n1 2 3 4 \r\n"}, {"input": "1 1\r\n1\r\n", "output": "1 0\r\n1 \r\n"}, {"input": "1 1\r\n381183829\r\n", "...
false
stdio
import sys def main(input_path, output_path, submission_path): # Read input with open(input_path) as f: n, m = map(int, f.readline().split()) a = list(map(int, f.readline().split())) # Read submission output with open(submission_path) as f: lines = f.readlines() if ...
true
765/D
765
D
PyPy 3
TESTS
14
296
10,956,800
188865217
a=int(input()) z=list(map(int,input().split())) flag=0 for i in range(a): if(z[i]>len(z) or z[i]<0): flag=1 break if(z[i]==z[z[i]-1]): continue else: flag=1 if(flag==1): print(-1) exit(0) if(len(z)==z.count(z[0])): print(1) ans=[1]*len(z) print(*ans) ...
43
218
16,179,200
24660574
n = int(input()) f = [None] + list(map(int, input().split(' '))) invalid = False g = [None] + [0] * n h = [None] x_is_f_which = [None] + [0] * n m = 0 vis = [None] + [False] * n for i in range(1, n + 1): x = f[i] if f[x] != x: invalid = True break if not vis[x]: vis[x] = True ...
Codeforces Round 397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
CF
2,017
2
512
Artsem and Saunders
Artsem has a friend Saunders from University of Chicago. Saunders presented him with the following problem. Let [n] denote the set {1, ..., n}. We will also write f: [x] → [y] when a function f is defined in integer points 1, ..., x, and all its values are integers from 1 to y. Now then, you are given a function f: [...
The first line contains an integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — values f(1), ..., f(n) (1 ≤ f(i) ≤ n).
If there is no answer, print one integer -1. Otherwise, on the first line print the number m (1 ≤ m ≤ 106). On the second line print n numbers g(1), ..., g(n). On the third line print m numbers h(1), ..., h(m). If there are several correct answers, you may output any of them. It is guaranteed that if a valid answer e...
null
null
[{"input": "3\n1 2 3", "output": "3\n1 2 3\n1 2 3"}, {"input": "3\n2 2 2", "output": "1\n1 1 1\n2"}, {"input": "2\n2 1", "output": "-1"}]
1,700
["constructive algorithms", "dsu", "math"]
43
[{"input": "3\r\n1 2 3\r\n", "output": "3\r\n1 2 3\r\n1 2 3\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "1\r\n1 1 1\r\n2\r\n"}, {"input": "2\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "2\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "2\r\n2 2\r\n", "output": "1\r\n1...
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()) f_list = list(map(int, f.readline().strip().split())) with open(submission_path) as sub_f: lines = [line.strip(...
true
765/A
765
A
Python 3
TESTS
6
109
307,200
75074315
n=int(input()) home=input() l,status=[],"home" while n: n-=1 x,y=[x for x in input().split("->")] l.append(x+"->"+y) if ((x+"->"+y) in l) and ((y+"->"+x) in l): status="home" i1,i2=l.index(x+"->"+y),l.index(y+"->"+x) l[i1],l[i2]="---","---" else: status="contest" prin...
23
31
0
146716007
n = int(input()) h = input() g = 0 b = 0 for i in range(0, n): s, e = input().split("->") if s == h: g += 1 if e == h: b += 1 print(["home", "contest"][g > b])
Codeforces Round 397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
CF
2,017
2
512
Neverending competitions
There are literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (for some reason they prefer name "snookah")! When a competition takes place somewhere far from their hometown, Ivan, Artsem and Konstantin take a flight to the contest and back. Jinotega's best friends, team ...
In the first line of input there is a single integer n: the number of Jinotega's flights (1 ≤ n ≤ 100). In the second line there is a string of 3 capital Latin letters: the name of Jinotega's home airport. In the next n lines there is flight information, one flight per line, in form "XXX->YYY", where "XXX" is the name ...
If Jinotega is now at home, print "home" (without quotes), otherwise print "contest".
null
In the first sample Jinotega might first fly from SVO to CDG and back, and then from SVO to LHR and back, so now they should be at home. In the second sample Jinotega must now be at RAP because a flight from RAP back to SVO is not on the list.
[{"input": "4\nSVO\nSVO->CDG\nLHR->SVO\nSVO->LHR\nCDG->SVO", "output": "home"}, {"input": "3\nSVO\nSVO->HKT\nHKT->SVO\nSVO->RAP", "output": "contest"}]
900
["implementation", "math"]
23
[{"input": "4\r\nSVO\r\nSVO->CDG\r\nLHR->SVO\r\nSVO->LHR\r\nCDG->SVO\r\n", "output": "home\r\n"}, {"input": "3\r\nSVO\r\nSVO->HKT\r\nHKT->SVO\r\nSVO->RAP\r\n", "output": "contest\r\n"}, {"input": "1\r\nESJ\r\nESJ->TSJ\r\n", "output": "contest\r\n"}, {"input": "2\r\nXMR\r\nFAJ->XMR\r\nXMR->FAJ\r\n", "output": "home\r\n"...
false
stdio
null
true
765/D
765
D
Python 3
TESTS
14
280
7,475,200
62645507
n = int(input()) f = list(map(int, input().split())) s = set(f) m = len(s) first = set(f[:m]) if(sorted(f) != f or len(first) != m): print(-1) exit() g = [-1 for i in range(n+1)] h = list(first) for i in range(1, m+1): g[h[i-1]] = i for i in range(1, n+1): g[i] = max(g[i], 1) print(m) print(*g[1:]) print(*h)
43
233
21,504,000
137259240
import sys input = lambda : sys.stdin.readline().rstrip() write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x)) debug = lambda x: sys.stderr.write(x+"\n") YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO) LI = lambda : list(map(int, input().split())) # sys.setrecursionlim...
Codeforces Round 397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
CF
2,017
2
512
Artsem and Saunders
Artsem has a friend Saunders from University of Chicago. Saunders presented him with the following problem. Let [n] denote the set {1, ..., n}. We will also write f: [x] → [y] when a function f is defined in integer points 1, ..., x, and all its values are integers from 1 to y. Now then, you are given a function f: [...
The first line contains an integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — values f(1), ..., f(n) (1 ≤ f(i) ≤ n).
If there is no answer, print one integer -1. Otherwise, on the first line print the number m (1 ≤ m ≤ 106). On the second line print n numbers g(1), ..., g(n). On the third line print m numbers h(1), ..., h(m). If there are several correct answers, you may output any of them. It is guaranteed that if a valid answer e...
null
null
[{"input": "3\n1 2 3", "output": "3\n1 2 3\n1 2 3"}, {"input": "3\n2 2 2", "output": "1\n1 1 1\n2"}, {"input": "2\n2 1", "output": "-1"}]
1,700
["constructive algorithms", "dsu", "math"]
43
[{"input": "3\r\n1 2 3\r\n", "output": "3\r\n1 2 3\r\n1 2 3\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "1\r\n1 1 1\r\n2\r\n"}, {"input": "2\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "2\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "2\r\n2 2\r\n", "output": "1\r\n1...
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()) f_list = list(map(int, f.readline().strip().split())) with open(submission_path) as sub_f: lines = [line.strip(...
true
61/D
61
D
PyPy 3-64
TESTS
2
62
0
171793222
class Tree: def __init__(self, n): self.n = n self.graph = [] for _ in range(n + 1): self.graph.append([]) self.visited = [] self.sub_weights = [0] * (n + 1) def init_visited(self): self.visited = [False] * (self.n + 1) def calc_sub_weights(self...
56
482
17,920,000
154002425
n=int(input()) q={1:0} l=[] a=0 for i in range(n-1): x,y,w=map(int,input().split()) a+=2*w if x>y:x,y=y,x l+=[[x,y,w]] l.sort(key=lambda x:x[0]) for i in l:q[i[1]]=q[i[0]]+i[2] print(a-max(q.values()))
Codeforces Beta Round 57 (Div. 2)
CF
2,011
2
256
Eternal Victory
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal! He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too ti...
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities. Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
A single integer number, the minimal length of Shapur's travel. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
null
null
[{"input": "3\n1 2 3\n2 3 4", "output": "7"}, {"input": "3\n1 2 3\n1 3 3", "output": "9"}]
1,800
["dfs and similar", "graphs", "greedy", "shortest paths", "trees"]
56
[{"input": "3\r\n1 2 3\r\n2 3 4\r\n", "output": "7\r\n"}, {"input": "3\r\n1 2 3\r\n1 3 3\r\n", "output": "9\r\n"}, {"input": "5\r\n5 3 60\r\n4 3 63\r\n2 1 97\r\n3 1 14\r\n", "output": "371\r\n"}, {"input": "3\r\n2 1 63\r\n3 1 78\r\n", "output": "204\r\n"}, {"input": "13\r\n8 2 58\r\n2 1 49\r\n13 10 41\r\n11 9 67\r\n6 4...
false
stdio
null
true
354/A
354
A
Python 3
TESTS
4
109
0
47524634
n,l,r,ql,qr=map(int,input().split()) A=list(map(int,input().split())) B=[] for i in range(n): # print(sum(A[:i]),sum(A[i:])) result=sum(A[:i])*l+sum(A[i:])*r if i<n-i: result+=(n-i-i-1)*qr if i>n-i: result+=(i-n+i-1)*ql B.append(result) print(min(B))
23
92
13,209,600
210667782
n, l, r, ql, qr = map(int, input().split()) q = list(map(int, input().split())) # 一开始都从队尾弹出 val = sum(q) * r res = val + qr * (n - 1) for i in range(1, n + 1): j = n - i val += l * q[i - 1] - r * q[i - 1] if i < j: res = min(res, val + qr * (j - i - 1)) elif i > j: res = min(res, val + q...
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
289/A
289
A
Python 3
TESTS
4
92
4,505,600
134619134
n,m = map(int,input().split()) z =set() for i in range(n): a,b = map(int,input().split()) for j in range(a,b+1): z.add(j) l = len(z) if l > m : x = l%m print(m-x) else : print(m-l)
28
248
9,625,600
200491957
import sys import math from typing import Callable def main() -> None: read: Callable[[], str] = sys.stdin.readline n, k = (int(i) for i in read().split()) total_length_covered = 0 for _ in range(n): l, r = (int(i) for i in read().split()) total_length_covered += r - l + 1 multiplier = math.ceil(total_lengt...
Codeforces Round 177 (Div. 2)
CF
2,013
2
256
Polo the Penguin and Segments
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the r...
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequal...
In a single line print a single integer — the answer to the problem.
null
null
[{"input": "2 3\n1 2\n3 4", "output": "2"}, {"input": "3 7\n1 2\n3 3\n4 7", "output": "0"}]
1,100
["brute force", "implementation"]
28
[{"input": "2 3\r\n1 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3 7\r\n1 2\r\n3 3\r\n4 7\r\n", "output": "0\r\n"}, {"input": "3 7\r\n1 10\r\n11 47\r\n74 128\r\n", "output": "3\r\n"}, {"input": "5 4\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "7 4\r\n2 2\r\n-1 -1\r\n0 1\r\n7 8\r\n-3 -2\r\n9...
false
stdio
null
true
186/B
186
B
Python 3
TESTS
3
62
0
162810395
from sys import stdin from random import randint #quickSort en segunda posición. El ascending dice si es decreciente o creciente def quickSort(L, ascending): if len(L) <= 1: return L smaller, equal, larger = [], [], [] pivot = L[randint(0, len(L) - 1)][1] for x in L: if x[1] < pivot: ...
36
92
102,400
221978794
resLis = [] n , t1 , t2 , k = map(int, input().split()) for i in range(n): spA , spB = map(int, input().split()) heightA = (spA * t1 * ((100-k)/100)) + (spB * t2) heightB = (spB * t1 * ((100-k)/100)) + (spA * t2) maxHeight = "{:.2f}".format(max(heightA , heightB)) resLis.append((i+1,float(maxHeight))) resLis...
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Growing Mushrooms
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ...
The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two inte...
Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
null
- First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3.
[{"input": "2 3 3 50\n2 4\n4 2", "output": "1 15.00\n2 15.00"}, {"input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970", "output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99"}]
1,200
["greedy", "sortings"]
36
[{"input": "2 3 3 50\r\n2 4\r\n4 2\r\n", "output": "1 15.00\r\n2 15.00\r\n"}, {"input": "4 1 1 1\r\n544 397\r\n280 101\r\n280 101\r\n693 970\r\n", "output": "4 1656.07\r\n1 937.03\r\n2 379.99\r\n3 379.99\r\n"}, {"input": "10 1 1 25\r\n981 1\r\n352 276\r\n164 691\r\n203 853\r\n599 97\r\n901 688\r\n934 579\r\n910 959\r\n...
false
stdio
null
true
821/D
821
D
PyPy 3
TESTS
0
124
0
43543252
def check(d,c,final): a,b=d if (a+1,b) in c and rightturn(d,(a+1,b),final):return (a+1,b) elif (a,b+1) in c and rightturn(d,(a,b+1),final):return (a,b+1) elif (a-1,b) in c and rightturn(d,(a-1,b),final):return (a-1,b) elif (a,b-1) in c and rightturn(d,(a,b-1),final):return (a,b-1) else:return (-...
69
218
10,752,000
190678233
import heapq import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def dijkstra(s, n): dist = [inf] * n dist[s] = 0 visit = [0] * n p = [] heapq.heappush(p, (dist[s], s)) while p: d, u = heapq.heappop(p) if dist[u] < d: continue visi...
Codeforces Round 420 (Div. 2)
CF
2,017
4
256
Okabe and City
Okabe likes to be able to walk through his city on a path lit by street lamps. That way, he doesn't get beaten up by schoolchildren. Okabe's city is represented by a 2D grid of cells. Rows are numbered from 1 to n from top to bottom, and columns are numbered 1 to m from left to right. Exactly k cells in the city are l...
The first line of input contains three space-separated integers n, m, and k (2 ≤ n, m, k ≤ 104). Each of the next k lines contains two space-separated integers ri and ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m) — the row and the column of the i-th lit cell. It is guaranteed that all k lit cells are distinct. It is guaranteed that th...
Print the minimum number of coins Okabe needs to pay to complete his walk, or -1 if it's not possible.
null
In the first sample test, Okabe can take the path $$(1,1)\rightarrow(2,1)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(4,3)\rightarrow(4,4)$$, paying only when moving to (2, 3) and (4, 4). In the fourth sample, Okabe can take the path $$(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(3,2)\rightarrow(3,3)\rightarrow(4,4...
[{"input": "4 4 5\n1 1\n2 1\n2 3\n3 3\n4 3", "output": "2"}, {"input": "5 5 4\n1 1\n2 1\n3 1\n3 2", "output": "-1"}, {"input": "2 2 4\n1 1\n1 2\n2 1\n2 2", "output": "0"}, {"input": "5 5 4\n1 1\n2 2\n3 3\n4 4", "output": "3"}]
2,200
["dfs and similar", "graphs", "shortest paths"]
69
[{"input": "4 4 5\r\n1 1\r\n2 1\r\n2 3\r\n3 3\r\n4 3\r\n", "output": "2\r\n"}, {"input": "5 5 4\r\n1 1\r\n2 1\r\n3 1\r\n3 2\r\n", "output": "-1\r\n"}, {"input": "2 2 4\r\n1 1\r\n1 2\r\n2 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 5 4\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n", "output": "3\r\n"}, {"input": "7 10 53\r\n1 1\r...
false
stdio
null
true
549/E
549
E
Python 3
PRETESTS
4
46
0
11463288
def dist(x1, y1, x2, y2): return ((y1-y2)**2+(x1-x2)**2)**(1/2) n, m = list(map(int, input().split())) mishamax = 0 mishamin = float('inf') sashamax = 0 sashamin = float('inf') mishax, mishay = 0, 0 sashax, sashay = 0, 0 misha = [] sasha = [] for i in range(n): x, y = list(map(int, input().split())) misha.a...
85
108
1,024,000
282613017
# Read the first line of input which contains n and m nm = input() nOm = nm.split() n = int(nOm[0]) # Number of Misha's points m = int(nOm[1]) # Number of Sasha's points # Initialize lists to store the coordinates of the points a = [] # Misha's points b = [] # Sasha's points # Read Misha's points for i in range(0...
Looksery Cup 2015
CF
2,015
2
256
Sasha Circle
Berlanders like to eat cones after a hard day. Misha Square and Sasha Circle are local authorities of Berland. Each of them controls its points of cone trade. Misha has n points, Sasha — m. Since their subordinates constantly had conflicts with each other, they decided to build a fence in the form of a circle, so that ...
The first line contains two integers n and m (1 ≤ n, m ≤ 10000), numbers of Misha's and Sasha's trade points respectively. The next n lines contains pairs of space-separated integers Mx, My ( - 104 ≤ Mx, My ≤ 104), coordinates of Misha's trade points. The next m lines contains pairs of space-separated integers Sx, Sy...
The only output line should contain either word "YES" without quotes in case it is possible to build a such fence or word "NO" in the other case.
null
In the first sample there is no possibility to separate points, because any circle that contains both points ( - 1, 0), (1, 0) also contains at least one point from the set (0, - 1), (0, 1), and vice-versa: any circle that contains both points (0, - 1), (0, 1) also contains at least one point from the set ( - 1, 0), ...
[{"input": "2 2\n-1 0\n1 0\n0 -1\n0 1", "output": "NO"}, {"input": "4 4\n1 0\n0 1\n-1 0\n0 -1\n1 1\n-1 1\n-1 -1\n1 -1", "output": "YES"}]
2,700
["geometry", "math"]
85
[{"input": "2 2\r\n-1 0\r\n1 0\r\n0 -1\r\n0 1\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n1 0\r\n0 1\r\n-1 0\r\n0 -1\r\n1 1\r\n-1 1\r\n-1 -1\r\n1 -1\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n-1 0\r\n1 0\r\n0 -2\r\n0 0\r\n0 2\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n-3 -4\r\n3 2\r\n1 5\r\n4 0\r\n5 2\r\n-2 -1\r\...
false
stdio
null
true
107/A
107
A
PyPy 3
TESTS
10
77
3,276,800
131734598
n, m = list(map(int, input().split())) grath = {} used = [False for i in range(n + 1)] for i in range(n): grath[i + 1] = [[], []] for i in range(m): x = list(map(int,input().split())) grath[x[0]][1].append(x[1]) grath[x[0]][1].append(x[2]) grath[x[1]][0].append(x[0]) grath[x[1]][0].append(x[2]) ...
38
77
307,200
29620288
n, m = map(int, input().split()) t, p = [], [[0, 0, 0] for i in range(n + 1)] for i in range(m): a, b, d = map(int, input().split()) p[b][1], p[a][0], p[a][2] = a, b, d for a in range(1, n + 1): if p[a][1] == 0: b, c, d = a, p[a][0], p[a][2] if not c: continue while c: if...
Codeforces Beta Round 83 (Div. 1 Only)
CF
2,011
1
256
Dorm Water Supply
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle). For each house, the...
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ...
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water...
null
null
[{"input": "3 2\n1 2 10\n2 3 20", "output": "1\n1 3 10"}, {"input": "3 3\n1 2 20\n2 3 10\n3 1 5", "output": "0"}, {"input": "4 2\n1 2 60\n3 4 50", "output": "2\n1 2 60\n3 4 50"}]
1,400
["dfs and similar", "graphs"]
38
[{"input": "3 2\r\n1 2 10\r\n2 3 20\r\n", "output": "1\n1 3 10\n"}, {"input": "3 3\r\n1 2 20\r\n2 3 10\r\n3 1 5\r\n", "output": "0\n"}, {"input": "4 2\r\n1 2 60\r\n3 4 50\r\n", "output": "2\n1 2 60\n3 4 50\n"}, {"input": "10 10\r\n10 3 70\r\n1 9 98\r\n9 10 67\r\n5 2 78\r\n8 6 71\r\n4 8 95\r\n7 1 10\r\n2 5 73\r\n6 7 94\...
false
stdio
null
true
107/A
107
A
PyPy 3-64
TESTS
10
93
4,505,600
228609952
n, p = list(map(int, input().split())) adj = [[] for _ in range(n)] for _ in range(p): u, v, w = list(map(int, input().split())) adj[u - 1].append((v - 1, w)) # find tanks, those are the nodes that have no incoming edges tanks = [] for u in range(n): if not any(v == u for x in adj for v, _ in x): ...
38
77
4,505,600
114314322
graph = input() graph = graph.split(' ') n = int(graph[0]) p = int(graph[1]) matrix =[[0] * n for i in range(n)] conn = dict() for i in range(p): line = input() line = line.split(' ') matrix[int(line[0])-1][int(line[1])-1] = int(line[2]) conn[int(line[0])-1]=int(line[1])-1 iniciales = [i for i in co...
Codeforces Beta Round 83 (Div. 1 Only)
CF
2,011
1
256
Dorm Water Supply
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle). For each house, the...
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ...
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water...
null
null
[{"input": "3 2\n1 2 10\n2 3 20", "output": "1\n1 3 10"}, {"input": "3 3\n1 2 20\n2 3 10\n3 1 5", "output": "0"}, {"input": "4 2\n1 2 60\n3 4 50", "output": "2\n1 2 60\n3 4 50"}]
1,400
["dfs and similar", "graphs"]
38
[{"input": "3 2\r\n1 2 10\r\n2 3 20\r\n", "output": "1\n1 3 10\n"}, {"input": "3 3\r\n1 2 20\r\n2 3 10\r\n3 1 5\r\n", "output": "0\n"}, {"input": "4 2\r\n1 2 60\r\n3 4 50\r\n", "output": "2\n1 2 60\n3 4 50\n"}, {"input": "10 10\r\n10 3 70\r\n1 9 98\r\n9 10 67\r\n5 2 78\r\n8 6 71\r\n4 8 95\r\n7 1 10\r\n2 5 73\r\n6 7 94\...
false
stdio
null
true
279/B
279
B
PyPy 3-64
TESTS
8
154
13,107,200
223774199
a , b = map(int,input().split()) li = list(map(int,input().split())) l , r , c = 0 , 0 , 0 ans = 0 while r < a: if c < b: c+=li[r] r+=1 if c <= b : ans = r - l else: c -= li[l] ; l +=1 print(ans)
38
154
11,059,200
227748962
n,t = list(map(int,input().split())) nums = [int(i) for i in input().split()] r = 0 tot = 0 ans = 0 for i in range(n): while r < n and tot + nums[r] <= t: tot += nums[r] r += 1 ans = max(ans, r - i) tot -= nums[i] print(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
279/B
279
B
Python 3
TESTS
8
154
5,734,400
196296021
n, t = [int(i) for i in input().split()] k = [int(i) for i in input().split()] s = sum(k) if s <= t: print(n) else: x = 0 y = n-1 while s > t: if k[x] > k[y]: s -= k[x] x += 1 else: s -= k[y] y -= 1 print(y-x+1)
38
154
11,059,200
230433557
def solve(): n, k = map(int, input().split()) l = [int(i) for i in input().split()] cur = 0 r = 0 ans = 0 for i in range(n): while r < n and cur + l[r] <= k: cur += l[r] r += 1 ans = max(ans, r-i) cur -= l[i] print(ans) # t = int(input()) t =...
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
109/D
109
D
PyPy 3
TESTS
1
248
0
83812044
#!/usr/bin/env python3 def is_lucky(v): while v > 0: r = v % 10 if r != 4 and r != 7: return False v //= 10 return True n = int(input()) arr = list(map(int, input().split())) arr_with_pos = sorted([[a, i] for i, a in enumerate(arr)]) pos, tpos = None, None # pos and targ...
76
1,684
54,067,200
223431265
def check(x): return all(digit not in str(x) for digit in "01235689") def change(a, b): x, y = rpos[a], rpos[b] if x == y: return 1 res.append((x + 1, y + 1)) rpos[pos[x]], rpos[pos[y]] = rpos[pos[y]], rpos[pos[x]] pos[x], pos[y] = pos[y], pos[x] return 1 n = int(input()) a = list(...
Codeforces Beta Round 84 (Div. 1 Only)
CF
2,011
3
256
Lucky Sorting
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya got an array consisting of n numbers, it is the gift for his birthday. Now he wants to sort it...
The first line contains an integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n positive integers, not exceeding 109 — the array that needs to be sorted in the non-decreasing order.
On the first line print number k (0 ≤ k ≤ 2n) — the number of the swaps in the sorting. On the following k lines print one pair of distinct numbers (a pair per line) — the indexes of elements to swap. The numbers in the array are numbered starting from 1. If it is impossible to sort the given sequence, print the single...
null
null
[{"input": "2\n4 7", "output": "0"}, {"input": "3\n4 2 1", "output": "1\n1 3"}, {"input": "7\n77 66 55 44 33 22 11", "output": "7\n1 7\n7 2\n2 6\n6 7\n3 4\n5 3\n4 5"}]
2,000
["constructive algorithms", "sortings"]
76
[{"input": "2\r\n4 7\r\n", "output": "0\r\n"}, {"input": "3\r\n4 2 1\r\n", "output": "1\r\n1 3\r\n"}, {"input": "7\r\n77 66 55 44 33 22 11\r\n", "output": "9\r\n4 7\r\n1 7\r\n1 6\r\n2 6\r\n2 5\r\n3 5\r\n2 3\r\n1 2\r\n1 4\r\n"}, {"input": "7\r\n1 2 3 4 5 6 7\r\n", "output": "0\r\n"}, {"input": "4\r\n47 1 7 2\r\n", "outp...
false
stdio
import sys def is_lucky(num): s = str(num) for c in s: if c not in {'4', '7'}: return False return True def can_be_sorted(arr): sorted_arr = sorted(arr) if arr == sorted_arr: return True return any(is_lucky(num) for num in arr) def main(input_path, output_path, sub...
true
4/D
4
D
PyPy 3-64
TESTS
3
46
0
222174153
# import sys # # sys.stdin = open('input.txt', 'r') def main(): n, w, h = map(int, input().split()) envelopes = [] for i in range(n): temp_w, temp_h = map(int, input().split()) if temp_h >= h and temp_w >= w: envelopes.append((temp_w, temp_h, i + 1)) if len(envelopes) ==...
33
311
5,017,600
222172243
import sys input = sys.stdin.readline n, width, height = map(int, input().split()) arr = [] for i in range(n): w, h = map(int, input().split()) if w > width and h > height: arr.append((w, h, i+1)) F = [1]*len(arr) parent = [-1]*len(arr) arr.sort(key=lambda x:x[0], reverse=True) for i in range(le...
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
186/B
186
B
PyPy 3-64
TESTS
3
62
0
173504937
n,t1,t2,k = list(map(int, input().split())) players = [] for i in range(n): a,b = sorted(list(map(int,input().split()))) before = a*t1 after = b*t2 before -= (before * k / 100) players.append((before + after,i + 1)) players.sort(key = lambda i: (-i[0], i[1])) for val,i in players: print(i,...
36
92
204,800
217927052
n, t1, t2, k = map(int, input().split()) k = k / 100 # print(k) dashboard = {} for _ in range(n): a, b = map(int, input().split()) meters1 = (a * t1) - (a * t1 * k) + (b * t2) meters2 = (b * t1) - (b * t1 * k) + (a * t2) # print(meters) dashboard[_ + 1] = [max(meters1, meters2)] dashboard = sorted(d...
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Growing Mushrooms
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ...
The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two inte...
Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
null
- First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3.
[{"input": "2 3 3 50\n2 4\n4 2", "output": "1 15.00\n2 15.00"}, {"input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970", "output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99"}]
1,200
["greedy", "sortings"]
36
[{"input": "2 3 3 50\r\n2 4\r\n4 2\r\n", "output": "1 15.00\r\n2 15.00\r\n"}, {"input": "4 1 1 1\r\n544 397\r\n280 101\r\n280 101\r\n693 970\r\n", "output": "4 1656.07\r\n1 937.03\r\n2 379.99\r\n3 379.99\r\n"}, {"input": "10 1 1 25\r\n981 1\r\n352 276\r\n164 691\r\n203 853\r\n599 97\r\n901 688\r\n934 579\r\n910 959\r\n...
false
stdio
null
true
289/A
289
A
Python 3
TESTS
4
216
6,656,000
89611046
n,k= map(int, input().split()) s = 0 for i in range(n): l,r = map(int, input().split()) s+= r-l + 1 res = k - (s%k) if res == 7: res = 0 print(res)
28
278
0
157929794
import sys input = sys.stdin.readline n, k = map(int, input().split()) c = 0 for _ in range(n): a, b = map(int, input().split()) c += b-a+1 print(k-c%k if c%k != 0 else 0)
Codeforces Round 177 (Div. 2)
CF
2,013
2
256
Polo the Penguin and Segments
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the r...
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequal...
In a single line print a single integer — the answer to the problem.
null
null
[{"input": "2 3\n1 2\n3 4", "output": "2"}, {"input": "3 7\n1 2\n3 3\n4 7", "output": "0"}]
1,100
["brute force", "implementation"]
28
[{"input": "2 3\r\n1 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3 7\r\n1 2\r\n3 3\r\n4 7\r\n", "output": "0\r\n"}, {"input": "3 7\r\n1 10\r\n11 47\r\n74 128\r\n", "output": "3\r\n"}, {"input": "5 4\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "7 4\r\n2 2\r\n-1 -1\r\n0 1\r\n7 8\r\n-3 -2\r\n9...
false
stdio
null
true
61/D
61
D
Python 3
TESTS
2
109
307,200
77089898
from os import sys num = int(input()) vertices=[] [vertices.append([]) for c in range(num+1)] weightUnder=[-1]*(num+1) visited = [-1]*(num+1) sys.setrecursionlimit(100000) def calculateWeight(i): totalWeightUnder=0 visited[i]=1 for (vert,weight) in vertices[i]: if visited[vert]==-1: tot...
56
514
16,793,600
137852017
def dfs(g, x, p): cost = 0 for y, w in g[x]: if not y == p: cost = max(dfs(g, y, x)+w, cost) return cost n = int(input()) g = [[] for i in range(n+1)] ans = 0 for _ in range(n-1): x, y, w = map(int, input().split()) g[x].append((y, w)) g[y].append((x, w)) ans+= 2*w an...
Codeforces Beta Round 57 (Div. 2)
CF
2,011
2
256
Eternal Victory
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal! He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too ti...
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities. Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
A single integer number, the minimal length of Shapur's travel. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
null
null
[{"input": "3\n1 2 3\n2 3 4", "output": "7"}, {"input": "3\n1 2 3\n1 3 3", "output": "9"}]
1,800
["dfs and similar", "graphs", "greedy", "shortest paths", "trees"]
56
[{"input": "3\r\n1 2 3\r\n2 3 4\r\n", "output": "7\r\n"}, {"input": "3\r\n1 2 3\r\n1 3 3\r\n", "output": "9\r\n"}, {"input": "5\r\n5 3 60\r\n4 3 63\r\n2 1 97\r\n3 1 14\r\n", "output": "371\r\n"}, {"input": "3\r\n2 1 63\r\n3 1 78\r\n", "output": "204\r\n"}, {"input": "13\r\n8 2 58\r\n2 1 49\r\n13 10 41\r\n11 9 67\r\n6 4...
false
stdio
null
true
61/D
61
D
PyPy 3-64
TESTS
2
62
0
215534219
import sys from collections import defaultdict input = sys.stdin.readline n = int(input()) d = defaultdict(list) s = 0 for i in range(n - 1): u, v, w = map(int, input().split()) d[u].append([v, w]) d[v].append([u, w]) s += w v = [0] * (n + 1) f = [0] * (n + 1) l = [[0, 0, i] for i in rang...
56
514
19,251,200
117504169
# Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from heapq import * from math import inf def main(): n=int(input()) tree,su=[[] for _ in range(n+1)],0 for i in range(n-1): x,y,w=map(int,input().split()) tree[x].append((y,w))...
Codeforces Beta Round 57 (Div. 2)
CF
2,011
2
256
Eternal Victory
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal! He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too ti...
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities. Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
A single integer number, the minimal length of Shapur's travel. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
null
null
[{"input": "3\n1 2 3\n2 3 4", "output": "7"}, {"input": "3\n1 2 3\n1 3 3", "output": "9"}]
1,800
["dfs and similar", "graphs", "greedy", "shortest paths", "trees"]
56
[{"input": "3\r\n1 2 3\r\n2 3 4\r\n", "output": "7\r\n"}, {"input": "3\r\n1 2 3\r\n1 3 3\r\n", "output": "9\r\n"}, {"input": "5\r\n5 3 60\r\n4 3 63\r\n2 1 97\r\n3 1 14\r\n", "output": "371\r\n"}, {"input": "3\r\n2 1 63\r\n3 1 78\r\n", "output": "204\r\n"}, {"input": "13\r\n8 2 58\r\n2 1 49\r\n13 10 41\r\n11 9 67\r\n6 4...
false
stdio
null
true
355/B
355
B
Python 3
TESTS
3
108
307,200
77215548
rubles = list(map(int, input().split())) transport = list(map(int, input().split())) bus = list(map(int, input().split())) trolley = list(map(int, input().split())) bus_price = [rubles[2]] trolley_price = [rubles[2]] def get_prices(price): sum = 0 for item in price: if item < rubles[1]: su...
27
46
0
183497126
""" https://codeforces.com/problemset/problem/355/B """ c1, c2, c3, c4 = [int(x) for x in input().split()] n, m = [int(x) for x in input().split()] bus = [int(x) for x in input().split()] trolleys = [int(x) for x in input().split()] cb, tb = 0, 0 for v in bus: if c2 < v * c1: cb += c2 else: cb...
Codeforces Round 206 (Div. 2)
CF
2,013
1
256
Vasya and Public Transport
Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m. Public transport is not free. There are 4 types of tickets: 1. A ticket for one ride...
The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets. The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of buses and trolleys Vasya is going to use. The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Vasya is go...
Print a single number — the minimum sum of burles Vasya will have to spend on the tickets.
null
In the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1) + 3 + 7 = 12 burles. In the second sample the profitable strategy is to buy one ticket of t...
[{"input": "1 3 7 19\n2 3\n2 5\n4 4 4", "output": "12"}, {"input": "4 3 2 1\n1 3\n798\n1 2 3", "output": "1"}, {"input": "100 100 8 100\n3 5\n7 94 12\n100 1 47 0 42", "output": "16"}]
1,100
["greedy", "implementation"]
27
[{"input": "1 3 7 19\r\n2 3\r\n2 5\r\n4 4 4\r\n", "output": "12\r\n"}, {"input": "4 3 2 1\r\n1 3\r\n798\r\n1 2 3\r\n", "output": "1\r\n"}, {"input": "100 100 8 100\r\n3 5\r\n7 94 12\r\n100 1 47 0 42\r\n", "output": "16\r\n"}, {"input": "3 103 945 1000\r\n7 9\r\n34 35 34 35 34 35 34\r\n0 0 0 0 0 0 0 0 0\r\n", "output": ...
false
stdio
null
true
449/B
449
B
PyPy 3
TESTS
4
1,980
73,625,600
148193998
# ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode...
45
1,871
131,072,000
167802107
import sys import math import collections from heapq import heappush, heappop input = sys.stdin.readline ints = lambda: list(map(int, input().split())) n, m, k = ints() graph = [[] for _ in range(n + 1)] for _ in range(m): a, b, c = ints() graph[a].append((b, c, 0)) graph[b].append((a, c, 0)) trains = ...
Codeforces Round 257 (Div. 1)
CF
2,014
2
256
Jzzhu and Cities
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One ...
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least o...
Output a single integer representing the maximum number of the train routes which can be closed.
null
null
[{"input": "5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "output": "2"}, {"input": "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3", "output": "2"}]
2,000
["graphs", "greedy", "shortest paths"]
45
[{"input": "5 5 3\r\n1 2 1\r\n2 3 2\r\n1 3 3\r\n3 4 4\r\n1 5 5\r\n3 5\r\n4 5\r\n5 5\r\n", "output": "2\r\n"}, {"input": "2 2 3\r\n1 2 2\r\n2 1 3\r\n2 1\r\n2 2\r\n2 3\r\n", "output": "2\r\n"}, {"input": "5 4 3\r\n1 2 999999999\r\n2 3 1000000000\r\n3 4 529529529\r\n5 1 524524524\r\n5 524444444\r\n5 529999999\r\n2 1000000...
false
stdio
null
true
455/B
455
B
PyPy 3-64
TESTS
4
46
0
216139793
def is_winner(words, k): # Build a set to store all the words for fast lookup. word_set = set(words) # Define a memoization dictionary to store the results of subproblems. memo = {} def can_win(word): # If the current word is already in the memo, return the result directly. if word...
75
701
41,984,000
229069437
max_alpha = 26 class TrieNode: def __init__(self): self.children = [0] * max_alpha def add_trie(root, word): v = root for char in word: c = ord(char) - ord('a') if not v.children[c]: v.children[c] = TrieNode() v = v.children[c] def dfs(v): v.win = False ...
Codeforces Round 260 (Div. 1)
CF
2,014
1
256
A Lot of Games
Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players. Given a group of n non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the re...
The first line contains two integers, n and k (1 ≤ n ≤ 105; 1 ≤ k ≤ 109). Each of the next n lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters.
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
null
null
[{"input": "2 3\na\nb", "output": "First"}, {"input": "3 1\na\nb\nc", "output": "First"}, {"input": "1 2\nab", "output": "Second"}]
1,900
["dfs and similar", "dp", "games", "implementation", "strings", "trees"]
75
[{"input": "2 3\r\na\r\nb\r\n", "output": "First\r\n"}, {"input": "3 1\r\na\r\nb\r\nc\r\n", "output": "First\r\n"}, {"input": "1 2\r\nab\r\n", "output": "Second\r\n"}, {"input": "5 6\r\nabas\r\ndsfdf\r\nabacaba\r\ndartsidius\r\nkolobok\r\n", "output": "Second\r\n"}, {"input": "4 2\r\naaaa\r\nbbbb\r\nccccc\r\ndumbavumba...
false
stdio
null
true
289/A
289
A
Python 3
TESTS
4
216
0
94058580
n , k = map(int,input().split()) s = 0 for _ in range(n): l , r = map(int,input().split()) s +=r-l+1 if s <= k: print(k-s) if s >k: t = s%k print(k-t)
28
310
0
172509592
''' # Submitted By M7moud Ala3rj Don't Copy This Code, CopyRight . [email protected] © 2022-2023 :) ''' # Problem Name = "Polo the Penguin and Segments" # Class: A from math import ceil import sys #sys.setrecursionlimit(2147483647) input = sys.stdin.readline def print(*args, end='\n', sep=' ') -> None: sys.stdout....
Codeforces Round 177 (Div. 2)
CF
2,013
2
256
Polo the Penguin and Segments
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the r...
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequal...
In a single line print a single integer — the answer to the problem.
null
null
[{"input": "2 3\n1 2\n3 4", "output": "2"}, {"input": "3 7\n1 2\n3 3\n4 7", "output": "0"}]
1,100
["brute force", "implementation"]
28
[{"input": "2 3\r\n1 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3 7\r\n1 2\r\n3 3\r\n4 7\r\n", "output": "0\r\n"}, {"input": "3 7\r\n1 10\r\n11 47\r\n74 128\r\n", "output": "3\r\n"}, {"input": "5 4\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "7 4\r\n2 2\r\n-1 -1\r\n0 1\r\n7 8\r\n-3 -2\r\n9...
false
stdio
null
true
289/A
289
A
Python 3
TESTS
4
186
8,089,600
124452518
import logging l = logging.Logger("") h = logging.StreamHandler() f = logging.Formatter(fmt="[{filename}:{lineno}] {msg}", style="{") h.setFormatter(f) l.addHandler(h) bug=l.info # To disable uncomment the next line # bug = lambda x:None # teste=(1,2,3,4) # bug(f'{teste=}') # bug(f'{2*teste=}') # -------------------...
28
342
0
209876430
n,k=map(int,input().split()) range_number=0 for i in range(n): a,b=map(int,input().split()) range_number=range_number+(b-a)+1 if range_number%k==0: print(0) else: print(k-(range_number%k))
Codeforces Round 177 (Div. 2)
CF
2,013
2
256
Polo the Penguin and Segments
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the r...
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequal...
In a single line print a single integer — the answer to the problem.
null
null
[{"input": "2 3\n1 2\n3 4", "output": "2"}, {"input": "3 7\n1 2\n3 3\n4 7", "output": "0"}]
1,100
["brute force", "implementation"]
28
[{"input": "2 3\r\n1 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3 7\r\n1 2\r\n3 3\r\n4 7\r\n", "output": "0\r\n"}, {"input": "3 7\r\n1 10\r\n11 47\r\n74 128\r\n", "output": "3\r\n"}, {"input": "5 4\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "7 4\r\n2 2\r\n-1 -1\r\n0 1\r\n7 8\r\n-3 -2\r\n9...
false
stdio
null
true
154/A
154
A
Python 3
TESTS
4
154
7,065,600
37552154
s = list(input()) v = {} k = int(input()) for _ in range(k): vs = list(input()) v[vs[0]] = vs[1] v[vs[1]] = vs[0] ans = 0 c1 = 1 i = 0 while i < len(s) - 1: if s[i] in v and s[i+1] == v[s[i]]: j = i+1 c2 = 0 while j < len(s) and s[j] == s[i+1]: c2 += 1 j ...
42
216
2,662,400
169072088
import sys;sc = sys.stdin.readline;out=sys.stdout.write s=str(sc());n=int(sc());ans=0 for e in range(n): ss=str(sc());a,b=0,0 for e in s: if e==ss[0]:a+=1 elif e==ss[1]:b+=1 else :ans+=min(a,b);a=0;b=0 ans+=min(a,b) out(str(ans))
Codeforces Round 109 (Div. 1)
CF
2,012
2
256
Hometask
Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about ...
The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbi...
Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
null
In the first sample you should remove two letters b. In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
[{"input": "ababa\n1\nab", "output": "2"}, {"input": "codeforces\n2\ndo\ncs", "output": "1"}]
1,600
["greedy"]
42
[{"input": "ababa\r\n1\r\nab\r\n", "output": "2\r\n"}, {"input": "codeforces\r\n2\r\ndo\r\ncs\r\n", "output": "1\r\n"}, {"input": "nllnrlrnll\r\n1\r\nrl\r\n", "output": "1\r\n"}, {"input": "aludfbjtwnkgnfl\r\n1\r\noy\r\n", "output": "0\r\n"}, {"input": "pgpgppgggpbbnnn\r\n2\r\npg\r\nnb\r\n", "output": "7\r\n"}, {"input...
false
stdio
null
true
186/B
186
B
Python 3
TESTS
3
62
0
221484358
n, t1, t2, k = map(int, input().split()) results = [] # Initialize a list to store results for i in range(n): a, b = map(int, input().split()) a,b = min(a,b), max(a,b) # Calculate the growth in the first part growth_1 = a * t1 # Calculate the reduction during the break reduced_growth = ...
36
92
204,800
225755346
n, t1, t2, k = map(int, input().split()) plans = {} for i in range(n): a, b = map(int, input().split()) plan1 = ((a * t1) * ((100-k)/100)) + (b * t2) plan2 = ((b * t1) * ((100-k)/100)) + (a * t2) plan = plan1 if plan1 > plan2 else plan2 plans[i+1] = plan sorted_dict = dict(sorted(plans.item...
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Growing Mushrooms
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ...
The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two inte...
Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
null
- First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3.
[{"input": "2 3 3 50\n2 4\n4 2", "output": "1 15.00\n2 15.00"}, {"input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970", "output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99"}]
1,200
["greedy", "sortings"]
36
[{"input": "2 3 3 50\r\n2 4\r\n4 2\r\n", "output": "1 15.00\r\n2 15.00\r\n"}, {"input": "4 1 1 1\r\n544 397\r\n280 101\r\n280 101\r\n693 970\r\n", "output": "4 1656.07\r\n1 937.03\r\n2 379.99\r\n3 379.99\r\n"}, {"input": "10 1 1 25\r\n981 1\r\n352 276\r\n164 691\r\n203 853\r\n599 97\r\n901 688\r\n934 579\r\n910 959\r\n...
false
stdio
null
true
186/B
186
B
Python 3
TESTS
3
62
0
217925408
n, t1, t2, k = map(int, input().split()) k = k / 100 # print(k) dashboard = {} for _ in range(n): a, b = map(int, input().split()) meters = min(a, b) * t1 - (min(a, b) * t1 * k) + max(a, b) * t2 # print(meters) dashboard[_ + 1] = [meters] dashboard = sorted(dashboard.items(), key=lambda x: x[1], reverse...
36
92
4,505,600
134721286
n,t1,t2,k=list(map(int,input().split(" "))) k = (100-k) * 0.01 ai=[] bi=[] for _ in range(n): a,b = list(map(int,input().split(" "))) ai.append(a) bi.append(b) scores=list() for i,j in zip(ai,bi) : scores.append( round( max( (i*t1*k)+(t2*j),(t1*j*k)+(t2*i)) ,2) ) for i,j in sorted(zip(range(1,n+1),sco...
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Growing Mushrooms
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ...
The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two inte...
Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
null
- First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3.
[{"input": "2 3 3 50\n2 4\n4 2", "output": "1 15.00\n2 15.00"}, {"input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970", "output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99"}]
1,200
["greedy", "sortings"]
36
[{"input": "2 3 3 50\r\n2 4\r\n4 2\r\n", "output": "1 15.00\r\n2 15.00\r\n"}, {"input": "4 1 1 1\r\n544 397\r\n280 101\r\n280 101\r\n693 970\r\n", "output": "4 1656.07\r\n1 937.03\r\n2 379.99\r\n3 379.99\r\n"}, {"input": "10 1 1 25\r\n981 1\r\n352 276\r\n164 691\r\n203 853\r\n599 97\r\n901 688\r\n934 579\r\n910 959\r\n...
false
stdio
null
true
551/A
551
A
Python 3
TESTS
17
171
512,000
64460943
n=int(input()) l=list(map(int,input().split())) r=set(l) y=list(r) y.reverse() c=0 z=[] for i in range(len(y)): z.append(c+1) q=l.count(y[i]) c+=q for i in range(n): r=y.index(l[i]) print(z[r],end=" ")
36
31
102,400
146582377
n=int(input()) arr=[int(i) for i in input().split(" ")] temp=arr[:] temp.sort(reverse=True) d={} i=0 k=1 while i < n: d[temp[i]]=k while(i+1 < n and temp[i]==temp[i+1]): i+=1 k+=1 i+=1 k+=1 result="" for i in arr: result+=str(d[i])+" " print(result)
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
186/B
186
B
Python 3
TESTS
5
124
204,800
105491783
n , t1 , t2 , k = map(int , input().split()) speeds = [] for i in range(n): u , v = map(int , input().split()) speeds.append([u , v]) tally = [] for i in range(n): if t1*(k/100) > t2: tally.append([i + 1 , (t1*((100-k)/100)*max(speeds[i][0] , speeds[i][1])) + t2*(min(speeds[i][0] , speeds[i][1])...
36
122
4,505,600
134923908
n, t1, t2, k = [int(x) for x in input().split()] k = (100-k)/100 res = [] dict = {} for i in range(n): a, b = [int(x) for x in input().split()] temp = max(a*t1*k + b*t2, b*t1*k + a*t2) if temp in dict: dict[temp].append(i+1) else: dict[temp] = [i+1] res.append(temp) res.sort...
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Growing Mushrooms
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ...
The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two inte...
Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
null
- First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3.
[{"input": "2 3 3 50\n2 4\n4 2", "output": "1 15.00\n2 15.00"}, {"input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970", "output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99"}]
1,200
["greedy", "sortings"]
36
[{"input": "2 3 3 50\r\n2 4\r\n4 2\r\n", "output": "1 15.00\r\n2 15.00\r\n"}, {"input": "4 1 1 1\r\n544 397\r\n280 101\r\n280 101\r\n693 970\r\n", "output": "4 1656.07\r\n1 937.03\r\n2 379.99\r\n3 379.99\r\n"}, {"input": "10 1 1 25\r\n981 1\r\n352 276\r\n164 691\r\n203 853\r\n599 97\r\n901 688\r\n934 579\r\n910 959\r\n...
false
stdio
null
true
895/D
895
D
PyPy 3
TESTS
10
139
1,228,800
102141405
MOD = int(1e9 + 7) #primo para el módulo pows = 0 fact = 0 n = 0 def perms(m, nums): """ Entradas: m -> entero con la cantidad total de elementos a permutar nums -> lista de enteros donde la posicón i tiene las ocurrencias del i-ésimo elemento Descripción: Función que calcula las ...
60
1,918
55,705,600
115937069
from typing import List, Set, Dict, Tuple, Text, Optional, Callable, Any from collections import deque import collections from types import GeneratorType import itertools import operator import functools import random import copy import heapq import math import sys import os from atexit import register from io import B...
Codeforces Round 448 (Div. 2)
CF
2,017
4
256
String Mark
At the Byteland State University marks are strings of the same length. Mark x is considered better than y if string y is lexicographically smaller than x. Recently at the BSU was an important test work on which Vasya recived the mark a. It is very hard for the teacher to remember the exact mark of every student, but h...
First line contains string a, second line contains string b. Strings a, b consist of lowercase English letters. Their lengths are equal and don't exceed 106. It is guaranteed that a is lexicographically smaller than b.
Print one integer  — the number of different strings satisfying the condition of the problem modulo 109 + 7.
null
In first sample from string abc can be obtained strings acb, bac, bca, cab, cba, all of them are larger than abc, but smaller than ddd. So the answer is 5. In second sample any string obtained from abcdef is larger than abcdeg. So the answer is 0.
[{"input": "abc\nddd", "output": "5"}, {"input": "abcdef\nabcdeg", "output": "0"}, {"input": "abacaba\nubuduba", "output": "64"}]
2,100
["combinatorics", "math", "strings"]
60
[{"input": "abc\r\nddd\r\n", "output": "5\r\n"}, {"input": "abcdef\r\nabcdeg\r\n", "output": "0\r\n"}, {"input": "abacaba\r\nubuduba\r\n", "output": "64\r\n"}, {"input": "aac\r\nbbb\r\n", "output": "1\r\n"}, {"input": "aaaccc\r\nbbbbbb\r\n", "output": "9\r\n"}, {"input": "aaaaaa\r\nzzzzzz\r\n", "output": "0\r\n"}, {"in...
false
stdio
null
true
354/A
354
A
PyPy 3
TESTS
4
77
17,715,200
132757448
# Author Name: Ajay Meena # Codeforce : https://codeforces.com/profile/majay1638 import sys import math import bisect import heapq from bisect import bisect_right from sys import stdin, stdout # -------------- INPUT FUNCTIONS ------------------ def get_ints_in_variables(): return map( int, sys.stdin.readline().s...
23
93
13,209,600
210302820
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(int, input().split())) n, l, r, ql, qr = MII() a = LII() pre = [0] * n suf = [0] * n for i in range(n): pre[i] = a[i] if i...
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
353/C
353
C
Python 3
TESTS
2
62
0
4746304
m = int(input()) listValue = input().split() x = input().rstrip('\n') reverse_x = list(x) # re = int(x[::-1], 2) reverse_int = [int(s) for s in reverse_x] while reverse_int[-1] == 0 and len(reverse_int) > 1: reverse_int.pop(-1) reverse_len = len(reverse_int) values = [int(ele) for ele in listValue] sum_primary ...
36
184
14,028,800
194276945
import sys input = sys.stdin.readline n = int(input()) w = list(map(int, input().split())) s = input()[:-1] if '1' in s: a = s.rindex('1') b, c = 0, 0 for i in range(a+1): if s[i] == '0': b += w[i] else: c += w[i] q = c x = 0 for i in range(a, -1, -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
354/A
354
A
PyPy 3-64
TESTS
4
31
512,000
156232094
n, l, r, ql, qr = map(int, input().split()) arr = list(map(int,input().split())) s1, s2 = 0, 0 ans = 1e9 prefixsum = [0]+arr.copy() for i in range(1,len(arr)+1): prefixsum[i] += prefixsum[i-1] # 2 1 #print(prefixsum) # n-k-k for k in range(n): #print("k=" + str(k) + " n-k=" + str(n-k)) curr = prefixsum[k] ...
23
93
13,209,600
210724922
n,l,r,ql,qr = map(int, input().split()) a = list(map(int, input().split())) right = sum(a) left = 0 ans = right*r + (n - 1)*qr for i in range(n): left += a[i] right -= a[i] rightTime = n - i - 1 leftTime = i + 1 if leftTime > rightTime: ans = min(ans, left*l + right*r + (leftTime - rightTim...
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
992/D
992
D
Python 3
TESTS
2
124
1,228,800
49909573
from itertools import combinations,permutations from collections import defaultdict import math import sys import os bp=6*(10**18) def solution(n,k,arr): tica=[0]*200007 for i in range(n-1,-1,-1): if arr[i]==1: tica[i]=tica[i+1]+1 else: tica[i]=0 ans=0 for i i...
134
982
24,985,600
108600095
n, k = map(int, input().split()) A = list(map(int, input().split())) from itertools import accumulate C = [0]+A C = list(accumulate(C)) A = [0]+A P = [0]*(n+1) x = 0 # P[i]: iの直前にある2以上の項のindex for i in range(1, n+1): P[i] = x if A[i] > 1: x = i INF = 2*10**18+1 ans = 0 for i in range(1, n+1): p = 1 ...
Codeforces Round 489 (Div. 2)
CF
2,018
2
256
Nastya and a Game
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $$\frac{p}{s} = k$$, where p is the product of all integers on the given array, s is their sum, and k is a given...
The first line contains two integers n and k (1 ≤ n ≤ 2·105, 1 ≤ k ≤ 105), where n is the length of the array and k is the constant described above. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 108) — the elements of the array.
In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k.
null
In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $${ \frac { 1 } { 1 } } = 1$$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $$\frac{18}{9}=2$$. Subsegme...
[{"input": "1 1\n1", "output": "1"}, {"input": "4 2\n6 3 8 1", "output": "2"}]
2,100
["brute force", "implementation", "math"]
134
[{"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "4 2\r\n6 3 8 1\r\n", "output": "2\r\n"}, {"input": "94 58\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 29 58 1 1 1 29 58 58 1 1 29 1 1 1 1 2 1 58 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 29 1 1 1 1 1 58 1 29 1 1 1 1 1 1 1 1 1 1 1 1 58 1 1 1 1 1 2 1 1 1\r\...
false
stdio
null
true
270/B
270
B
PyPy 3-64
TESTS
3
92
0
226171666
n = int(input()) a = [int(x) for x in map(int, input().split())] cnt = 0 for i in range(n - 1, 0, -1): if a[i] < a[i - 1]: cnt += 1 print(cnt)
41
248
10,342,400
136228613
n = int(input()) l = list(map(int,input().split())) ans = n-1 while ans>0 and l[ans-1]<l[ans]: ans-=1 print(ans)
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
19/C
19
C
PyPy 3
TESTS
1
248
0
52644286
n = int(input()) a = list(map(int,input().split()))[:n] b = [] for i in range(len(a)): if a[i] not in b: b.append(a[i]) print(len(b)) print(*b)
70
1,216
14,131,200
174568804
import sys n=int(input()) a=list(map(int,input().split())) M=10**9+1 g={} for i in range(n): g[a[i]]=g.get(a[i],[])+[i] p=[1] for i in range(n): p+=[hash(M*p[-1])] h=[0]*(n+1) for i in range(n): h[i+1]=hash(h[i]*M+a[i]) gh=lambda k,l:hash(h[k+l]-h[k]*p[l])%sys.hash_info.modulus i,t=0,0 while i < n: for ...
Codeforces Beta Round 19
ICPC
2,010
2
256
Deletion of Repeats
Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. Bob didn't like that string, because it contained repeats: a repeat of length x is such a substring of length 2x, that its first half coin...
The first input line contains integer n (1 ≤ n ≤ 105) — length of the string. The following line contains n space-separated integer numbers from 0 to 109 inclusive — numbers that stand for the letters of the string. It's guaranteed that each letter can be met in the string at most 10 times.
In the first line output the length of the string's part, left after Bob's deletions. In the second line output all the letters (separated by a space) of the string, left after Bob deleted all the repeats in the described way.
null
null
[{"input": "6\n1 2 3 1 2 3", "output": "3\n1 2 3"}, {"input": "7\n4 5 6 5 6 7 7", "output": "1\n7"}]
2,200
["greedy", "hashing", "string suffix structures"]
70
[{"input": "6\r\n1 2 3 1 2 3\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "7\r\n4 5 6 5 6 7 7\r\n", "output": "1\r\n7 \r\n"}, {"input": "10\r\n5 7 2 1 8 8 5 10 2 5\r\n", "output": "5\r\n8 5 10 2 5 \r\n"}, {"input": "10\r\n0 1 1 1 0 3 0 1 4 0\r\n", "output": "7\r\n1 0 3 0 1 4 0 \r\n"}, {"input": "10\r\n0 1 0 2 0 0 1 1 ...
false
stdio
null
true
109/D
109
D
PyPy 3
TESTS
1
280
0
100695767
import sys input=sys.stdin.readline def ok(x): z=[] while(x>0): if(x%10==7 or x%10==4): return 1 x=x//10 return 0 n=int(input()) l=input().split() li=[int(i) for i in l] poss=1 for i in range(1,n): if(li[i]<li[i-1]): poss=0 break if(poss): print(0) exi...
76
966
38,502,400
182925810
from collections import defaultdict import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n = int(input()) a = [0] + list(map(int, input().split())) x = list(a) x.sort() s = 0 for i in range(1, n + 1): u = [0] * 10 for j in str(a[i]): u[int(j)] = 1 ok = 1 for j in rang...
Codeforces Beta Round 84 (Div. 1 Only)
CF
2,011
3
256
Lucky Sorting
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya got an array consisting of n numbers, it is the gift for his birthday. Now he wants to sort it...
The first line contains an integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n positive integers, not exceeding 109 — the array that needs to be sorted in the non-decreasing order.
On the first line print number k (0 ≤ k ≤ 2n) — the number of the swaps in the sorting. On the following k lines print one pair of distinct numbers (a pair per line) — the indexes of elements to swap. The numbers in the array are numbered starting from 1. If it is impossible to sort the given sequence, print the single...
null
null
[{"input": "2\n4 7", "output": "0"}, {"input": "3\n4 2 1", "output": "1\n1 3"}, {"input": "7\n77 66 55 44 33 22 11", "output": "7\n1 7\n7 2\n2 6\n6 7\n3 4\n5 3\n4 5"}]
2,000
["constructive algorithms", "sortings"]
76
[{"input": "2\r\n4 7\r\n", "output": "0\r\n"}, {"input": "3\r\n4 2 1\r\n", "output": "1\r\n1 3\r\n"}, {"input": "7\r\n77 66 55 44 33 22 11\r\n", "output": "9\r\n4 7\r\n1 7\r\n1 6\r\n2 6\r\n2 5\r\n3 5\r\n2 3\r\n1 2\r\n1 4\r\n"}, {"input": "7\r\n1 2 3 4 5 6 7\r\n", "output": "0\r\n"}, {"input": "4\r\n47 1 7 2\r\n", "outp...
false
stdio
import sys def is_lucky(num): s = str(num) for c in s: if c not in {'4', '7'}: return False return True def can_be_sorted(arr): sorted_arr = sorted(arr) if arr == sorted_arr: return True return any(is_lucky(num) for num in arr) def main(input_path, output_path, sub...
true
1005/A
1005
A
Python 3
TESTS
6
31
0
221947694
num = int(input()) text = input() print(text.count("1")) arr = text.split() arr.append("1") arr[0] = "01" result = list() ctr = 0 for i in arr: if i == "1": result.append(ctr) ctr = 1 else: ctr += 1 print(*result) # Tue Sep 05 2023 17:22:57 GMT+0300 (Moscow Standard Time)
16
31
0
132092901
length = int(input()) full_string = input() def step_counter(full_string): normal_split = full_string.split() t = normal_split.count('1') return_max = [normal_split[i-1] for i in range(1, len(normal_split)) if normal_split[i]=='1'] return_max.append(normal_split[-1]) print(t) print(' '.join(re...
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
510/C
510
C
PyPy 3-64
TESTS
12
62
2,457,600
224622843
def lowercase_letters(): for i in range(ord('a'), ord('z')+1): yield chr(i) lower_chars = {}; for ch in lowercase_letters(): lower_chars[ch] = set() n = int(input()) words = [] for _ in range(n): words.append(input()) def combinations(arr): for i in range(len(arr)-1): for j in range(i...
36
46
0
226856775
ans = [] cycle = False def dfs(curr, arr, visited): if visited[curr] == 0: visited[curr] = 1 for child in arr[curr]: dfs(child, arr, visited) ans.append(chr(curr + 97)) visited[curr] = 2 elif visited[curr] == 1: global cycle cycle = True n = int(inp...
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
5/E
5
E
Python 3
TESTS
5
92
0
175819714
n = int(input()) arr = [*map(int,input().split())] i = arr.index(max(arr)) arr = arr[i:]+arr[:i] stack,res = [[arr[0],1]],0 for j in range(1,n): if arr[j]>stack[-1][0]: while stack[-1][0]<arr[j]: res += stack.pop()[1] if arr[j]<stack[-1][0]: res += 1; stack += [arr[j],1], elif arr[j]==s...
46
2,244
103,526,400
66465098
n = int(input()) hill = tuple(map(int, input().split())) pairs = 0 highest, at = max((h, k) for k, h in enumerate(hill)) last = highest count = 0 p = list() push = p.append pop = p.pop for at in range(at - 1, at - n, -1): current = hill[at] while current > last: pairs += count last, count = pop(...
Codeforces Beta Round 5
ICPC
2,010
4
256
Bindian Signalizing
Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by n hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night. In case of any danger the watchman could make a fire on the hill. One watchman coul...
The first line of the input data contains an integer number n (3 ≤ n ≤ 106), n — the amount of hills around the capital. The second line contains n numbers — heights of the hills in clockwise order. All height numbers are integer and lie between 1 and 109.
Print the required amount of pairs.
null
null
[{"input": "5\n1 2 4 5 3", "output": "7"}]
2,400
["data structures"]
46
[{"input": "5\r\n1 2 4 5 3\r\n", "output": "7\r\n"}, {"input": "3\r\n2118 2118 2118\r\n", "output": "3\r\n"}, {"input": "3\r\n2221 1976 2221\r\n", "output": "3\r\n"}, {"input": "3\r\n140 989 2895\r\n", "output": "3\r\n"}, {"input": "4\r\n2440 2440 2440 2440\r\n", "output": "6\r\n"}, {"input": "4\r\n1178 1178 2577 2577\...
false
stdio
null
true
988/F
988
F
PyPy 3
TESTS
4
124
4,403,200
136114949
def process(A, B, a): n = len(A) A.sort() m = len(B) B.sort() i1 = 0 i2 = 0 answer = 0 umbrellas = {} current_cost = [float('inf') for i in range(a+1)] current_cost[0] = 0 for i in range(a+1): if i1 < n: l, r = A[i1] else: l, r = a+1, a...
37
93
3,174,400
186750059
from collections import defaultdict import sys input = sys.stdin.readline INF = 10 ** 15 a, n, m = map(int, input().split()) rain = [0] * a umb = [-1] * (a+1) for _ in range(n): l, r = map(int, input().split()) for i in range(l, r): rain[i] = 1 for _ in range(m): x, p = map(int, input().split()) ...
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
386/A
386
A
Python 3
TESTS
1
30
0
208287187
n = int(input()) prices = list(map(int, input().split())) sorted_prices_idx = sorted(range(len(prices)), key=lambda x: prices[x], reverse=True) winner = sorted_prices_idx[0] if winner == 0: print(1, prices[1]) else: print(winner + 1, prices[winner - 1])
42
31
0
225813969
n = int(input()) bids = list(map(int, input().split())) highest_bid = -1 second_highest_bid = -1 winner_index = -1 for i in range(n): if bids[i] > highest_bid: second_highest_bid = highest_bid highest_bid = bids[i] winner_index = i elif bids[i] > second_highest_bid: second_highes...
Testing Round 9
CF
2,014
1
256
Second-Price Auction
In this problem we consider a special type of an auction, which is called the second-price auction. As in regular auction n bidders place a bid which is price a bidder ready to pay. The auction is closed, that is, each bidder secretly informs the organizer of the auction price he is willing to pay. After that, the auct...
The first line of the input contains n (2 ≤ n ≤ 1000) — number of bidders. The second line contains n distinct integer numbers p1, p2, ... pn, separated by single spaces (1 ≤ pi ≤ 10000), where pi stands for the price offered by the i-th bidder.
The single output line should contain two integers: index of the winner and the price he will pay. Indices are 1-based.
null
null
[{"input": "2\n5 7", "output": "2 5"}, {"input": "3\n10 2 8", "output": "1 8"}, {"input": "6\n3 8 2 9 4 14", "output": "6 9"}]
800
["implementation"]
42
[{"input": "2\r\n5 7\r\n", "output": "2 5\r\n"}, {"input": "3\r\n10 2 8\r\n", "output": "1 8\r\n"}, {"input": "6\r\n3 8 2 9 4 14\r\n", "output": "6 9\r\n"}, {"input": "4\r\n4707 7586 4221 5842\r\n", "output": "2 5842\r\n"}, {"input": "5\r\n3304 4227 4869 6937 6002\r\n", "output": "4 6002\r\n"}, {"input": "6\r\n5083 328...
false
stdio
null
true
652/E
652
E
PyPy 3
TESTS
1
498
11,571,200
68974672
from random import * if random() > 0.5: print("No") else: print("Yes")
83
2,947
148,377,600
129007124
from collections import deque import sys input = sys.stdin.readline def lowlink(): o, parent, dist, B = dfs(1) l = [n + 1] * (n + 1) for i in range(n + 1): l[i] = o[i] for i in range(1, n + 1): for j in B[i]: l[i] = min(l[i], l[j]) x = [[] for _ in range(n + ...
Educational Codeforces Round 10
ICPC
2,016
3
512
Pursuit For Artifacts
Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience. In that country there are n islands and m bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful ar...
The first line contains two integers n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of islands and bridges in the game. Each of the next m lines contains the description of the bridge — three integers xi, yi, zi (1 ≤ xi, yi ≤ n, xi ≠ yi, 0 ≤ zi ≤ 1), where xi and yi are the islands connected by the i-th bridge, z...
If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes).
null
null
[{"input": "6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6", "output": "YES"}, {"input": "5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4", "output": "NO"}, {"input": "5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2", "output": "YES"}]
2,300
["dfs and similar", "dsu", "graphs", "trees"]
83
[{"input": "6 7\r\n1 2 0\r\n2 3 0\r\n3 1 0\r\n3 4 1\r\n4 5 0\r\n5 6 0\r\n6 4 0\r\n1 6\r\n", "output": "YES\r\n"}, {"input": "5 4\r\n1 2 0\r\n2 3 0\r\n3 4 0\r\n2 5 1\r\n1 4\r\n", "output": "NO\r\n"}, {"input": "5 6\r\n1 2 0\r\n2 3 0\r\n3 1 0\r\n3 4 0\r\n4 5 1\r\n5 3 0\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "1 0\r\...
false
stdio
null
true
108/B
108
B
Python 3
TESTS
3
62
0
219607290
n = int(input()) l = list(map(int, input().split())) l.sort() for i in range(n-1): if l[i] == 1: continue if l[i+1] < l[i] * 2 - 1: print("YES") exit(0) print("NO")
65
342
8,601,600
83196107
#!/usr/bin/env python3 n = int(input()) a = sorted(list(map(int, input().split()))) ok = True for i in range(1, len(a)): if a[i] != a[i-1] and a[i-1] * 2 > a[i]: ok = False break print("NO" if ok else "YES")
Codeforces Beta Round 83 (Div. 2 Only)
CF
2,011
2
256
Datatypes
Tattah's youngest brother, Tuftuf, is new to programming. Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava. Today, Tuftuf was introduced to Gava's unsigned integer datatypes. G...
The first line contains integer n (2 ≤ n ≤ 105) — the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of n integers (1 ≤ ai ≤ 109) — sizes of datatypes in bits. Some datatypes may have equal sizes.
Print "YES" if Tuftuf will stop using Gava, and "NO" otherwise.
null
In the second example, x = 7 (1112) fits in 3 bits, but x2 = 49 (1100012) does not fit in 4 bits.
[{"input": "3\n64 16 32", "output": "NO"}, {"input": "4\n4 2 1 3", "output": "YES"}]
1,400
["math", "sortings"]
65
[{"input": "3\r\n64 16 32\r\n", "output": "NO\r\n"}, {"input": "4\r\n4 2 1 3\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 5 3 3 2\r\n", "output": "YES\r\n"}, {"input": "52\r\n474 24 24 954 9 234 474 114 24 114 234 24 114 114 234 9 9 24 9 54 234 54 9 954 474 9 54 54 54 234 9 114 24 54 114 954 954 474 24 54 54 234 234 4...
false
stdio
null
true
108/B
108
B
Python 3
TESTS
3
92
0
9136053
import sys n = int(input()) sizes = list(map(int, input().split())) sizes.sort() need = 0 for i in range(1, len(sizes)): a, b = sizes[i-1], sizes[i] if a != 1: a *= 2 if a > b: print('YES') sys.exit() print('NO')
65
342
10,752,000
136730296
def s(): input() a = list(map(int,input().split())) a.sort() for i in range(1,len(a)): if a[i]!=a[i-1] and a[i]<a[i-1]*2: return 'YES' return 'NO' print(s())
Codeforces Beta Round 83 (Div. 2 Only)
CF
2,011
2
256
Datatypes
Tattah's youngest brother, Tuftuf, is new to programming. Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava. Today, Tuftuf was introduced to Gava's unsigned integer datatypes. G...
The first line contains integer n (2 ≤ n ≤ 105) — the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of n integers (1 ≤ ai ≤ 109) — sizes of datatypes in bits. Some datatypes may have equal sizes.
Print "YES" if Tuftuf will stop using Gava, and "NO" otherwise.
null
In the second example, x = 7 (1112) fits in 3 bits, but x2 = 49 (1100012) does not fit in 4 bits.
[{"input": "3\n64 16 32", "output": "NO"}, {"input": "4\n4 2 1 3", "output": "YES"}]
1,400
["math", "sortings"]
65
[{"input": "3\r\n64 16 32\r\n", "output": "NO\r\n"}, {"input": "4\r\n4 2 1 3\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 5 3 3 2\r\n", "output": "YES\r\n"}, {"input": "52\r\n474 24 24 954 9 234 474 114 24 114 234 24 114 114 234 9 9 24 9 54 234 54 9 954 474 9 54 54 54 234 9 114 24 54 114 954 954 474 24 54 54 234 234 4...
false
stdio
null
true
109/D
109
D
Python 3
TESTS
4
216
307,200
55968100
#!/usr/bin/env python3 n = int(input()) l = list(map(int, input().split())) # Store original indices in the list sorted_l = [(l[i], i) for i in range(len(l))] sorted_l.sort() sorted_l, destination = zip(*sorted_l) sorted_l = list(sorted_l) destination = list(destination) if (l == sorted_l): # List was already sort...
76
1,684
54,067,200
223431265
def check(x): return all(digit not in str(x) for digit in "01235689") def change(a, b): x, y = rpos[a], rpos[b] if x == y: return 1 res.append((x + 1, y + 1)) rpos[pos[x]], rpos[pos[y]] = rpos[pos[y]], rpos[pos[x]] pos[x], pos[y] = pos[y], pos[x] return 1 n = int(input()) a = list(...
Codeforces Beta Round 84 (Div. 1 Only)
CF
2,011
3
256
Lucky Sorting
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya got an array consisting of n numbers, it is the gift for his birthday. Now he wants to sort it...
The first line contains an integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n positive integers, not exceeding 109 — the array that needs to be sorted in the non-decreasing order.
On the first line print number k (0 ≤ k ≤ 2n) — the number of the swaps in the sorting. On the following k lines print one pair of distinct numbers (a pair per line) — the indexes of elements to swap. The numbers in the array are numbered starting from 1. If it is impossible to sort the given sequence, print the single...
null
null
[{"input": "2\n4 7", "output": "0"}, {"input": "3\n4 2 1", "output": "1\n1 3"}, {"input": "7\n77 66 55 44 33 22 11", "output": "7\n1 7\n7 2\n2 6\n6 7\n3 4\n5 3\n4 5"}]
2,000
["constructive algorithms", "sortings"]
76
[{"input": "2\r\n4 7\r\n", "output": "0\r\n"}, {"input": "3\r\n4 2 1\r\n", "output": "1\r\n1 3\r\n"}, {"input": "7\r\n77 66 55 44 33 22 11\r\n", "output": "9\r\n4 7\r\n1 7\r\n1 6\r\n2 6\r\n2 5\r\n3 5\r\n2 3\r\n1 2\r\n1 4\r\n"}, {"input": "7\r\n1 2 3 4 5 6 7\r\n", "output": "0\r\n"}, {"input": "4\r\n47 1 7 2\r\n", "outp...
false
stdio
import sys def is_lucky(num): s = str(num) for c in s: if c not in {'4', '7'}: return False return True def can_be_sorted(arr): sorted_arr = sorted(arr) if arr == sorted_arr: return True return any(is_lucky(num) for num in arr) def main(input_path, output_path, sub...
true
290/E
290
E
Python 3
TESTS
11
590
17,100,800
36769276
a = input() a = a.split('Q') for i in a: if (len(i) % 2 != 0): print("No") exit(0) print("Yes")
28
1,090
56,320,000
197627903
a = input() b = [] h = '' c = 0 for i in a: if i == 'Q': c += 1 if c == 0: print('Yes') exit(0) r = -1 for i in range(1001): if i*i == c: r = i break if r == -1: print('No') exit(0) h = [a.split('Q')[0], a.split('Q')[-1]] c = [len(h[0]), len(h[1])] if c[0] % 2 != 0 or c[1] % 2 != 0: print('No') exit(0) c[...
April Fools Day Contest 2013
ICPC
2,013
2
256
HQ
The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset — a language called HQ...
The only line of the input is a string between 1 and 106 characters long.
Output "Yes" or "No".
null
The rest of the problem statement was destroyed by a stray raccoon. We are terribly sorry for the inconvenience.
[{"input": "HHHH", "output": "Yes"}, {"input": "HQHQH", "output": "No"}, {"input": "HHQHHQH", "output": "No"}, {"input": "HHQQHHQQHH", "output": "Yes"}]
2,500
["*special", "constructive algorithms"]
28
[{"input": "HHHH\r\n", "output": "Yes\r\n"}, {"input": "HQHQH\r\n", "output": "No\r\n"}, {"input": "HHQHHQH\r\n", "output": "No\r\n"}, {"input": "HHQQHHQQHH\r\n", "output": "Yes\r\n"}, {"input": "Q\r\n", "output": "Yes\r\n"}, {"input": "HHHHHHHHHHQHHH\r\n", "output": "No\r\n"}, {"input": "HHQHQQQHHH\r\n", "output": "No...
false
stdio
null
true
290/E
290
E
Python 3
TESTS
11
748
17,203,200
36769535
a = input() a = a.split('Q') c = 0 for i in a: if i == 'Q': c += 1 for i in a: if (len(i) % 2 != 0): print("No") exit(0) if c % 2 == 0: print("Yes") else: print("No")
28
1,090
56,320,000
197627903
a = input() b = [] h = '' c = 0 for i in a: if i == 'Q': c += 1 if c == 0: print('Yes') exit(0) r = -1 for i in range(1001): if i*i == c: r = i break if r == -1: print('No') exit(0) h = [a.split('Q')[0], a.split('Q')[-1]] c = [len(h[0]), len(h[1])] if c[0] % 2 != 0 or c[1] % 2 != 0: print('No') exit(0) c[...
April Fools Day Contest 2013
ICPC
2,013
2
256
HQ
The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset — a language called HQ...
The only line of the input is a string between 1 and 106 characters long.
Output "Yes" or "No".
null
The rest of the problem statement was destroyed by a stray raccoon. We are terribly sorry for the inconvenience.
[{"input": "HHHH", "output": "Yes"}, {"input": "HQHQH", "output": "No"}, {"input": "HHQHHQH", "output": "No"}, {"input": "HHQQHHQQHH", "output": "Yes"}]
2,500
["*special", "constructive algorithms"]
28
[{"input": "HHHH\r\n", "output": "Yes\r\n"}, {"input": "HQHQH\r\n", "output": "No\r\n"}, {"input": "HHQHHQH\r\n", "output": "No\r\n"}, {"input": "HHQQHHQQHH\r\n", "output": "Yes\r\n"}, {"input": "Q\r\n", "output": "Yes\r\n"}, {"input": "HHHHHHHHHHQHHH\r\n", "output": "No\r\n"}, {"input": "HHQHQQQHHH\r\n", "output": "No...
false
stdio
null
true
290/E
290
E
Python 3
TESTS
10
280
17,100,800
36769470
a = input() b = [a.split('Q')[0], a.split('Q')[-1]] for i in b: if (len(i) % 2 != 0): print("No") exit(0) print("Yes")
28
1,090
56,320,000
197627903
a = input() b = [] h = '' c = 0 for i in a: if i == 'Q': c += 1 if c == 0: print('Yes') exit(0) r = -1 for i in range(1001): if i*i == c: r = i break if r == -1: print('No') exit(0) h = [a.split('Q')[0], a.split('Q')[-1]] c = [len(h[0]), len(h[1])] if c[0] % 2 != 0 or c[1] % 2 != 0: print('No') exit(0) c[...
April Fools Day Contest 2013
ICPC
2,013
2
256
HQ
The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset — a language called HQ...
The only line of the input is a string between 1 and 106 characters long.
Output "Yes" or "No".
null
The rest of the problem statement was destroyed by a stray raccoon. We are terribly sorry for the inconvenience.
[{"input": "HHHH", "output": "Yes"}, {"input": "HQHQH", "output": "No"}, {"input": "HHQHHQH", "output": "No"}, {"input": "HHQQHHQQHH", "output": "Yes"}]
2,500
["*special", "constructive algorithms"]
28
[{"input": "HHHH\r\n", "output": "Yes\r\n"}, {"input": "HQHQH\r\n", "output": "No\r\n"}, {"input": "HHQHHQH\r\n", "output": "No\r\n"}, {"input": "HHQQHHQQHH\r\n", "output": "Yes\r\n"}, {"input": "Q\r\n", "output": "Yes\r\n"}, {"input": "HHHHHHHHHHQHHH\r\n", "output": "No\r\n"}, {"input": "HHQHQQQHHH\r\n", "output": "No...
false
stdio
null
true
285/B
285
B
Python 3
TESTS
31
310
5,734,400
13319703
n,s,t = input().split() n=int(n) s=int(s) t=int(t) l=input().split() for i in range(n): l[i]=int(l[i]) j=0 i=s flag=0 while(j<n): if(i==l[i-1]): if(i==t): flag=1 break break i=l[i-1] if(i==t): flag=1 j+=1 break j+=1 if(flag): print(...
33
154
11,059,200
217177488
A = [int(i) for i in input().split()] n = A[0] s = A[1] t = A[2] B = [int(i) for i in input().split()] used = [0 for i in range(n+1)] count = 0 flag = True if(s==t): print(0) exit(0) i = s-1 while(1): if(B[i]==i+1): print(-1) break elif(B[i]!=i+1 and B[i]!=t): count += 1 if(used[B[i]...
Codeforces Round 175 (Div. 2)
CF
2,013
2
256
Find Marble
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not. First Petya puts a marble under the glass in position s. Then he performs some (pos...
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct. Note that s ca...
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
null
null
[{"input": "4 2 1\n2 3 4 1", "output": "3"}, {"input": "4 3 3\n4 1 3 2", "output": "0"}, {"input": "4 3 4\n1 2 3 4", "output": "-1"}, {"input": "3 1 3\n2 1 3", "output": "-1"}]
1,200
["implementation"]
33
[{"input": "4 2 1\r\n2 3 4 1\r\n", "output": "3\r\n"}, {"input": "4 3 3\r\n4 1 3 2\r\n", "output": "0\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n", "output": "-1\r\n"}, {"input": "3 1 3\r\n2 1 3\r\n", "output": "-1\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "10 6 7\r\n10 7 8 1 5 6 2 9 4 3\r\n", "output...
false
stdio
null
true
290/E
290
E
Python 3
TESTS
8
528
10,137,600
36769923
a = input() b = [] h = '' c = 0 for i in a: if i == 'Q': c += 1 if c == 0: print('Yes') exit(0) r = -1 for i in range(1000): if i*i == c: r = i break if r == -1: print('No') exit(0) h = [a.split('Q')[0], a.split('Q')[-1]] c = [len(h[0]), len(h[1])] if c[0] % 2 != 0 or c[1] % 2 != 0: print('No') exit(0) c[...
28
1,090
56,320,000
197627903
a = input() b = [] h = '' c = 0 for i in a: if i == 'Q': c += 1 if c == 0: print('Yes') exit(0) r = -1 for i in range(1001): if i*i == c: r = i break if r == -1: print('No') exit(0) h = [a.split('Q')[0], a.split('Q')[-1]] c = [len(h[0]), len(h[1])] if c[0] % 2 != 0 or c[1] % 2 != 0: print('No') exit(0) c[...
April Fools Day Contest 2013
ICPC
2,013
2
256
HQ
The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset — a language called HQ...
The only line of the input is a string between 1 and 106 characters long.
Output "Yes" or "No".
null
The rest of the problem statement was destroyed by a stray raccoon. We are terribly sorry for the inconvenience.
[{"input": "HHHH", "output": "Yes"}, {"input": "HQHQH", "output": "No"}, {"input": "HHQHHQH", "output": "No"}, {"input": "HHQQHHQQHH", "output": "Yes"}]
2,500
["*special", "constructive algorithms"]
28
[{"input": "HHHH\r\n", "output": "Yes\r\n"}, {"input": "HQHQH\r\n", "output": "No\r\n"}, {"input": "HHQHHQH\r\n", "output": "No\r\n"}, {"input": "HHQQHHQQHH\r\n", "output": "Yes\r\n"}, {"input": "Q\r\n", "output": "Yes\r\n"}, {"input": "HHHHHHHHHHQHHH\r\n", "output": "No\r\n"}, {"input": "HHQHQQQHHH\r\n", "output": "No...
false
stdio
null
true
285/B
285
B
Python 3
TESTS
31
280
5,939,200
109433825
# Question: F - 2 # Assignment 7 # Daniel Perez, bd2255 def rainy(SHU,S,E): minimum = 0 current = S while True: if SHU[current] == S: if S == E: return minimum return -1 elif SHU[current] == E: minimum += 1 return minimum ...
33
154
11,366,400
208956947
n,s,t=[int(x)-1 for x in input().split()] p=[int(x)-1 for x in input().split()] vis=[True]*(n+1) k=0 while(s!=t): if vis[s]==False: k=-1 break vis[s]=False s=p[s] k+=1 print(k)
Codeforces Round 175 (Div. 2)
CF
2,013
2
256
Find Marble
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not. First Petya puts a marble under the glass in position s. Then he performs some (pos...
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct. Note that s ca...
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
null
null
[{"input": "4 2 1\n2 3 4 1", "output": "3"}, {"input": "4 3 3\n4 1 3 2", "output": "0"}, {"input": "4 3 4\n1 2 3 4", "output": "-1"}, {"input": "3 1 3\n2 1 3", "output": "-1"}]
1,200
["implementation"]
33
[{"input": "4 2 1\r\n2 3 4 1\r\n", "output": "3\r\n"}, {"input": "4 3 3\r\n4 1 3 2\r\n", "output": "0\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n", "output": "-1\r\n"}, {"input": "3 1 3\r\n2 1 3\r\n", "output": "-1\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "10 6 7\r\n10 7 8 1 5 6 2 9 4 3\r\n", "output...
false
stdio
null
true
549/D
549
D
PyPy 3
TESTS
3
93
23,347,200
32455566
n, m = map(int, input().split()) m += 1 p = ''.join(input() + 'X' for i in range(n)) + 'X' * m print(sum(p[i + 1] != p[i] != p[i + m] for i in range(n * m)))
47
124
0
31211585
n,m=[int(i) for i in input().split()] l=[] for i in range(n): l.append(input()) la=[0 for i in range(m)] s=0 for i in range(n-1,-1,-1): for j in range(m-1,-1,-1): if l[i][j]=='W': if la[j]!=1: x=1-la[j] s+=1 for k in range(j,-1,-1): ...
Looksery Cup 2015
CF
2,015
1
256
Haar Features
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept. Let's consider a rectangular image that is represent...
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature. Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if i...
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
null
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations: 1. add the sum of pixels in...
[{"input": "6 8\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW", "output": "2"}, {"input": "3 3\nWBW\nBWW\nWWW", "output": "4"}, {"input": "3 6\nWWBBWW\nWWBBWW\nWWBBWW", "output": "3"}, {"input": "4 4\nBBBB\nBBBB\nBBBB\nBBBW", "output": "4"}]
1,900
["greedy", "implementation"]
47
[{"input": "6 8\r\nBBBBBBBB\r\nBBBBBBBB\r\nBBBBBBBB\r\nWWWWWWWW\r\nWWWWWWWW\r\nWWWWWWWW\r\n", "output": "2\r\n"}, {"input": "3 3\r\nWBW\r\nBWW\r\nWWW\r\n", "output": "4\r\n"}, {"input": "3 6\r\nWWBBWW\r\nWWBBWW\r\nWWBBWW\r\n", "output": "3\r\n"}, {"input": "4 4\r\nBBBB\r\nBBBB\r\nBBBB\r\nBBBW\r\n", "output": "4\r\n"}, ...
false
stdio
null
true
613/B
613
B
Python 3
TESTS
16
514
15,155,200
15434283
def main(): from bisect import bisect n, A, cf, cm, m = map(int, input().split()) skills = list(map(int, input().split())) idxes = sorted(range(n), key=skills.__getitem__) sorted_skills = [skills[_] for _ in idxes] bottom_lift, a = [0] * n, 0 for i, b in enumerate(sorted_skills): bot...
35
405
17,510,400
15438013
def main(): from bisect import bisect n, A, cf, cm, m = map(int, input().split()) skills = list(map(int, input().split())) xlat = sorted(range(n), key=skills.__getitem__) sorted_skills = [skills[_] for _ in xlat] bottom_lift, a, c = [], 0, 0 for i, b in enumerate(sorted_skills): c +=...
Codeforces Round 339 (Div. 1)
CF
2,016
2
256
Skills
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A. Along with...
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015). The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units. On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers s...
null
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1. In the second test one should increase all skills to maximum.
[{"input": "3 5 10 1 5\n1 3 1", "output": "12\n2 5 2"}, {"input": "3 5 10 1 339\n1 3 1", "output": "35\n5 5 5"}]
1,900
["binary search", "brute force", "dp", "greedy", "sortings", "two pointers"]
35
[{"input": "3 5 10 1 5\r\n1 3 1\r\n", "output": "12\r\n2 5 2 \r\n"}, {"input": "3 5 10 1 339\r\n1 3 1\r\n", "output": "35\r\n5 5 5 \r\n"}, {"input": "2 6 0 1 4\r\n5 1\r\n", "output": "5\r\n5 5 \r\n"}, {"input": "1 1000000000 1000 1000 1000000000000000\r\n0\r\n", "output": "1000000001000\r\n1000000000 \r\n"}, {"input": ...
false
stdio
null
true
239/B
239
B
Python 3
TESTS
6
92
0
153503222
def tape(): num_ints = '' point_ = 0 # 0 left, 1 right first_line = input() num_queries = int(first_line.split(' ')[1]) len_seq = int(first_line.split(' ')[0]) orig_seq = input() #print('orig_seq = {}'.format(orig_seq)) for i in range(num_queries): dir_ = 1 l_and_r =...
33
218
4,505,600
207288695
import sys input = lambda: sys.stdin.readline().rstrip() from collections import defaultdict N,Q = map(int, input().split()) S = input() for _ in range(Q): l,r = map(int, input().split()) l-=1;r-=1 ans = [0]*10 s = [c for c in S[l:r+1]] idx=0 d=1 m = len(s) for i in range(m): ...
Codeforces Round 148 (Div. 2)
CF
2,012
2
256
Easy Tape Programming
There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. - Current character pointer (CP); - Dire...
The first line of input contains two integers n and q (1 ≤ n, q ≤ 100) — represents the length of the sequence s and the number of queries. The second line contains s, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of s are not separated with spaces. The next q lines ea...
For each query print 10 space separated integers: x0, x1, ..., x9 where xi equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in input.
null
null
[{"input": "7 4\n1>3>22<\n1 3\n4 7\n7 7\n1 7", "output": "0 1 0 1 0 0 0 0 0 0\n2 2 2 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n2 3 2 1 0 0 0 0 0 0"}]
1,500
["brute force", "implementation"]
33
[{"input": "7 4\r\n1>3>22<\r\n1 3\r\n4 7\r\n7 7\r\n1 7\r\n", "output": "0 1 0 1 0 0 0 0 0 0 \r\n2 2 2 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n2 3 2 1 0 0 0 0 0 0 \r\n"}, {"input": "5 2\r\n>>>>>\r\n1 5\r\n1 2\r\n", "output": "0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "1 3\r\n9\r\n1 1\r\n1 1\r\n1 1\...
false
stdio
null
true
1006/B
1006
B
PyPy 3-64
TESTS
15
155
1,740,800
162254441
import sys sc = sys.stdin.readline n,x=map(int,sc().rstrip().split()) arr=list(map(int,sc().rstrip().split())) arr1=list() arr2=sorted(arr) count=0 for e in range(x): arr1.append(arr2[n-e-1]) count+=arr2[n-e-1] sys.stdout.write(str(count)+"\n") o=0 for e in range(n): if arr1.__contains__(arr[e]) and x...
37
77
307,200
104238434
def max_profit(n,k,l,d): a=[] p,i=0,-1 while(len(a)!=k-1): p+=1 i+=1 if l[i] in d: a.append(p) p=0 d.remove(l[i]) a.append(n-sum(a)) print(*a) n,k=map(int,input().split()) l=list(map(int,input().split())) d=sorted(l,reverse=True)[:k] prin...
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
1009/A
1009
A
Python 3
TESTS
6
93
0
51817695
input().split() c = list(map(int, input().split())) a = list(map(int, input().split())) if min(a) >= max(c): print(len(a)) exit() if a[0] < min(c): print(0) exit() counter = 0 i = 0 j = 0 while j < len(c): if a[i] >= c[j]: counter += 1 i, j = 1 + i, 1 + j elif a[i] < c[j]: ...
19
31
0
212730740
[a,b] = [int(x) for x in input().split()] N =[int(a) for a in input().split()] M =[int(b) for b in input().split()] count=0 for i in range(a): if len(N) == 0 or len(M) == 0: break elif N[0] <=M[0]: N.pop(0) M.pop(0) count+=1 else: N.pop(0) print(count)
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
1006/B
1006
B
PyPy 3
TESTS
15
218
3,993,600
101246835
import math import copy def dtb(n): return bin(n).replace("0b","") def btd(n): return int(n,2) t=1 for k in range(t): n,kk=map(int,input().split()) a=list(map(int,input().split()))[:n] c=copy.copy(a) a.sort(reverse=True) b=[] f=[] ans=0 for i in range(kk): ans+=a[i] ...
37
77
307,200
108281536
#t=int(input()) n,k = map(int, input().strip().split(' ')) lst = list(map(int, input().strip().split(' '))) l=[] l[:]=lst[:] l.sort(reverse=True) l=l[:k] print(sum(l)) c=[] j=0 c1=0 for i in range(n): if j==k-1: c.append(n-i) break elif lst[i] in l: c1+=1 c.append(c1) del...
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
690/B1
690
B1
Python 3
TESTS
1
46
0
19002935
n = int(input()) Resh = [list(map(str, input())) for i in range(n)] check = 0 for i in range(1, n-1): for j in range(1, n-1): if Resh[i][j] != "0": if int(Resh[i][j]) == int(Resh[i-1][j-1]) + int(Resh[i-1][j+1]) + int(Resh[i+1][j-1]) + int(Resh[i+1][j+1]): check += 1 if check ...
21
140
716,800
19006875
N = int(input()) grid = [] x1 = 50 y1 = 50 x2 = -1 y2 = -1 for y in range(N): grid.append(list(map(int, input()))) for x, num in enumerate(grid[-1]): if num == 4: x1 = min(x1, x) y1 = min(y1, y) x2 = max(x2, x) y2 = max(y2, y) if x1 == 51: print('No')...
Helvetic Coding Contest 2016 online mirror (teams, unrated)
ICPC
2,016
2
256
Recover Polygon (easy)
The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good. Heidi knows that the lair can be represented as a rectangle on a lattice, with si...
The first line of each test case contains one integer N, the size of the lattice grid (5 ≤ N ≤ 50). The next N lines each contain N characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4. Cells are given in the same order as they a...
The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise.
null
The lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (x1, y1), (x1, y2), (x2, y1), (x2, y2)), has a non-zero area and be contained inside of the grid (that is, 0 ≤ x1 < x2 ≤ N, 0 ≤ y1 < y2 ≤ N), and result in the levels of Zombie Contamination as reported in the inpu...
[{"input": "6\n000000\n000000\n012100\n024200\n012100\n000000", "output": "Yes"}]
1,700
[]
21
[{"input": "6\r\n000000\r\n000000\r\n012100\r\n024200\r\n012100\r\n000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000000\r\n012210\r\n024420\r\n012210\r\n000000\r\n000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000100\r\n001210\r\n002420\r\n001210\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "10\r\n...
false
stdio
null
true
978/D
978
D
PyPy 3-64
TESTS
3
62
0
180731077
def solve(cd,a,n,d): ans = 0 flag = True for i in range(n-2): if cd-d[i]==0: pass elif cd-d[i]==1: d[i]+=1 d[i+1]-=1 ans+=1 elif cd-d[i]==-1: d[i]-=1 d[i+1]+=1 ...
39
93
19,456,000
182297791
#!/usr/bin/env python3 import math import sys input = lambda: sys.stdin.readline().rstrip("\r\n") from bisect import bisect_left as bs def test_case(): n = int(input()) b = list(map(int, input().split())) if len(b) <= 2: print(0) return opt = [-1, 0, 1] ans = float("inf") for...
Codeforces Round 481 (Div. 3)
ICPC
2,018
1
256
Almost Arithmetic Progression
Polycarp likes arithmetic progressions. A sequence $$$[a_1, a_2, \dots, a_n]$$$ is called an arithmetic progression if for each $$$i$$$ ($$$1 \le i < n$$$) the value $$$a_{i+1} - a_i$$$ is the same. For example, the sequences $$$[42]$$$, $$$[5, 5, 5]$$$, $$$[2, 11, 20, 29]$$$ and $$$[3, 2, 1, 0]$$$ are arithmetic progr...
The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100\,000)$$$ — the number of elements in $$$b$$$. The second line contains a sequence $$$b_1, b_2, \dots, b_n$$$ $$$(1 \le b_i \le 10^{9})$$$.
If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't us...
null
In the first example Polycarp should increase the first number on $$$1$$$, decrease the second number on $$$1$$$, increase the third number on $$$1$$$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $$$[25, 20, 15, 10]$$$, which is an arithme...
[{"input": "4\n24 21 14 10", "output": "3"}, {"input": "2\n500 500", "output": "0"}, {"input": "3\n14 5 1", "output": "-1"}, {"input": "5\n1 3 6 9 12", "output": "1"}]
1,500
["brute force", "implementation", "math"]
39
[{"input": "4\r\n24 21 14 10\r\n", "output": "3\r\n"}, {"input": "2\r\n500 500\r\n", "output": "0\r\n"}, {"input": "3\r\n14 5 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n1 3 6 9 12\r\n", "output": "1\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000000 1\r\n", "output": "0\r\n"}, {"in...
false
stdio
null
true
354/A
354
A
PyPy 3
TESTS
11
156
0
70628301
ll = [int(x) for x in input().split()] n = ll[0] l = ll[1] r = ll[2] q1 = ll[3] q2 = ll[4] w = [int(x) for x in input().split()] i = 0 lp = 0 rp = n-1 li = 0 ri = 0 ans = 0 if l == r: ans = sum(w)*l else: while i<n: if l<r: if li == 0: ans+=w[lp]*l li = 1 lp+=1 ri = 0 else: if w[lp]*l+q1<w...
23
93
14,131,200
210300682
from collections import defaultdict def L(): return input() def LI(): return int(input()) def LII(): return list(map(int, input().split())) def solve(): n, l, r, ql, qr = LII() arr = LII() l_sum, r_sum = 0, sum(arr) * r global_min = r_sum + (n - 1) * qr for idx, val in enumerate(arr): l_sum, r_sum = l_su...
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
108/B
108
B
PyPy 3
TESTS
3
92
0
150274320
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) ans = "NO" a.sort(reverse = True) while a and not a[-1] ^ 1: a.pop() a.reverse() for i in range(len(a) - 1): if 2 * a[i] > a[i + 1]: ans = "YES" break print(ans)
65
372
8,089,600
79041360
n=int(input()) l=list(map(int,input().split())) l.sort() done=0 for i in range(n-1): if l[i]<l[i+1] and 2*l[i]>l[i+1]: done=1 break if done==1: print('YES') else: print('NO')
Codeforces Beta Round 83 (Div. 2 Only)
CF
2,011
2
256
Datatypes
Tattah's youngest brother, Tuftuf, is new to programming. Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava. Today, Tuftuf was introduced to Gava's unsigned integer datatypes. G...
The first line contains integer n (2 ≤ n ≤ 105) — the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of n integers (1 ≤ ai ≤ 109) — sizes of datatypes in bits. Some datatypes may have equal sizes.
Print "YES" if Tuftuf will stop using Gava, and "NO" otherwise.
null
In the second example, x = 7 (1112) fits in 3 bits, but x2 = 49 (1100012) does not fit in 4 bits.
[{"input": "3\n64 16 32", "output": "NO"}, {"input": "4\n4 2 1 3", "output": "YES"}]
1,400
["math", "sortings"]
65
[{"input": "3\r\n64 16 32\r\n", "output": "NO\r\n"}, {"input": "4\r\n4 2 1 3\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 5 3 3 2\r\n", "output": "YES\r\n"}, {"input": "52\r\n474 24 24 954 9 234 474 114 24 114 234 24 114 114 234 9 9 24 9 54 234 54 9 954 474 9 54 54 54 234 9 114 24 54 114 954 954 474 24 54 54 234 234 4...
false
stdio
null
true
354/A
354
A
Python 3
TESTS
5
61
4,608,000
25709733
n, l, r, q1, q2 = map(int, input().split()) item = list(map(int, input().split())) pre = [item[0]] + [ 0 for i in range(1, n)] suf = [ 0 for i in range(0, n-1)] + [item[n-1], 0] for i in range(1, n): pre[i] = pre[i-1] + item[i] for i in range(n-2, -1, -1): suf[i] = suf[i+1] + item[i] # print(pre, suf) ans = 1e20 for ...
23
93
14,336,000
210297589
from math import inf n, l, r, ql, qr = map(int, input().split()) q = list(map(int, input().split())) f = [0] * (n + 1) for i in range(1, n + 1): f[i] += f[i - 1] + q[i - 1] ans = inf for i in range(0, n + 1): sl = f[i] * l sr = (f[n] - f[i]) * r cL = i cR = n - i sum = sl + sr if cL > cR:...
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
667/B
667
B
Python 3
TESTS
2
109
0
98038070
n = int(input()) l = sorted([int(i)for i in input().split()]) SUM_SR =((max(l)+1)//2)*2 + 1 i = 0 while i < len(l)-1: SUM_SR -= l[i] i+=1 print(SUM_SR)
51
62
3,174,400
153409263
a,b=input(),list(map(int,input().split()));print(2*max(b)+1-sum(b))
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
353/C
353
C
PyPy 3-64
TESTS
2
92
0
216844510
def bit(i, x): return (x >> i) & 1 def f(x, a): result = 0 for i, ai in enumerate(a): result += ai * bit(i, x) return result def main(): n = int(input()) a = list(map(int, input().split())) s = input() if '1' not in s: print(0) return m = int(s[::-1], 2)...
36
186
12,697,600
229448459
from itertools import accumulate from os import path from sys import stdin, stdout filename = "../templates/input.txt" if path.exists(filename): stdin = open(filename, 'r') def input(): return stdin.readline().rstrip() def print(*args, sep=' ', end='\n'): stdout.write(sep.join(map(str, args))) std...
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
526/D
526
D
PyPy 3-64
TESTS
2
62
0
223312495
def prefix_function(s: str): n = len(s) pi = [0] * n k = 0 for i in range(1, n): while k > 0 and s[i] != s[k]: k = pi[k - 1] if s[i] == s[k]: k += 1 pi[i] = k return pi n, k = map(int, input().split()) s = input() pi = prefix_function(s) ans = [0] * n for i in range(n): L = i + 1 ...
57
140
22,528,000
223321082
def prefix_function(s: str): n = len(s) pi = [0] * n k = 0 for i in range(1, n): while k > 0 and s[i] != s[k]: k = pi[k - 1] if s[i] == s[k]: k += 1 pi[i] = k return pi def z_function(s: str): n = len(s) z = [0] * n l, r = 0, 0 for i in range(1, n): z[i] = 0 if i >= r else...
ZeptoLab Code Rush 2015
CF
2,015
1
256
Om Nom and Necklace
One day Om Nom found a thread with n beads of different colors. He decided to cut the first several beads from this thread to make a bead necklace and present it to his girlfriend Om Nelly. Om Nom knows that his girlfriend loves beautiful patterns. That's why he wants the beads on the necklace to form a regular patter...
The first line contains two integers n, k (1 ≤ n, k ≤ 1 000 000) — the number of beads on the thread that Om Nom found and number k from the definition of the regular sequence above. The second line contains the sequence of n lowercase Latin letters that represent the colors of the beads. Each color corresponds to a s...
Print a string consisting of n zeroes and ones. Position i (1 ≤ i ≤ n) must contain either number one if the first i beads on the thread form a regular sequence, or a zero otherwise.
null
In the first sample test a regular sequence is both a sequence of the first 6 beads (we can take A = "", B = "bca"), and a sequence of the first 7 beads (we can take A = "b", B = "ca"). In the second sample test, for example, a sequence of the first 13 beads is regular, if we take A = "aba", B = "ba".
[{"input": "7 2\nbcabcab", "output": "0000011"}, {"input": "21 2\nababaababaababaababaa", "output": "000110000111111000011"}]
2,200
["hashing", "string suffix structures", "strings"]
57
[{"input": "7 2\r\nbcabcab\r\n", "output": "0000011"}, {"input": "21 2\r\nababaababaababaababaa\r\n", "output": "000110000111111000011"}, {"input": "321 2\r\nabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaaba...
false
stdio
null
true
761/D
761
D
Python 3
TESTS
1
61
7,065,600
38014671
def binary_search(l, r, k): while (l <= r): mid = l+r >> 1 if (mid < k): l = mid+1 else: r = mid-1 return l def main(): n, l, r = map(int, input().split()) a = list(map(int, input().split())) p = list(map(int, input().split())) p = [(p[i], i+1) fo...
63
311
26,316,800
147505370
import sys input = lambda: sys.stdin.buffer.readline().decode().strip() n, l, r = map(int, input().split()) a, p = [[int(x) for x in input().split()] for _ in range(2)] sor, mi, b = sorted(range(n), key=p.__getitem__), l - r, [0] * n for _ in range(n): i = sor[_] b[i] = max(l, a[i] + mi) if b[i] > r: ...
Codeforces Round 394 (Div. 2)
CF
2,017
2
256
Dasha and Very Difficult Problem
Dasha logged into the system and began to solve problems. One of them is as follows: Given two sequences a and b of length n each you need to write a sequence c of length n, the i-th element of which is calculated as follows: ci = bi - ai. About sequences a and b we know that their elements are in the range from l to...
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ 109) — the length of the sequence and boundaries of the segment where the elements of sequences a and b are. The next line contains n integers a1, a2, ..., an (l ≤ ai ≤ r) — the elements of the sequence a. The next line contains n distinct in...
If there is no the suitable sequence b, then in the only line print "-1". Otherwise, in the only line print n integers — the elements of any suitable sequence b.
null
Sequence b which was found in the second sample is suitable, because calculated sequence c = [2 - 3, 2 - 4, 2 - 8, 9 - 9] = [ - 1, - 2, - 6, 0] (note that ci = bi - ai) has compressed sequence equals to p = [3, 2, 1, 4].
[{"input": "5 1 5\n1 1 1 1 1\n3 1 5 4 2", "output": "3 1 5 4 2"}, {"input": "4 2 9\n3 4 8 9\n3 2 1 4", "output": "2 2 2 9"}, {"input": "6 1 5\n1 1 1 1 1 1\n2 3 5 4 1 6", "output": "-1"}]
1,700
["binary search", "brute force", "constructive algorithms", "greedy", "sortings"]
63
[{"input": "5 1 5\r\n1 1 1 1 1\r\n3 1 5 4 2\r\n", "output": "3 1 5 4 2 "}, {"input": "4 2 9\r\n3 4 8 9\r\n3 2 1 4\r\n", "output": "2 2 2 9 "}, {"input": "6 1 5\r\n1 1 1 1 1 1\r\n2 3 5 4 1 6\r\n", "output": "-1\r\n"}, {"input": "5 1 7\r\n1 4 4 6 5\r\n5 2 1 4 3\r\n", "output": "2 2 1 6 4 "}, {"input": "5 10 100\r\n12 14 ...
false
stdio
import sys def read_file(path): with open(path, 'r') as f: return f.read().strip() def main(input_path, output_path, submission_path): # Read input with open(input_path, 'r') as f: lines = f.readlines() n, l, r = map(int, lines[0].split()) a = list(map(int, lines[1].split())) p...
true
285/B
285
B
Python 3
TESTS
31
248
7,475,200
119611008
n,f,l=map(int,input().split()) li=list(map(int,input().split())) if f==l and li[f-1]==l: print(0) exit() posOfMarble=f shuff=0 for i in range(n): posOfMarble=li[posOfMarble-1] shuff+=1 if posOfMarble==l: print(shuff) exit() print(-1)
33
154
12,390,400
229476921
a, b, c = map(int, input().split()) p = list(map(int, input().split())) for i in range(a): if b == c: print(i) exit() b = p[b-1] else: print(-1)
Codeforces Round 175 (Div. 2)
CF
2,013
2
256
Find Marble
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not. First Petya puts a marble under the glass in position s. Then he performs some (pos...
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct. Note that s ca...
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
null
null
[{"input": "4 2 1\n2 3 4 1", "output": "3"}, {"input": "4 3 3\n4 1 3 2", "output": "0"}, {"input": "4 3 4\n1 2 3 4", "output": "-1"}, {"input": "3 1 3\n2 1 3", "output": "-1"}]
1,200
["implementation"]
33
[{"input": "4 2 1\r\n2 3 4 1\r\n", "output": "3\r\n"}, {"input": "4 3 3\r\n4 1 3 2\r\n", "output": "0\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n", "output": "-1\r\n"}, {"input": "3 1 3\r\n2 1 3\r\n", "output": "-1\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "10 6 7\r\n10 7 8 1 5 6 2 9 4 3\r\n", "output...
false
stdio
null
true
797/D
797
D
PyPy 3
TESTS
19
998
23,859,200
121426082
class Node: def __init__(self, value): self.left = None self.right = None self.val = value def process1(root): if root is None: return [], 0 work_l, not_l = process1(root.left) work_r, not_r = process1(root.right) work_here = [root.val] not_here = not_l+n...
55
155
17,100,800
180934403
from sys import stdin input=lambda :stdin.readline()[:-1] n=int(input()) par=[-1]*n left=[-1]*n right=[-1]*n a=[0]*n for i in range(n): v,l,r=map(int,input().split()) a[i]=v l-=1 r-=1 if l!=-2: par[l]=i left[i]=l if r!=-2: par[r]=i right[i]=r root=-1 for i in range(n): if par[i]==-1: ...
Educational Codeforces Round 19
ICPC
2,017
1
256
Broken BST
Let T be arbitrary binary tree — tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent — it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree T: 1. Set po...
First line contains integer number n (1 ≤ n ≤ 105) — number of vertices in the tree. Each of the next n lines contains 3 numbers v, l, r (0 ≤ v ≤ 109) — value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't exist then number - 1 ...
Print number of times when search algorithm will fail.
null
In the example the root of the tree in vertex 2. Search of numbers 5 and 15 will return fail because on the first step algorithm will choose the subtree which doesn't contain numbers you are looking for.
[{"input": "3\n15 -1 -1\n10 1 3\n5 -1 -1", "output": "2"}, {"input": "8\n6 2 3\n3 4 5\n12 6 7\n1 -1 8\n4 -1 -1\n5 -1 -1\n14 -1 -1\n2 -1 -1", "output": "1"}]
2,100
["data structures", "dfs and similar"]
55
[{"input": "3\r\n15 -1 -1\r\n10 1 3\r\n5 -1 -1\r\n", "output": "2\r\n"}, {"input": "8\r\n6 2 3\r\n3 4 5\r\n12 6 7\r\n1 -1 8\r\n4 -1 -1\r\n5 -1 -1\r\n14 -1 -1\r\n2 -1 -1\r\n", "output": "1\r\n"}, {"input": "1\r\n493041212 -1 -1\r\n", "output": "0\r\n"}, {"input": "10\r\n921294733 5 9\r\n341281094 -1 -1\r\n35060484 10 -1...
false
stdio
null
true
545/C
545
C
Python 3
TESTS
8
217
9,011,200
228648520
n=int(input()) x=[] h=[] for i in range(n): xi,hi=map(int,input().split()) x.append(xi) h.append(hi) cut=2 for i in range(1,n-1): if x[i]-h[i]>x[i-1]: cut+=1 elif x[i]+h[i]<x[i+1]: cut+=1 x[i]=x[i]+h[i] print(cut)
67
171
16,588,800
186748506
# watchu lookin at? import sys from math import * # from itertools import * # from heapq import heapify, heappop, heappush # from bisect import bisect, bisect_left, bisect_right from collections import deque, Counter, defaultdict as dd mod = 10**9+7 abc = "abcdefghijklmnopqrstuvwxyz" # Sangeeta Singh def input(): re...
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
545/C
545
C
Python 3
TESTS
8
217
8,806,400
228928821
n=int(input()) x=[] h=[] for i in range(n): X,H=map(int,input().split()) x.append(X) h.append(H) temp=x[0] N=2 for i in range(1,n-1): if x[i]-h[i]>temp: N+=1 temp=x[i] else: if x[i]+h[i]<x[i+1]: N+=1 temp=x[i]+h[i] else: temp=x[i] p...
67
202
0
208053251
n = int(input()) cmp = -1000000030 tree = 0 t_x = 0 while n > 0: x, h = map(int, input().split()) # First try to cut the tree down at the left, at [x-h,x] if x - h > cmp: cmp = x tree += 1 # If not possible, try cutting it down at the right, at [x,x+h] elif x > cmp: cmp = ...
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
290/E
290
E
Python 3
TESTS
11
122
2,764,800
6200906
a = input() print("Yes" if a.replace("HH", "").replace("Q", "") == "" else "No")
28
1,090
56,320,000
197627903
a = input() b = [] h = '' c = 0 for i in a: if i == 'Q': c += 1 if c == 0: print('Yes') exit(0) r = -1 for i in range(1001): if i*i == c: r = i break if r == -1: print('No') exit(0) h = [a.split('Q')[0], a.split('Q')[-1]] c = [len(h[0]), len(h[1])] if c[0] % 2 != 0 or c[1] % 2 != 0: print('No') exit(0) c[...
April Fools Day Contest 2013
ICPC
2,013
2
256
HQ
The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset — a language called HQ...
The only line of the input is a string between 1 and 106 characters long.
Output "Yes" or "No".
null
The rest of the problem statement was destroyed by a stray raccoon. We are terribly sorry for the inconvenience.
[{"input": "HHHH", "output": "Yes"}, {"input": "HQHQH", "output": "No"}, {"input": "HHQHHQH", "output": "No"}, {"input": "HHQQHHQQHH", "output": "Yes"}]
2,500
["*special", "constructive algorithms"]
28
[{"input": "HHHH\r\n", "output": "Yes\r\n"}, {"input": "HQHQH\r\n", "output": "No\r\n"}, {"input": "HHQHHQH\r\n", "output": "No\r\n"}, {"input": "HHQQHHQQHH\r\n", "output": "Yes\r\n"}, {"input": "Q\r\n", "output": "Yes\r\n"}, {"input": "HHHHHHHHHHQHHH\r\n", "output": "No\r\n"}, {"input": "HHQHQQQHHH\r\n", "output": "No...
false
stdio
null
true
545/C
545
C
Python 3
TESTS
8
218
8,806,400
229093653
n = int(input()) x,h = [],[] for _ in range(n): a,b = map(int,input().split()) x.append(a) h.append(b) end,cnt = x[0],2 for i in range(1,n - 1): if x[i] - end > h[i]: cnt += 1 end = x[i] elif x[i + 1] - x[i] > h[i]: cnt += 1 end = x[i] + h[i] else: end = x...
67
202
0
228651047
n = int(input()) ans = 1 lx, lh = map(int, input().split()) flag = False for _ in range(n-1): x, h = map(int, input().split()) if flag: if lh < x-lx: ans += 1 lx += lh if h < x-lx: ans += 1 flag = False else: flag = True lx, lh = x, h if flag: ...
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
545/C
545
C
Python 3
TESTS
8
202
9,011,200
229175700
n=int(input()) wood_cut=2#端点树必可倒 x,h=[],[] for i in range(n): coordinate,height=map(int,input().split()) x.append(coordinate) h.append(height) for i in range(1,n-1): if x[i]-x[i-1]>h[i]: wood_cut+=1#树往左边倒不影响下一棵树 elif x[i+1]-x[i]>h[i]: wood_cut+=1 x[i]+=h[i]#往右边倒会影响下一棵树 print(...
67
202
0
228663784
# -*- coding: utf-8 -*- """ Created on Wed Oct 18 18:38:49 2023 @author: GaoMingze 2300011427 """ k=1 y=0 n=int(input()) s,h=map(int,input().split()) p=s for i in range(1,n): s,h=map(int,input().split()) k+=1 if s<=p: k-=1 p=y if s-h>p: p=s else: p=s+h ...
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
545/C
545
C
Python 3
TESTS
8
218
8,192,000
229800835
n = int(input()) pos = [0 for i in range(n+1)] height = [0 for i in range(n+1)] answer = 2 for i in range(n): pos[i+1],height[i+1] = map(int,input().split()) for i in range(2,n): if pos[i]-height[i] > pos[i-1]: answer += 1 elif pos[i]+height[i] < pos[i+1]: answer += 1 pos[i] += heigh...
67
202
0
228735565
n = int(input()) point = -float("inf") ans = 1 x0, h0 = map(int, input().split()) for i in range(n-1): x, h = map(int, input().split()) if x0 - point > h0: ans += 1 point = x0 elif x0 + h0 < x: ans += 1 point = x0 + h0 else: point = x0 x0, h0 = x, h print(ans)
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
1006/B
1006
B
Python 3
TESTS
4
93
307,200
97031699
import sys,math n,k = map(int,input().split(' ')) arr = list(map(int,input().split(' '))) arr.sort() s = sum(arr[n-k:]) sepe = arr[n-k:-1] x = 0 print(s) ans = [] for i in range(1,n): if arr[i] in sepe: ans.append(len(arr[x:i])) sepe[sepe.index(arr[i])] = None x = i if len(arr[x:])>0: an...
37
77
614,400
108756136
from collections import defaultdict n,k=map(int,input().split()) array=list(map(int,input().split())) mysub=sorted(array,reverse=True) mysub=mysub[:k] print(sum(mysub)) d=defaultdict(lambda:0) for item in mysub: d[item]+=1 count=1 total=0 su=x=0 while(x<n): if(d[array[x]]<=0): x+=1 count+=1...
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
39/F
39
F
Python 3
TESTS
9
62
0
188642881
n,m,k=map(int,input().split()) lis=list(map(int,input().split())) klis=list(map(int,input().split())) dicti={} mini=10**9+1 for t,i in enumerate(lis): dicti[i]=[0,t] for j in klis: if j%i==0: dicti[i][0]+=1 mini=min(dicti[i][0],mini) count=0 #print(dicti) out=[] for i in dicti: if di...
35
92
0
171492765
n, m, k = list(map(int, input().split())) frogs = list(map(int, input().split())) mosquitos = list(map(int, input().split())) best_frogs = [] min_kills = len(mosquitos) for i, frog in enumerate(frogs): kills = sum([1 if m % frog == 0 else 0 for m in mosquitos]) if kills < min_kills: min_kills = kills ...
School Team Contest 1 (Winter Computer School 2010/11)
ICPC
2,010
2
64
Pacifist frogs
Thumbelina has had an accident. She has found herself on a little island in the middle of a swamp and wants to get to the shore very much. One can get to the shore only by hills that are situated along a straight line that connects the little island with the shore. Let us assume that the hills are numbered from 1 to n...
The first line contains three integers n, m and k (1 ≤ n ≤ 109, 1 ≤ m, k ≤ 100) — the number of hills, frogs and mosquitoes respectively. The second line contains m integers di (1 ≤ di ≤ 109) — the lengths of the frogs’ jumps. The third line contains k integers — the numbers of the hills on which each mosquito is sleep...
In the first line output the number of frogs that smash the minimal number of mosquitoes, in the second line — their numbers in increasing order separated by spaces. The frogs are numbered from 1 to m in the order of the jump length given in the input data.
null
null
[{"input": "5 3 5\n2 3 4\n1 2 3 4 5", "output": "2\n2 3"}, {"input": "1000000000 2 3\n2 5\n999999995 999999998 999999996", "output": "1\n2"}]
1,300
["implementation"]
35
[{"input": "5 3 5\r\n2 3 4\r\n1 2 3 4 5\r\n", "output": "2\r\n2 3\r\n"}, {"input": "1000000000 2 3\r\n2 5\r\n999999995 999999998 999999996\r\n", "output": "1\r\n2\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "2 2 1\r\n2 1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "3 2 2\r\n2 4\r\n3 2\r...
false
stdio
null
true
766/D
766
D
PyPy 3
TESTS
8
483
20,787,200
177393692
import sys def get_int(): return int(sys.stdin.readline().strip()) def get_ints(): return map(int,sys.stdin.readline().strip().split()) def get_list(): return list(map(int,sys.stdin.readline().strip().split())) def get_string(): return sys.stdin.readline().strip() def findSyn(node): v = [] ...
32
951
50,278,400
221751596
import sys input = sys.stdin.buffer.readline def find_root(root_dict, x): L = [] while x != root_dict[x]: L.append(x) x = root_dict[x] for y in L: root_dict[y] = x return x def process(A, X, Queries): n = len(A) root_dict = [i for i in range(2*n)] d = {} ...
Codeforces Round 396 (Div. 2)
CF
2,017
4
256
Mahmoud and a Dictionary
Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discovers a new relation between two words. He know that if two words have ...
The first line of input contains three integers n, m and q (2 ≤ n ≤ 105, 1 ≤ m, q ≤ 105) where n is the number of words in the dictionary, m is the number of relations Mahmoud figured out and q is the number of questions Mahmoud asked after telling all relations. The second line contains n distinct words a1, a2, ..., ...
First, print m lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print "NO" (without quotes) and ignore it, otherwise print "YES" (without quotes). After that print q lines, one per each question. If the two words have the same mean...
null
null
[{"input": "3 3 4\nhate love like\n1 love like\n2 love hate\n1 hate like\nlove like\nlove hate\nlike hate\nhate like", "output": "YES\nYES\nNO\n1\n2\n2\n2"}, {"input": "8 6 5\nhi welcome hello ihateyou goaway dog cat rat\n1 hi welcome\n1 ihateyou goaway\n2 hello ihateyou\n2 hi goaway\n2 hi hello\n1 hi hello\ndog cat\nd...
2,000
["data structures", "dfs and similar", "dp", "dsu", "graphs"]
32
[{"input": "3 3 4\r\nhate love like\r\n1 love like\r\n2 love hate\r\n1 hate like\r\nlove like\r\nlove hate\r\nlike hate\r\nhate like\r\n", "output": "YES\r\nYES\r\nNO\r\n1\r\n2\r\n2\r\n2\r\n"}, {"input": "8 6 5\r\nhi welcome hello ihateyou goaway dog cat rat\r\n1 hi welcome\r\n1 ihateyou goaway\r\n2 hello ihateyou\r\n2...
false
stdio
null
true
285/B
285
B
PyPy 3
TESTS
31
342
10,547,200
52483783
n,s,t = map(int,input().split()) p = list(map(int,input().split())) i = s-1 n = 0 flag = 0 while 1: if p[i] == s: flag = 1 break if p[i] == t: n += 1 break else: i = p[i] - 1 n += 1 if s == t: flag = 0 print(n if flag==0 else'-1')
33
154
13,004,800
207990894
a = input() b = input() a = a.split(" ") b = b.split(" ") for i in range(len(a)): a[i] = int(a[i]) for j in range(len(b)): b[j] = int(b[j]) c = a[1] d = a[2] e = 0 f = 0 if c == d: print(0) else: for k in range(a[0]): e = b[c-1] c = e f = f + 1 if e == d: ...
Codeforces Round 175 (Div. 2)
CF
2,013
2
256
Find Marble
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not. First Petya puts a marble under the glass in position s. Then he performs some (pos...
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct. Note that s ca...
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
null
null
[{"input": "4 2 1\n2 3 4 1", "output": "3"}, {"input": "4 3 3\n4 1 3 2", "output": "0"}, {"input": "4 3 4\n1 2 3 4", "output": "-1"}, {"input": "3 1 3\n2 1 3", "output": "-1"}]
1,200
["implementation"]
33
[{"input": "4 2 1\r\n2 3 4 1\r\n", "output": "3\r\n"}, {"input": "4 3 3\r\n4 1 3 2\r\n", "output": "0\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n", "output": "-1\r\n"}, {"input": "3 1 3\r\n2 1 3\r\n", "output": "-1\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "10 6 7\r\n10 7 8 1 5 6 2 9 4 3\r\n", "output...
false
stdio
null
true
285/B
285
B
PyPy 3
TESTS
31
312
10,752,000
185141425
n,s,t=map(int,input().split()) a=list(map(int,input().split())) ind=s ans=1 temp=0 if s==t and a[ind-1]==s: print(0) elif s!=t and a[ind-1]==s: print(-1) else: while a[ind-1]!=t: ind=a[ind-1] ans+=1 if ans>n: print(-1) temp=1 break if temp==0: ...
33
154
13,312,000
170769868
import sys input = sys.stdin.readline N,S,T = map(int,input().split()) operations = list(map(int,input().split())) ans = 0 S -= 1 T -= 1 for i in range(N): if S == T : print(ans) exit() S = operations[S] -1 ans += 1 print(-1)
Codeforces Round 175 (Div. 2)
CF
2,013
2
256
Find Marble
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not. First Petya puts a marble under the glass in position s. Then he performs some (pos...
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct. Note that s ca...
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
null
null
[{"input": "4 2 1\n2 3 4 1", "output": "3"}, {"input": "4 3 3\n4 1 3 2", "output": "0"}, {"input": "4 3 4\n1 2 3 4", "output": "-1"}, {"input": "3 1 3\n2 1 3", "output": "-1"}]
1,200
["implementation"]
33
[{"input": "4 2 1\r\n2 3 4 1\r\n", "output": "3\r\n"}, {"input": "4 3 3\r\n4 1 3 2\r\n", "output": "0\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n", "output": "-1\r\n"}, {"input": "3 1 3\r\n2 1 3\r\n", "output": "-1\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "10 6 7\r\n10 7 8 1 5 6 2 9 4 3\r\n", "output...
false
stdio
null
true