id
stringlengths
14
117
description
stringlengths
29
13k
code
stringlengths
10
49.8k
test_samples
dict
source
class label
3 classes
prompt
stringlengths
391
104k
449_B. Jzzhu and Cities_29251
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 ...
import os, sys from io import IOBase, BytesIO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.wr...
{ "input": [ "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3\n", "5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5\n", "5 5 3\n1 2 999999999\n2 3 1000000000\n3 4 529529529\n5 1 524524524\n5 3 1000000000\n5 524444444\n5 529999999\n2 1000000000\n", "3 2 2\n1 2 4\n2 3 4\n2 2\n3 6\n", "5 5 2\n1 2 100\n2 3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: 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 ...
493_D. Vasya and Chess_29255
Vasya decided to learn to play chess. Classic chess doesn't seem interesting to him, so he plays his own sort of chess. The queen is the piece that captures all squares on its vertical, horizontal and diagonal lines. If the cell is located on the same vertical, horizontal or diagonal line with queen, and the cell cont...
n = int(input()) if n & 1 == 0: print('white\n1 2') else: print('black')
{ "input": [ "3\n", "2\n", "999999999\n", "11\n", "999999996\n", "900001\n", "6\n", "1000000000\n", "5\n", "100\n", "346367\n", "939698497\n", "12345\n", "19\n", "7\n", "13\n", "99966246\n", "9\n", "4\n", "666666\n", "111111\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya decided to learn to play chess. Classic chess doesn't seem interesting to him, so he plays his own sort of chess. The queen is the piece that captures all squares on its vertic...
518_C. Anya and Smartphone_29259
Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k + 1)-th ...
'''input 5 4 2 3 1 5 2 4 4 4 4 4 ''' import math from sys import stdin, stdout def get_order(pemut): order = dict() for i in range(1, n + 1): order[permut[i - 1]] = i r_order = dict() for i in order: r_order[order[i]] = i return order, r_order # main starts n, m, k = list(map(int, stdin.readline().split...
{ "input": [ "5 4 2\n3 1 5 2 4\n4 4 4 4\n", "8 3 3\n1 2 3 4 5 6 7 8\n7 8 1\n", "71 96 75634\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71\n27 26 27...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on diffe...
544_C. Writing Code_29263
Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly ai bugs in every line of code that he writes. Let's call a sequence of non-negative integers v1, v2, ..., vn a plan, if v1 + v2 + ... + vn = m...
a = list(map(int, input().split())) n = a[0] m = a[1] b = a[2] mod = a[3] ac = list(map(int,input().split())) ac = [0] + ac dp = [[[0 for k in range(b+1)] for _ in range(m+1)] for z in range(2)] for i in range(n+1) : for x in range(b+1) : dp[i%2][0][x] = 1 for i in range(1,n+1) : for j in rang...
{ "input": [ "3 6 5 1000000007\n1 2 3\n", "3 3 3 100\n1 1 1\n", "3 5 6 11\n1 2 1\n", "1 1 1 1\n0\n", "3 10 10 150691913\n8 7 10\n", "1 5 5 1000\n500\n", "21 63 40 1009\n4 4 2 2 4 4 3 2 4 2 0 3 3 4 3 4 3 0 4 2 4\n", "29 157 50 1\n3 0 0 3 1 1 2 0 4 4 1 2 2 1 0 0 2 0 3 2 2 3 3 1 4 1 1 4 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly ai bugs...
592_C. The Big Race_29269
Vector Willman and Array Bolt are the two most famous athletes of Byteforces. They are going to compete in a race with a distance of L meters today. <image> Willman and Bolt have exactly the same speed, so when they compete the result is always a tie. That is a problem for the organizers because they want a winner. ...
from sys import stdin import math # stdin = open('in.txt') # t, w, b = map(int, stdin.readline().split()) t, w, b = map(int, input().split()) d = math.gcd(w, b) l = (w*b)//d add = min(w, b) ans = add*(t//l) m = t%l ans += min(add, m+1) ans -= 1 d = math.gcd(ans, t) ans //= d t //= d print(ans, '/', t, sep='')
{ "input": [ "10 3 2\n", "7 1 2\n", "94268 714 345\n", "1000000000000000000 1000000000 2000000000\n", "40525189 6365 12730\n", "321076647734423976 7 8\n", "370330636048898430 61721772674816405 6\n", "19 1 19\n", "674664088 435395270 5\n", "262610 5583 4717\n", "799999999999...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vector Willman and Array Bolt are the two most famous athletes of Byteforces. They are going to compete in a race with a distance of L meters today. <image> Willman and Bolt have ex...
614_A. Link/Cut Tree_29273
Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed...
#from math import pow def pow(num, n): ans = 1 while n>0: ans*=num n-=1 return ans l,r,k = map(int, input().split()) i = 0; while (pow(k,i)<l): i+=1 count = 0 a = [] while (l<=pow(k,i)<=r): a.append(int(pow(k,i))) count += 1 i+=1 if count == 0: print(-1) else: f...
{ "input": [ "1 10 2\n", "2 4 5\n", "1 243 3\n", "18102 43332383920 28554\n", "438133886369772308 942612870269666780 7193\n", "1 2576683920000 2\n", "19562 31702689720 17701\n", "366070689449360724 928290634811046396 8230\n", "1000000000 1000000000000000000 1000000000\n", "1 10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately...
709_C. Letters Cyclic Shift_29284
You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z' <image> 'y' <image> 'x' <image> 'b' <image> 'a' <image> 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' ...
s=input() l=len(s) a1=a2=1000000 q=[] k=0 b='' j=0 if 'a' in s: for i in s: if i=='a': if len(q)>0 and j-q[-1]>1: q.append(j) k=j break q.append(j) j+=1 if len(q)==l: print('a'*(l-1)+'z') exit() if q[0]>0...
{ "input": [ "codeforces\n", "abacaba\n", "abbbbbbbabbbbbbbbbbbbbbbbbbbbbbbabbabbbbbabbbbbbbbbbbabbbbbbbbabbabbbbbbbbbbbbbbabbabbbaababbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbabbbbbbbbbbbbbbbbabbbbabbbbbbbbbbbbbbbabbbbbbbbbaababbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbabbabbbbbbbbbbbbbbbbabb...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z' <image> 'y' <image> 'x'...
79_B. Colorful Field_29294
Fox Ciel saw a large field while she was on a bus. The field was a n × m rectangle divided into 1 × 1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes. After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following...
n,m,k,t=map(int,input().split(" ")) field=[] for _ in range(k): a,b=map(int,input().split(" ")) field.append( (a-1)*m+(b-1) ) field.sort() #print("field=",field) dic={0:"Carrots",1:"Kiwis",2:"Grapes"} for _ in range(t): flag=0 x,y=map(int,input().split(" ")) value=(x-1)*m+(y-1) for i in rang...
{ "input": [ "4 5 5 6\n4 3\n1 3\n3 3\n2 5\n3 2\n1 3\n1 4\n2 3\n2 4\n1 1\n1 1\n", "5 5 1 3\n2 2\n2 2\n2 2\n2 2\n", "1 6 3 12\n1 2\n1 4\n1 6\n1 1\n1 2\n1 6\n1 2\n1 5\n1 3\n1 4\n1 5\n1 5\n1 4\n1 6\n1 3\n", "1 2 1 2\n1 1\n1 2\n1 1\n", "3 3 3 5\n1 1\n2 2\n3 3\n1 1\n2 2\n2 2\n2 2\n3 3\n", "2 3 2 2\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Fox Ciel saw a large field while she was on a bus. The field was a n × m rectangle divided into 1 × 1 cells. Some cells were wasteland, and other each cell contained crop plants: eith...
867_B. Save the problem!_29303
Attention: we lost all the test cases for this problem, so instead of solving the problem, we need you to generate test cases. We're going to give you the answer, and you need to print a test case that produces the given answer. The original problem is in the following paragraph. People don't use cash as often as they...
a = int(input()) n = 2*(a-1)+1 m = 2 print(n, m) print(1, 2)
{ "input": [ "3\n", "314\n", "18\n", "20\n", "100000\n", "99990\n", "6\n", "17\n", "4\n", "5\n", "10\n", "99971\n", "4063\n", "13\n", "1\n", "9\n", "14\n", "65535\n", "1023\n", "8\n", "11\n", "239\n", "15\n", "16\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Attention: we lost all the test cases for this problem, so instead of solving the problem, we need you to generate test cases. We're going to give you the answer, and you need to prin...
892_D. Gluttony_29307
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 ≤ xi ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. <image> Input The first line contains one integer n (1 ≤ n ≤ 22)...
n = int(input()) a = list(map(int, input().split())) sorted_a = sorted(a) shifted_sorted_a = [sorted_a[-1]] + sorted_a[:-1] #print(sorted_a) #print(shifted_sorted_a) for i in range(len(a)): pos_in_sorted = sorted_a.index(a[i]) print(shifted_sorted_a[pos_in_sorted], end=" ") print()
{ "input": [ "4\n1000 100 10 1\n", "2\n1 2\n", "22\n33 2 19 26 18 13 27 9 25 35 6 24 20 22 11 5 1 30 17 15 7 29\n", "22\n133295371 188010892 71730560 209842234 193069109 184556873 87395258 234247052 230809052 211444018 148989732 17810977 158722706 11753932 100093528 1000000000 43672080 61357581 171830...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 ≤ xi ≤ n, 0 < k < n) th...
914_D. Bash and a Tough Math Puzzle_29311
Bash likes playing with arrays. He has an array a1, a2, ... an of n integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, sometimes the guess is not correct. However, Bash will be satisfied if his guess is almost correct. Suppose he guesses that the gcd of the ele...
#!/usr/bin/env python3 import os import sys from atexit import register from io import StringIO from math import gcd sys.stdin = StringIO(os.read(0, os.fstat(0).st_size).decode()) sys.stdout = StringIO() register(lambda: os.write(1, sys.stdout.getvalue().encode())) input = lambda: sys.stdin.readline().rstrip('\r\n') ...
{ "input": [ "3\n2 6 3\n4\n1 1 2 2\n1 1 3 3\n2 1 9\n1 1 3 2\n", "5\n1 2 3 4 5\n6\n1 1 4 2\n2 3 6\n1 1 4 2\n1 1 5 2\n2 5 10\n1 1 5 2\n", "1\n1000000000\n1\n1 1 1 1000000000\n", "4\n3 3 7 7\n1\n1 1 4 3\n", "4\n3 3 14 7\n1\n1 1 4 3\n", "3\n2 6 3\n4\n1 1 2 2\n1 1 3 3\n2 1 9\n1 2 3 2\n", "1\n11...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bash likes playing with arrays. He has an array a1, a2, ... an of n integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, someti...
937_B. Vile Grasshoppers_29315
The weather is fine today and hence it's high time to climb the nearby pine and enjoy the landscape. The pine's trunk includes several branches, located one above another and numbered from 2 to y. Some of them (more precise, from 2 to p) are occupied by tiny vile grasshoppers which you're at war with. These grasshoppe...
p,y = map(int,input().split()) for x in range(y,p,-1): # print(x) # check flag = True for i in range(2,min(p,int(x**0.5))+1): if x%i == 0: flag = False break if flag: print(x) exit() print(-1)
{ "input": [ "3 4\n", "3 6\n", "3 9\n", "3888 723508350\n", "4096 287894773\n", "20555 592508508\n", "649229491 965270051\n", "1000000000 1000000000\n", "13179877 557546766\n", "71233638 269883787\n", "5 50\n", "469621113 834270209\n", "8711 269883787\n", "2601 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The weather is fine today and hence it's high time to climb the nearby pine and enjoy the landscape. The pine's trunk includes several branches, located one above another and numbere...
962_D. Merge Equals_29319
You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value x that occurs in the array 2 or more times. Take the first two occurrences of x in this array (the two leftmost occurrences). Remove the left of these two occur...
n = int(input()) d = {} for i, x in enumerate(map(int, input().split())): while x in d: del d[x] x *= 2 d[x] = i print(len(d)) print(*d)
{ "input": [ "7\n3 4 1 2 2 1 1\n", "5\n10 40 20 50 30\n", "5\n1 1 3 1 1\n", "34\n967614464 967614464 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 100...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value x that occurs in the ar...
p02608 AIsing Programming Contest 2020 - XYZ Triplets_29335
Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions: * 1 \leq x,y,z * x^2 + y^2 + z^2 + xy + yz + zx = n Given an integer N, find each of f(1),f(2),f(3),\ldots,f(N). Constraints * All values in input are integers. * 1 \leq N \leq 10^4 Input Input is given from Sta...
n=int(input()) import math nn=int(math.sqrt(n))+1 a=[0]*(100*n) for x in range(1,nn+1): for y in range(1,nn+1): for z in range(1,nn+1): a[(x+y+z)**2-x*y-x*z-y*z]+=1 for i in range(1,n+1): print(a[i])
{ "input": [ "20", "27", "13", "3", "2", "4", "8", "14", "1", "31", "53", "10", "5", "6", "11", "15", "9", "16", "12", "22", "7", "29", "23", "21", "18", "17", "32", "33", "45", "25", "28", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions: * 1 \leq x,y,z * x^2 + y^2 + z^2 + xy + yz + zx = n Given an integer N, find e...
p02739 AtCoder Grand Contest 043 - Topology_29339
Given are a positive integer N and a sequence of length 2^N consisting of 0s and 1s: A_0,A_1,\ldots,A_{2^N-1}. Determine whether there exists a closed curve C that satisfies the condition below for all 2^N sets S \subseteq \\{0,1,\ldots,N-1 \\}. If the answer is yes, construct one such closed curve. * Let x = \sum_{i ...
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) A = list(map(int,readline().rstrip().decode())) minimal_sets = [] for n in range(1<<N): s = n is_min = (A[n] == 0) while s: s = (s - 1) & n if A[n] == 1 a...
{ "input": [ "2\n1000", "1\n10", "2\n1001", "1\n11", "1\n0", "1\n13", "2\n1010", "2\n1100", "2\n1110", "2\n0001", "2\n0101", "1\n12", "2\n0111", "2\n1111", "2\n0011", "2\n0010", "2\n0100", "2\n1101", "2\n1011", "2\n0110", "1\n17", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given are a positive integer N and a sequence of length 2^N consisting of 0s and 1s: A_0,A_1,\ldots,A_{2^N-1}. Determine whether there exists a closed curve C that satisfies the condi...
p02874 AtCoder Grand Contest 040 - Two Contests_29342
10^9 contestants, numbered 1 to 10^9, will compete in a competition. There will be two contests in this competition. The organizer prepared N problems, numbered 1 to N, to use in these contests. When Problem i is presented in a contest, it will be solved by all contestants from Contestant L_i to Contestant R_i (inclus...
# python template for atcoder1 import sys sys.setrecursionlimit(10**9) input = sys.stdin.readline class SegmentTree(): ''' 非再帰 segment tree ''' def __init__(self, n, func, init=float('inf')): ''' n->配列の長さ func:func(a,b)->val, func=minだとRMQになる 木の高さhとすると, n:h...
{ "input": [ "4\n1 20\n2 19\n3 18\n4 17", "4\n4 7\n1 4\n5 8\n2 5", "10\n457835016 996058008\n456475528 529149798\n455108441 512701454\n455817105 523506955\n457368248 814532746\n455073228 459494089\n456651538 774276744\n457667152 974637457\n457293701 800549465\n456580262 636471526", "4\n0 20\n2 19\n3 1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: 10^9 contestants, numbered 1 to 10^9, will compete in a competition. There will be two contests in this competition. The organizer prepared N problems, numbered 1 to N, to use in the...
p03008 diverta 2019 Programming Contest 2 - Squirrel Merchant_29346
The squirrel Chokudai has N acorns. One day, he decides to do some trades in multiple precious metal exchanges to make more acorns. His plan is as follows: 1. Get out of the nest with N acorns in his hands. 2. Go to Exchange A and do some trades. 3. Go to Exchange B and do some trades. 4. Go to Exchange A and do some...
n = int(input()) A = [int(item) for item in input().split()] B = [int(item) for item in input().split()] dp = [0] * (n+1) AB = [] for a, b in zip(A, B): if b > a: AB.append((a, b)) for i in range(n+1): for a, b in AB: if i - a >= 0: y = dp[i-a] + b - a if y > dp[i]: ...
{ "input": [ "23\n1 1 1\n2 1 1", "23\n1 1 1\n2 2 1", "23\n1 2 1\n2 1 1", "23\n1 2 1\n2 0 1", "23\n0 2 1\n2 0 1", "23\n1 1 2\n1 1 0", "23\n1 1 1\n2 0 1", "23\n1 4 1\n2 1 1", "23\n1 1 2\n2 3 0", "23\n0 2 1\n4 0 1", "23\n1 8 1\n2 1 1", "23\n0 2 1\n1 0 1", "23\n1 1 2\n2...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The squirrel Chokudai has N acorns. One day, he decides to do some trades in multiple precious metal exchanges to make more acorns. His plan is as follows: 1. Get out of the nest wi...
p03148 AtCoder Beginner Contest 116 - Various Sushi_29350
There are N pieces of sushi. Each piece has two parameters: "kind of topping" t_i and "deliciousness" d_i. You are choosing K among these N pieces to eat. Your "satisfaction" here will be calculated as follows: * The satisfaction is the sum of the "base total deliciousness" and the "variety bonus". * The base total de...
import sys input=sys.stdin.readline sys.setrecursionlimit(10**9) n,k=map(int,input().split()) Sushi=[list(map(int,input().split())) for i in range(n)] Sushi=sorted(Sushi,key=lambda x: x[1],reverse=True) ans=0 A=[] kind=set() for t,d in Sushi[:k]: ans+=d if t in kind: A.append(d) else: kind.a...
{ "input": [ "6 5\n5 1000000000\n2 990000000\n3 980000000\n6 970000000\n6 960000000\n4 950000000", "7 4\n1 1\n2 1\n3 1\n4 6\n4 5\n4 5\n4 5", "5 3\n1 9\n1 7\n2 6\n2 5\n3 1", "6 5\n5 1000000000\n2 990000000\n3 1109665355\n6 970000000\n6 960000000\n4 950000000", "7 4\n1 1\n2 1\n3 1\n4 6\n4 5\n4 5\n3 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N pieces of sushi. Each piece has two parameters: "kind of topping" t_i and "deliciousness" d_i. You are choosing K among these N pieces to eat. Your "satisfaction" here wil...
p03292 AtCoder Beginner Contest 103 - Task Scheduling Problem_29354
You have three tasks, all of which need to be completed. First, you can complete any one task at cost 0. Then, just after completing the i-th task, you can complete the j-th task at cost |A_j - A_i|. Here, |x| denotes the absolute value of x. Find the minimum total cost required to complete all the task. Constrain...
a,b,c=sorted(map(int,input().split())) print(abs(a-b)+abs(b-c))
{ "input": [ "1 6 3", "100 100 100", "11 5 5", "1 12 3", "100 100 000", "11 5 2", "1 24 3", "3 5 2", "1 16 4", "4 3 2", "100 111 000", "4 3 0", "2 16 7", "100 111 001", "5 3 0", "100 111 010", "110 111 110", "0 1 6", "1 1 8", "110 001 010...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have three tasks, all of which need to be completed. First, you can complete any one task at cost 0. Then, just after completing the i-th task, you can complete the j-th task at...
p03448 AtCoder Beginner Contest 087 - Coins_29358
You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan). In how many ways can we select some of these coins so that they are X yen in total? Coins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that co...
a = int(input()) b = int(input()) c = int(input()) x = int(input()) li = [[i, j, k] for i in range(a+1) for j in range(b+1) for k in range(c+1) if 500*i+100*j+50*k == x] print(len(li))
{ "input": [ "2\n2\n2\n100", "30\n40\n50\n6000", "5\n1\n0\n150", "2\n2\n4\n100", "30\n40\n77\n6000", "5\n0\n0\n150", "30\n47\n77\n6000", "5\n0\n0\n0", "2\n2\n5\n100", "2\n2\n10\n100", "30\n47\n77\n10696", "5\n0\n1\n0", "2\n0\n10\n100", "30\n56\n77\n10696", "...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan). In how many ways can we select some of these coins so that they are X yen in total? Coins...
p03608 AtCoder Beginner Contest 073 - joisino's travel_29362
There are N towns in the State of Atcoder, connected by M bidirectional roads. The i-th road connects Town A_i and B_i and has a length of C_i. Joisino is visiting R towns in the state, r_1,r_2,..,r_R (not necessarily in this order). She will fly to the first town she visits, and fly back from the last town she visi...
import itertools N, M, R = map(int, input().split()) r = [int(x) - 1 for x in input().split()] G = [[10 ** 7 for j in range(N)] for i in range(N)] for i in range(N): G[i][i] = 0 for i in range(M): a, b, c = map(int, input().split()) G[a - 1][b - 1] = c G[b - 1][a - 1] = c def warshall_floyd(G): for k in range...
{ "input": [ "4 6 3\n2 3 4\n1 2 4\n2 3 3\n4 3 1\n1 4 1\n4 2 2\n3 1 6", "3 3 3\n1 2 3\n1 2 1\n2 3 1\n3 1 4", "3 3 2\n1 3\n2 3 2\n1 3 6\n1 2 2", "4 6 3\n2 3 4\n1 2 4\n2 3 6\n4 3 1\n1 4 1\n4 2 2\n3 1 6", "5 3 2\n1 3\n2 3 2\n1 3 6\n1 2 2", "4 6 3\n2 3 4\n1 2 4\n2 3 6\n4 3 1\n1 4 1\n4 1 2\n3 1 6", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N towns in the State of Atcoder, connected by M bidirectional roads. The i-th road connects Town A_i and B_i and has a length of C_i. Joisino is visiting R towns in the st...
p03769 AtCoder Grand Contest 012 - Tautonym Puzzle_29366
We will call a string x good if it satisfies the following condition: * Condition: x can be represented as a concatenation of two copies of another string y of length at least 1. For example, `aa` and `bubobubo` are good; an empty string, `a`, `abcabcabc` and `abba` are not good. Eagle and Owl created a puzzle on ...
def test(): from itertools import combinations s = '111223111223' result = 0 for k in range(1, len(s) // 2 + 1): for idx in combinations(range(len(s)), 2 * k): success = True for ii in range(k): if s[idx[ii]] != s[idx[ii + k]]: success...
{ "input": [ "7", "299", "8", "203", "3", "45", "6", "22", "10", "35", "17", "52", "12", "84", "24", "2", "14", "0", "1", "39", "580", "9", "200", "4", "48", "13", "19", "16", "26", "40", "21", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We will call a string x good if it satisfies the following condition: * Condition: x can be represented as a concatenation of two copies of another string y of length at least 1. ...
p03938 AtCoder Grand Contest 007 - Construct Sequences_29370
You are given a permutation p of the set {1, 2, ..., N}. Please construct two sequences of positive integers a_1, a_2, ..., a_N and b_1, b_2, ..., b_N satisfying the following conditions: * 1 \leq a_i, b_i \leq 10^9 for all i * a_1 < a_2 < ... < a_N * b_1 > b_2 > ... > b_N * a_{p_1}+b_{p_1} < a_{p_2}+b_{p_2} < ... < a...
n = int(input()) x = list(map(int, input().split())) a = [20001*i for i in range(1,n+1)] b = [20001*(n+1-i) for i in range(1,n+1)] for i in range(n): b[x[i]-1] += i for x in a:print(x, end=' ') print() for x in b:print(x, end=' ')
{ "input": [ "3\n3 2 1", "3\n2 3 1", "2\n1 2", "2\n1 0", "2\n0 -1", "3\n2 0 1", "3\n1 3 2", "3\n1 -1 0", "3\n0 2 1", "3\n0 1 2", "3\n2 1 0", "2\n2 1", "2\n2 -1", "2\n-1 0", "2\n0 1", "2\n-1 2", "3\n1 0 2", "3\n-1 3 1", "3\n-1 0 1", "3\n0 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a permutation p of the set {1, 2, ..., N}. Please construct two sequences of positive integers a_1, a_2, ..., a_N and b_1, b_2, ..., b_N satisfying the following conditi...
p00030 Sum of Integers_29374
Write a program that extracts n different numbers from the numbers 0 to 9 and outputs the number of combinations that add up to s. Each n number is from 0 to 9, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is 1 + 2 + 3 = 6 0 +...
def dfs(depth,prev,sum): if depth == n: return sum == s ret = 0 for i in range(prev + 1,10): ret += dfs(depth + 1,i,sum + i) return ret while True: n,s = map(int,input().split()) if n == 0: break print(dfs(0,-1,0))
{ "input": [ "3 6\n3 1\n0 0", "3 5\n3 1\n0 0", "3 0\n5 1\n0 0", "1 0\n5 1\n0 0", "3 6\n3 0\n0 0", "2 5\n1 1\n0 0", "2 0\n1 1\n0 0", "3 2\n0 0\n0 0", "2 2\n0 0\n0 0", "3 7\n3 1\n0 0", "3 8\n3 1\n0 0", "1 5\n1 1\n0 0", "3 5\n1 1\n0 0", "3 9\n10 1\n0 0", "3 8\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Write a program that extracts n different numbers from the numbers 0 to 9 and outputs the number of combinations that add up to s. Each n number is from 0 to 9, and the same number ca...
p00161 Sport Meet_29378
An autumn sports festival is held. There are four events: foot race, ball-carrying, obstacle race, and relay. There are n teams participating, and we would like to commend the team with the shortest total time in this 4th event as the "winner", the next smallest team as the "runner-up", and the second team from the bot...
# -*- coding: utf-8 -*- import sys import os for s in sys.stdin: N = int(s) if N == 0: break A = [] for i in range(N): lst = list(map(int, input().split())) id = lst[0] data = lst[1:] time_sum = data[0] * 60 + data[1] + \ data[2] * 60 + data[...
{ "input": [ "8\n34001 3 20 3 8 6 27 2 25\n20941 3 5 2 41 7 19 2 42\n90585 4 8 3 12 6 46 2 34\n92201 3 28 2 47 6 37 2 58\n10001 3 50 2 42 7 12 2 54\n63812 4 11 3 11 6 53 2 22\n54092 3 33 2 54 6 18 2 19\n25012 3 44 2 58 6 45 2 46\n4\n1 3 23 1 23 1 34 4 44\n2 5 12 2 12 3 41 2 29\n3 5 24 1 24 2 0 3 35\n4 4 49 2 22 4...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: An autumn sports festival is held. There are four events: foot race, ball-carrying, obstacle race, and relay. There are n teams participating, and we would like to commend the team wi...
p00488 Lunch_29384
problem At the JOI pasta shop, the recommended pasta for lunch and the set menu of freshly squeezed juice are popular. When ordering this set menu, choose one from each of the three pasta and two juices of the day. The price is the total price of pasta and juice minus 50 yen. Given the price of pasta and juice for a ...
pasta = [int(input()) for i in range(3)] juice = [int(input()) for j in range(2)] print(min(pasta) + min(juice) - 50)
{ "input": [ "800\n700\n900\n198\n330", "800\n1126\n900\n198\n330", "800\n1126\n900\n132\n330", "800\n1126\n206\n132\n330", "800\n1126\n180\n132\n330", "800\n1126\n180\n29\n330", "800\n1126\n280\n29\n330", "800\n1860\n376\n29\n330", "800\n1860\n376\n39\n330", "800\n1860\n376\n7...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: problem At the JOI pasta shop, the recommended pasta for lunch and the set menu of freshly squeezed juice are popular. When ordering this set menu, choose one from each of the three ...
p00817 True Liars_29388
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ashore on a foggy island. Though he was exhausted and despaired, he was still fortunate to remember a legend of the foggy island, which he had heard from patriarchs in his childhood. This must be the island in the legen...
from collections import defaultdict class UnionFind(): def __init__(self, n): self.parent = list(range(n)) self.rank = [0] * n def find(self, x): if self.parent[x] == x: return x else: self.parent[x] = self.find(self.parent[x]) return self.p...
{ "input": [ "2 1 1\n1 2 no\n2 1 no\n3 2 1\n1 1 yes\n2 2 yes\n3 3 yes\n2 2 1\n1 2 yes\n2 3 no\n5 4 3\n1 2 yes\n1 3 no\n4 5 yes\n5 6 yes\n6 7 no\n0 0 0", "2 1 1\n1 2 no\n2 1 no\n3 2 1\n1 1 sey\n2 2 yes\n3 3 yes\n2 2 1\n1 2 yes\n2 3 no\n5 4 3\n1 2 yes\n1 3 no\n4 5 yes\n5 6 yes\n6 7 no\n0 0 0", "2 1 1\n1 2 n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ashore on a foggy island. Though he was exhausted and despaired, he was still fort...
p00948 Distribution Center_29391
Example Input 4 3 1000 1 2000 2 3000 3 Output 2 3 4 4
N,M = map(int,input().split()) XY = [] for _ in range(M): x,y = map(int,input().split()) xy = (x,y) XY.append(xy) XY.sort(key = lambda x:x[0]) # x sort *minY, = range(N) *maxY, = range(N) for _,y in XY: y0,y1 = y-1, y minY[y1] = minY[y0] maxY[y0] = maxY[y1] ans = [maxY[i] - minY[i]+1 for i i...
{ "input": [ "4 3\n1000 1\n2000 2\n3000 3", "8 3\n1000 1\n2000 2\n3000 3", "8 3\n1010 1\n2889 2\n3000 5", "9 3\n1010 1\n2889 2\n4734 5", "9 3\n1010 2\n2889 2\n4734 5", "4 3\n1000 1\n2000 2\n3000 1", "8 3\n1000 1\n2889 2\n439 3", "16 3\n1010 1\n2889 2\n3000 3", "8 3\n1010 1\n2889 2\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Example Input 4 3 1000 1 2000 2 3000 3 Output 2 3 4 4 ### Input: 4 3 1000 1 2000 2 3000 3 ### Output: 2 3 4 4 ### Input: 8 3 1000 1 2000 2 3000 3 ### Output: 2 3 4 4 1 1 1 1 ...
p01533 Acrophobia_29398
C: Acrophobia Yayoi Takasugi is a super-selling idol. There is one thing she is not good at. It's a high place ... She is extremely afraid of heights. This time, due to the producer's inadequacy, she decided to take on the following challenges on a variety show. This location will be held in a room in a ninja mansion...
from itertools import permutations from collections import deque w, h = map(int, input().split()) mp = ["X" * (w + 4)] * 2 + ["XX" + input() + "XX" for _ in range(h)] + ["X" * (w + 4)] * 2 m_lst = [] holes = [] for y in range(2, h + 2): for x in range(2, w + 2): if mp[y][x] == "S": sx, sy = x, y if mp[...
{ "input": [ "3 4\nS.M\n...\n...\nM.G", "3 4\nM.S\n...\n...\nM.G", "3 4\nM.S\n...\n...\nG.M", "3 4\nS.N\n...\n...\nM.G", "3 4\nM.S\n...\n...\nMG.", "3 4\nL.S\n...\n...\nN.G", "3 4\nL.S\n...\n...\nNG.", "3 4\nM.S\n...\n...\n.GM", "3 4\n.MS\n...\n...\nM.G", "3 4\n.MS\n...\n...\nG...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: C: Acrophobia Yayoi Takasugi is a super-selling idol. There is one thing she is not good at. It's a high place ... She is extremely afraid of heights. This time, due to the producer'...
p01689 Dowsing Machine_29401
C --Dowsing Machine Story People make noise with X and Y, but the coming era will be "D". "Paklin Monster D" is a very popular game in which "D people" search for treasure using the "D machine" developed by the secret society "R team". In this game, the person D in the square with the grid map repeatedly moves to th...
def bfs(start_y,start_x,goal_y,goal_x): bfs_map = [[-1 for x in range(w)] for y in range(h)] move_y = [1,-1,0,0] move_x = [0,0,1,-1] data_y = [start_y] data_x = [start_x] bfs_map[start_y][start_x] = 1 while len(data_x) != 0: y = data_y.pop(0) x = data_x.pop(0) for i i...
{ "input": [ "6 10 3 4\n##########\n#........#\n#...D....#\n#........#\n#........#\n##########\n2 4 6\n3 2 0\n7 4 2\n8 4 3\n1 4 1", "6 10 3 4\n##########\n#........#\n#...D....\"\n#........#\n#........#\n##########\n2 4 6\n3 2 0\n7 4 2\n8 4 3\n1 4 1", "6 10 3 4\n##########\n#........#\n#...D....\"\n#........
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: C --Dowsing Machine Story People make noise with X and Y, but the coming era will be "D". "Paklin Monster D" is a very popular game in which "D people" search for treasure using the...
p01833 Modern Announce Network_29404
Example Input 4 2 1 1 1 2 3 4 3 1 2 2 4 3 4 Output 2 1
import sys readline = sys.stdin.readline write = sys.stdout.write from collections import deque INF = 10**9 def bfs(N, G, s): dist = [INF]*N *lb, = range(N) dist[s] = 0 que = deque([s]) while que: v = que.popleft() d = dist[v] + 1 l = lb[v] for w in G[v]: ...
{ "input": [ "4 2 1 1\n1 2\n3\n4\n3\n1 2\n2 4\n3 4", "4 2 1 1\n1 2\n3\n4\n3\n1 4\n2 4\n3 4", "4 2 1 1\n2 2\n3\n4\n3\n1 2\n2 4\n3 4", "4 2 1 1\n1 1\n3\n4\n3\n1 2\n2 4\n3 4", "4 2 1 1\n1 2\n3\n4\n3\n2 4\n2 4\n3 4", "4 2 1 1\n1 2\n3\n4\n3\n1 1\n2 4\n3 4", "4 2 1 1\n1 2\n3\n4\n3\n2 4\n4 4\n3 4...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Example Input 4 2 1 1 1 2 3 4 3 1 2 2 4 3 4 Output 2 1 ### Input: 4 2 1 1 1 2 3 4 3 1 2 2 4 3 4 ### Output: 2 1 ### Input: 4 2 1 1 1 2 3 4 3 1 4 2 4 3 4 ### Output: 2 1 ### C...
p01968 Hierarchical Calculator_29407
B: 階層的計算機 (Hierarchical Calculator) Problem Ebi-chan has N formulae: y = a_i x for i =1, ..., N (inclusive). Now she considers a subsequence of indices with length k: s_1, s_2, ..., s_k. At first, let x_0 be 1 and evaluate s_1-th formulae with x = x_0. Next, let x_1 be the output of s_1 and evaluate s_2-th formulae w...
# -*- coding: utf-8 -*- from collections import Counter def inpl(): return tuple(map(int, input().split())) N = int(input()) A = list(map(int, input().split())) C = Counter(A) if C[-2] % 2 == 0: ans = [i+1 for i, a in enumerate(A) if abs(a) == 2] print(len(ans)) if len(ans): print(*ans, sep="\n") ...
{ "input": [ "4\n2 0 -2 1", "4\n2 -1 -2 1", "4\n2 0 -1 1", "4\n1 -1 -2 0", "4\n0 -1 -1 1", "4\n-1 -1 0 2", "4\n-1 -2 0 2", "4\n2 -1 1 2", "4\n-1 1 -2 0", "4\n2 -1 2 2", "4\n1 2 -1 1", "4\n0 -1 0 -2", "4\n1 0 -2 -1", "4\n2 2 -1 1", "4\n-1 2 -2 2", "4\n-1 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: B: 階層的計算機 (Hierarchical Calculator) Problem Ebi-chan has N formulae: y = a_i x for i =1, ..., N (inclusive). Now she considers a subsequence of indices with length k: s_1, s_2, ...,...
p02255 Insertion Sort_29412
Write a program of the Insertion Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode: for i = 1 to A.length-1 key = A[i] /* insert A[i] into the sorted sequence A[0,...,j-1] */ j = i - 1 while j >= 0 and A[j] > key A[j+1] = A[j] j-- A[j+1] = key Note...
n = int(input()) a = list(map(int, input().split())) print(*a, sep=' ') for i in range(1, n): v = a[i] j = i - 1 while j >= 0 and a[j] > v: a[j+1] = a[j] a[j] = v j -= 1 print(*a, sep=' ')
{ "input": [ "3\n1 2 3", "6\n5 2 4 6 1 3", "3\n2 2 3", "6\n5 2 5 6 1 3", "3\n2 4 3", "6\n5 2 5 8 1 3", "3\n2 4 5", "6\n5 4 5 8 1 3", "3\n2 4 8", "6\n5 4 5 7 1 3", "3\n2 6 8", "6\n5 4 5 7 1 0", "3\n4 6 8", "6\n5 4 5 0 1 0", "3\n4 2 8", "6\n5 4 5 1 1 0", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Write a program of the Insertion Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode: for i = 1 to A.length-1 key =...
p02403 Print a Rectangle_29416
Draw a rectangle which has a height of H cm and a width of W cm. Draw a 1-cm square by single '#'. Constraints * 1 ≤ H ≤ 300 * 1 ≤ W ≤ 300 Input The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space. The input ends with two 0 (when both H and W are zero)...
while True: H,W = map(int,input().split()) if H == 0 and W == 0: exit() for i in range(H): print("#"*W) print()
{ "input": [ "3 4\n5 6\n2 2\n0 0", "3 4\n5 6\n2 3\n0 0", "3 4\n5 6\n2 0\n0 0", "3 4\n5 6\n2 4\n0 0", "4 4\n5 6\n2 0\n0 0", "4 4\n5 6\n4 0\n0 0", "4 3\n5 6\n4 -1\n0 0", "3 5\n5 6\n2 2\n0 0", "3 4\n5 0\n2 2\n0 0", "3 4\n5 6\n2 1\n0 0", "6 4\n5 6\n2 4\n0 0", "3 4\n5 6\n4 -...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Draw a rectangle which has a height of H cm and a width of W cm. Draw a 1-cm square by single '#'. Constraints * 1 ≤ H ≤ 300 * 1 ≤ W ≤ 300 Input The input consists of multiple dat...
1005_E2. Median on Segments (General Case Edition)_29425
You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-dec...
def cv(r, n, d): s = [0]*(2*n + 1) q = n ans = 0 s[q] = 1 z = 0 for i in range(n): if d[i] < r: q -= 1 z -= s[q] else: z += s[q] q += 1 ans += z s[q] += 1 return ans n, r = map(int,input().split()) d = list(map(...
{ "input": [ "15 2\n1 2 3 1 2 3 1 2 3 1 2 3 1 2 3\n", "5 4\n1 4 5 60 4\n", "3 1\n1 1 1\n", "1 200000\n1\n", "2 2\n1 1\n", "3 1\n3 2 1\n", "1 1\n2\n", "2 2\n2 1\n", "10 7\n14 20 3 3 8 16 17 13 6 4\n", "3 1\n1 3 2\n", "2 1\n1 2\n", "4 3\n3 5 2 3\n", "1 1\n1\n", "2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly th...
1029_D. Concatenated Multiples_29429
You are given an array a, consisting of n positive integers. Let's call a concatenation of numbers x and y the number that is obtained by writing down numbers x and y one right after another without changing the order. For example, a concatenation of numbers 12 and 3456 is a number 123456. Count the number of ordered...
import os import sys from io import BytesIO, IOBase # import string # characters = string.ascii_lowercase # digits = string.digits # sys.setrecursionlimit(int(1e6)) # dir = [-1,0,1,0,-1] # moves = 'NESW' inf = float('inf') from functools import cmp_to_key from collections import defaultdict as dd from collections impo...
{ "input": [ "5 2\n3 7 19 3 3\n", "6 11\n45 1 10 12 11 7\n", "4 2\n2 78 4 10\n", "2 994305000\n922337204 1000000000\n", "100 2\n38 415 2960 8618 8531 6194 6811 4778 3955 1056 7147 9144 8424 5766 4492 2994 978 1102 2028 3569 8960 6106 9363 7714 5179 7801 4768 9025 6562 4469 5578 5224 6845 362 3229 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array a, consisting of n positive integers. Let's call a concatenation of numbers x and y the number that is obtained by writing down numbers x and y one right after...
104_C. Cthulhu_29433
...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu... Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super...
n, m = [int(i) for i in input().split()] adj = [[] for i in range(n+1)] seen = [False for i in range(n+1)] pai = [0 for i in range(n+1)] ciclos = 0 def dfs (u): seen[u] = True global ciclos for v in adj[u]: if not seen[v]: pai[v] = u dfs(v) elif v != pai[u]: ...
{ "input": [ "6 6\n6 3\n6 4\n5 1\n2 5\n1 4\n5 4\n", "6 5\n5 6\n4 6\n3 1\n5 1\n1 2\n", "5 5\n2 3\n2 4\n5 4\n4 1\n1 2\n", "2 0\n", "12 10\n1 11\n3 6\n5 7\n4 7\n6 8\n11 7\n3 12\n11 12\n7 9\n12 2\n", "10 10\n1 10\n5 9\n6 2\n8 9\n9 1\n5 4\n2 8\n1 3\n6 3\n4 1\n", "4 3\n1 2\n2 3\n3 1\n", "30 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: ...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu... Whereas on the o...
1093_F. Vasya and Array_29438
Vasya has got an array consisting of n integers, and two integers k and len in addition. All numbers in the array are either between 1 and k (inclusive), or equal to -1. The array is good if there is no segment of len consecutive equal numbers. Vasya will replace each -1 with some number from 1 to k (inclusive) in suc...
def vasya_and_array(): n,k,leng = [int(x) for x in input().split()] if(leng==1): return 0 a = [int(x) for x in input().split()] mod = 998244353 a.insert(0,0) dp = [[0 for x in range((k+1))] for y in range(n+1)] sumdp = [0 for _ in range(n+1)] sumdp[0]=1 count = [0 for _ in ra...
{ "input": [ "6 3 2\n1 1 -1 -1 -1 -1\n", "5 2 3\n1 -1 1 -1 2\n", "10 42 7\n-1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n", "1 100 1\n-1\n", "1 100 2\n-1\n", "6 3 2\n1 1 -1 -1 0 -1\n", "5 2 3\n2 -1 1 -1 2\n", "5 2 3\n2 -1 2 -1 2\n", "6 3 4\n1 1 -1 -1 1 -1\n", "6 3 2\n1 2 -1 -1 -1 -1\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has got an array consisting of n integers, and two integers k and len in addition. All numbers in the array are either between 1 and k (inclusive), or equal to -1. The array is ...
1142_A. The Beatles_29444
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n ⋅ k cities. The cities are numerated from 1 to n ⋅ k, the distance between the neighboring cities is exactly 1 km. Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on...
from math import gcd def lcm(a,b): return a*b//gcd(a,b) def mod(a,m): a%=m if a<0: a+=m return a n,k=map(int, input().split()) a,b=map(int, input().split()) st,ed=[],[] nk=n*k for i in range(0,n): st.append(mod(i*k-a,nk)) st.append(mod(i*k+a,nk)) ed.append(mod(i*k-b,nk)) ed.append(mod(i*k+b,nk)) mn=10000...
{ "input": [ "3 2\n0 0\n", "2 3\n1 1\n", "1 10\n5 3\n", "100000 99737\n34242 43667\n", "3 3\n1 0\n", "99871 99873\n2979 17955\n", "14621 29242\n7 13\n", "23981 21841\n10376 10637\n", "36739 36739\n18369 18369\n", "3 10\n1 3\n", "2 98763\n10021 19979\n", "97979 85648\n41...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n ⋅ k cities. The cities are numerated from 1 to n ⋅ k, the distance between the neigh...
1162_C. Hide and Seek_29448
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent. Alice initially has a token on some cell on the line, and Bob tries to guess where it is. Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order....
def main(): n, k = map(int, input().split()) x = [int(i) for i in input().split()] ans = 3 * n - 2 a, b = set(), set() for val in x: if val - 1 in a: b.add((val, val - 1)) if val + 1 in a: b.add((val, val + 1)) a.add(val) ans -= len(a) + len(b) ...
{ "input": [ "4 8\n1 2 3 4 4 3 2 1\n", "100000 1\n42\n", "5 3\n5 1 4\n", "1 2\n1 1\n", "50 75\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent. Alice initially has a tok...
1183_B. Equalize Prices_29452
There are n products in the shop. The price of the i-th product is a_i. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly. In fact, the owner of the shop can change the price of some product i in such a way that the difference between the old price of this ...
q = int(input()) for qq in range(q): n, k = map(int, input().split()) *a, = map(int, input().split()) m = 0 M = 1e9 for x in a: M = min(M, x + k) m = max(m, x - k) if M >= m: print(M) else: print(-1)
{ "input": [ "4\n5 1\n1 1 2 3 1\n4 2\n6 4 8 5\n2 2\n1 6\n3 5\n5 2 5\n", "1\n3 100000000\n100000000 100000000 100000000\n", "1\n2 1\n100000000 100000000\n", "5\n8 19551489\n31131899 17585660 3913937 14521321 5400652 96097161 58347480 35051437\n1 55716143\n15578410\n7 19560555\n65980880 53703939 2025950...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n products in the shop. The price of the i-th product is a_i. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly...
1200_C. Round Corridor_29456
Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by n sectors, and the outer area is equally divided by m sectors. A wall exists between each pair of sectors of same area (inner or outer), but there is no wall between the inner area and the outer area. A wa...
n,m,q = map(int,input().split()) s = [] for i in range(q): s.append(list(map(int,input().split()))) def gcd(a,b): if a == 0: return b return gcd(b % a, a) def lcm(a,b): return (a // gcd(a,b))*b g = lcm(n,m) a,b = g//m,g//n for i in s: s1,e1,s2,e2 = i if s1==1: sa = e1 ...
{ "input": [ "4 6 3\n1 1 2 3\n2 6 1 2\n2 6 2 4\n", "999999937 999999874000003969 1\n1 979917353 2 983249032983207370\n", "1 1 10\n1 1 1 1\n1 1 1 1\n1 1 2 1\n1 1 1 1\n2 1 2 1\n2 1 1 1\n1 1 2 1\n1 1 2 1\n2 1 1 1\n2 1 1 1\n", "3 9 1\n1 1 2 4\n", "2 3 5\n2 3 1 2\n2 3 2 3\n1 1 1 1\n1 2 1 2\n2 1 2 1\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by n sectors, and the outer area is equally divided by m sectors. A wal...
1242_B. 0-1 MST_29462
Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook with a description of a graph. It is an undirected weighted graph on n vertices. It is a complete graph: each pair of vertices is conne...
import collections as cc import sys input=sys.stdin.readline #sys.setrecursionlimit(10**9) I=lambda:list(map(int,input().split())) n,m=I() g=[set() for i in range(n+1)] xx=[0]*(n+1) for i in range(m): x,y=I() g[x].add(y) g[y].add(x) parent=[i for i in range(n+1)] def find(x): while x!=parent[x]: x=parent[x] retu...
{ "input": [ "6 11\n1 3\n1 4\n1 5\n1 6\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6\n", "3 0\n", "1 0\n", "2 0\n", "2 1\n1 2\n", "3 1\n2 3\n", "10 10\n1 5\n1 8\n1 9\n5 8\n8 9\n4 7\n2 3\n3 10\n2 6\n2 10\n", "3 3\n1 2\n3 1\n2 3\n", "6 9\n1 2\n1 4\n1 5\n2 3\n2 5\n2 6\n3 5\n4 6\n5 6\n", "100...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook...
1261_B2. Optimal Subsequences (Hard Version)_29465
This is the harder version of the problem. In this version, 1 ≤ n, m ≤ 2⋅10^5. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more e...
from bisect import bisect_left, bisect_right, insort_right class SquareSkipList: def __init__(self, values=None, sorted_=False, square=1000, seed=42): inf = float("inf") self.square = square if values is None: self.rand_y = seed self.layer1 = [inf] self.la...
{ "input": [ "7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n", "3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n", "1\n1\n3\n1 1\n1 1\n1 1\n", "1\n1000000000\n1\n1 1\n", "2\n1 10\n3\n2 2\n2 1\n1 1\n", "2\n3922 3922\n3\n2 2\n2 1\n1 1\n", "5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This is the harder version of the problem. In this version, 1 ≤ n, m ≤ 2⋅10^5. You can hack this problem if you locked it. But you can hack the previous problem only if you locked bot...
1283_D. Christmas Trees_29469
There are n Christmas trees on an infinite number line. The i-th tree grows at the position x_i. All x_i are guaranteed to be distinct. Each integer point can be either occupied by the Christmas tree, by the human or not occupied at all. Non-integer points cannot be occupied by anything. There are m people who want t...
import heapq n,m = map(int,input().split()) x = sorted(map(int,input().split())) # (distance, position, direction, maxval) pos = [(1,x[0]-1,-1,m), (1,x[-1]+1,1,m)] for i in range(n-1): left = (x[i+1]-x[i])//2 right = (x[i+1]-x[i]-1)//2 if left: pos.append((1,x[i]+1,1,left)) if right: p...
{ "input": [ "3 5\n0 3 1\n", "2 6\n1 5\n", "2 6\n1 5\n", "2 6\n1 5\n", "2 6\n1 5\n", "3 5\n0 3 1\n", "2 6\n1 5\n", "3 5\n0 3 1\n", "3 5\n0 3 1\n", "2 6\n1 5\n", "3 5\n0 3 1\n", "3 5\n0 3 1\n", "2 6\n2 5\n", "2 3\n1 5\n", "2 6\n0 5\n", "3 5\n0 3 2\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n Christmas trees on an infinite number line. The i-th tree grows at the position x_i. All x_i are guaranteed to be distinct. Each integer point can be either occupied by t...
1327_A. Sum of Odd Integers_29473
You are given two integers n and k. Your task is to find if n can be represented as a sum of k distinct positive odd (not divisible by 2) integers or not. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 10^5) — the number of test cases. The next t lines...
import collections class Solution: def solve(self, n, k): isKOdd = k & 1 isNOdd = n & 1 if isKOdd ^ isNOdd: return False return k**2 <= n sol = Solution() t = int(input().strip()) # [n,m] = list(map(int, input().strip().split())) for i in range(t): [n, k] = l...
{ "input": [ "6\n3 1\n4 2\n10 3\n10 2\n16 4\n16 5\n", "1\n10000000 9999998\n", "1\n10000000 3164\n", "1\n2 65536\n", "11\n4 2\n4 2\n4 2\n4 2\n4 2\n4 2\n4 2\n4 2\n4 2\n4 2\n4 2\n", "1\n9999999 9999997\n", "1\n1926 817\n", "1\n1000000 6326\n", "1\n994999 23\n", "1\n10000000 32588...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two integers n and k. Your task is to find if n can be represented as a sum of k distinct positive odd (not divisible by 2) integers or not. You have to answer t indepe...
1368_A. C+=_29477
Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a "+=" operation that adds the right-hand side value to the left-hand side variable. For example, performing "a += b" when a = 2, b = 3 changes the value of a to 5 (the value of b does not change). In a prototype progr...
t=int(input()) c=[] for i in range(t): a,b,n=input().split() a,b,n=int(a),int(b),int(n) count=0 while True: if a<b: a=a+b else: b=a+b count+=1 if max(a,b)>n: break c.append(count) for j in c: print(j)
{ "input": [ "2\n1 2 3\n5 4 100\n", "16\n1 1 1\n3 4 7\n4 5 13\n456 123 7890123\n1 1 1000000000\n45 12 782595420\n1 1000000000 1000000000\n1 999999999 1000000000\n1 99999 676497416\n5 6 930234861\n8 9 881919225\n500000000 500000000 1000000000\n1000000000 1000000000 1000000000\n999999999 1000000000 1000000000\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a "+=" operation that adds the right-hand side value to the left-hand side variabl...
1389_C. Good String_29481
Let's call left cyclic shift of some string t_1 t_2 t_3 ... t_{n - 1} t_n as string t_2 t_3 ... t_{n - 1} t_n t_1. Analogically, let's call right cyclic shift of string t as string t_n t_1 t_2 t_3 ... t_{n - 1}. Let's say string t is good if its left cyclic shift is equal to its right cyclic shift. You are given str...
t = int(input()) for i in range(t): s=input() ans=0 e=len(s) qr=0 for j in range(10): for q in range(10): b=False r=0 for i in range(len(s)): if j!=q: if (s[i]==str(j)) and (not b): ...
{ "input": [ "3\n95831\n100120013\n252525252525\n", "1\n1111\n", "1\n0111\n", "3\n95831\n100120013\n289014129764\n", "1\n0011\n", "1\n0101\n", "3\n142345\n100120013\n253858690946\n", "3\n97433\n36392895\n317241747758\n", "3\n97433\n585375\n317241747758\n", "3\n97433\n193252\n31...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's call left cyclic shift of some string t_1 t_2 t_3 ... t_{n - 1} t_n as string t_2 t_3 ... t_{n - 1} t_n t_1. Analogically, let's call right cyclic shift of string t as string t...
1409_F. Subsequences of Length Two_29485
You are given two strings s and t consisting of lowercase Latin letters. The length of t is 2 (i.e. this string consists only of two characters). In one move, you can choose any character of s and replace it with any lowercase Latin letter. More formally, you choose some i and replace s_i (the character at the positio...
n,k = map(int, input().split()) s,p = input(), input() a,b = p[0],p[1] if a==b: x = min(s.count(a)+k,n) print(x*(x-1)>>1) else: dp = [[-10000]*(k+2) for _ in range(n+2)] dp[1][1] = 0 for i in range(n): for q in range(i+2,0,-1): for t in range(min(k,i+1)+1,0,-1): d...
{ "input": [ "7 2\nabacaba\naa\n", "15 6\nqwertyhgfdsazxc\nqa\n", "7 3\nasddsaf\nsd\n", "4 2\nbbaa\nab\n", "200 100\ncxqtpwtozcnavklpcksprgecocdraaryxzztqkcfzsrhdrzabtwdukzmsgpnvvgigbtyaxubrngiomfatojjvnvhogqqlujsrglodzwmpyjijtzdzcczlxvhmkkefuereazzwfqbndaccpvlupgixdezadhvwdfzxqzmnkcgwktubylaiezru...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two strings s and t consisting of lowercase Latin letters. The length of t is 2 (i.e. this string consists only of two characters). In one move, you can choose any char...
1430_D. String Deletion_29489
You have a string s consisting of n characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: 1. select an integer i from 1 to the length of the string s, then delete the character s_i (the string length gets reduced by 1, the indices of characters ...
''' Auther: ghoshashis545 Ashis Ghosh College: jalpaiguri Govt Enggineering College ''' from os import path import sys from heapq import heappush,heappop from functools import cmp_to_key as ctk from collections import deque,defaultdict as dd from bisect import bisect,bisect_left,bisect_right,insort,insort_left...
{ "input": [ "5\n6\n111010\n1\n0\n1\n1\n2\n11\n6\n101010\n", "5\n6\n111010\n1\n1\n1\n1\n2\n11\n6\n101010\n", "5\n6\n111000\n1\n1\n1\n1\n2\n11\n6\n101010\n", "5\n6\n011011\n1\n0\n1\n1\n2\n11\n6\n111000\n", "5\n6\n111111\n1\n0\n1\n1\n2\n11\n6\n101010\n", "5\n6\n111000\n1\n1\n1\n1\n2\n18\n6\n0000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have a string s consisting of n characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: 1. select an integ...
1453_F. Even Harder_29492
Gildong is now developing a puzzle game. The puzzle consists of n platforms numbered from 1 to n. The player plays the game as a character that can stand on each platform and the goal of the game is to move the character from the 1-st platform to the n-th platform. The i-th platform is labeled with an integer a_i (0 ≤...
MAX_N = 3000 inf = MAX_N def solve(): global inf n = int(input()) a = [0] + list(map(int, input().split())) dp = [[0] * (n + 1) for i in range(n + 1)] for i in range(2, n + 1): cnt = 0 for j in range(i, n + 1): dp[i][j] = inf for j in range(i - 1, 0, -1): ...
{ "input": [ "3\n4\n1 1 1 0\n5\n4 3 2 1 0\n9\n4 1 4 2 1 0 2 1 0\n", "3\n4\n1 1 1 0\n5\n4 3 2 1 0\n9\n4 1 4 3 1 0 2 1 0\n", "3\n4\n1 1 1 0\n5\n4 3 2 1 0\n9\n4 1 4 2 1 0 2 0 0\n", "3\n4\n1 1 1 0\n5\n2 3 2 1 0\n9\n3 1 4 1 1 0 2 1 0\n", "3\n4\n1 1 1 0\n5\n4 3 2 1 0\n9\n4 2 4 3 1 0 2 1 0\n", "3\n4\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Gildong is now developing a puzzle game. The puzzle consists of n platforms numbered from 1 to n. The player plays the game as a character that can stand on each platform and the goal...
1504_B. Flip the Bits_29498
There is a binary string a of length n. In one operation, you can select any prefix of a with an equal number of 0 and 1 symbols. Then all symbols in the prefix are inverted: each 0 becomes 1 and each 1 becomes 0. For example, suppose a=0111010000. * In the first operation, we can select the prefix of length 8 sin...
# from sys import stdin,stdout # input=stdin.readline import math # t=int(input()) from collections import Counter import bisect for _ in range(int(input())): n = int(input()) a = list(map(int,input())) b = list(map(int,input())) # print(a,b) count = [0 for i in range(n)] if a[0]: count[...
{ "input": [ "5\n10\n0111010000\n0100101100\n4\n0000\n0000\n3\n001\n000\n12\n010101010101\n100110011010\n6\n000111\n110100\n", "5\n10\n0111010000\n0100101100\n4\n0000\n0000\n3\n000\n000\n12\n010101010101\n100110011010\n6\n000111\n110100\n", "5\n10\n0111010000\n0101101100\n4\n0000\n0000\n3\n000\n000\n12\n0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a binary string a of length n. In one operation, you can select any prefix of a with an equal number of 0 and 1 symbols. Then all symbols in the prefix are inverted: each 0 b...
1528_A. Parsa's Humongous Tree_29501
Parsa has a humongous tree on n vertices. On each vertex v he has written two integers l_v and r_v. To make Parsa's tree look even more majestic, Nima wants to assign a number a_v (l_v ≤ a_v ≤ r_v) to each vertex v such that the beauty of Parsa's tree is maximized. Nima's sense of the beauty is rather bizarre. He de...
import sys input = sys.stdin.buffer.readline def main(): t = int(input()); INF = float("inf") for _ in range(t): n = int(input()) L = []; R = [] for i in range(n): l,r = map(int,input().split()) L.append(l); R.append(r) G = [[] for _ in range(n)] ...
{ "input": [ "3\n2\n1 6\n3 8\n1 2\n3\n1 3\n4 6\n7 9\n1 2\n2 3\n6\n3 14\n12 20\n12 19\n2 12\n10 17\n3 17\n3 2\n6 5\n1 5\n2 6\n4 6\n", "3\n2\n1 6\n3 8\n1 2\n3\n1 3\n4 6\n7 9\n1 2\n2 3\n6\n4 14\n12 20\n12 19\n2 12\n10 17\n3 17\n3 2\n6 5\n1 5\n2 6\n4 6\n", "3\n2\n1 6\n3 8\n1 2\n3\n1 3\n4 6\n7 9\n1 2\n2 3\n6\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Parsa has a humongous tree on n vertices. On each vertex v he has written two integers l_v and r_v. To make Parsa's tree look even more majestic, Nima wants to assign a number a_v (...
226_B. Naughty Stone Piles_29513
There are n piles of stones of sizes a1, a2, ..., an lying on the table in front of you. During one move you can take one pile and add it to the other. As you add pile i to pile j, the size of pile j increases by the current size of pile i, and pile i stops existing. The cost of the adding operation equals the size of...
class CodeforcesTask226BSolution: def __init__(self): self.result = '' self.n = 0 self.piles = [] self.q = 0 self.variants = [] def read_input(self): self.n = int(input()) self.piles = [int(x) for x in input().split(" ")] self.q = int(input()) ...
{ "input": [ "5\n2 3 4 1 1\n2\n2 3\n", "2\n7 4\n1\n7\n", "1\n1\n3\n2 1 10\n", "3\n1 6 8\n1\n6\n", "1\n3\n1\n2\n", "1\n10\n5\n5 3 7 7 1\n", "2\n7 4\n1\n7\n", "5\n9 5 7 3 3\n1\n3\n", "2\n2 9\n5\n4 10 7 3 4\n", "1\n7\n4\n6 2 3 3\n", "4\n8 10 4 4\n3\n7 8 1\n", "5\n9 5 7 3 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n piles of stones of sizes a1, a2, ..., an lying on the table in front of you. During one move you can take one pile and add it to the other. As you add pile i to pile j, t...
275_A. Lights Out_29519
Lenny is playing a game on a 3 × 3 grid of lights. In the beginning of the game all lights are switched on. Pressing any of the lights will toggle it and all side-adjacent lights. The goal of the game is to switch all the lights off. We consider the toggling as follows: if the light was switched on then it will be swit...
matrix=[[1]*3 for i in range(3)] togg=[[0]*3 for i in range(3)] for i in range(3): togg[i]=list(map(int,input().split())) for i in range(3): for j in range(3): if togg[i][j]%2!=0: matrix[i][j]=int(not matrix[i][j]) if i-1>=0: matrix[i-1][j]=int(not matrix[i-1][j]...
{ "input": [ "1 0 0\n0 0 0\n0 0 1\n", "1 0 1\n8 8 8\n2 0 3\n", "16 69 64\n48 21 80\n81 51 51\n", "62 74 80\n95 74 93\n2 47 95\n", "71 84 18\n100 19 67\n9 76 15\n", "13 85 77\n25 50 45\n65 79 9\n", "24 54 37\n60 63 6\n1 84 26\n", "0 0 0\n0 0 0\n0 0 1\n", "32 15 65\n7 54 36\n5 51 3\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Lenny is playing a game on a 3 × 3 grid of lights. In the beginning of the game all lights are switched on. Pressing any of the lights will toggle it and all side-adjacent lights. The...
298_C. Parity Game_29523
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: * Write parity(a...
a=input() b=input() tot1=0 tot2=0 for i in a: tot1+=int(i) for i in b: tot2+=int(i) if tot1+tot1%2>=tot2: print("YES") else: print("NO")
{ "input": [ "0011\n1110\n", "01011\n0110\n", "0110011\n01100110\n", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n11\n", "1\n0\n", "11111\n111111...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (s...
321_A. Ciel and Robot_29527
Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operation. There are four move operations at all: * 'U': go up, (x, y) → (x, y+1); * 'D': go down, (x, y) → (x, y-1); * 'L': go left...
#http://codeforces.com/problemset/problem/321/A import logging as log class Point(object): def __init__(self, x,y): self.x, self.y = x, y def __str__(self): return "({}, {})".format(self.x, self.y) def __add__(self, other): #add 2 points together return Point(self.x + other.x, self.y + other.y) de...
{ "input": [ "0 0\nD\n", "-1 1000000000\nLRRLU\n", "2 2\nRU\n", "1 2\nRU\n", "285102789 570205594\nRRDULRULULRRDUURRLURUDDULLRDUL\n", "825368095 -825368096\nRD\n", "999999999 -999999999\nRRRRRRRRRRRRRRRRRRRRRRRRRDDDDDDDDDDDDDDDDDDDDDDDDDLLLLLLLLLLLLLLLLLLLLLLLUUUUUUUUUUUUUUUUUUUUUUU\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operatio...
369_C. Valera and Elections_29531
The city Valera lives in is going to hold elections to the city Parliament. The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in some way by integers from 1 to n, inclusive. Furthermore, for each r...
n = int(input()) white = [] g = [[] for _ in range(n)] d = [0] * n for i in range(n - 1): x, y, t = list(map(int, input().strip().split())) x -= 1; y -= 1; # print(x,y) if x >= 0 and y >= 0 and x < n and y < n: g[x].append(y) g[y].append(x) if t == 2: white.append((x, y))...
{ "input": [ "5\n1 2 2\n2 3 2\n3 4 2\n4 5 2\n", "5\n1 2 1\n2 3 2\n2 4 1\n4 5 1\n", "5\n1 2 2\n1 3 2\n1 4 2\n1 5 2\n", "4\n1 2 2\n2 3 1\n2 4 2\n", "5\n1 5 1\n5 4 2\n4 3 1\n3 2 2\n", "10\n1 10 2\n10 9 2\n10 8 2\n9 7 2\n8 6 1\n7 5 1\n6 4 1\n5 3 1\n4 2 1\n", "2\n1 2 1\n", "2\n1 2 2\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The city Valera lives in is going to hold elections to the city Parliament. The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path alo...
391_C2. The Tournament_29534
This problem consists of three subproblems: for solving subproblem C1 you will receive 4 points, for solving subproblem C2 you will receive 4 points, and for solving subproblem C3 you will receive 8 points. Manao decided to pursue a fighter's career. He decided to begin with an ongoing tournament. Before Manao joined,...
m = 301000 ns = [0] * m es = [0] * m c = [0] * m b = [0] * m t = [0] * m P = 0 def add(b, k): k = t[k] while k: e = es[k] if b[-1] > e: b[-1] = e b[e] += 1 k = ns[k] def delete(b): for i in range(b[m - 1], m + 1): if b[i]: b[i] -= 1 b[-1] = i ...
{ "input": [ "3 2\n1 1\n1 4\n2 2\n", "2 1\n3 2\n4 0\n", "5 2\n2 10\n2 10\n1 1\n3 1\n3 1\n", "6 4\n1 159371\n1 73897\n1 181693\n4 187816\n3 154227\n1 188146\n", "15 1\n8 79548\n2 109344\n1 40450\n5 65400\n10 93553\n10 83467\n4 122907\n6 55664\n5 111819\n2 56263\n10 188767\n6 15848\n10 110978\n3 137...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This problem consists of three subproblems: for solving subproblem C1 you will receive 4 points, for solving subproblem C2 you will receive 4 points, and for solving subproblem C3 you...
413_C. Jeopardy!_29538
'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2. The finals will have n questions, m of them are auction quest...
n, m = tuple(map(int, str.split(input()))) a = tuple(map(int, str.split(input()))) mi = tuple(map(int, str.split(input()))) ms = sorted(map(lambda i: a[i - 1], mi), reverse=True) points = 0 for i, ai in enumerate(a): if i + 1 not in mi: points += ai for m in ms: if m > points: points += m ...
{ "input": [ "4 1\n1 3 7 5\n3\n", "2 2\n100 200\n1 2\n", "3 2\n10 3 8\n2 3\n", "3 2\n1 2 4\n3 2\n", "3 1\n65 81 6\n2\n", "50 5\n223036 65304 301127 8945 10289 15638 260 246 68 14 23 6 3 2 8 2 1 392212 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 242747 1 1 1 243737 1 1 1 1 1 1 10000000 572890\n18 50 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: 'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coin...
43_E. Race_29542
Today s kilometer long auto race takes place in Berland. The track is represented by a straight line as long as s kilometers. There are n cars taking part in the race, all of them start simultaneously at the very beginning of the track. For every car is known its behavior — the system of segments on each of which the s...
import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') n, s = map(int, input().split()) data = [dict() for _ in range(n)] for i in range(n): k, *a = map(int, input().split()) cur = 0 for j in range(0, 2 * k, 2): data[i][cur] = a[j] ...
{ "input": [ "5 33\n2 1 3 3 10\n1 11 3\n2 5 3 3 6\n2 3 1 10 3\n2 6 3 3 5\n", "2 33\n2 5 1 2 14\n1 3 11\n", "2 33\n2 1 3 10 3\n1 11 3\n", "3 228385\n2 307 733 43 78\n2 252 801 157 169\n3 86 346 133 886 467 173\n", "9 81812\n8 31 410 547 18 22 77 449 5 491 8 10 382 746 21 61 523\n1 452 181\n1 724 11...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Today s kilometer long auto race takes place in Berland. The track is represented by a straight line as long as s kilometers. There are n cars taking part in the race, all of them sta...
462_B. Appleman and Card Game_29546
Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on i...
n,k=map(int,input().split()) s=input() di=dict() for i in s:di[i]=di.get(i,0)+1 ans=0 se=list(set(s)) se.sort(key=lambda x:di[x],reverse=1) for i in se: ans+=min(k,di[i])*min(di[i],k) k-=min(di[i],k) if not k:break print(ans)
{ "input": [ "6 4\nYJSNPI\n", "15 10\nDZFDFZDFDDDDDDF\n", "1 1\nV\n", "100 100\nMQSBDAJABILIBCUEOWGWCEXMUTEYQKAIWGINXVQEOFDUBSVULROQHQRZZAALVQFEFRAAAYUIMGCAFQGIAEFBETRECGSFQJNXHHDN\n", "100 15\nKKTFFUTFCKUIKKKKFIFFKTUKUUKUKKIKKKTIFKTKUCFFKKKIIKKKKKKTFKFKKIRKKKFKUUKIKUUUFFKKKKTUZKITUIKKIKUKKTIK\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on t...
486_A. Calculating Function_29550
For a positive integer n let's define a function f: f(n) = - 1 + 2 - 3 + .. + ( - 1)nn Your task is to calculate f(n) for a given integer n. Input The single line contains the positive integer n (1 ≤ n ≤ 1015). Output Print f(n) in a single line. Examples Input 4 Output 2 Input 5 Output -3 Note f(4)...
if __name__=="__main__": n=int(input()) if(n&1): print(-1*(n+1)//2) else: print(n//2)
{ "input": [ "4\n", "5\n", "29906716\n", "103\n", "2\n", "9999999999999\n", "98979894985999\n", "99999999999999\n", "107\n", "1000000001\n", "106\n", "101\n", "46017661651072\n", "208170109961052\n", "3037000499\n", "108\n", "577935\n", "30524602...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For a positive integer n let's define a function f: f(n) = - 1 + 2 - 3 + .. + ( - 1)nn Your task is to calculate f(n) for a given integer n. Input The single line contains the pos...
534_B. Covered Path_29556
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass. Assuming that at each of the seconds the speed is constant, and...
from collections import Counter,defaultdict,deque import heapq as hq from itertools import count, islice #alph = 'abcdefghijklmnopqrstuvwxyz' #from math import factorial as fact import math import sys input=sys.stdin.readline #print=sys.stdout.write #tt = int(input()) #total=0 #n = int(input()) #n,m,k = [int(x) for x ...
{ "input": [ "10 10\n10 0\n", "5 6\n4 2\n", "1 1\n100 10\n", "71 71\n9 5\n", "1 21\n3 10\n", "22 41\n68 4\n", "34 48\n47 4\n", "1 100\n99 10\n", "99 99\n99 1\n", "13 31\n7 5\n", "90 81\n21 6\n", "11 1\n2 10\n", "77 78\n46 3\n", "1 1\n2 1\n", "1 1\n3 5\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second...
559_B. Equivalent Strings_29560
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: 1. They are equal. 2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2...
def equal(s): if len(s)%2==1 : return s m = len(s)//2 a=equal(s[:m]) b=equal(s[m:]) return a+b if a<b else b+a print("YES" if equal(input())==equal(input()) else 'NO')
{ "input": [ "aabb\nabab\n", "aaba\nabaa\n", "ab\nab\n", "abc\nabc\n", "hagnzomowtledfdotnll\nledfdotnllomowthagnz\n", "baaaaa\nabaaaa\n", "aabbaaaa\naaaaabab\n", "aab\naba\n", "a\na\n", "qgiufelsfhanx\naaaaaaaaaaaaa\n", "snyaydaeobufdg\nsnyaydaeobufdg\n", "aabaababaaba...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: 1. They ...
584_E. Anton and Ira_29564
Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible. More formally, we have two permutations, p and s of numbers from 1 to n. We can swap pi and pj, by pay...
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = {} for i in range(n): c[b[i]] = i b = [] for i in range(n): a[i] = c[a[i]] print(sum(abs(a[i] - i) for i in range(n)) >> 1) while True: for i in range(n): if a[i] < i: for j in range(a[i], i): ...
{ "input": [ "4\n4 2 1 3\n3 2 4 1\n", "2\n1 2\n2 1\n", "10\n9 3 5 1 4 8 6 2 7 10\n3 5 1 4 8 6 2 7 10 9\n", "2\n1 2\n1 2\n", "9\n3 5 4 2 9 1 8 6 7\n7 5 9 8 2 6 1 3 4\n", "8\n2 8 4 7 5 3 6 1\n3 6 5 1 8 4 7 2\n", "10\n7 10 5 8 9 3 4 6 1 2\n1 4 6 5 2 3 10 9 7 8\n", "9\n1 6 4 5 9 8 7 3 2\n2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by pa...
606_E. Freelancer's Dreams_29567
Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired flat in Moscow costs q dollars. Mikhail is determined to follow his dreams and registered at a freelance site. He has suggestions to wo...
from fractions import Fraction def higher(x1, y1, x2, y2): if x1 == 0: if x2 == 0: return def min_days(p, q, pr): ma = max(a for a, b in pr) mb = max(b for a, b in pr) pr.sort(key=lambda t: (t[0], -t[1])) ch = [(0, mb)] for a, b in pr: if a == ch[-1][0]: ...
{ "input": [ "4 1 1\n2 3\n3 2\n2 3\n3 2\n", "3 20 20\n6 2\n1 3\n2 6\n", "2 4 2\n2 4\n5 2\n", "2 5 2\n2 4\n5 2\n", "2 5 4\n2 4\n5 2\n", "2 5 4\n2 2\n4 3\n", "1 3 5\n2 3\n", "2 3 3\n2 4\n5 2\n", "2 4 8\n2 4\n5 2\n", "1 4 6\n2 3\n", "2 2 3\n2 4\n5 2\n", "10 18 25\n4 8\n16 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired...
678_D. Iterated Linear Function_29576
Consider a linear function f(x) = Ax + B. Let's define g(0)(x) = x and g(n)(x) = f(g(n - 1)(x)) for n > 0. For the given integer values A, B, n and x find the value of g(n)(x) modulo 109 + 7. Input The only line contains four integers A, B, n and x (1 ≤ A, B, x ≤ 109, 1 ≤ n ≤ 1018) — the parameters from the problem s...
import sys import bisect from bisect import bisect_left as lb input_=lambda: sys.stdin.readline().strip("\r\n") from math import log from math import gcd from math import atan2,acos from random import randint sa=lambda :input_() sb=lambda:int(input_()) sc=lambda:input_().split() sd=lambda:list(map(int,input_().split())...
{ "input": [ "3 4 1 1\n", "3 4 2 1\n", "3 4 3 1\n", "1 1000000000 10000000000000 12\n", "1 1 352875518515340737 1\n", "1 1000000000 1000000000000 12\n", "1 1171281 1000000000000000000 100\n", "1 100000000 10000000000000000 10000\n", "1 1000000000 1000000000000000 12\n", "1 1000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Consider a linear function f(x) = Ax + B. Let's define g(0)(x) = x and g(n)(x) = f(g(n - 1)(x)) for n > 0. For the given integer values A, B, n and x find the value of g(n)(x) modulo ...
768_A. Oath of the Night's Watch_29586
"Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I ple...
nb_steward = int(input()) strengths = [int(x) for x in input().split()] mins = strengths.count(min(strengths)) maxs = strengths.count(max(strengths)) print(max(0, nb_steward - mins - maxs))
{ "input": [ "3\n1 2 5\n", "2\n1 5\n", "6\n1 1 1 1 1 1\n", "2\n1 1\n", "5\n1 2 3 3 3\n", "3\n2 1 2\n", "4\n5 5 5 5\n", "6\n1 2 5 5 5 5\n", "5\n2 1 1 1 3\n", "4\n1 1 5 5\n", "6\n1 1 3 3 2 2\n", "7\n1 1 1 1 1 1 1\n", "8\n1000000000 1000000000 1000000000 999999999 9999...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: "Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall liv...
791_E. Bear and Company_29590
Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible. Limak has a string s t...
# http://codeforces.com/contest/771/problem/D """ DP-solution. For each state (v, k, x, v_is_last_letter) we trial a step along the v, k and x axes and check that dp[future_state] = min(dp[future_state], dp[state] + cost_of_move) Hence this implicitly reults in the one with least cost. V, K, X are arrays that contain...
{ "input": [ "5\nLIMAK\n", "20\nVKVKVVVKVOVKVQKKKVVK\n", "7\nVVKEVKK\n", "4\nVKVK\n", "5\nBVVKV\n", "75\nZXPZMAKZZZZZZAZXAZAAPOAFAZUZZAZABQZZAZZBZAAAZZFANYAAZZZZAZHZARACAAZAZDPCAVZ\n", "75\nVVVVVKVKVVKKEVVVVVAKVKKZKVVPKKZKAVKVAKVMZKZVUVKKIVVZVVVKVKZVVVVKKVKVZZVOVKV\n", "12\nVKVKVKVKVKV...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is goin...
812_B. Sagheer, the Hausmeister_29594
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides...
n, m = list(map(int, input().split())) m += 2 l = [] do = False for i in range(n): s = input().strip() if s.find('1') != -1 or do: do = True l.append(s) n = len(l) if n == 0: print(0) exit() dp = [] for i in range(n): dp.append([None] * 2) for i in range(n): R = 0 for j in range(m): if l[i][j] == '1': ...
{ "input": [ "3 4\n001000\n000010\n000010\n", "2 2\n0010\n0100\n", "4 3\n01110\n01110\n01110\n01110\n", "8 8\n0111111110\n0111101110\n0111111110\n0111111110\n0111111110\n0110111100\n0101111110\n0110111110\n", "5 44\n0001010010001111111001111111000010100100000010\n0001111001111001101111011111010110...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the un...
858_E. Tests Renumeration_29599
The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website. Unfortunately, the archive with Olympiad's data is a mess. For example, the files with tests are named arbitrary without any logic. Vladimir wants to re...
import random def genTemp(): sl = "" firstTime = True while firstTime or sl in pre or sl in post: sl = "" firstTime = False for i in range(6): sl += chr(random.randint(ord("a"), ord("z"))) return sl n = int(input()) e = 0 pre = set() post = set() for i in range(n):...
{ "input": [ "5\n1 0\n11 1\n111 0\n1111 1\n11111 0\n", "2\n1 0\n2 1\n", "5\n01 0\n2 1\n2extra 0\n3 1\n99 0\n", "3\nt0dfz3 0\n3 0\n1 1\n", "4\n4 1\n3 1\n1 0\n2 0\n", "4\n3 1\nnj94jx 0\n3a5ad1 0\n1 0\n", "5\nynagvf 1\n3 1\nojz4mm 1\ndovec3 0\nnc1jye 0\n", "4\n4i2i2a 0\n4 1\npf618n 1\nlx6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website. Unfortunately, t...
883_K. Road Widening_29602
Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy! The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time o...
import os,io input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n = int(input()) s = [] g = [] forwardBound = [] backwardBound = [] for _ in range(n): sCur,gCur = map(int,input().split()) s.append(sCur) g.append(gCur) forwardBound.append(s[0] + g[0]) for i in range(1,n): if forwardBound[-1] <...
{ "input": [ "4\n1 100\n100 1\n1 100\n100 1\n", "3\n1 1\n100 100\n1 1\n", "3\n4 5\n4 5\n4 10\n", "3\n3 3\n2 0\n1 2\n", "4\n1 3\n2 3\n3 1\n1 0\n", "5\n2 0\n3 0\n3 0\n3 5\n2 4\n", "3\n1 3\n1 3\n2 1\n", "3\n1 3\n2 1\n3 0\n", "9\n4 7\n2 0\n7 3\n9 5\n8 8\n6 5\n6 8\n5 3\n8 7\n", "8\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy! The Mayor thinks that one of the main city streets could be co...
928_C. Dependency management_29607
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity. A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with lengt...
def scan_project(): name, version_str = input().split() return (name, int(version_str)) n = int(input()) projects, depends = [], {} for i in range(n): if i > 0: input() project = scan_project() projects.append(project) depends[project] = [scan_project() for j in range(int(input()))] ro...
{ "input": [ "3\nabc 1\n2\nabc 3\ncba 2\n\nabc 3\n0\n\ncba 2\n0\n", "9\ncodehorses 5\n3\nwebfrmk 6\nmashadb 1\nmashadb 2\n \ncommons 2\n0\n \nmashadb 3\n0\n \nwebfrmk 6\n2\nmashadb 3\ncommons 2\n \nextra 4\n1\nextra 3\n \nextra 3\n0\n \nextra 1\n0\n \nmashadb 1\n1\nextra 3\n \nmashadb 2\n1\nextra 1\n", "4...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries a...
982_E. Billiard_29613
Consider a [billiard table](https://en.wikipedia.org/wiki/Billiard_table) of rectangular size n × m with four pockets. Let's introduce a coordinate system with the origin at the lower left corner (see the picture). <image> There is one ball at the point (x, y) currently. Max comes to the table and strikes the ball. ...
from functools import reduce import math def bezout(a, b): if a == 0: return (0, 1) else: x, y = bezout(b % a, a) return y - (b//a) * x, x def lcm(a, b): return abs(a*b) // math.gcd(a, b) def CRT(a1, m1, a2, m2): g = math.gcd(m1, m2) if (a1-a2) % g != 0: return -1 ...
{ "input": [ "4 4 2 0 1 1\n", "10 10 10 1 -1 0\n", "4 3 2 2 -1 1\n", "584801838 775270595 121061956 644380885 -1 1\n", "603278410 844534002 499505824 32181172 1 -1\n", "320049957 92820858 176731509 15650804 1 1\n", "1000000000 999999999 999999998 999999999 1 1\n", "978089453 426264909 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Consider a [billiard table](https://en.wikipedia.org/wiki/Billiard_table) of rectangular size n × m with four pockets. Let's introduce a coordinate system with the origin at the lower...
p02562 AtCoder Library Practice Contest - MinCostFlow_29626
You are given a grid of N rows and M columns. The square at the i-th row and j-th column will be denoted as (i,j). A nonnegative integer A_{i,j} is written for each square (i,j). You choose some of the squares so that each row and column contains at most K chosen squares. Under this constraint, calculate the maximum v...
import heapq class mcf_graph: def __init__(self, n=0): self._n = n self.g = [[] for _ in range(n)] self.pos = [] def add_edge(self, frm, to, cap, cost): m = len(self.pos) e1 = self._edge(to, cap, cost) e2 = self._edge(frm, 0, -cost) e1.rev = e2 ...
{ "input": [ "3 2\n10 10 1\n10 10 1\n1 1 10", "3 1\n5 3 2\n1 4 8\n7 6 9", "3 1\n10 10 1\n10 10 1\n1 1 10", "3 2\n5 10 1\n10 10 1\n1 1 10", "1 1\n5 3 2\n1 4 8\n7 6 9", "3 2\n5 10 1\n10 6 1\n1 1 10", "3 2\n5 10 1\n10 6 1\n1 1 15", "1 1\n1 3 2\n1 4 8\n7 12 9", "3 1\n18 10 0\n8 10 1\n1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a grid of N rows and M columns. The square at the i-th row and j-th column will be denoted as (i,j). A nonnegative integer A_{i,j} is written for each square (i,j). You...
p02693 AtCoder Beginner Contest 165 - We Love Golf_29630
Takahashi the Jumbo will practice golf. His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive). If he can achieve the objective, print `OK`; if he cannot, print `NG`. Constraints * All values in input are integers. * 1 \leq A \leq B \...
k=int(input()) a,b=map(int,input().split()) print('OK' if b//k > (a-1)//k else 'NG')
{ "input": [ "4\n5 7", "7\n500 600", "1\n11 11", "4\n5 9", "4\n1 1", "4\n2 9", "4\n2 16", "4\n2 23", "4\n0 23", "4\n1 23", "4\n1 20", "4\n1 8", "4\n1 11", "4\n1 15", "4\n1 2", "4\n2 2", "4\n1 7", "7\n500 983", "4\n1 9", "4\n4 16", "4\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Takahashi the Jumbo will practice golf. His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive). If ...
p02822 AtCoder Beginner Contest 149 - Surrounded Nodes_29633
Given is a tree T with N vertices. The i-th edge connects Vertex A_i and B_i (1 \leq A_i,B_i \leq N). Now, each vertex is painted black with probability 1/2 and white with probability 1/2, which is chosen independently from other vertices. Then, let S be the smallest subtree (connected subgraph) of T containing all th...
import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def MI1(): return map(int1, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().spli...
{ "input": [ "4\n1 2\n2 3\n3 4", "4\n1 2\n1 3\n1 4", "7\n4 7\n3 1\n2 6\n5 2\n7 1\n2 7", "3\n1 2\n2 3", "4\n1 2\n2 3\n2 4", "4\n1 2\n2 3\n1 4", "7\n4 7\n3 1\n2 6\n5 4\n7 1\n2 7", "3\n1 2\n1 3", "7\n4 7\n3 1\n3 6\n5 4\n7 1\n2 7", "7\n4 7\n3 1\n2 6\n5 1\n7 1\n2 7", "7\n4 7\n3 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given is a tree T with N vertices. The i-th edge connects Vertex A_i and B_i (1 \leq A_i,B_i \leq N). Now, each vertex is painted black with probability 1/2 and white with probabilit...
p02958 AtCoder Beginner Contest 135 - 0 or 1 Swap_29637
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}. You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it. Print `YES` if you can sort p in ascending order in th...
N=int(input()) p=list(map(int,input().split())) a=0 for i in range(N): if p[i]!=i+1: a+=1 if a>=3: print('NO') else: print('YES')
{ "input": [ "5\n2 4 3 5 1", "7\n1 2 3 4 5 6 7", "5\n5 2 3 4 1", "5\n3 4 3 5 1", "5\n5 0 3 4 1", "5\n3 4 3 0 1", "5\n5 0 3 7 1", "5\n3 4 3 1 1", "5\n5 0 3 4 2", "5\n3 4 1 1 1", "5\n4 0 3 4 2", "5\n3 8 1 1 1", "5\n8 0 3 4 2", "5\n3 8 1 1 2", "5\n11 0 3 4 2", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}. You can perform the following operation at most once: choose integers i and j (1 \leq i < ...
p03240 AtCoder Beginner Contest 112 - Pyramid_29643
In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc. The pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0). Aoki, an explorer, conducted a survey to identify the center ...
N=int(input()) z=[] for i in range(N): z.append(list(map(int,input().split()))) h=max(zz[2] for zz in z) while(1): flag=1 for x in range(101): for y in range(101): for zz in z: if max(h-abs(zz[0]-x)-abs(zz[1]-y),0) != zz[2]: break else: ...
{ "input": [ "2\n0 0 100\n1 1 98", "3\n99 1 191\n100 1 192\n99 0 192", "4\n2 3 5\n2 1 5\n1 2 5\n3 2 5", "2\n-1 0 100\n2 1 98", "3\n99 1 191\n000 1 192\n99 0 192", "2\n-2 0 100\n2 2 98", "3\n99 1 191\n000 1 154\n99 0 192", "3\n99 1 191\n000 1 210\n99 0 192", "2\n-1 -1 101\n2 3 98", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc. The pyramid had center coordinates (C_X, C_Y) and height H...
p03393 AtCoder Grand Contest 022 - Diverse Word_29647
Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order. A word is called diverse if and only if it is a nonempty string of English lowercase...
from string import* from itertools import* b = ascii_lowercase s = input() if len(s) < 26: print(s + sorted(set(b) - set(s))[0]) else: d = -[p*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], s[-2::-1])])][0] - 2 print(-(d < -26) or s[:d] + sorted(set(s[d+1:]) - set(b[:ord(s[d]) - 97]))[0]...
{ "input": [ "zyxwvutsrqponmlkjihgfedcba", "atcoder", "abcdefghijklmnopqrstuvwzyx", "abc", "redocta", "azcdefghijklmnopqrstuvwbyx", "cba", "redoctb", "bca", "bac", "abd", "atcodes", "abcdefghijklmnopqrytuvwzsx", "btcoder", "bad", "acb", "cab", "d...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains a...
p03556 AtCoder Beginner Contest 077 - Around Square_29651
Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer. Constraints * 1 \leq N \leq 10^9 * N is an integer. Input Input is given from Standard Input in the following format: N Output Print the largest square number not exceeding N...
import math;print(round((math.sqrt(int(input()))//1))**2)
{ "input": [ "81", "10", "271828182", "70", "12", "115087508", "135", "167542739", "7", "16", "317532200", "315132601", "2", "262957414", "385753315", "269979070", "193485646", "0", "286256676", "334866881", "582764924", "26", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer. Constraints * 1 \leq N \leq 10^9 * N is an i...
p03711 AtCoder Beginner Contest 062 - Grouping_29655
Based on some criterion, Snuke divided the integers from 1 through 12 into three groups as shown in the figure below. Given two integers x and y (1 ≤ x < y ≤ 12), determine whether they belong to the same group. b4ab979900ed647703389d4349eb84ee.png Constraints * x and y are integers. * 1 ≤ x < y ≤ 12 Input Input i...
L=[1,0,1,2,1,2,1,1,2,1,2,1] a,b=map(int,input().split()) if L[a-1]==L[b-1]: print("Yes") else: print("No")
{ "input": [ "2 4", "1 3", "2 8", "1 10", "1 4", "2 3", "2 10", "4 3", "7 3", "7 6", "7 4", "7 5", "4 5", "1 5", "1 2", "0 2", "-1 2", "2 1", "1 1", "2 0", "2 6", "3 3", "7 1", "5 4", "7 10", "5 5", "2 5", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Based on some criterion, Snuke divided the integers from 1 through 12 into three groups as shown in the figure below. Given two integers x and y (1 ≤ x < y ≤ 12), determine whether th...
p03867 AtCoder Regular Contest 064 - Rotated Palindromes_29659
Takahashi and Aoki are going to together construct a sequence of integers. First, Takahashi will provide a sequence of integers a, satisfying all of the following conditions: * The length of a is N. * Each element in a is an integer between 1 and K, inclusive. * a is a palindrome, that is, reversing the order of elem...
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") n,k = list(map(int, input().split())) M = 10**9+7 ### 約数列挙 def fs(n): s = set() for i in range(1,int(n**0.5)+2): if n%i==0: s.add(i) s.ad...
{ "input": [ "1000000000 1000000000", "1 10", "4 2", "6 3", "1000001000 1000000000", "1 5", "2 2", "11 3", "1000001000 1010000000", "2 4", "16 3", "1000000000 1010000000", "2 0", "4 4", "25 3", "1001000000 1010000000", "6 4", "25 1", "1001000...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Takahashi and Aoki are going to together construct a sequence of integers. First, Takahashi will provide a sequence of integers a, satisfying all of the following conditions: * The ...
p04031 AtCoder Beginner Contest 043 - Be Together_29663
Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them. He may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if a_i=a_j (i≠j), he has to pay the cost separately for transforming each of them (See ...
n=int(input()) A=list(map(int,input().split())) r=4*10**6 for i in range(min(A),max(A)+1): d=0 for j in range(n): d+=(A[j]-i)**2 r=min(r,d) print(r)
{ "input": [ "2\n4 8", "3\n1 1 3", "3\n4 2 5", "4\n-100 -100 -100 -100", "2\n5 8", "3\n1 1 6", "3\n4 3 5", "4\n-100 -45 -100 -100", "2\n5 11", "3\n1 1 8", "3\n3 3 5", "3\n0 1 8", "3\n3 0 5", "2\n10 6", "3\n0 0 8", "3\n3 0 10", "3\n0 0 14", "3\n3 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them. He may transform each integer at most once. Transforming an integer x into ...
p00115 Starship UAZ Advance_29667
Stellar history 2005.11.5. You are about to engage an enemy spacecraft as the captain of the UAZ Advance spacecraft. Fortunately, the enemy spaceship is still unnoticed. In addition, the space coordinates of the enemy are already known, and the "feather cannon" that emits a powerful straight beam is ready to launch. Af...
# -*- coding: utf-8 -*- import sys import os import math import random # refs # https://shikousakugo.wordpress.com/2012/06/27/ray-intersection-2/ def det(a, b, c): return + a[0] * b[1] * c[2] \ + a[2] * b[0] * c[1] \ + a[1] * b[2] * c[0] \ - a[2] * b[1] * c[0] \ - a[1]...
{ "input": [ "-10 6 6\n10 6 6\n0 10 0\n0 10 10\n0 0 10", "-10 0 0\n10 0 0\n0 10 0\n0 10 10\n0 0 10", "-10 6 6\n10 2 6\n0 10 0\n0 10 10\n0 0 10", "-10 0 0\n10 0 0\n0 10 0\n0 10 10\n0 1 10", "-10 6 6\n20 2 6\n0 10 0\n0 10 10\n0 0 10", "-10 0 0\n10 0 0\n0 10 0\n0 0 10\n0 1 10", "-10 6 6\n20 2...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Stellar history 2005.11.5. You are about to engage an enemy spacecraft as the captain of the UAZ Advance spacecraft. Fortunately, the enemy spaceship is still unnoticed. In addition, ...
p00248 Magic Square_29671
A witch named Marie lived deep in a remote forest. Since she is a witch, she magically covers everything she needs to live, such as food, water, and fuel. Her magic is activated by drawing a magic circle using some magical stones and strings. This magic circle is drawn by placing stones and tying several pairs of ston...
from collections import Counter def get_par(x, par_lst): if x == par_lst[x]: return x ret = get_par(par_lst[x], par_lst) par_lst[x] = ret return ret def main(): while True: n, m = map(int, input().split()) if n == 0: break par_lst = [i for i in range(n)] counter = [0] * n flag =...
{ "input": [ "5 4\n1 2\n2 3\n3 4\n4 5\n4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n5 0\n0 0", "5 4\n1 2\n2 3\n3 4\n4 5\n4 6\n1 2\n1 3\n1 4\n2 4\n2 4\n3 4\n5 0\n0 0", "5 4\n1 2\n2 3\n3 4\n5 5\n4 6\n1 2\n1 3\n1 4\n2 4\n2 4\n3 4\n5 0\n0 0", "5 4\n2 2\n2 3\n3 4\n5 5\n4 6\n1 2\n1 3\n1 4\n1 4\n1 4\n3 4\n0 0\n0 0", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A witch named Marie lived deep in a remote forest. Since she is a witch, she magically covers everything she needs to live, such as food, water, and fuel. Her magic is activated by d...
p00429 String_29674
Given a string consisting of only numbers from 0 to 9, consider the operation of creating a new string from that string according to the following rules. Read the given string one character at a time from the left end. Go, if the same number a continues r, write the number r and the number a in this order without separ...
def change(x) : i = 0 ans = [] while True : if i >= len(x) : break cnt = 1 j = 1 while True : if i+j >= len(x) or x[i+j] != x[i] : ans = ans + list(str(cnt)) ans.append(x[i]) i += cnt break ...
{ "input": [ "5\n11\n5\n11\n0", "5\n11\n5\n21\n0", "5\n18\n5\n21\n0", "5\n18\n5\n1\n0", "5\n18\n6\n1\n0", "5\n28\n6\n1\n0", "5\n28\n11\n1\n0", "5\n28\n3\n1\n0", "5\n43\n3\n1\n0", "5\n43\n3\n0\n0", "5\n11\n5\n15\n0", "5\n11\n5\n17\n0", "5\n0\n5\n21\n0", "5\n6\n5\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given a string consisting of only numbers from 0 to 9, consider the operation of creating a new string from that string according to the following rules. Read the given string one cha...
p00769 Hierarchical Democracy_29680
Hierarchical Democracy The presidential election in Republic of Democratia is carried out through multiple stages as follows. 1. There are exactly two presidential candidates. 2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a ...
N = int(input()) def rec (): global now,l if s[now].isdigit(): res = 0 while (now < l and s[now].isdigit()): res = ((10 * res) + int(s[now])) now += 1 return (res+1)//2 else: g = [] while(now < l and s[now] == '['): now += 1 g.append(rec()) now += 1 g.sort() return sum(g[:(len(g)+1)//2])...
{ "input": [ "6\n[[123][4567][89]]\n[[5][3][7][3][9]]\n[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]\n[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]\n[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5]...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Hierarchical Democracy The presidential election in Republic of Democratia is carried out through multiple stages as follows. 1. There are exactly two presidential candidates. 2. At...
p01303 Petting Cats_29688
Many cats live on the campus of a school. Natsume's daily routine is to pet those cats. However, the cats may be capricious and go for a walk off campus. The campus site is a rectangle with each side parallel to the x-axis or y-axis and is surrounded by a fence except for the gate, but cats can freely enter and exit t...
for _ in range(int(input())): x, y, w, h = map(int,input().split()) c = 0 for _ in range(int(input())): a, b = map(int,input().split()) if x <= a and a <= x + w and y <= b and b <= y + h:c += 1 print(c)
{ "input": [ "2\n1 3 20 10\n4\n21 13\n1 15\n10 10\n25 10\n1 3 20 10\n4\n21 13\n1 15\n10 10\n25 10", "2\n1 3 20 10\n4\n21 13\n1 15\n10 10\n41 10\n1 3 20 10\n4\n21 13\n1 15\n10 10\n25 10", "2\n1 3 6 10\n4\n21 13\n1 15\n10 10\n25 10\n1 3 20 10\n4\n21 13\n1 15\n10 10\n25 10", "2\n1 3 20 10\n4\n21 13\n1 15...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Many cats live on the campus of a school. Natsume's daily routine is to pet those cats. However, the cats may be capricious and go for a walk off campus. The campus site is a rectang...
p01784 Parentheses_29694
Example Input 3 ()(()(( ))()()(() )())(()) Output Yes
def solve(): N = int(input()) T0 = [] T1 = [] su = 0 for i in range(N): s = input() a = 0 for c in s: if c == '(': a += 1 elif a > 0: a -= 1 b = 0 for c in reversed(s): if c == ')': ...
{ "input": [ "3\n()(()((\n))()()(()\n)())(())", "3\n()(()((\n))()(((()\n)())(())", "3\n()(()((\n))()(((()\n))(())()", "3\n()(()((\n))()(((()\n)())((*)", "3\n()(()((\n))()(((()\n)())((*(", "3\n()(()((\n))()(((()\n)()(((*(", "3\n()(()((\n)(((()())\n)()(((*(", "3\n()(()((\n)(((')())\n)()(...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Example Input 3 ()(()(( ))()()(() )())(()) Output Yes ### Input: 3 ()(()(( ))()()(() )())(()) ### Output: Yes ### Input: 3 ()(()(( ))()(((() )())(()) ### Output: No ### Code:...
p01919 Country in Distortion_29696
D: Country In Distortion- story Alice was completely bored. This is because the White Rabbit, who is always playing with him, is out to Trump Castle. (Ah, I wish I had gone out with him in this case.) Alice thought. However, this is a country of distortion. If you go out so easily, you will get very tired. What does ...
import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline inf = float('inf') n,m = map(int,input().split()) pathT = [{} for _ in range(n)] for i in range(m): x,y,t = map(int,input().split()) x -= 1 y -= 1 pathT[x][y] = t pathT[y][x] = t import heapq def Dijkstra(edge,start): d = [[fl...
{ "input": [ "4 4\n1 2 3\n2 3 1\n2 4 5\n3 4 2\n5\n4 5 6", "4 4\n1 3 3\n2 3 1\n2 4 5\n3 4 2\n5\n4 5 6", "4 4\n1 2 3\n2 3 1\n2 4 5\n3 4 2\n5\n4 5 7", "4 4\n1 3 4\n2 3 1\n2 4 5\n3 4 2\n5\n4 5 6", "4 4\n1 3 4\n2 3 0\n2 4 5\n3 4 2\n5\n4 5 6", "4 4\n1 3 4\n2 3 1\n2 2 10\n3 4 1\n5\n4 5 6", "4 4\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: D: Country In Distortion- story Alice was completely bored. This is because the White Rabbit, who is always playing with him, is out to Trump Castle. (Ah, I wish I had gone out with...
p02057 MOD Rush_29699
F: MOD Rush problem Given a positive integer sequence A of length N and a positive integer sequence B of length M. For all (i, j) (1 \ leq i \ leq N, 1 \ leq j \ leq M), find the remainder of A_i divided by B_j and output the sum of them. Input format N M A_1 A_2 ... A_N B_1 B_2 ... B_M Constraint * 1 \ leq N,...
from collections import Counter from itertools import accumulate N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) K = max(A) table = [0]*(K+1) table[0] = M for b, v in Counter(B).items(): for j in range(b, K+1, b): table[j] -= b*v for j in range(...
{ "input": [ "3 3\n5 1 6\n2 3 4", "3 3\n5 2 6\n2 3 4", "3 3\n4 2 6\n2 3 4", "2 3\n4 2 6\n2 3 4", "2 3\n4 2 6\n2 2 4", "2 4\n4 2 6\n2 2 4", "3 3\n5 1 7\n2 3 4", "3 0\n5 4 6\n2 3 4", "2 4\n8 3 6\n2 3 4", "2 4\n4 2 6\n2 3 5", "2 2\n2 2 6\n2 0 6", "2 2\n5 2 5\n2 0 6", "...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: F: MOD Rush problem Given a positive integer sequence A of length N and a positive integer sequence B of length M. For all (i, j) (1 \ leq i \ leq N, 1 \ leq j \ leq M), find the r...
p02199 Run_29702
Run, Twins E869120 You started running from home to school at a speed of $ P $ meters per minute. square1001 noticed E869120's forgotten thing $ A $ minutes after E869120 left home and chased at $ Q $ meters per minute. Then E869120 noticed something left behind $ B $ minutes after E869120 left home and turned back ...
a,b = map(int,input().split()) p,q,r = map(int,input().split()) print((b*p+b*r+a*q)/(r+q))
{ "input": [ "14 86\n9 1 20", "14 86\n15 1 20", "14 86\n4 1 20", "11 86\n4 1 20", "20 86\n4 1 20", "20 86\n4 1 37", "20 86\n4 0 37", "20 86\n4 0 39", "20 73\n4 0 39", "20 73\n5 0 39", "20 73\n5 0 47", "20 73\n5 0 27", "20 73\n10 0 27", "20 124\n10 0 27", "20...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Run, Twins E869120 You started running from home to school at a speed of $ P $ meters per minute. square1001 noticed E869120's forgotten thing $ A $ minutes after E869120 left home ...
p02353 RSQ and RUQ_29705
Write a program which manipulates a sequence $A$ = {$a_0, a_1, ..., a_{n-1}$} with the following operations: * $update(s, t, x)$: change $a_s, a_{s+1}, ..., a_t$ to $x$. * $getSum(s, t)$: print the sum of $a_s, a_{s+1}, ..., a_t$. Note that the initial values of $a_i ( i = 0, 1, ..., n-1 )$ are 0. Constraints * $...
#!/usr/bin/env python3 import sys sys.setrecursionlimit(10**6) input = sys.stdin.buffer.readline INF = 10 ** 9 + 1 # sys.maxsize # float("inf") MOD = 10 ** 9 + 7 def debug(*x): print(*x, file=sys.stderr) def set_depth(depth): global DEPTH, SEGTREE_SIZE, NONLEAF_SIZE DEPTH = depth SEGTREE_SIZE = 1 <...
{ "input": [ "6 7\n0 1 3 1\n0 2 4 -2\n1 0 5\n1 0 1\n0 3 5 3\n1 3 4\n1 0 5", "6 7\n0 1 3 1\n0 2 4 -2\n1 0 4\n1 0 1\n0 3 5 3\n1 3 4\n1 0 5", "6 7\n0 1 3 1\n0 2 2 -2\n1 0 4\n1 0 1\n0 3 5 3\n1 3 4\n1 0 5", "6 7\n0 1 3 1\n0 2 4 -2\n1 0 5\n1 0 1\n0 3 5 0\n1 3 4\n1 0 5", "6 7\n0 2 3 1\n0 2 4 -2\n1 0 4\n1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Write a program which manipulates a sequence $A$ = {$a_0, a_1, ..., a_{n-1}$} with the following operations: * $update(s, t, x)$: change $a_s, a_{s+1}, ..., a_t$ to $x$. * $getSum(s,...
1006_D. Two Strings Swaps_29715
You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and sw...
n = int(input()) a = input() b = input() l = 0; r = n-1; ans = 0 while l < r: x1 = a[l]; x2 = a[r] y1 = b[l]; y2 = b[r] if (x1 == y1 and x2 == y2) or (x1 == y2 and x2 == y1) or (x1 == x2 and y1 == y2): ans += 0 elif (x1 == y1 or x2 == y2) or (x1 == y2 or x2 == y1) or (y1 == y2): ans...
{ "input": [ "7\nabacaba\nbacabaa\n", "5\nzcabd\ndbacz\n", "1\na\nb\n", "5\nahmad\nyogaa\n", "5\nahmad\nyngaa\n", "7\nabacaba\naabacab\n", "5\nzcabd\nebacz\n", "7\nabacaaa\naabacab\n", "5\nzcabd\neaacz\n", "5\nzcabd\nzcabd\n", "5\nahmac\nyngaa\n", "5\nahamc\nyngaa\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to d...
102_C. Homework_29719
Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to l...
import itertools import math import time def timer(f): def tmp(*args, **kwargs): t = time.time() res = f(*args, **kwargs) print("Время выполнения функции: %f" % (time.time()-t)) return res return tmp #n = int(input()) s = input() n = int(input()) array = [[0, chr(i+ord('a'))...
{ "input": [ "abcdefgh\n10\n", "aaaaa\n4\n", "abacaba\n4\n", "halasouqgfxfcrwhqgllaqiphaxekljz\n87\n", "fntrmjfquczybyjllywsqwllsxdmqynmyfcqhakftitvvfbxtqktbfsvvvanjbkqubyxu\n63\n", "abcdefghijklmnopqrsttsrqponmlkjihgfedcba\n0\n", "aaaaaaaaaaaaaaaaaaaaaeeeeeeeeeeeeeeeeeeee\n20\n", "abc...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the tas...
1051_C. Vasya and Multisets_29723
Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the q...
n = int(input()) s = list(map(int, input().split())) d = {} mt = False mti = -1 for i in range(len(s)): if s[i] not in d: d[s[i]] = 1 else: d[s[i]] += 1 if d[s[i]] > 2: mt = True mti = i good = [] for i in d.keys(): if d[i] == 1: good.append(i) if len(...
{ "input": [ "4\n3 5 7 1\n", "3\n3 5 1\n", "8\n3 5 3 3 2 1 4 3\n", "100\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contai...
1073_D. Berland Fair_29727
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies. Polycarp has de...
from sys import stdin input = stdin.readline n, t = map(int, input().split()) lst = list(map(int, input().split())) s = sum(lst) x = min(lst) candy = n * (t // s) t -= s * (t // s) while t >= x: s, c = 0, 0 for i in lst: if i <= t - s: c += 1 s += i candy += c * (t // s) ...
{ "input": [ "3 38\n5 2 5\n", "5 21\n2 4 100 2 6\n", "2 2\n1 2\n", "11 618\n33 17 17 39 26 2 11 1 33 22 44\n", "7 20\n23 6 10 12 1 1 24\n", "15 32767\n1 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192\n", "1 1\n2\n", "5 16\n18 9 1 1 20\n", "10 25\n2 2 5 1 5 4 5 5 5 1\n", "1 53...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1....
1095_D. Circular Dance_29731
There are n kids, numbered from 1 to n, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as p_1, p_2, ..., p_n (all these numbers are from 1 to n and are distinct, so p is a permutation). Let the next kid for a kid p_i be kid p_{i + 1} if i < n and p_1 otherwise. After the da...
n = int(input()) p = [(None, None)] for _ in range(n): p.append(list(map(int, input().split()))) if n==3: print("1 2 3") else: for idx, (a, b) in enumerate(p[1:], 1): if a in p[b]: p[idx] = [b] elif b in p[a]: p[idx] = [a] return_val = [p[1][0]] n-=1 while n: return_val.append(p[return_val[...
{ "input": [ "5\n3 5\n1 4\n2 4\n1 5\n2 3\n", "3\n2 3\n3 1\n1 2\n", "3\n3 2\n1 3\n1 2\n", "3\n2 3\n3 1\n2 1\n", "4\n2 3\n3 4\n4 1\n1 2\n", "6\n2 4\n5 3\n1 6\n2 5\n3 6\n4 1\n", "8\n8 5\n8 1\n6 4\n2 6\n7 3\n2 1\n4 3\n7 5\n", "5\n4 2\n5 3\n1 5\n2 3\n4 1\n", "7\n2 3\n6 3\n7 6\n5 1\n1 2\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n kids, numbered from 1 to n, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as p_1, p_2, ..., p_n (all these numbers are from ...