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
1443_B. Saving the City_9915
Bertown is a city with n buildings in a straight line. The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length n, where the i-th character is "1" if there is a mine under the building number i and "0" otherwise. Bertown's best sapper knows how to activate...
# ========== //\\ //|| ||====//|| # || // \\ || || // || # || //====\\ || || // || # || // \\ || || // || # ========== // \\ ======== ||//====|| # code def solve(): a,b = map(int, input().split()) s = str...
{ "input": [ "2\n1 1\n01000010\n5 1\n01101110\n", "2\n1 1\n01000110\n5 1\n01101110\n", "2\n2 1\n01000110\n5 1\n01101110\n", "2\n2 1\n01000110\n5 1\n01101101\n", "2\n2 1\n11000111\n5 1\n01101001\n", "2\n1 1\n01000010\n10 1\n01101110\n", "2\n1 1\n01000110\n5 2\n01101110\n", "2\n1 1\n1100...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bertown is a city with n buildings in a straight line. The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length n, where...
1468_K. The Robot_9919
There is a robot on a checkered field that is endless in all directions. Initially, the robot is located in the cell with coordinates (0, 0). He will execute commands which are described by a string of capital Latin letters 'L', 'R', 'D', 'U'. When a command is executed, the robot simply moves in the corresponding dire...
#region Header #!/usr/bin/env python3 # from typing import * import sys import io import math import collections import decimal import itertools import bisect import heapq def input(): return sys.stdin.readline()[:-1] # sys.setrecursionlimit(1000000) #endregion # _INPUT = """# paste here... # """ # sys.stdin = ...
{ "input": [ "4\nL\nRUUDL\nLLUU\nDDDUUUUU\n", "4\nL\nLUUDR\nLLUU\nDDDUUUUU\n", "4\nL\nUURDL\nLLUU\nDDDUUUUU\n", "4\nL\nUURDL\nLLUU\nUUUUUDDD\n", "4\nL\nLUUDR\nLLUU\nUUUUUDDD\n", "4\nL\nRUUDL\nLLUU\nUUUUUDDD\n", "4\nL\nUULDR\nLLUU\nUDUUUDDU\n", "4\nL\nRDUUL\nUULL\nUUUUUDDD\n", "4\nL...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a robot on a checkered field that is endless in all directions. Initially, the robot is located in the cell with coordinates (0, 0). He will execute commands which are descri...
1493_E. Enormous XOR_9923
You are given two integers l and r in binary representation. Let g(x, y) be equal to the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of all integers from x to y inclusive (that is x ⊕ (x+1) ⊕ ... ⊕ (y-1) ⊕ y). Let's define f(l, r) as the maximum of all values of g(x, y) satisfying l ≤ x ≤ y ≤ r. ...
i=int;p=input N=i(p());L=p();R=p() l=i(L,2);r=i(R,2);a=R if l-l%2<r:a=R[:-1]+'1' if i(L[-1])and l+1==r:a=R if L==R:a=L if L[0]<R[0]:a='1'*N print(a)
{ "input": [ "7\n0010011\n1111010\n", "4\n1010\n1101\n", "1\n0\n0\n", "2\n00\n10\n", "29\n00001010001110100010011100110\n11001110001010000110111110101\n", "168\n0111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
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 l and r in binary representation. Let g(x, y) be equal to the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of all integers from x to y...
1516_B. AGAGA XOOORRR_9927
Baby Ehab is known for his love for a certain operation. He has an array a of length n, and he decided to keep doing the following operation on it: * he picks 2 adjacent elements; he then removes them and places a single integer in their place: their [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR...
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) r = 0 for j in range(len(a)): r ^= a[j] if not r: print("Yes") else: t = 0 i = 0 s = 0 while i < len(a) and t < 2: s ^= a[i] if s == r: ...
{ "input": [ "2\n3\n0 2 2\n4\n2 3 1 10\n", "1\n5\n3 3 3 2 2\n", "1\n4\n3 3 3 0\n", "1\n4\n0 3 3 3\n", "1\n5\n1 8 7 7 1\n", "1\n3\n0 0 0\n", "15\n2\n660632533 660632533\n2\n774306959 774306959\n2\n971739496 784083784\n2\n775353256 672614891\n2\n403290217 403290217\n2\n651261320 651261320\n2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Baby Ehab is known for his love for a certain operation. He has an array a of length n, and he decided to keep doing the following operation on it: * he picks 2 adjacent elements;...
1543_C. Need for Pink Slips_9931
After defeating a Blacklist Rival, you get a chance to draw 1 reward slip out of x hidden valid slips. Initially, x=3 and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival's Car. Initially, the probability of drawing these in a random guess are c, m, and p, respectively. There...
a = 0 def dp(c, m, p, v, h, ans): global a a += h*p*ans if (c > 1e-6): if (m < 1e-6): dp(c-min(c, v), 0, p+min(c, v), v, h+1, ans*c) else: dp(c-min(c, v), m+min(c, v)/2.0, p+min(c, v)/2.0, v, h+1, ans*c) if (m > 1e-6): if (c < 1e-6): dp(0, m-...
{ "input": [ "4\n0.2 0.2 0.6 0.2\n0.4 0.2 0.4 0.8\n0.4998 0.4998 0.0004 0.1666\n0.3125 0.6561 0.0314 0.2048\n", "10\n0.1816 0.7813 0.0371 0.6860\n0.4789 0.1463 0.3748 0.6186\n0.1431 0.3218 0.5351 0.6905\n0.7071 0.0774 0.2155 0.1087\n0.7082 0.0144 0.2774 0.7364\n0.0824 0.4306 0.4870 0.5198\n0.2039 0.7827 0.013...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After defeating a Blacklist Rival, you get a chance to draw 1 reward slip out of x hidden valid slips. Initially, x=3 and these hidden valid slips are Cash Slip, Impound Strike Releas...
171_D. Broken checker_9935
"This problem is rubbish! There is not statement, and there are only 5 test cases. The problemsetter took liberties with this problem!" — people complained in the comments to one round on Codeforces. And even more... No, wait, the checker for the problem was alright, that's a mercy. Input The only line of the input c...
vals = [2, 3, 1, 2, 1] i = int(input()) print(vals[i-1])
{ "input": [ "5\n", "4\n", "2\n", "3\n", "1\n" ], "output": [ "1", "2", "3", "1", "2" ] }
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: "This problem is rubbish! There is not statement, and there are only 5 test cases. The problemsetter took liberties with this problem!" — people complained in the comments to one roun...
191_B. Demonstration_9939
In the capital city of Berland, Bertown, demonstrations are against the recent election of the King of Berland. Berland opposition, led by Mr. Ovalny, believes that the elections were not fair enough and wants to organize a demonstration at one of the squares. Bertown has n squares, numbered from 1 to n, they are numb...
from sys import stdin, stdout n, k = map(int, stdin.readline().split()) value = int(stdin.readline()) prices = list(map(int, stdin.readline().split())) used = {} challengers = prices[:-1] for i in range(n - 1): challengers[i] = (challengers[i], i) challengers.sort(reverse = True) ind = challengers[k - 1][1] cn...
{ "input": [ "5 4\n1000000000000000\n5 4 3 2 1\n", "5 2\n8\n3 2 4 1 5\n", "5 2\n8\n2 4 5 3 1\n", "10 1\n297\n215 281 102 280 243 225 294 296 7383 2623\n", "10 4\n1834\n11 44 49 420 93 653 306 596 535 842\n", "10 2\n1523\n661 230 363 300 28 72 676 741 837 984\n", "100 10\n9199\n89 409 428 4...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the capital city of Berland, Bertown, demonstrations are against the recent election of the King of Berland. Berland opposition, led by Mr. Ovalny, believes that the elections were...
215_B. Olympic Medal_9943
The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of r1 cm, inner radius of r2 cm, (0 < r2 < r1) made of metal with density p1 g/cm3. The second part is an inner disk with radius r2 cm, it is made of metal with density p2 g/cm3. The disk is nested ...
from math import sqrt f = lambda: list(map(int, input().split())) r1 = max(f()[1:]) p1s = f()[1:] p2 = min(f()[1:]) a, b = f() print(str(sqrt(max(b * p1 * r1**2 / float(a * p2 + b * p1) for p1 in p1s))))
{ "input": [ "4 2 3 6 4\n2 1 2\n3 10 6 8\n2 1\n", "3 1 2 3\n1 2\n3 3 2 1\n1 2\n", "2 1 2\n3 3 1 2\n2 2 3\n306 406\n", "2 3 2\n3 2 3 1\n2 2 1\n733 883\n", "1 5000\n1 5000\n1 1\n1 5000\n", "1 5\n1 3\n1 7\n515 892\n", "3 1 2 3\n1 2\n3 3 2 1\n52 56\n", "2 1 2\n3 2 3 1\n2 1 3\n452 219\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of r1 cm, inner radius of r2 cm, (0 < r2 < r1) made of metal ...
239_B. Easy Tape Programming_9947
There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. * Current character pointer (CP); *...
n, q = map(int, input().split()) s = input() for _ in range(q): l, r = map(int, input().split()) t = list(s[l-1:r]) p, d = 0, 1 res = [0] * 10 while 0 <= p < len(t): if '0' <= t[p] <= '9': k = int(t[p]) res[k] += 1 if k > 0: t[p] = str(k-1)...
{ "input": [ "7 4\n1&gt;3&gt;22&lt;\n1 3\n4 7\n7 7\n1 7\n", "21 33\n007317842806111438>67\n2 11\n3 21\n3 12\n5 8\n14 14\n10 14\n9 17\n7 17\n1 12\n12 18\n3 10\n2 20\n5 5\n10 13\n14 20\n2 19\n1 13\n6 11\n6 9\n8 13\n16 16\n1 15\n18 20\n12 12\n7 20\n3 11\n13 21\n3 11\n12 13\n8 15\n13 17\n5 5\n2 16\n", "5 1\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A...
335_C. More Reclamation_9957
In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land. The river area can be represented by a grid with r rows and exactly two columns — each cell represents a rectangular area. The rows are numbered ...
r,n = [int(x) for x in input().split()] cells = [[int(x) for x in input().split()] for i in range(n)] cells.sort() #print(cells) out = False res = {True:"WIN",False:"LOSE"} if len(cells) == 0: print(res[r%2 == 1]) else: out = False #print(cells[0][0] > 1) #print(cells[-1][0] < r) for i in range(1,n): ...
{ "input": [ "1 1\n1 2\n", "12 2\n4 1\n8 1\n", "3 1\n1 1\n", "10 4\n8 2\n2 2\n5 2\n7 2\n", "30 4\n11 2\n8 2\n21 1\n23 1\n", "18 4\n6 2\n10 2\n13 1\n12 1\n", "97 15\n63 2\n35 2\n74 2\n20 2\n60 1\n31 2\n68 1\n21 2\n42 1\n29 1\n44 2\n79 1\n73 2\n53 1\n77 1\n", "99 14\n17 2\n82 2\n85 2\n52...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land. The rive...
358_C. Dima and Containers_9961
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck. Inna wants her present to show Dima how great a programmer he is. For that, she is going to g...
n = int(input()) r = ['popStack', 'popQueue', 'popFront' ] r2 = ['pushStack', 'pushQueue', 'pushFront' ] _ = 0 while _ < n: x = [] i = 0 while _ < n: z = int(input()) _ += 1 if z == 0: break x.append([z, i]) i+=1 if len(x) <= 3: if len(x) ...
{ "input": [ "10\n0\n1\n0\n1\n2\n0\n1\n2\n3\n0\n", "4\n1\n2\n3\n0\n", "55\n73792\n39309\n73808\n47389\n34803\n87947\n32460\n14649\n70151\n35816\n8272\n78886\n71345\n61907\n16977\n85362\n0\n43792\n8118\n83254\n89459\n32230\n87068\n82617\n94847\n83528\n37629\n31438\n97413\n62260\n13651\n47564\n43543\n61292\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's prese...
381_B. Sereja and Stairs_9965
Sereja loves integer sequences very much. He especially likes stairs. Sequence a1, a2, ..., a|a| (|a| is the length of the sequence) is stairs if there is such index i (1 ≤ i ≤ |a|), that the following condition is met: a1 < a2 < ... < ai - 1 < ai > ai + 1 > ... > a|a| - 1 > a|a|. For example, sequences [1, 2, 3, 2...
from sys import stdin,stdout from math import factorial as f inp=stdin.readline op=stdout.write n=int(inp()) a=list(map(int,inp().split())) a.sort() # op(str(a)) if(len(set(a))==n): op(str(len(a))+"\n") a=[str(i) for i in a] op(' '.join(a[::-1])+"\n") else: temp=float('inf') i=0 rev=[] while...
{ "input": [ "6\n1 1 2 2 3 3\n", "5\n1 2 3 4 5\n", "2\n1 2\n", "47\n3 4 5 3 1 4 4 3 4 6 1 5 1 3 5 3 6 5 1 4 3 2 6 5 3 1 4 6 4 6 2 1 1 1 4 3 6 1 6 6 3 5 1 4 6 4 4\n", "17\n15 29 28 23 20 12 9 30 4 13 1 25 11 20 6 23 10\n", "13\n8 23 26 8 15 13 35 36 28 8 4 33 6\n", "31\n189 73 300 133 414 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sereja loves integer sequences very much. He especially likes stairs. Sequence a1, a2, ..., a|a| (|a| is the length of the sequence) is stairs if there is such index i (1 ≤ i ≤ |a|),...
402_A. Nuts_9969
You have a nuts and lots of boxes. The boxes have a wonderful feature: if you put x (x ≥ 0) divisors (the spacial bars that can divide a box) to it, you get a box, divided into x + 1 sections. You are minimalist. Therefore, on the one hand, you are against dividing some box into more than k sections. On the other hand...
from math import ceil k, a, b, v = map(int, input().split()) res = 0 c = ceil(a / v) while (b >= k - 1) and (c > 0): res += 1 c -= k b -= k - 1 if c > 0: res += 1 c -= b + 1 if c > 0: res += c print(res)
{ "input": [ "100 100 1 1000\n", "3 10 1 3\n", "3 10 3 3\n", "6 978 10 5\n", "10 697 1 34\n", "60 1000 2 2\n", "510 996 830 417\n", "10 849 3 28\n", "2 4 1000 1\n", "307 999 10 7\n", "974 999 3 4\n", "7 897 49 42\n", "8 995 3 28\n", "872 997 1 1\n", "10 267 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have a nuts and lots of boxes. The boxes have a wonderful feature: if you put x (x ≥ 0) divisors (the spacial bars that can divide a box) to it, you get a box, divided into x + 1 ...
42_B. Game of chess unfinished_9973
Once Volodya was at the museum and saw a regular chessboard as a museum piece. And there were only four chess pieces on it: two white rooks, a white king and a black king. "Aha, blacks certainly didn't win!", — Volodya said and was right for sure. And your task is to say whether whites had won or not. Pieces on the ch...
import sys b = 'sabcdefgh' a1,a2,a3,a4 = map(str,input().split()) x1 = b.index(a1[0])-1 y1 = int(a1[1]) -1 x2 = b.index(a2[0])-1 y2 = int(a2[1]) -1 x3 = b.index(a3[0])-1 y3 = int(a3[1]) -1 x4 = b.index(a4[0])-1 y4 = int(a4[1]) -1 c = [] for i in range(8): c.append([0]*8) pr = 0 pr1 = 0 pr4 = 0 pr3 = 0 for i in rang...
{ "input": [ "a2 b1 a3 a1\n", "a6 b4 c8 a8\n", "a6 c4 b6 b8\n", "e8 e7 d8 g8\n", "b3 a8 d3 a3\n", "b2 c2 b3 b1\n", "c6 b2 g6 b4\n", "a5 c5 c3 a1\n", "d4 e5 b7 a5\n", "e8 e7 f8 h8\n", "h7 h8 c7 a8\n", "a5 b5 g2 a8\n", "d8 d7 h8 f8\n", "a1 a2 b4 c2\n", "g8 h5 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once Volodya was at the museum and saw a regular chessboard as a museum piece. And there were only four chess pieces on it: two white rooks, a white king and a black king. "Aha, black...
474_D. Flowers_9979
We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red. But, for a dinner to be tasty...
#a, b, c, d = [int(x) for x in stdin.readline().split()] #a, b, c, d = map( int, stdin.readline().split() ) from sys import stdin, stdout modconst=1000000007 n,k=map(int, stdin.readline().split()) f=[0]*100001 ss=[0]*100001 f[0]=0 for i in range(1,k): f[i]=1 f[k]=2 for i in range(k+1,100001): f[i]=(f[i-1]+f[i...
{ "input": [ "3 2\n1 3\n2 3\n4 4\n", "1 2\n64329 79425\n", "1 1\n1 3\n", "1 1\n2 3\n", "3 1\n1 3\n2 3\n4 4\n", "1 2\n2 3\n", "3 1\n1 3\n1 3\n4 4\n", "1 1\n3 3\n", "1 2\n2 4\n", "3 2\n1 3\n1 3\n4 4\n", "1 2\n3 6\n", "1 1\n1 1\n", "3 1\n1 3\n1 3\n1 4\n", "3 2\n1 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Theref...
498_C. Array and Operations_9982
You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i1, j1), (i2, j2), ..., (im, jm). Each good pair (ik, jk) meets the following conditions: ik + jk is an odd number and 1 ≤ ik < jk ≤ n. In one operation you can perform a sequence of actions: * ...
# by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO,IOBase def matching(n,m,path): match1 = [-1]*n match2 = [-1]*m for node in range(n): for nei in path[node]: if match2[nei] == -1: match1[node] = nei match2[...
{ "input": [ "3 2\n8 3 8\n1 2\n2 3\n", "3 2\n8 12 8\n1 2\n2 3\n", "20 10\n512 64 536870912 256 1 262144 8 2097152 8192 524288 32 2 16 16777216 524288 64 268435456 256 67108864 131072\n17 20\n2 13\n11 12\n18 19\n4 7\n4 13\n8 9\n14 17\n8 19\n7 10\n", "10 25\n2048 536870912 64 65536 524288 2048 4194304 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i1, j1), (i2, j2), ..., (im, jm). Each good pair (ik, jk) meet...
521_D. Shop_9986
Vasya plays one very well-known and extremely popular MMORPG game. His game character has k skill; currently the i-th of them equals to ai. Also this game has a common rating table in which the participants are ranked according to the product of all the skills of a hero in the descending order. Vasya decided to 'upgra...
def euclid(a, b): if b == 0: return (1, 0, a) else: (x, y, g) = euclid(b, a%b) return (y, x - a//b*y, g) def modDivide(a, b, p): (x, y, g) = euclid(b, p) return a // g * (x + p) % p def comb(n, k): return modDivide(fac[n], fac[k] * fac[n-k] % P, P) k, n, m = list(map(int, input().split())) a = list(map(in...
{ "input": [ "2 4 3\n13 20\n1 1 14\n1 2 30\n2 1 6\n3 2 2\n", "10 6 6\n2 2 1 8 5 3 8 5 3 4\n3 8 5\n3 10 1\n3 7 8\n2 3 5\n1 5 1\n3 8 5\n", "1 1 1\n8\n2 1 8\n", "10 10 10\n8 8 8 8 8 8 8 8 8 8\n2 5 8\n2 4 8\n2 1 8\n2 7 8\n2 10 8\n2 9 8\n2 6 8\n2 3 8\n2 2 8\n2 8 8\n", "10 7 7\n4 7 6 3 4 3 5 4 3 6\n2 10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya plays one very well-known and extremely popular MMORPG game. His game character has k skill; currently the i-th of them equals to ai. Also this game has a common rating table in...
549_A. Face Detection_9990
The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them. In this problem an image is a rectangular table that consists of lowercase Latin letters. A face on the image is a 2 ...
n, m = [int(i) for i in input().split()] img = [list(input()) for i in range(n)] cnt = 0 for i in range(n - 1): for j in range(m - 1): arr = [img[i][j], img[i + 1][j], img[i][j + 1], img[i + 1][j + 1]] if 'f' in arr and 'a' in arr and 'c' in arr and 'e' in arr: cnt += 1 print(cnt)
{ "input": [ "4 2\nxx\ncf\nae\nxx\n", "4 4\nxxxx\nxfax\nxcex\nxxxx\n", "1 4\nface\n", "2 3\nfac\ncef\n", "2 5\nacdmw\nefazb\n", "5 5\nacnbx\nefacp\nlrefa\norqce\nzvbay\n", "5 5\nbyjvu\nkmaca\nalefe\nwcacg\nrefez\n", "5 5\nkjxbw\neacra\nxefhx\nucmcz\npgtjk\n", "2 2\ncc\ncf\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have t...
597_A. Divisibility_9995
Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k. Input The only line contains three space-separated integers k, a and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018). Output Print the required number. E...
k, a, b = map(int, input().split()) def Do(a, b,): suma = 0 if a < 0: if b == 0: print(-a // k + 1) if b > 0: suma = 1 a = -a if a % k == 0: if b % k == 0: suma += b // k + a // k ...
{ "input": [ "2 -4 4\n", "1 1 10\n", "3 124 456\n", "3 -191381 -1910\n", "3 -191380 -1910\n", "1000000000000000000 0 0\n", "100000000000000321 1000000000000000000 1000000000000000000\n", "2 0 1\n", "3 -381 281913\n", "3 383 281913\n", "1 -1000000000000000000 100000000000000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k. Input The o...
618_C. Constellation_9999
Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars numbered from 1 to n. For each i, the i-th star is located at coordinates (xi, yi). No two stars are located at the same position. In the evening Noku is going to take a look at the night sky. He would like to find three d...
f = lambda: list(map(int, input().split())) d = lambda x, y: (x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2 r = lambda x, y, z: (x[0] - y[0]) * (x[1] - z[1]) == (x[1] - y[1]) * (x[0] - z[0]) n = int(input()) t = [f() for i in range(n)] j = k = -1 b = c = 0 x = t.pop() for i in range(n - 1): a = d(x, t[i]) if j < 0 ...
{ "input": [ "3\n0 1\n1 0\n1 1\n", "5\n0 0\n0 2\n2 0\n2 2\n1 1\n", "4\n0 0\n131072 0\n131072 131072\n200000 0\n", "10\n999999999 1\n999999998 1\n999999997 1\n1000000000 1\n999999996 1\n999999995 1\n999999994 1\n999999992 1\n999999993 1\n0 0\n", "4\n0 0\n0 1\n0 2\n3 3\n", "4\n0 1\n0 2\n0 3\n7 7...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars numbered from 1 to n. For each i, the i-th star is located at coordinates (xi, yi). No...
639_A. Bear and Displayed Friends_10003
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti. Spring is starting and the Winter sleep is ...
import sys window = set() n, k, q = [int(x) for x in input().split()] arr = [int(x) for x in input().split()] for i in range(q): a, b = [int(x) for x in input().split()] if (a == 1): if (len(window) < k): window.add(arr[b - 1]) else: window.add(arr[b - 1]) m = min(window) window.remove(m) else: ...
{ "input": [ "6 3 9\n50 20 51 17 99 24\n1 3\n1 4\n1 5\n1 2\n2 4\n2 2\n1 1\n2 4\n2 3\n", "4 2 8\n300 950 500 200\n1 3\n2 4\n2 3\n1 1\n1 2\n2 1\n2 2\n2 3\n", "20 2 15\n12698951 55128070 116962690 156763505 188535242 194018601 269939893 428710623 442819431 483000923 516768937 552903993 633087286 656092270 67...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. Th...
690_A2. Collective Mindsets (medium)_10009
Way to go! Heidi now knows how many brains there must be for her to get one. But throwing herself in the midst of a clutch of hungry zombies is quite a risky endeavor. Hence Heidi wonders: what is the smallest number of brains that must be in the chest for her to get out at all (possibly empty-handed, but alive)? The ...
n = int(input()) if n & 1: print(n//2) else: k = 1 while k <= n: k *= 2 print((n - k//2)//2)
{ "input": [ "1\n", "3\n", "99\n", "19\n", "536870910\n", "100\n", "987654321\n", "8\n", "13\n", "1000000000\n", "536870912\n", "4124980\n", "20\n", "14\n", "11\n", "18\n", "9\n", "17\n", "16\n", "2\n", "6\n", "9999\n", "10\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Way to go! Heidi now knows how many brains there must be for her to get one. But throwing herself in the midst of a clutch of hungry zombies is quite a risky endeavor. Hence Heidi won...
733_D. Kostya the Sculptor_10015
Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya's idea and wants to present him a rectangular parallelepiped of marble from which he can carve the sphere. Zahar has n stones which are rectangul...
n = int(input()) data = {} ans = 0 res = 0 pos = 0 ind = [] for i in range(n): tmp = sorted(list(map(int, input().split()))) if min(tmp) > ans: ans = min(tmp) ind = [i, -1] t = (tmp[1], tmp[2]) if t in data: third, _ = data[t] if min([third + tmp[0], tmp[1], tmp[2]]) > ans: ans = min([third + tmp[0], ...
{ "input": [ "7\n10 7 8\n5 10 3\n4 2 6\n5 5 5\n10 2 8\n4 2 1\n7 7 7\n", "6\n5 5 5\n3 2 4\n1 4 1\n2 1 3\n3 2 4\n3 3 4\n", "2\n999999999 1000000000 1000000000\n1000000000 1000000000 1000000000\n", "3\n20 5 30\n20 6 30\n10 10 10\n", "1\n1 1 1\n", "3\n5 20 30\n6 30 20\n10 10 10\n", "3\n20 5 30...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya's idea and...
779_B. Weird Rounding_10021
Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k. In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10k. For example, if k = 3, in the number 30020 it is enough to delete a single digit (2). In this case, the result ...
n , k = map(int,input().split()) n = list(str(n)) counter = 0 zero_counter = 0 if n.count('0') >= k : for i in range(len(n)-1 , -1 , -1): if (n[i] != '0'): counter += 1 if (n[i] == '0'): zero_counter += 1 if (zero_counter == k): break print(counter) else: print(len(n) - 1)
{ "input": [ "10203049 2\n", "30020 3\n", "100 9\n", "1024 2\n", "650 1\n", "1034543 4\n", "11000 4\n", "78400 3\n", "10340 3\n", "532415007 8\n", "1421011 2\n", "12300 3\n", "101010110 5\n", "1000002333 6\n", "1010101010 2\n", "1011110 3\n", "10005 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k. In the given number of n Polycarp wants to remove the least number of digits to get a number ...
802_J. Send the Fool Further! (easy)_10025
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to d...
def dfs(u,cur): global ans vis[u]=True flag=True for x in vec[u] : v=x[0] c=x[1] if not vis[v] : dfs(v,cur+c) flag=False if flag: ans=max(cur,ans) ans=0 vec=[] vis=[] i=0 n=int(input()) while(i<n) : vec.append([]) vis.append(False) ...
{ "input": [ "6\n1 2 3\n0 2 100\n1 4 2\n0 3 7\n3 5 10\n", "4\n0 1 4\n0 2 2\n2 3 3\n", "11\n1 0 1664\n2 0 881\n3 2 4670\n4 2 1555\n5 1 1870\n6 2 1265\n7 2 288\n8 7 2266\n9 2 1536\n10 6 3378\n", "3\n1 0 5987\n2 0 8891\n", "11\n1 0 6646\n2 0 8816\n3 2 9375\n4 2 5950\n5 1 8702\n6 2 2657\n7 2 885\n8 7 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she ...
825_F. String Compression_10028
Ivan wants to write a letter to his friend. The letter is a string s consisting of lowercase Latin letters. Unfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string s instead of...
def prefix(s): p = [0] for i in range(1, len(s)): j = p[-1] while j > 0 and s[j] != s[i]: j = p[j - 1] if s[i] == s[j]: j += 1 p.append(j) return p s = input() n = len(s) ans = [0] * (n + 1) i = n - 1 while i >= 0: p = prefix(s[i:]) ans[i] =...
{ "input": [ "cczabababab\n", "abcab\n", "aaaaaaaaaa\n", "cbbbcccbbc\n", "hltcdvuobkormkxkbmpfjniilublkrckmvvxemcyietgxcyjgrjwsdsgsfmoqnmbxozfavxopklhldhnsjpxhejxaxuctxeifglx\n", "agdmdjkbfnleldamiiedfheefgaimecnllgkjdkdcfejainklmhaklcjkgkimgfiiajiiihhdngjedgmefnjmbglghjjejfjkaha\n", "kbyj...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ivan wants to write a letter to his friend. The letter is a string s consisting of lowercase Latin letters. Unfortunately, when Ivan started writing the letter, he realised that it i...
848_D. Shake It!_10031
A never-ending, fast-changing and dream-like world unfolds, as the secret door opens. A world is an unordered graph G, in whose vertex set V(G) there are two special vertices s(G) and t(G). An initial world has vertex set {s(G), t(G)} and an edge between them. A total of n changes took place in an initial world. In e...
mod = int(1e9 + 7) n, m = map(int, input().split()) f = [ [0 for i in range(60)] for j in range(60) ] g = [ [0 for i in range(60)] for j in range(60) ] s = [ [0 for i in range(60)] for j in range(60) ] inv = [ 1 ] f[0][0] = s[0][0] = 1 def pow(x, exp) : res = 1 for i in range(0, 31) : if exp & 1 : res ...
{ "input": [ "4 4\n", "31 8\n", "3 2\n", "7 3\n", "6 1\n", "50 33\n", "48 20\n", "3 1\n", "3 4\n", "4 1\n", "50 50\n", "6 6\n", "8 5\n", "3 3\n", "10 4\n", "6 7\n", "1 2\n", "4 2\n", "7 5\n", "7 6\n", "2 2\n", "50 2\n", "4 5\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A never-ending, fast-changing and dream-like world unfolds, as the secret door opens. A world is an unordered graph G, in whose vertex set V(G) there are two special vertices s(G) an...
870_B. Maximum of Maximums of Minimums_10035
You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get? D...
I = lambda: map(int, input().split()) _, k = I() A = list(I()) print(min(A) if k<2 else max(A) if k>2 else max(A[0], A[-1]))
{ "input": [ "5 1\n-4 -5 -3 -2 -1\n", "5 2\n1 2 3 4 5\n", "3 2\n1 5 3\n", "10 2\n10 9 1 -9 -7 -9 3 8 -10 5\n", "14 2\n-14 84 44 46 -75 -75 77 -49 44 -82 -74 -51 -9 -50\n", "9 3\n2 2 2 2 9 2 2 2 2\n", "5 2\n5 2 1 9 3\n", "7 3\n1 1 1 10 1 1 1\n", "2 2\n-333653905 224013643\n", "5...
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 a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum in...
897_A. Scarborough Fair_10039
Are you going to Scarborough Fair? Parsley, sage, rosemary and thyme. Remember me to one who lives there. He once was the true love of mine. Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there. Willem asks his friend, Grick for directions, Grick helped...
_,m=map(int,input().split()) a=input() for _ in [0]*m: l,r,b,c=input().split();l=int(l)-1;r=int(r) a=a[:l]+a[l:r].replace(b,c)+a[r:] print(a)
{ "input": [ "3 1\nioi\n1 1 i n\n", "5 3\nwxhak\n3 3 h x\n1 5 x a\n1 3 w g\n", "94 13\nbcaaaaaaccacddcdaacbdaabbcbaddbccbccbbbddbadddcccbddadddaadbdababadaacdcdbcdadabdcdcbcbcbcbbcd\n52 77 d d\n21 92 d b\n45 48 c b\n20 25 d a\n57 88 d b\n3 91 b d\n64 73 a a\n5 83 b d\n2 69 c c\n28 89 a b\n49 67 c b\n41 62...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Are you going to Scarborough Fair? Parsley, sage, rosemary and thyme. Remember me to one who lives there. He once was the true love of mine. Willem is taking the girl to the highe...
918_B. Radio Station_10043
As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. The school has n servers. Each server has a name and an ip (names aren't necessarily unique, but ips are). Dustin knows the ip and name of ...
a , b = map(int , input().split()) names = dict() for i in range(a): x , y = input().split() names[y] = x for i in range(b): com = input() h = com.split()[-1][:-1] print(f"{com} #{names[h]}")
{ "input": [ "2 2\nmain 192.168.0.2\nreplica 192.168.0.1\nblock 192.168.0.1;\nproxy 192.168.0.2;\n", "3 5\ngoogle 8.8.8.8\ncodeforces 212.193.33.27\nserver 138.197.64.57\nredirect 138.197.64.57;\nblock 8.8.8.8;\ncf 212.193.33.27;\nunblock 8.8.8.8;\ncheck 138.197.64.57;\n", "2 2\nmain 0.0.0.255\nmainn 0.0....
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. Th...
940_B. Our Tanya is Crying Out Loud_10047
Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: 1. Subtract 1 from x. This operation costs you A coins. 2. Divide x by k. Can be performed...
n = int(input()) k = int(input()) A = int(input()) B = int(input()) ans = 0 if k == 1: print(A*(n-1)) exit(0) while n > 1: subt = (n % k) ans += A * subt n -= (n%k) ans += min(A * (n - (n // k)),B) n //= k if n == 0: ans -= A print(ans)
{ "input": [ "19\n3\n4\n2\n", "9\n2\n3\n1\n", "5\n5\n2\n20\n", "1000000000\n1999999999\n789987\n184569875\n", "1000999777\n1934999345\n2356346\n34534565\n", "2000000000\n2\n1\n2000000000\n", "1999888325\n3\n2\n2000000000\n", "20\n1\n20\n1\n", "15\n2\n5\n2\n", "241375690\n17\n2\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allo...
993_E. Nikita and Order Statistics_10052
Nikita likes tasks on order statistics, for example, he can easily find the k-th number in increasing order on a segment of an array. But now Nikita wonders how many segments of an array there are such that a given number x is the k-th number in increasing order on this segment. In other words, you should find the numb...
from math import pi from cmath import exp def fft(a, lgN, rot=1): # rot=-1 for ifft N = 1<<lgN assert len(a)==N rev = [0]*N for i in range(N): rev[i] = (rev[i>>1]>>1)+(i&1)*(N>>1) A = [a[rev[i]] for i in range(N)] h = 1 while h<N: w_m = exp((0+1j) * rot * (pi / h)) for k in range(0, N, h<<1): ...
{ "input": [ "5 3\n1 2 3 4 5\n", "2 6\n-5 9\n", "6 99\n-1 -1 -1 -1 -1 -1\n", "2 -1000000000\n465132 210\n", "5 -2\n-1 -1 -4 -5 1\n", "10 5\n-3 2 1 -5 -3 6 -5 10 -10 -10\n", "10 9\n-2 6 0 -6 7 -8 -5 4 -3 3\n", "5 -6\n-4 2 -7 -1 -5\n", "10 29\n88 57 -3 -9 16 48 -84 80 -73 -46\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Nikita likes tasks on order statistics, for example, he can easily find the k-th number in increasing order on a segment of an array. But now Nikita wonders how many segments of an ar...
p02631 AtCoder Beginner Contest 171 - Red Scarf_10066
There are N Snuke Cats numbered 1, 2, \ldots, N, where N is even. Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written. Recently, they learned the operation called xor (exclusive OR). What is xor? For n non-negative integers x_1, x_2, \ldots, x_n, their xor, x_1~\textrm{xor}~x_2~\...
N = int(input()) A = list(map(int, input().split())) temp = A[0] for a in A[1:]: temp ^= a print(*[a^temp for a in A])
{ "input": [ "4\n20 11 9 24", "4\n5 11 9 24", "4\n5 11 10 24", "4\n0 11 10 24", "4\n1 11 10 24", "4\n1 11 10 29", "4\n1 11 0 29", "4\n1 11 0 49", "4\n1 2 0 49", "4\n2 2 0 49", "4\n2 0 0 49", "4\n2 0 1 49", "4\n0 0 1 49", "4\n1 0 1 49", "4\n1 0 2 49", "4\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N Snuke Cats numbered 1, 2, \ldots, N, where N is even. Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written. Recently, they learned the...
p02762 AtCoder Beginner Contest 157 - Friend Suggestions_10070
An SNS has N users - User 1, User 2, \cdots, User N. Between these N users, there are some relationships - M friendships and K blockships. For each i = 1, 2, \cdots, M, there is a bidirectional friendship between User A_i and User B_i. For each i = 1, 2, \cdots, K, there is a bidirectional blockship between User C_i...
N,M,K = map(int,input().split()) fr = [[] for i in range(N+1)] bl = [[] for i in range(N+1)] for _ in range(M): A,B = map(int, input().split()) fr[A].append(B) fr[B].append(A) for _ in range(K): C,D = map(int, input().split()) bl[C].append(D) bl[D].append(C) leader = [0]*(N+1) d = {} for root i...
{ "input": [ "4 4 1\n2 1\n1 3\n3 2\n3 4\n4 1", "5 10 0\n1 2\n1 3\n1 4\n1 5\n3 2\n2 4\n2 5\n4 3\n5 3\n4 5", "10 9 3\n10 1\n6 7\n8 2\n2 5\n8 4\n7 3\n10 9\n6 4\n5 8\n2 6\n7 5\n3 1", "5 10 0\n1 2\n1 3\n1 4\n1 5\n3 2\n2 4\n2 5\n4 3\n1 3\n4 5", "5 10 0\n1 2\n1 3\n2 4\n1 5\n3 2\n2 4\n2 5\n4 3\n1 3\n4 5",...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: An SNS has N users - User 1, User 2, \cdots, User N. Between these N users, there are some relationships - M friendships and K blockships. For each i = 1, 2, \cdots, M, there is a b...
p02897 AtCoder Beginner Contest 142 - Odds of Oddness_10074
Given is an integer N. Takahashi chooses an integer a from the positive integers not greater than N with equal probability. Find the probability that a is odd. Constraints * 1 \leq N \leq 100 Input Input is given from Standard Input in the following format: N Output Print the probability that a is odd. Your ...
N = int(input()) print((N - N // 2) / N)
{ "input": [ "4", "1", "5", "8", "11", "19", "21", "3", "9", "39", "-1", "53", "33", "-3", "47", "69", "17", "-11", "25", "-13", "-25", "57", "-9", "37", "23", "-5", "-7", "45", "15", "87", "29"...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given is an integer N. Takahashi chooses an integer a from the positive integers not greater than N with equal probability. Find the probability that a is odd. Constraints * 1 \le...
p03032 AtCoder Beginner Contest 128 - equeue_10078
Your friend gave you a dequeue D as a birthday present. D is a horizontal cylinder that contains a row of N jewels. The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values. In the beginning, you have no jewel in your hands. You can perform at most K operations on...
N, K = map(int, input().split()) V = list(map(int, input().split())) ans = 0 for i in range(min(N, K) + 1): for j in range(i+1): Juel = V[:j] + V[N-i+j:] Juel.sort() sum_v = 0 for k in range(min(K-i, i)): Juel[k] = max(Juel[k], 0) ans = max(ans, sum(Juel)) print(...
{ "input": [ "6 4\n-10 8 2 1 2 6", "6 4\n-6 -100 50 -2 -5 -3", "6 3\n-6 -100 50 -2 -5 -3", "6 4\n-10 8 2 0 2 6", "6 4\n-1 -100 50 -2 -5 -3", "6 3\n-6 -100 50 -2 -9 -3", "6 4\n-10 3 2 0 2 6", "6 4\n-1 -100 78 -2 -5 -3", "6 4\n-10 3 2 0 2 12", "6 4\n-10 3 3 0 2 12", "6 7\n-1 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Your friend gave you a dequeue D as a birthday present. D is a horizontal cylinder that contains a row of N jewels. The values of the jewels are V_1, V_2, ..., V_N from left to righ...
p03173 Educational DP Contest - Slimes_10082
There are N slimes lining up in a row. Initially, the i-th slime from the left has a size of a_i. Taro is trying to combine all the slimes into a larger slime. He will perform the following operation repeatedly until there is only one slime: * Choose two adjacent slimes, and combine them into a new slime. The new sli...
from functools import lru_cache N = int(input()) A = list(map(int, input().split())) INF = 1.0e+100 dp = [[[None, INF] for _ in range(N+1)] for _ in range(N+1)] for i in range(N): dp[i][i+1] = [A[i], 0] for g in range(2, N+1): for l in range(N-g+1): r = l + g for i in range(l+1, r): ...
{ "input": [ "6\n7 6 8 6 1 1", "5\n10 10 10 10 10", "3\n1000000000 1000000000 1000000000", "4\n10 20 30 40", "6\n7 9 8 6 1 1", "5\n10 8 10 10 10", "3\n1001000000 1000000000 1000000000", "4\n10 20 30 46", "6\n7 9 13 6 1 1", "5\n19 8 10 10 10", "3\n1001000000 1000000001 10000...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N slimes lining up in a row. Initially, the i-th slime from the left has a size of a_i. Taro is trying to combine all the slimes into a larger slime. He will perform the fo...
p03321 AtCoder Regular Contest 099 - Independence_10086
In the State of Takahashi in AtCoderian Federation, there are N cities, numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th road connects City A_i and City B_i. Every road connects two distinct cities. Also, for any two cities, there is at most one road that directly connects them. One day, it ...
from collections import deque N, M = map(int, input().split()) adjL = [set(range(N)) for _ in range(N)] for v in range(N): adjL[v].remove(v) for _ in range(M): A, B = map(int, input().split()) A, B = A-1, B-1 adjL[A].remove(B) adjL[B].remove(A) def getSizes(adjList): def bfs(vSt): colo...
{ "input": [ "5 5\n1 2\n1 3\n3 4\n3 5\n4 5", "5 1\n1 2", "10 39\n7 2\n7 1\n5 6\n5 8\n9 10\n2 8\n8 7\n3 10\n10 1\n8 10\n2 3\n7 4\n3 9\n4 10\n3 4\n6 1\n6 7\n9 5\n9 7\n6 9\n9 4\n4 6\n7 5\n8 3\n2 5\n9 2\n10 7\n8 6\n8 9\n7 3\n5 3\n4 5\n6 3\n2 10\n5 10\n4 2\n6 2\n8 4\n10 6", "4 3\n1 2\n1 3\n2 3", "5 0\n...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the State of Takahashi in AtCoderian Federation, there are N cities, numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th road connects City A_i and City B_i...
p03475 AtCoder Beginner Contest 084 - Special Trains_10090
A railroad running from west to east in Atcoder Kingdom is now complete. There are N stations on the railroad, numbered 1 through N from west to east. Tomorrow, the opening ceremony of the railroad will take place. On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i t...
N=int(input()) CSF=[[0]]+[list(map(int, input().split())) for _ in range(N-1)] for j in range(1,N): t=0 for i in range(j,N): if t<CSF[i][1]: t=CSF[i][1] elif t%CSF[i][2]!=0: t=t+CSF[i][2]-t%CSF[i][2] t+=CSF[i][0] print(t) print(0)
{ "input": [ "3\n6 5 1\n1 10 1", "4\n12 13 1\n44 17 17\n66 4096 64", "4\n12 24 6\n52 16 4\n99 2 2", "3\n6 6 1\n1 10 1", "4\n1 13 1\n44 17 17\n66 4096 64", "4\n11 24 6\n52 16 4\n99 2 2", "3\n6 1 1\n1 10 2", "3\n6 10 1\n1 10 1", "4\n15 24 6\n52 16 4\n99 2 2", "3\n6 6 1\n2 10 1", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A railroad running from west to east in Atcoder Kingdom is now complete. There are N stations on the railroad, numbered 1 through N from west to east. Tomorrow, the opening ceremony...
p03640 AtCoder Regular Contest 080 - Grid Coloring_10094
We have a grid with H rows and W columns of squares. Snuke is painting these squares in colors 1, 2, ..., N. Here, the following conditions should be satisfied: * For each i (1 ≤ i ≤ N), there are exactly a_i squares painted in Color i. Here, a_1 + a_2 + ... + a_N = H W. * For each i (1 ≤ i ≤ N), the squares painted i...
h,w=map(int,input().split()) n=int(input()) a=list(map(int,input().split())) p=list() for i in range(n): p.extend([i+1 for j in range(a[i])]) #print(p) for i in range(h): if i%2==0: print(*p[i*w:i*w+w]) else: print(*p[i*w:i*w+w][::-1])
{ "input": [ "1 1\n1\n1", "3 5\n5\n1 2 3 4 5", "2 2\n3\n2 1 1", "2 2\n2\n3 1 1", "2 1\n1\n2", "1 2\n1\n2", "2 2\n3\n1 1 2", "3 5\n5\n1 2 5 4 3", "1 4\n2\n3 1 0", "2 2\n3\n1 2 1", "2 2\n2\n2 2 1", "1 4\n3\n2 2 0", "3 5\n5\n1 2 6 4 2", "2 2\n3\n4 0 0", "2 1\n3...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a grid with H rows and W columns of squares. Snuke is painting these squares in colors 1, 2, ..., N. Here, the following conditions should be satisfied: * For each i (1 ≤ i ≤...
p03796 AtCoder Beginner Contest 055 - Training Camp_10098
Snuke loves working out. He is now exercising N times. Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i. Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7. Constraints * 1 ≤ N ≤ 10...
a = 1 for i in range(1,int(input())+1): a = (a*i)%(10**9+7) print(a)
{ "input": [ "100000", "3", "10", "100001", "1", "15", "110001", "21", "110011", "39", "11", "6", "4", "7", "2", "101001", "8", "18", "110000", "111011", "30", "110111", "65", "5", "101000", "12", "110100", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke loves working out. He is now exercising N times. Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i. Find Snuke'...
p03964 AtCoder Beginner Contest 046 - AtCoDeer and Election Report_10102
AtCoDeer the deer is seeing a quick report of election results on TV. Two candidates are standing for the election: Takahashi and Aoki. The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes. AtCoDeer has checked the report N times, and when he c...
N = int(input()) TA = [map(int, input().split()) for _ in range(N)] T, A = 1, 1 for t, a in TA: n = max(-(- T // t), -(- A // a)) T, A = n * t, n * a print(T + A)
{ "input": [ "5\n3 10\n48 17\n31 199\n231 23\n3 2", "4\n1 1\n1 1\n1 5\n1 100", "3\n2 3\n1 1\n3 2", "5\n3 1\n48 17\n31 199\n231 23\n3 2", "4\n1 1\n1 1\n1 9\n1 100", "3\n3 3\n1 1\n3 2", "3\n4 3\n1 1\n3 1", "3\n4 3\n1 2\n3 1", "3\n2 3\n1 1\n1 2", "4\n1 2\n1 1\n1 9\n1 100", "5\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: AtCoDeer the deer is seeing a quick report of election results on TV. Two candidates are standing for the election: Takahashi and Aoki. The report shows the ratio of the current numbe...
p00055 Sequence_10106
I have a sequence defined as follows: * All even-numbered terms are equal to the previous term multiplied by 2. * All odd-numbered terms are equal to the previous term divided by 3. Create a program that reads the first term a of this sequence and outputs the sum s (10) from the first term to the tenth term. Inp...
while True: try: a1 = float(input()) lst = [a1] for i in range(2,11): if i % 2: a1 /= 3 lst.append(a1) else: a1 *= 2 lst.append(a1) print(sum(lst)) except EOFError: break
{ "input": [ "1.0\n2.0\n3.0", "1.0\n2.0\n3.0275795971873247", "1.0\n2.0\n3.649719120761334", "1.0\n2.783489291479195\n3.649719120761334", "1.0\n2.783489291479195\n4.155640422734946", "1.1344311401157299\n2.783489291479195\n4.155640422734946", "1.1344311401157299\n2.783489291479195\n4.61372...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: I have a sequence defined as follows: * All even-numbered terms are equal to the previous term multiplied by 2. * All odd-numbered terms are equal to the previous term divided by 3. ...
p00186 Aizu Chicken_10110
In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu local chicken is very delicious, but it is difficult to breed, so the production volume is small and the price is high. <image> Relative...
def check(mid, b, c1, c2, q1): if mid + (b - mid * c1) // c2 < q1: return False return True while True: s = input() if s == "0": break q1, b, c1, c2, q2 = map(int, s.split()) max_aizu = min(b // c1, q2) if max_aizu <= 0: print("NA") continue if c2 >= c1: max_normal = (b - m...
{ "input": [ "48 9297 240 126 32\n20 3010 157 141 7\n30 117002 5680 962 15\n8 1673 1712 190 22\n64 8478 87 54 307\n23 5477 117 92 12\n50 7558 1396 187 17\n279 88677 4522 514 14\n0", "48 9297 240 126 32\n20 3010 157 141 7\n30 117002 5680 962 15\n8 1673 1712 190 22\n64 8478 87 54 307\n23 5477 117 92 12\n50 7558...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In April 2008, Aizuwakamatsu City succeeded in making yakitori with a length of 20 m 85 cm. The chicken used at this time is Aizu local chicken, which is a specialty of Aizu. Aizu loc...
p00341 Cuboid Made with Bars_10113
The educational program (AHK Education) of the Aiz Broadcasting Association broadcasts a handicraft program for children, "Play with Tsukuro". This time I will make a box with sticks, but I would like to see if I can make a rectangular parallelepiped using the 12 sticks I prepared. However, the stick must not be cut or...
ls = list(map(int, input().split())) ls.sort() if ls[0]==ls[1]==ls[2]==ls[3] and ls[4]==ls[5]==ls[6]==ls[7] and ls[8]==ls[9]==ls[10]==ls[11]: print('yes') else: print('no')
{ "input": [ "1 1 2 2 3 1 2 3 3 3 1 2", "1 1 3 4 8 9 7 3 4 5 5 5", "1 1 2 2 3 1 1 3 3 3 1 2", "1 1 3 4 8 9 7 3 3 5 5 5", "1 0 2 2 3 1 1 3 3 3 1 2", "1 1 3 7 8 9 7 3 3 5 5 5", "0 0 2 2 3 1 1 3 3 3 1 2", "1 1 2 2 5 1 2 3 3 3 1 2", "1 1 3 4 8 9 3 3 4 5 5 5", "1 1 2 2 3 1 2 3 2 3 1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The educational program (AHK Education) of the Aiz Broadcasting Association broadcasts a handicraft program for children, "Play with Tsukuro". This time I will make a box with sticks,...
p00537 Railroad Trip_10117
Railroad Trip There are N cities in the JOI country, numbered 1, 2, ..., and N, respectively. In addition, there are N − 1 railroads, which are numbered 1, 2, ..., and N − 1, respectively. The railroad i (1 ≤ i ≤ N − 1) connects the city i and the city i + 1 in both directions. There are two ways to get on the JOI ra...
import itertools def main(): n, m = map(int, input().split()) plst = tuple(map(int, input().split())) abclst = [tuple(map(int, input().split())) for _ in range(n - 1)] sec = [0] * n for pi, pi1 in zip(plst, plst[1:]): p1, p2 = min(pi, pi1) - 1, max(pi, pi1) - 1 sec[p1] += 1 sec[p2] -= 1 s...
{ "input": [ "4 4\n1 3 2 4\n120 90 100\n110 50 80\n250 70 130", "4 4\n1 3 2 4\n120 90 100\n110 50 80\n250 70 24", "4 4\n1 3 2 4\n120 90 100\n110 50 80\n250 75 130", "4 4\n1 3 2 4\n120 90 100\n110 50 33\n250 70 24", "4 4\n1 3 2 4\n120 90 100\n110 50 96\n250 75 130", "4 4\n1 3 2 4\n120 90 100\n1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Railroad Trip There are N cities in the JOI country, numbered 1, 2, ..., and N, respectively. In addition, there are N − 1 railroads, which are numbered 1, 2, ..., and N − 1, respect...
p00840 Mobile Computing_10121
There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones back home and make nice mobile arts of them to decorate their 2-dimensional living rooms. In their 2-dimensional world, a mobile is defi...
def solve(): from itertools import combinations from sys import stdin f_i = stdin def dfs(base, stones): key = tuple(stones) if key in rec: return set((base + l, base + r) for l, r in rec[key]) edges = set() positions = set() for i in ran...
{ "input": [ "5\n1.3\n3\n1\n2\n1\n1.4\n3\n1\n2\n1\n2.0\n3\n1\n2\n1\n1.59\n4\n2\n1\n1\n3\n1.7143\n4\n1\n2\n3\n5", "5\n1.562976116968605\n3\n1\n2\n1\n1.4\n3\n1\n2\n1\n2.0\n3\n1\n2\n1\n1.59\n4\n2\n1\n1\n3\n1.7143\n4\n1\n2\n3\n5", "5\n1.562976116968605\n3\n1\n2\n1\n1.4\n3\n1\n2\n1\n2.0\n3\n1\n2\n1\n1.59\n4\n2...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones ...
p01104 Making Lunch Boxes_10125
Making Lunch Boxes Taro has been hooked on making lunch boxes recently. Taro has obtained a new lunch box recipe book today, and wants to try as many of the recipes listed in the book as possible. Enough of the ingredients for all the recipes are at hand, but they all are in vacuum packs of two. If only one of them i...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import time,random,resource sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 mod2 = 998244353 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): r...
{ "input": [ "4 3\n110\n101\n011\n110\n7 1\n1\n1\n1\n1\n1\n1\n1\n4 5\n10000\n01000\n00100\n00010\n6 6\n111111\n011000\n100000\n000010\n100001\n100100\n0 0", "4 3\n110\n101\n011\n110\n7 1\n0\n1\n1\n1\n1\n1\n1\n4 5\n10000\n01000\n00100\n00010\n6 6\n111111\n011000\n100000\n000010\n100001\n100100\n0 0", "4 3\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Making Lunch Boxes Taro has been hooked on making lunch boxes recently. Taro has obtained a new lunch box recipe book today, and wants to try as many of the recipes listed in the boo...
p01557 ReverseSort_10132
A permutation of N numbers from 1 to N A1, A2, ……, AN is given. You can perform the operation reverse (i, j) to reverse the order of the numbers in the interval [i, j] (1 ≤ i ≤ j ≤ N) for this permutation. For example, if reverse (2, 4) is applied to [1, 2, 3, 4, 5], it becomes [1, 4, 3, 2, 5]. Calculate how many opera...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**13 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin....
{ "input": [ "3\n1 2 3", "5\n1 4 3 5 2", "10\n3 1 5 2 7 4 9 6 10 8", "5\n3 1 5 2 4", "10\n3 0 5 2 7 4 9 6 10 8", "5\n3 1 5 4 2", "3\n1 3 2", "10\n3 0 5 2 7 4 9 6 17 8", "10\n3 0 5 1 7 4 9 6 17 8", "10\n3 0 5 1 7 4 12 6 17 8", "10\n3 0 5 2 7 4 9 6 11 8", "10\n3 0 5 2 7 4...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A permutation of N numbers from 1 to N A1, A2, ……, AN is given. You can perform the operation reverse (i, j) to reverse the order of the numbers in the interval [i, j] (1 ≤ i ≤ j ≤ N)...
p01719 Invest Master_10136
After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks. Ikta does not currently own any shares, but owns x yen. He has invested i...
I=input n,d,x=map(int,I().split()) w=[list(map(int,I().split()))for i in range(d)];d-=1 p=[[w[i+1][j]-w[i][j]for j in range(n)]for i in range(d)] b=x for i in range(d): dp = [False for j in range(b+1)] dp[0]=0 for j in range(n): for k in range(b): if w[i][j]+k<b+1 and id(dp[k])!=False: ...
{ "input": [ "1 2 5\n6\n10000", "2 3 5\n4 5\n6 3\n8 5", "3 3 10\n10 9 6\n8 7 3\n7 5 1", "2 2 5\n3 2\n5 4", "3 3 10\n10 9 6\n14 7 3\n7 5 1", "3 3 10\n10 8 13\n14 3 4\n5 5 1", "3 3 10\n10 8 13\n14 3 4\n5 4 1", "1 2 5\n4\n10000", "2 3 5\n4 5\n6 3\n8 6", "2 2 6\n3 2\n5 4", "3 3...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To ...
p01992 Shiritori Compression_10140
D: Shiritori Compression problem Ebi-chan and Kana-chan did a shiritori. Ebi-chan is looking at the memo of the word that came out in Shiritori. Ebi-chan wants to remove redundant continuous subsequences from this word sequence w_1, w_2, ..., w_N until they no longer exist. The redundant continuous subsequence that ...
from collections import deque N = int(input()) idxs = [deque() for i in range(26)] def ctoi(c): return ord(c) - ord('a') cs = [] for i in range(N): c = input()[0] cs.append(c) ci = ctoi(c) idxs[ci].append(i) dp = [i for i in range(N)] dp[0] = 0 for i in range(N): c = cs[i] ci = ctoi(c) ...
{ "input": [ "7\nbanana\nat\ntomb\nbus\nsound\ndoes\nsome", "7\nbanana\nta\ntomb\nbus\nsound\ndoes\nsome", "7\nananab\nta\nsomb\nbus\nsound\ndoes\nsemo", "7\nbanaoa\nta\ntomb\nbus\nsound\ndoes\nsome", "7\nbanaoa\nta\nuomb\nbus\nsound\ndoes\nsome", "7\nbanaoa\nua\ntomb\nbus\nsound\ndoes\nsome",...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: D: Shiritori Compression problem Ebi-chan and Kana-chan did a shiritori. Ebi-chan is looking at the memo of the word that came out in Shiritori. Ebi-chan wants to remove redundant ...
p02138 U and U_10144
Problem There are $ 2 $ teams, Team UKU and Team Ushi. Initially, Team UKU has $ N $ people and Team Uku has $ M $ people. Team UKU and Team Ushi decided to play a game called "U & U". "U & U" has a common score for $ 2 $ teams, and the goal is to work together to minimize the common score. In "U & U", everyone's phys...
def inpl(): return list(map(int, input().split())) N, M = inpl() n2 = N * 1 n1 = N * 1 m1 = M * 1 mall = M * 2 # うくをほろぼす ans1 = 0 while True: mall -= n1 m1 = -(-mall//2) if mall <= 0: break ans1 += 1 if n2 - m1 >= 0: n2 -= m1 else: n1 = n1 + n2 - m1 n2...
{ "input": [ "64783 68943", "20 10", "1000000000000000000 1000000000000000000", "10 20", "57319 68943", "25 10", "1000000000000000000 1000000000010000000", "10 36", "105904 68943", "25 12", "1000000001000000000 1000000000010000000", "10 48", "109335 68943", "13 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem There are $ 2 $ teams, Team UKU and Team Ushi. Initially, Team UKU has $ N $ people and Team Uku has $ M $ people. Team UKU and Team Ushi decided to play a game called "U & U...
p02279 Rooted Trees_10147
A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a binary relation on V represented by a set of edges. Fig. 1 illustrates an example of a graph (or graphs). <image> Fig. 1 A free tree is a connnected, acyclic, undirected graph. A rooted tree is a free tree in which one of the vertic...
import sys n = int(input()) nodes = [[-1, None, None] for _ in range(n)] depths = [None for _ in range(n)] for _ in range(n): nid, k, *children = list(map(int, sys.stdin.readline().split())) if k > 0: nodes[nid][1] = children[0] children.append(None) for i in range(k): cid = children...
{ "input": [ "13\n0 3 1 4 10\n1 2 2 3\n2 0\n3 0\n4 3 5 6 7\n5 0\n6 0\n7 2 8 9\n8 0\n9 0\n10 2 11 12\n11 0\n12 0", "4\n1 3 3 2 0\n0 0\n3 0\n2 0", "4\n1 3 2 3 0\n0 0\n3 0\n2 0", "4\n1 3 2 3 0\n0 0\n3 0\n2 0" ], "output": [ "node 0: parent = -1, depth = 0, root, [1, 4, 10]\nnode 1: parent = 0, de...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a binary relation on V represented by a set of edges. Fig. 1 illustrates an example of a graph (or ...
p02426 Bit Mask_10150
A state with $n$ flags of ON or OFF can be represented by a sequence of bits where $0, 1, ..., n-1$ th flag corresponds to 1 (ON) or 0 (OFF). The state can be managed by the corresponding decimal integer, because the sequence of bits is a binary representation where each bit is 0 or 1. On the other hand, a mask is a s...
import sys class BitFlag: #ALL_ON = 0xffffffffffffffff ALL_OFF = 0x0000000000000000 def __init__(self, mask_digits): self.FLAGS = self.ALL_OFF self.MaskFor1bit = [1 << i for i in range(64)] self.Masks = [self.make_mask(digits) for digits in mask_digits] @staticmethod def...
{ "input": [ "3\n3 0 1 3\n1 3\n3 0 1 2\n8\n1 0\n2 1\n3 1\n4 2\n5 2\n6 2\n7 2\n8 2", "3\n3 0 1 3\n1 3\n3 0 0 2\n8\n1 0\n2 1\n3 1\n4 2\n5 2\n6 2\n7 2\n8 2", "3\n3 0 1 3\n0 3\n3 0 0 2\n8\n1 0\n2 1\n3 1\n4 2\n5 2\n6 2\n7 2\n8 2", "3\n3 0 1 3\n1 3\n3 0 0 2\n8\n1 0\n2 1\n3 1\n4 2\n5 1\n6 2\n7 0\n8 2", "...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A state with $n$ flags of ON or OFF can be represented by a sequence of bits where $0, 1, ..., n-1$ th flag corresponds to 1 (ON) or 0 (OFF). The state can be managed by the correspon...
1011_C. Fly_10160
Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on n - 2 intermediate planets. Formally: we number all the planets from 1 to n. 1 is Earth, n is Mars. Natasha will make exactly n flights: 1 → 2 → … n → 1. Flight from x to y consists of two phases: take-off from ...
from sys import stdin n=int(stdin.readline()) m=int(stdin.readline()) A=list(map(int,stdin.readline().split())) B=list(map(int,stdin.readline().split())) if 1 in A or 1 in B: print(-1) else: ans=(m*B[0])/(B[0]-1) for i in range(n-1,0,-1): ans=(ans*A[i])/(A[i]-1) ans=(ans*B[i])/(B[i]-1)...
{ "input": [ "2\n12\n11 8\n7 5\n", "3\n1\n1 4 1\n2 5 3\n", "6\n2\n4 6 3 3 5 6\n2 6 3 6 5 3\n", "90\n35\n1 68 16 30 24 1 1 1 35 1 1 67 1 1 1 1 33 16 37 77 83 1 77 26 1 1 68 67 70 62 1 47 1 1 1 84 1 65 1 32 83 1 1 1 28 1 71 76 84 1 1 5 1 74 10 1 1 1 38 87 13 1 7 66 81 49 1 9 1 11 1 25 1 1 1 1 7 1 1 36 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on n - 2 intermediate planets. Formally: we number all the planets from 1 to n...
1036_D. Vasya and Arrays_10164
Vasya has two arrays A and B of lengths n and m, respectively. He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array [1, ...
from sys import stdin input=stdin.readline n=int(input()) a=list(map(int,input().split())) m=int(input()) b=list(map(int,input().split())) if sum(a)!=sum(b): print(-1) exit() j,k=0,0 sa,sb=0,0 cnt=0 pa,pb=1,1 while j<n and k<m: if pa==1: sa+=a[j] if pb==1: sb+=b[k] if sa==sb: cnt+=1 j+=1 k...
{ "input": [ "3\n1 2 3\n3\n1 2 3\n", "5\n11 2 3 5 7\n4\n11 7 3 7\n", "2\n1 2\n1\n100\n", "2\n2 3\n1\n2\n", "49\n63 13 6 1 50 37 1 1 1 6 34 9 4 2 1 2 3 46 11 1 2 3 4 1 6 74 7 3 1 1 31 18 20 15 1 74 6 2 1 1 6 46 13 29 2 20 44 1 1\n45\n54 19 5 1 1 3 81 1 4 3 1 5 1 28 18 3 5 1 23 33 9 3 31 47 5 7 2 76...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has two arrays A and B of lengths n and m, respectively. He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment ...
1059_C. Sequence Transformation_10168
Let's call the following process a transformation of a sequence of length n. If the sequence is empty, the process ends. Otherwise, append the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) (GCD) of all the elements of the sequence to the result and remove one arbitrary element from t...
from math import log2 n = int(input()) if n == 3: print(1,1,3) elif n == 1: print(1) else: num = n // 2 num3 = 2 ** int(log2(num)) * 2 num2 = n % 2 print("1 " * (num2 + num),end="") cur_num = 2 while num > 1: num2 = num % 2 num //= 2 print((str(cur_num)+ " ") *(nu...
{ "input": [ "3\n", "1\n", "2\n", "20\n", "12\n", "21\n", "13\n", "11\n", "11111\n", "1111\n", "9\n", "15\n", "16\n", "10\n", "19\n", "7\n", "6\n", "18\n", "4\n", "5\n", "8\n", "14\n", "17\n", "99999\n", "100000\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's call the following process a transformation of a sequence of length n. If the sequence is empty, the process ends. Otherwise, append the [greatest common divisor](https://en.wi...
10_D. LCIS_10174
This problem differs from one which was on the online contest. The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n. The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other wo...
n = int(input()) a = [0] + list(map(int, input().split())) m = int(input()) b = [0] + list(map(int, input().split())) d = [[0 for j in range(m+1)] for i in range(n+1)] prev = [0] * (n+1) for i in range(1, n+1): for j in range(1, m+1): if a[i] == b[j]: d[i][j] = 1 for k in range(1, i...
{ "input": [ "5\n1 2 0 2 1\n3\n1 0 1\n", "7\n2 3 1 6 5 4 6\n4\n1 3 5 6\n", "9\n7 4 4 5 0 6 5 4 10\n4\n5 2 10 9\n", "13\n55 160 86 99 92 148 81 36 216 191 214 127 44\n85\n92 12 64 21 221 225 119 243 147 47 244 112 212 237 209 121 81 239 43 104 3 254 52 13 1 210 28 18 199 75 251 146 77 28 253 211 50 35 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This problem differs from one which was on the online contest. The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n. The sequence s1, s2, ..., sk is called th...
1121_C. System Testing_10178
Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing p...
n,k = map(int,input().split()) a = list(map(int,input().split())) caption = 0 tested = [0 for i in range(n)] interesting = [0 for i in range(n)] ans = 0 while len(a) != 0: m = min(a[0:k]) for j in range(m): for i in range(min(k,len(a))): tested[i] += 1 a[i] -= 1 if ca...
{ "input": [ "14 5\n48 19 6 9 50 20 3 42 38 43 36 21 44 6\n", "2 2\n49 100\n", "4 2\n32 100 33 1\n", "1 100\n79\n", "50 3\n33 7 96 30 68 37 44 50 100 71 12 100 72 43 17 75 59 96 16 34 25 3 90 45 7 55 92 59 30 25 96 23 40 41 95 99 93 79 89 11 76 60 4 100 75 14 37 39 87 47\n", "100 10\n3 114 77 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are n solutions, the i-th of them should be teste...
1148_E. Earth Wind and Fire_10182
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more operations of the following type: * take two stones with indices i and j so that s_i ≤ s_j, choose an integer d (0 ≤ 2 ⋅ d ≤ s_j - s_i)...
from collections import namedtuple Stone = namedtuple('Stone', ['s', 'i']) def debug(*args, **kwargs): import sys #print(*args, *('{}={}'.format(k, v) for k, v in kwargs.items()), sep='; ', file=sys.stderr) def solve(n, s, t): #debug(s=s, t=t) s = list(map(lambda s_i: Stone(s_i[1], s_i[0]), enumer...
{ "input": [ "3\n1 5 10\n3 5 7\n", "5\n2 2 7 4 9\n5 4 5 5 5\n", "1\n1\n1\n", "1\n1\n1000000000\n", "5\n16 2 8 19 4\n10 9 10 10 10\n", "5\n12 19 11 13 16\n14 14 14 14 15\n", "2\n1 5\n3 4\n", "5\n19 9 3 20 2\n11 11 10 11 10\n", "20\n53 86 76 100 16 12 13 97 79 23 28 64 42 10 23 56 59...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place. You can perform zero or more op...
116_B. Little Pigs and Wolves_10186
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n × m. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of ...
n,m = map(int,input().split()) x,cnt = [],0 for _ in range(n): s = list(map(str,input())) for i in s: x.append(i) if n==m==1: print(0) elif n==1 or m==1: for i in range(len(x)): if x[i]=='W': if i==0: if x[i+1]=='P': x[i+1]='.';cnt+=1 elif i==(len(x)-1): if x[i-1]=='P': x[i-1]='.';cnt+=1 else: ...
{ "input": [ "2 3\nPPW\nW.P\n", "3 3\nP.W\n.P.\nW.P\n", "10 10\nPPPPPPPPPP\nPPPPPPPWPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\n", "10 1\n.\nW\nW\nP\nP\n.\n.\n.\nW\nP\n", "4 8\n..PW..WW\nWWPP.PP.\nP...PW.P\nP.WW...P\n", "5 5\n.....\n..P.....
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n × m. Each cell in this grid was either empty, containing one little pig, or cont...
1207_B. Square Filling_10193
You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 × 2, and replace every element in the chosen sub...
#!/usr/bin/python3 import pdb n, m = map(int, input().split(" ")) matrix = [] for i in range(n): matrix.append(list(map(int, input().split(" ")))) def check_pos(matrix, x, y): if x + 1 >= len(matrix): return False if y + 1 >= len(matrix[0]): return False if matrix[x][y] == 0 or matrix[x+1][y] == 0 or matrix[x...
{ "input": [ "3 2\n0 0\n0 0\n0 0\n", "3 3\n1 1 1\n1 1 1\n0 1 1\n", "3 3\n1 0 1\n1 0 1\n0 0 0\n", "4 4\n1 1 1 1\n1 1 1 1\n0 1 0 0\n0 0 0 0\n", "2 2\n1 1\n1 0\n", "4 4\n1 1 0 0\n1 1 1 0\n1 0 1 1\n1 0 1 1\n", "3 3\n1 1 0\n1 1 0\n1 0 0\n", "3 3\n0 1 0\n0 1 1\n0 1 1\n", "3 3\n0 0 0\n0 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0. You may perform some oper...
1225_C. p-binary_10197
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer. For example,...
if __name__ == '__main__': n, p = map(int, input().split()) aws = 1 while True: min_number = aws * ((2**0) + p) if min_number > n: aws = -1 break elif min_number == n: break elif bin((n - aws * p)).count('1') <= aws: break ...
{ "input": [ "24 0\n", "1 1\n", "24 -1\n", "24 1\n", "4 -7\n", "47 23\n", "17 8\n", "35 11\n", "29 9\n", "5 2\n", "100500 -179\n", "13 6\n", "10 7\n", "2001 1000\n", "1 -1000\n", "1999 999\n", "536870812 1\n", "536870911 0\n", "12345678 -123\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative...
1249_E. By Elevator or Stairs?_10201
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor. Let: * a_i for all i from 1 to n-1 be the time required to go from the i-th floor...
N = 2*10**5 + 5; a=[0 for i in range(N)] b=[0 for i in range(N)] dp=[[0 for i in range(2)] for i in range(N)] def main(): n,c=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) dp[0][0] = 0 dp[0][1] = c for i in range(1,n): dp[i][0] = min(dp[i...
{ "input": [ "10 2\n7 6 18 6 16 18 1 17 17\n6 9 3 10 9 1 10 1 5\n", "10 1\n3 2 3 1 3 3 1 4 1\n1 2 3 4 4 1 2 1 3\n", "10 2\n7 6 18 6 16 18 1 17 17\n6 17 3 10 9 1 10 1 5\n", "10 1\n3 0 3 1 3 3 1 4 1\n1 2 3 4 4 1 2 1 3\n", "10 2\n7 6 18 6 16 18 2 17 17\n6 17 3 10 9 1 10 1 5\n", "10 1\n5 0 3 0 3 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total ...
1268_B. Domino for Young_10205
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 recta...
# by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO,IOBase def main(): n = int(input()) a = list(map(int,input().split())) base,su,bl = 0,sum(a),0 for i in range(n): if not base: bl += (a[i]+1)//2 else: bl += a[i]//2 ...
{ "input": [ "5\n3 2 2 2 1\n", "1\n1\n", "3\n3 3 3\n", "1\n300000\n", "10\n99 83 62 53 47 33 24 15 10 9\n", "100\n100 100 99 98 97 92 92 92 92 91 89 87 87 87 86 85 84 82 82 81 81 80 79 78 78 77 77 76 76 74 72 71 71 70 69 66 64 63 63 62 60 59 59 59 55 54 53 52 52 51 49 49 49 47 47 46 46 45 44 4...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goa...
1290_C. Prefix Enlightenment_10209
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you ...
from sys import stdin input = stdin.readline n , k = [int(i) for i in input().split()] pairs = [i + k for i in range(k)] + [i for i in range(k)] initial_condition = list(map(lambda x: x == '1',input().strip())) data = [i for i in range(2*k)] constrain = [-1] * (2*k) h = [0] * (2*k) L = [1] * k + [0] * k dp1 = [-1 for...
{ "input": [ "19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n", "8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n", "5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n", "7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n", "1 1\n1\n1\n1\n", "7 3\n0011100\n3\n1 4...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection...
1333_B. Kind Anton_10215
Once again, Boris needs the help of Anton in creating a task. This time Anton needs to solve the following problem: There are two arrays of integers a and b of length n. It turned out that array a contains only elements from the set \{-1, 0, 1\}. Anton can perform the following sequence of operations any number of ti...
# (i, j) 1 <= i < j <= n # Add ai to aj def solvable(a, b): seen_neg = False seen_pos = False for ai, bi in zip(a, b): if ai < bi: if seen_pos: pass else: return False elif ai > bi: if seen_neg: pass ...
{ "input": [ "5\n3\n1 -1 0\n1 1 -2\n3\n0 1 1\n0 2 2\n2\n1 0\n1 41\n2\n-1 0\n-1 -41\n5\n0 1 -1 1 -1\n1 1 -1 1 -1\n", "5\n3\n1 -1 0\n1 1 -2\n3\n0 1 1\n0 2 2\n2\n1 -1\n1 41\n2\n-1 0\n-1 -41\n5\n0 1 -1 1 -1\n1 1 -1 1 -1\n", "5\n3\n1 -1 0\n1 1 -2\n3\n0 1 1\n0 2 2\n2\n1 -1\n2 41\n2\n-1 0\n-1 -38\n5\n0 1 -1 1 -1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once again, Boris needs the help of Anton in creating a task. This time Anton needs to solve the following problem: There are two arrays of integers a and b of length n. It turned ou...
1353_E. K-periodic Garland_10219
You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change i...
for _ in range(int(input())): n,k = map(int,input().split()) l = list(map(int,list(input()))) prf = [] dp = [] ans = 0 for i in range(n): ans += l[i] prf.append(ans) dp.append(0) # print(prf) for j in range(1,n): if j<k: dp[n-1-j] = min(prf[-1]-prf[n-1-j]+(l[n-1-j]^0),prf[-1]-prf[n-j-1]+(l[n-1-j]^1))...
{ "input": [ "6\n9 2\n010001010\n9 3\n111100000\n7 4\n1111111\n10 3\n1001110101\n1 1\n1\n1 1\n0\n", "6\n9 2\n010001010\n9 3\n111100000\n7 4\n1111111\n10 3\n1001110101\n1 1\n0\n1 1\n0\n", "6\n9 2\n010001010\n9 5\n111100000\n7 4\n1111111\n10 3\n1001110101\n1 1\n0\n1 1\n0\n", "6\n9 2\n011001010\n9 3\n111...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is tur...
1373_D. Maximum Sum on Even Positions_10223
You are given an array a consisting of n integers. Indices of the array start from zero (i. e. the first element is a_0, the second one is a_1, and so on). You can reverse at most one subarray (continuous subsegment) of this array. Recall that the subarray of a with borders l and r is a[l; r] = a_l, a_{l + 1}, ..., a_...
import sys,random input=sys.stdin.readline T=int(input()) for _ in range(T): n=int(input()) A=list(map(int,input().split())) '''n=random.randint(1,10) A=[] for i in range(n): A.append(random.randint(1,10)) print(n) print(*A)''' B=[] C=[] for i in range(0,n,2): ...
{ "input": [ "4\n8\n1 7 3 4 7 6 2 9\n5\n1 2 1 2 1\n10\n7 8 4 5 7 6 8 9 7 3\n4\n3 1 2 1\n", "3\n5\n17 6 4 4 4\n7\n19 5 13 11 12 13 5\n1\n213567876\n", "4\n8\n1 7 3 4 8 6 2 9\n5\n1 2 1 2 1\n10\n7 8 4 5 7 6 8 9 7 3\n4\n3 1 2 1\n", "4\n8\n1 7 3 4 8 6 2 9\n5\n1 2 1 2 1\n10\n7 8 4 5 7 6 8 9 7 3\n4\n5 1 2 1\...
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 integers. Indices of the array start from zero (i. e. the first element is a_0, the second one is a_1, and so on). You can reverse at most on...
1396_C. Monster Invaders_10227
Ziota found a video game called "Monster Invaders". Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns. For the sake of simplicity, we only consider two different types of monsters and three different types of guns. Namely, the two types of monsters are: *...
import sys;input=sys.stdin.readline N, a, b, c, k = map(int, input().split()) X = list(map(int, input().split())) dp = [[10**18]*2 for _ in range(N+1)] dp[0][0] = 0 R = 10**18 for i in range(N): dp[i+1][0] = min(dp[i][0] + a*X[i]+c, dp[i][1] + min(a*(X[i]+2), a+b)) dp[i+1][1] = dp[i][0] + min(a*(X[i]+2), a+b) +...
{ "input": [ "4 2 4 4 1\n4 5 1 2\n", "4 1 3 4 3\n3 2 5 1\n", "2 2 5 7 3\n4 5\n", "77 2 8 8 3\n7 9 3 6 2 7 8 4 4 1 8 6 1 7 6 3 4 6 1 1 6 5 6 6 4 8 7 5 10 6 9 2 1 2 4 5 1 3 8 2 2 7 3 8 8 4 8 10 5 1 6 8 1 3 8 6 8 4 10 7 10 5 3 8 6 6 8 2 2 3 8 4 10 7 6 5 2\n", "100 4 8 9 1\n1 8 1 8 7 8 1 8 10 4 7 7 3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ziota found a video game called "Monster Invaders". Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns. For the sake of simp...
1438_E. Yurii Can Do Everything_10234
Yurii is sure he can do everything. Can he solve this task, though? He has an array a consisting of n positive integers. Let's call a subarray a[l...r] good if the following conditions are simultaneously satisfied: * l+1 ≤ r-1, i. e. the subarray has length at least 3; * (a_l ⊕ a_r) = (a_{l+1}+a_{l+2}+…+a_{r-2}...
from sys import * input = stdin.readline def solve(n, a, t): ans = 0 for i in range(n): sum = 0 high1 = a[i].bit_length()-1 for j in range(i+1, n-1): high2 = a[j+1].bit_length()-1 sum += a[j] if(sum >= (1<<(high1+1))): break if((a[i]^a[j+1]) == sum and (t == 0 or high1 != high2)): ans += 1 ...
{ "input": [ "10\n997230370 58052053 240970544 715275815 250707702 156801523 44100666 64791577 43523002 480196854\n", "8\n3 1 2 3 1 2 3 15\n", "10\n512647258 29130422 386945218 423464437 1022947864 244293674 452 216 953 822\n", "10\n252370317 29130422 386945218 423464437 1022947864 244293674 452 216 9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Yurii is sure he can do everything. Can he solve this task, though? He has an array a consisting of n positive integers. Let's call a subarray a[l...r] good if the following conditio...
1463_D. Pairs_10238
You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements ...
from collections import deque def solve(): n = int(input()) a = list(map(int, input().split())) In_a = [False]*(2*n + 1) for v in a: In_a[v] = True b = [] for v in range(2*n, 0, -1): if In_a[v] == False: b.append(v) def possible(K): tmpa = a[:K] ...
{ "input": [ "3\n1\n1\n5\n1 4 5 9 10\n2\n3 4\n", "3\n1\n1\n5\n1 4 5 9 10\n2\n1 4\n", "3\n1\n1\n5\n1 4 5 9 10\n2\n2 4\n", "3\n1\n1\n5\n1 4 5 7 10\n2\n1 4\n", "3\n1\n1\n5\n1 4 5 8 10\n2\n2 4\n", "3\n1\n1\n5\n1 2 5 9 10\n2\n2 4\n", "3\n1\n1\n5\n1 2 5 9 10\n2\n1 2\n", "3\n1\n1\n5\n1 4 6 9 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n -...
148_D. Bag of mice_10242
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chanc...
w,b = list( map(int, input().split()) ) p = [] for i in range(w+1): p.append([0]*(b+1)) for i in range(1,w+1): p[i][0] = 1 for i in range(1,w+1): for j in range(1,b+1): p[i][j] = i/(i+j) if j>=3: p[i][j] += (j/(i+j)) * ((j-1)/(i+j-1)) * ((j-2)/(i+j-2)) * p[i][j-3] if j>=2: ...
{ "input": [ "1 3\n", "5 5\n", "773 467\n", "50 372\n", "329 31\n", "155 534\n", "267 270\n", "459 487\n", "377 720\n", "0 1000\n", "713 65\n", "1000 483\n", "100 1\n", "67 420\n", "1000 0\n", "132 781\n", "10 583\n", "815 665\n", "691 417\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the prince...
1512_B. Almost Rectangle_10246
There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in ...
for _ in range(int(input())): n=int(input()) l=[] r1=-1 c1=-1 c2=-1 r2=-1 for i in range(n): s=input() for j in range(n): if(s[j]=="*"): if(r1==-1): r1=i c1=j else: r2=i ...
{ "input": [ "6\n4\n..*.\n....\n*...\n....\n2\n*.\n.*\n2\n.*\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n*...\n*...\n", "6\n4\n..*.\n....\n*...\n....\n2\n*.\n.*\n2\n*.\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n*...\n*...\n", "6\n4\n..*.\n.....
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a re...
166_C. Median_10252
A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define...
n, x = map(int, input().split()) a = list(map(int, input().split())) ret = 0 if x not in a: a.append(x) ret += 1 a.sort() while a[(len(a) + 1) // 2 - 1] != x: a.append(x) a.sort() ret += 1 print(ret)
{ "input": [ "3 4\n1 2 3\n", "3 10\n10 20 30\n", "100 37\n20 20 3 35 36 14 30 9 33 36 32 46 43 22 43 50 34 6 13 25 1 34 10 6 21 30 19 17 1 23 9 23 38 21 7 43 49 28 33 42 4 19 39 23 12 42 31 13 26 23 1 26 24 48 10 6 12 48 40 18 10 26 9 5 22 45 46 23 46 34 34 45 3 7 16 39 14 29 48 1 12 37 4 20 29 26 35 38 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered ...
208_B. Solitaire_10258
A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying ...
n = int(input()) arr = list(input().split()) def evl(c1, c2): return c1[0] == c2[0] or c1[1] == c2[1] vt = set() q = [arr[:]] while q: ar = q.pop() vt.add(''.join(ar)) if len(ar) > 3 and evl(ar[-1], ar[-4]): tmp = ar[:len(ar) - 1] tmp[-3] = ar[-1] if ''.join(tmp) not in vt: ...
{ "input": [ "2\n3S 2C\n", "4\n2S 2S 2C 2C\n", "21\nJS 5S 9S KH 9D JH 3S KH QH 5D TC 3S 5S 4H 4H 5S 7S AH 4S 3S 6S\n", "11\nJS KH JC JS 9S 9H 6H 7H JH AS AH\n", "51\n7C 6S 2S 6H 6S 4S 3S 9S 5S 4S 2S 9S 2S 3S 2S JS 2S 2S 9H 2S 9S 2S 3S 9S 4S 4S 9S 9S 2S 2S 3S 2S 6H 7S 3S 3H 6S 3S 2H 6S 3S 6H 7H 6S ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffl...
282_D. Yet Another Number Game_10267
Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and Bi...
from math import * n=int(input()) if n==3: li=list(map(int,input().split())) ans=0 flag=0 for i in li: ans^=i if ans==0: print("BitAryo") else: print("BitLGM") elif n==2: li=list(map(int,input().split())) li.sort() phi=(1+sqrt(5))/2 ch=[0]*(785) for i ...
{ "input": [ "2\n1 1\n", "3\n1 2 1\n", "2\n1 2\n", "3\n299 290 288\n", "3\n124 47 228\n", "2\n41 29\n", "2\n15 9\n", "3\n299 299 298\n", "3\n4 19 23\n", "1\n6\n", "2\n183 113\n", "3\n0 87 87\n", "3\n299 299 299\n", "2\n159 98\n", "2\n80 130\n", "3\n222 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausage...
305_C. Ivan and Powers of Two_10271
Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum o...
n = int(input()) a = list(map(int, input().split())) s = set() for i in a: j = i while j in s: s.remove(j) j += 1 s.add(j) print(max(s)-len(s)+1)
{ "input": [ "1\n3\n", "4\n0 1 1 1\n", "1\n2000000000\n", "26\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2\n", "1\n0\n", "1\n1\n", "1\n1646661570\n", "1\n2\n", "1\n4\n", "1\n110164340\n", "1\n8\n", "1\n7\n", "1\n127913041\n", "1\n12\n", "1\n130811826...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a pi...
353_B. Two Heaps_10277
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap. Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube fr...
from math import* from random import* n = int(input()) * 2 A = list(map(int, input().split())) amount = [0] * 101 B = [] for i in range(n): if amount[A[i]] < 2: amount[A[i]] += 1 B += [(A[i], i)] B.sort() x, y = [], [] for i in range(len(B)): if(i % 2 == 0): x.append(B[i][1]) else:...
{ "input": [ "1\n10 99\n", "2\n13 24 13 45\n", "10\n26 43 29 92 22 27 95 56 72 55 93 51 91 30 70 77 32 69 87 98\n", "50\n49 13 81 20 73 62 19 49 65 95 32 84 24 96 51 57 53 83 40 44 26 65 78 80 92 87 87 95 56 46 22 44 69 80 41 61 97 92 58 53 42 78 53 19 47 36 25 77 65 81 14 61 38 99 27 58 67 37 67 80 7...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap. Valera d...
424_A. Squats_10285
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up. For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster e...
n = int(input()) s = str(input()) half = n//2 sit = s.count('x') stand = s.count('X') ans = max(sit, stand) - half t = ans while ans!=0: if sit>stand: temp = s.index('x') s = s[:temp] + 'X' + s[temp+1:] sit = sit-1 stand = stand +1 ans = ans -1 else: temp = s.inde...
{ "input": [ "2\nXX\n", "6\nxXXxXx\n", "4\nxxXx\n", "4\nXXXX\n", "200\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", "104\nxxXxXxxXXXxx...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up. For another ex...
448_D. Multiplication Table_10289
Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and ...
def main(): from math import sqrt m, n, k = map(int, input().split()) if n < m: n, m = m, n lo, hi = 1, k + 1 while lo + 1 < hi: mid = (lo + hi) // 2 t = mid - 1 v = min(int(sqrt(t)), m) tn, tm = (t - 1) // m, t // n vv = [t // i for i in range(tm + 1,...
{ "input": [ "2 3 4\n", "2 2 2\n", "1 10 5\n", "143 1735 246128\n", "3082 23173 68350097\n", "264 3 775\n", "3 48 30\n", "3119 3 7305\n", "302 39 4623\n", "41 246624 2596292\n", "12 28 127\n", "357 184324 28748161\n", "20 2222 11312\n", "48 33111 5809\n", "3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champi...
493_A. Vasya and Football_10295
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card. Vasya is watching a recorded football match now and makes notes of all the fouls tha...
home=input() away=input() n=int(input()) yellows=set() reds=set() s={'h':home,'a':away} while n: x=input().split() time=int(x[0]) team=x[1] shirt=int(x[2]) card=x[3] if card=='y': if (shirt,team) in yellows and (shirt,team) not in reds:#2nd time print(s[team],shirt,time) ...
{ "input": [ "MC\nCSKA\n9\n28 a 3 y\n62 h 25 y\n66 h 42 y\n70 h 25 y\n77 a 4 y\n79 a 25 y\n82 h 42 r\n89 h 16 y\n90 a 13 r\n", "TANC\nXNCOR\n2\n15 h 27 r\n28 h 27 r\n", "ASGDFJH\nAHGRSDXGER\n3\n23 h 15 r\n68 h 15 y\n79 h 15 y\n", "ASFSHDSG\nADGYRTJNG\n5\n1 h 1 y\n2 h 1 y\n3 h 1 y\n4 h 1 r\n5 h 1 y\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the se...
516_E. Drazil and His Happy Friends_10299
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and...
n, m = (int(x) for x in input().split()) happy_boys = [0 for _ in range(n)] happy_girls = [0 for _ in range(m)] boys = [int(x) for x in input().split()] girls = [int(x) for x in input().split()] if boys[0]: for i in range(1, len(boys)): happy_boys[boys[i]] = 1 if girls[0]: for i in range(1, len(girls))...
{ "input": [ "2 3\n1 0\n1 1\n", "2 3\n0\n1 0\n", "2 4\n1 0\n1 2\n", "99999 100000\n2 514 415\n2 50216 61205\n", "3 5\n0\n5 4 3 2 1 0\n", "5 12\n0\n3 0 8 4\n", "98 49\n33 0 51 52 6 57 10 12 63 15 16 19 20 21 72 73 74 76 77 78 30 31 81 33 83 37 38 39 40 92 44 45 95 97\n15 4 5 7 9 11 13 17 18...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are n boys a...
570_B. Simple Game_10305
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen w...
n,m = map(int, input().split()) p = [m+int(m<n),m-int(m>1)] print(p[n-m<=m-1])
{ "input": [ "3 1\n", "4 3\n", "692501734 346250868\n", "616075979 207073797\n", "296647497 148323750\n", "401241775 170032078\n", "4 2\n", "541904957 459371829\n", "5 3\n", "3 2\n", "971573366 216791157\n", "5 1\n", "12412523 125123\n", "619189108 309594555\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose numb...
613_C. Necklace_10310
Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward). ...
from fractions import gcd from functools import reduce LETTERS = 'abcdefghijklmnopqrstuvwxyz' def necklace_odd(a): oi = next(i for i, ai in enumerate(a) if ai%2) o = a[oi] g = reduce(gcd, a) s = [LETTERS[i] * (a[i]//(2*g)) for i in range(len(a)) if i != oi] return g, (''.join(s) + (LETTERS[oi]*(...
{ "input": [ "3\n4 2 1\n", "1\n4\n", "2\n1 1\n", "3\n6 6 9\n", "2\n2 2\n", "3\n4 2 1\n", "4\n1100 220 66 11\n", "1\n5\n", "1\n100000\n", "3\n1 2 4\n", "3\n33333 33333 33333\n", "15\n4400 2750 18150 8250 2200 4950 1100 3850 10450 2750 3850 9900 4400 10450 11000\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut ...
633_C. Spy Syndrome 2_10313
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique. For a given sentence, the ciphe...
import sys p = sys.stdin.read().split() n, t = int(p[0]), p[1][::-1] d = {q.lower(): q for q in p[3:]} k, s = 0, [] l = sorted(set(map(len, d))) while n: k -= 1 if len(l) + k < 0: k, n = s.pop() elif n >= l[k] and t[n - l[k]:n] in d: s.append((k, n)) n -= l[k] k = 0 print(*[d[t[n - l...
{ "input": [ "12\niherehtolleh\n5\nHI\nHo\nthere\nHeLLo\nhello\n", "30\nariksihsidlihcdnaehsetahgnisol\n10\nKira\nhates\nis\nhe\nlosing\ndeath\nchildish\nL\nand\nNote\n", "5\ncbaed\n6\nbc\nea\nab\ncd\nabc\nde\n", "11\naaaaaaaaaaa\n3\naaaa\naaaaa\naaaaaa\n", "3\naab\n2\naa\nbaa\n", "3\naab\n2\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar ...
686_A. Free Ice Cream_10317
After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer. At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the q...
x, y = input().split(" ") a = x,y a = list(a) for i in range(len(a)): a[i]=int(a[i]) count=0 num =a[1] for i in range(a[0]): z,f = input().split(" ") char=z inp=int(f) if char == '+': num = num + inp elif char == '-': num = num - inp if num < 0: count+=1 ...
{ "input": [ "5 7\n+ 5\n- 10\n- 20\n+ 40\n- 20\n", "5 17\n- 16\n- 2\n- 98\n+ 100\n- 98\n", "6 1000000000\n+ 1000000000\n+ 1000000000\n+ 1000000000\n+ 1000000000\n+ 1000000000\n+ 1000000000\n", "11 1000\n- 100\n+ 100\n+ 100\n+ 100\n+ 100\n- 100\n- 100\n- 100\n- 100\n- 100\n- 100\n", "5 12\n- 12\n+ ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer. At the start of the day they have x ice cream pa...
730_B. Minimum and Maximum_10322
This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use function fflush(stdout), in Java — System.out.flush(), in Pascal — flush(output) and in Python — sys.stdout.flush(). In this problem, you need to find maximal and minimal elements of an ar...
import sys T = int(input()) def compare(l,r,arr): print('?',a[l]+1,a[r]+1) sys.stdout.flush() res = input() if (res == '>'): return 1; else: return 0; while (T > 0): T -= 1 Max_a = [] Min_a = [] n = int(input()) a = range(n) for i in range(int(n/2)): if (compare(...
{ "input": [ "2\n2\n \n&gt;\n \n3\n \n=\n \n=\n ", "1\n1\n1000000000\n", "1\n1\n1\n", "1\n10\n1 2 1 2 1 3 1 3 1 2\n", "1\n4\n1 1 2 2\n", "2\n2\n1 2\n2\n2 1\n", "6\n3\n1 2 3\n3\n1 3 2\n3\n2 1 3\n3\n2 3 1\n3\n3 1 2\n3\n3 2 1\n", "2\n2\n2 1\n3\n1 1 1\n", "2\n6\n2 1 2 1 2 2\n3\n2 2 1\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use function fflush(stdout), in Java — System.out.flush(...
774_F. Pens And Days Of Week_10326
Stepan has n pens. Every day he uses them, and on the i-th day he uses the pen number i. On the (n + 1)-th day again he uses the pen number 1, on the (n + 2)-th — he uses the pen number 2 and so on. On every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the pen he uses t...
import sys def Min(x, y): if x > y: return y else: return x def Gcd(x, y): if x == 0: return y else: return Gcd(y % x, x) def Lcm(x, y): return x * y // Gcd(x, y) n = int(input()) a = [int(i) for i in input().split()] d = [int(0) for i in range(0, n)] ok = 0 cur...
{ "input": [ "5\n5 4 5 4 4\n", "3\n3 3 3\n", "2\n1000000000 1000000000\n", "3\n999999999 1000000000 1000000000\n", "4\n1000000000 1000000000 1000000000 1000000000\n", "8\n1000000000 999999999 1000000000 999999999 1000000000 999999999 999999999 999999999\n", "7\n10 10 10 10 10 10 10\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Stepan has n pens. Every day he uses them, and on the i-th day he uses the pen number i. On the (n + 1)-th day again he uses the pen number 1, on the (n + 2)-th — he uses the pen numb...
81_A. Plug-in_10332
Polycarp thinks about the meaning of life very often. He does this constantly, even when typing in the editor. Every time he starts brooding he can no longer fully concentrate and repeatedly presses the keys that need to be pressed only once. For example, instead of the phrase "how are you" he can type "hhoow aaaare yy...
from collections import deque def plugin(s): # stack for characters to check string_stack = deque() # input string in a list arr_s = [c for c in s] # for each character, append to stack. if for c in arr_s: string_stack.append(c) if len(string_stack) > 1: if string...
{ "input": [ "hhoowaaaareyyoouu\n", "reallazy\n", "abacabaabacabaa\n", "xraccabccbry\n", "abb\n", "a\n", "aab\n", "babbbbabbabbbababbabbbbbbabaabaababaaabbbbbabbbbaaaaabbaaabbaabaabbbbabbbababbabaaabbababaaababbbaaa\n", "ab\n", "babbbbbababa\n", "gfj\n", "b\n", "aba...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp thinks about the meaning of life very often. He does this constantly, even when typing in the editor. Every time he starts brooding he can no longer fully concentrate and rep...
845_D. Driving Test_10336
Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types. * speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign (cancel the action of the previous sign of this type); * overtake is allowed: this sign means th...
n = int(input()) events = [] for i in range(n): events.append(list(map(lambda s: int(s), input().split()))) max_speeds = [] overtaking_forbidden = 0 current_speed = 0 ignores = 0 for event in events: if event[0] == 1: current_speed = event[1] while max_speeds and current_speed > max_speeds...
{ "input": [ "7\n1 20\n2\n6\n4\n6\n6\n2\n", "11\n1 100\n3 70\n4\n2\n3 120\n5\n3 120\n6\n1 150\n4\n3 300\n", "5\n1 100\n3 200\n2\n4\n5\n", "10\n1 37\n6\n5\n2\n5\n6\n5\n2\n4\n2\n", "10\n1 5\n4\n5\n4\n1 21\n6\n1 10\n1 1\n2\n3 1\n", "1\n1 100\n", "2\n1 100\n2\n", "10\n1 2\n4\n5\n4\n1 21\n6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types. * speed limit: this sign comes with a positive integer number — ma...
892_A. Greed_10341
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai ≤ bi). Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not! Input The first line of the input contains one integer n (2 ≤ n ≤ 100 000) — number of cola...
import sys sys.setrecursionlimit(100000000) # def input(): return sys.stdin.readline()[:-1] def iin(): return int(input()) def impin(): return map(int, input().split()) def irrin(): return [int(x) for x in input().split()] def imrin(n): return [int(input()) for _ in range(n)] n = iin() arr = irrin() brr = irrin() brr...
{ "input": [ "4\n4 1 0 3\n5 2 2 3\n", "3\n6 8 9\n6 10 12\n", "5\n0 0 5 0 0\n1 1 8 10 5\n", "2\n3 5\n3 6\n", "4\n1 0 0 0\n2 0 0 0\n", "2\n2 5\n2 5\n", "2\n0 0\n1 2\n", "5\n32 4 22 40 26\n39 20 36 98 44\n", "6\n18 25 3 10 13 37\n38 73 19 35 24 37\n", "2\n1000 1008\n10000 2352\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai ≤ bi). Jafar has decided to pour all remaining cola into just ...
914_A. Perfect Squares_10345
Given an array a1, a2, ..., an of n integers, find the largest number in the array that is not a perfect square. A number x is said to be a perfect square if there exists an integer y such that x = y2. Input The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array. The second ...
n = int(input()) a = [int(i) for i in input().split()] p = [] i = 0 while(True): p.append(i*i) i += 1 if(p[-1] > 1000000): break m = -99999999 for i in a: if i not in p: m = max(i,m) print(m)
{ "input": [ "2\n4 2\n", "8\n1 2 4 8 16 32 64 576\n", "2\n370881 659345\n", "5\n804610 765625 2916 381050 93025\n", "1\n-1000000\n", "2\n-1 0\n", "2\n99999 3\n", "53\n280988 756430 -515570 -248578 170649 -21608 642677 216770 827291 589500 940901 216097 -118956 -919104 -319264 -761585 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given an array a1, a2, ..., an of n integers, find the largest number in the array that is not a perfect square. A number x is said to be a perfect square if there exists an integer ...
936_C. Lock Puzzle_10349
Welcome to another task about breaking the code lock! Explorers Whitfield and Martin came across an unusual safe, inside of which, according to rumors, there are untold riches, among which one can find the solution of the problem of discrete logarithm! Of course, there is a code lock is installed on the safe. The lock...
n = int(input()) t = input()[:n] s = input()[:n] ops =[] def shift(k, cur): if k == 0: return cur return cur[:-k-1:-1] + cur [:-k] def move_to_front(k, curst): if k == n-1: ops.append(1) curst = curst[-1] +curst [:-1] else: ops.append(n-1) ops.append(k) ...
{ "input": [ "6\nabacbb\nbabcba\n", "3\naba\nbba\n", "49\nssizfrtawiuefcgtrrapgoivdxmmipwvdtqggsczdipnkzppi\npqzrmpifgttneasigivkrouigpdivczigcxdsmtwpzpwfsadr\n", "50\nzyzxzyzwzyzxzyzvzyzxzyzwzyzxzyzuzyzxzyzwzyzxzyzvzy\nwxzyzzzyzyvzzvyzxxzyxwzuzzzzzyzyzzxyyzzzywzxzzyzyy\n", "50\nabbabaabbaababbaba...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Welcome to another task about breaking the code lock! Explorers Whitfield and Martin came across an unusual safe, inside of which, according to rumors, there are untold riches, among ...
962_A. Equator_10353
Polycarp has created his own training plan to prepare for the programming contests. He will train for n days, all days are numbered from 1 to n, beginning from the first. On the i-th day Polycarp will necessarily solve a_i problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first...
n=int(input()) l=list(map(int,input().split())) s=sum(l)/2 #print(s) i=0; curr=0 while(curr<s): curr+=l[i] i+=1 print(i)
{ "input": [ "6\n2 2 2 2 2 2\n", "4\n1 3 2 1\n", "6\n1 1 1 1 1 2\n", "2\n1 3\n", "6\n3 3 3 2 4 4\n", "2\n2 3\n", "6\n7 3 10 7 3 11\n", "5\n1 1 1 1 1\n", "3\n2 1 2\n", "4\n1 8 7 3\n", "4\n3 2 3 3\n", "2\n5 6\n", "5\n1 1 1 1 3\n", "2\n9 10\n", "5\n2 2 2 4 3\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp has created his own training plan to prepare for the programming contests. He will train for n days, all days are numbered from 1 to n, beginning from the first. On the i-th...
98_B. Help King_10357
This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive. Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, ...
from fractions import * n,L=int(input()),0 while (n%2==0):n,L=n//2,L+1 if (n==1):print('%d/1'%L) else: s,t=1,1 for i in range(n): t=t*2%n s*=2 if (t==1): m=i+1 break r,t,i,ans=s,s*n,L,0 while (r>1): i,t=i+1,t//2 if (r-t>0): r,an...
{ "input": [ "2\n", "3\n", "4\n", "9556\n", "9859\n", "721\n", "9949\n", "659\n", "9992\n", "1108\n", "5\n", "9941\n", "6212\n", "64\n", "46\n", "563\n", "9993\n", "915\n", "43\n", "96\n", "8947\n", "9991\n", "1649\n", "40...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially f...
p02605 M-SOLUTIONS Programming Contest 2020 - Air Safety_10371
M-kun is a brilliant air traffic controller. On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude. Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the d...
def main(): import sys input = sys.stdin.readline N = int(input()) XYU = [list(map(str,input().split())) for _ in range(N)] from collections import deque INF = float('inf') UD = [0] * 200001 LR = [0] * 200001 UR = [0] * 400001 DL = [0] * 400001 DR = [0] * 400001 UL = [0] * 400001 for X,Y,u ...
{ "input": [ "8\n168 224 U\n130 175 R\n111 198 D\n121 188 L\n201 116 U\n112 121 R\n145 239 D\n185 107 L", "4\n20 30 U\n30 20 R\n20 10 D\n10 20 L", "2\n11 1 U\n11 47 D", "8\n285 224 U\n130 175 R\n111 198 D\n121 188 L\n201 116 U\n112 121 R\n145 239 D\n185 107 L", "4\n20 30 U\n30 20 R\n20 10 D\n19 20...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: M-kun is a brilliant air traffic controller. On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude. Each of the airplanes flies at...
p02736 AtCoder Grand Contest 043 - 123 Triangle_10374
Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3. Let x_{i,j} defined as follows: * x_{1,j} := a_j \quad (1 \leq j \leq N) * x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i) Find x_{N,1}. Constraints * 2 \leq N \leq 10^6 * a_i = 1,2,3 (1 \leq...
N = int(input()) a = input() def myc(n): ret = 0 while n % 2 == 0: ret += 1 n //= 2 return ret def func(c): odd = 0 count = 0 if a[0] == c: count += 1 if a[-1] == c: count += 1 for i in range(1, N // 2): odd += myc(N-i) - myc(i) if a[i] =...
{ "input": [ "10\n2311312312", "4\n1231", "4\n2332", "4\n3121", "4\n1311", "10\n1806305381", "4\n2465", "10\n2192114600", "4\n2113", "10\n3104206232", "4\n2121", "4\n2010", "10\n2763272837", "10\n1238013900", "10\n1665759416", "10\n2224468077", "4\n1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3. Let x_{i,j} defined as follows: * x_{1,j} := a_j \quad (1 \leq j \leq N) * x_{i,j} := | x_{i-1,j} ...
p03005 diverta 2019 Programming Contest 2 - Ball Distribution_10379
Takahashi is distributing N balls to K persons. If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls? Constraints * 1 \leq K \leq N \leq 100 * All values in input are intege...
a,b=map(int,input().split()) c=a-b if b!= 1: print(c) else: print(0)
{ "input": [ "8 5", "3 1", "3 2", "9 5", "4 2", "2 2", "14 5", "2 3", "0 3", "14 3", "1 3", "14 6", "-1 3", "-2 3", "4 12", "0 9", "0 13", "0 6", "20 2", "6 5", "9 2", "25 6", "-1 6", "1 11", "9 3", "-1 13", "1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Takahashi is distributing N balls to K persons. If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the p...
p03145 AtCoder Beginner Contest 116 - Right Triangle_10383
There is a right triangle ABC with ∠ABC=90°. Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC. It is guaranteed that the area of the triangle ABC is an integer. Constraints * 1 \leq |AB|,|BC|,|CA| \leq 100 * All values in input are integers. * The area of the triangl...
a,b,c=map(int,input().split()) print(str(int(0.5*a*b)))
{ "input": [ "3 4 5", "45 28 53", "5 12 13", "3 3 5", "45 38 53", "5 18 13", "0 3 5", "45 30 53", "5 24 13", "45 49 53", "5 13 13", "18 49 53", "1 10 1", "1 6 13", "18 97 37", "32 97 63", "1 2 7", "-1 1 0", "32 69 101", "-2 2 0", "32 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a right triangle ABC with ∠ABC=90°. Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC. It is guaranteed that the area of the...