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
505_B. Mr. Kitayuta's Colorful Graph_2838
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers — ui...
def build_graph(): line1 = input().strip().split() n = int(line1[0]) m = int(line1[1]) graph = {} for _ in range(m): line = input().strip().split() u = int(line[0]) v = int(line[1]) c = int(line[2]) if c not in graph: graph[c] = {j: [] for j in ran...
{ "input": [ "5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4\n", "4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4\n", "2 1\n1 2 1\n1\n1 2\n", "2 3\n1 2 3\n1 2 2\n1 2 1\n1\n1 2\n", "2 5\n1 2 1\n1 2 2\n1 2 3\n1 2 4\n1 2 5\n1\n1 2\n", "5 7\n1 1 1\n2 5 1\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, co...
554_C. Kyoya and Colored Balls_2844
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i...
from sys import stdin c = [[0]*1002 for _ in range(0,1002)] MOD = int(1e9+7) for i in range(0,1002): c[i][0] = c[i][i] = 1 for j in range(0,i): c[i][j] = (c[i-1][j-1] + c[i-1][j])%MOD r = map(int,stdin.read().split()) n = next(r) ans = 1 sum = 0 for _ in range(0,n): x = next(r) ans = (ans * ...
{ "input": [ "3\n2\n2\n1\n", "4\n1\n2\n3\n4\n", "25\n35\n43\n38\n33\n47\n44\n40\n36\n41\n42\n33\n30\n49\n42\n62\n39\n40\n35\n43\n31\n42\n46\n42\n34\n33\n", "47\n20\n21\n16\n18\n24\n20\n25\n13\n20\n22\n26\n24\n17\n18\n21\n22\n21\n23\n17\n15\n24\n19\n18\n21\n20\n19\n26\n25\n20\n17\n17\n17\n26\n32\n20\n2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls...
580_B. Kefa and Company_2848
Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend t...
n, d = map(int, input().split()) v = [] for i in range(0, n): a, b = map(int, input().split()) v.append((a, b)) v = sorted(v) lo = 0 totalFriendship = v[0][1] bestFriendship = totalFriendship for i in range(1, n): while v[i][0] - v[lo][0] >= d: totalFriendship -= v[lo][1] lo += 1 to...
{ "input": [ "5 100\n0 7\n11 32\n99 10\n46 8\n87 54\n", "4 5\n75 5\n0 100\n150 20\n75 1\n", "5 9\n0 98\n2 1000000000\n8 1000000000\n5 999999999\n3 989898989\n", "4 1\n2 4\n2 2\n3 3\n3 3\n", "3 1\n801 10101\n802 134509124\n801 1\n", "5 6\n5 11\n10 11\n11 11\n12 11\n100 1\n", "7 6\n5 11\n9 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each...
602_A. Two Bases_2852
After seeing the "ALL YOUR BASE ARE BELONG TO US" meme for the first time, numbers X and Y realised that they have different bases, which complicated their relations. You're given a number X represented in base bx and a number Y represented in base by. Compare those two numbers. Input The first line of the input con...
# print ("Input digits and base for number one") n1, b1 = (int(x) for x in input().split()) # print ("Input all the digits") d1 = list(int(x) for x in input().split()) d1.reverse() # print ("Input digits and base for number two") n2, b2 = (int(x) for x in input().split()) # print ("Input all the digits") d2 = list(int...
{ "input": [ "7 16\n15 15 4 0 0 7 10\n7 9\n4 8 0 3 1 5 0\n", "3 3\n1 0 2\n2 5\n2 4\n", "6 2\n1 0 1 1 1 1\n2 10\n4 7\n", "8 18\n1 1 4 15 7 4 9 3\n8 17\n1 10 2 10 3 11 14 10\n", "4 7\n3 0 6 6\n3 11\n7 10 10\n", "2 24\n20 9\n10 23\n21 10 15 11 6 8 20 16 14 11\n", "10 40\n39 39 39 39 39 39 39 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After seeing the "ALL YOUR BASE ARE BELONG TO US" meme for the first time, numbers X and Y realised that they have different bases, which complicated their relations. You're given a ...
673_A. Bear and Game_2858
Bear Limak likes watching sports on TV. He is going to watch a game today. The game lasts 90 minutes and there are no breaks. Each minute can be either interesting or boring. If 15 consecutive minutes are boring then Limak immediately turns TV off. You know that there will be n interesting minutes t1, t2, ..., tn. Yo...
n=int(input()) tl = list(map(int, input().split())) time=cnt=0 while cnt <15 and time<90: time+=1 if tl.count(time)>0: cnt=0 else: cnt+=1 print(time)
{ "input": [ "3\n7 20 88\n", "9\n15 20 30 40 50 60 70 80 90\n", "9\n16 20 30 40 50 60 70 80 90\n", "2\n1 18\n", "9\n6 20 27 28 40 53 59 70 85\n", "8\n10 20 30 40 50 60 70 80\n", "6\n13 17 32 47 60 66\n", "1\n1\n", "8\n5 17 20 35 42 53 67 76\n", "1\n90\n", "6\n14 29 43 59 70...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bear Limak likes watching sports on TV. He is going to watch a game today. The game lasts 90 minutes and there are no breaks. Each minute can be either interesting or boring. If 15 c...
698_B. Fix a Tree_2862
A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is cons...
from collections import defaultdict from sys import stdin,setrecursionlimit setrecursionlimit(10**6) import threading def f(a): n=len(a) a=list(map(lambda s:s-1,a)) root=None for i in range(len(a)): if a[i]==i: root=i vis=[0]*(n) traitors=[] for i in range(0,n): cycle=-1 cur=i move=set() while vis...
{ "input": [ "5\n3 2 2 5 3\n", "8\n2 3 5 4 1 6 6 7\n", "4\n2 3 3 4\n", "3\n2 1 1\n", "6\n6 2 6 2 4 2\n", "2\n1 2\n", "8\n2 1 2 2 6 5 6 6\n", "18\n2 3 4 5 2 7 8 9 10 7 11 12 14 15 13 17 18 18\n", "7\n1 2 3 4 5 6 7\n", "7\n4 3 2 6 3 5 2\n", "7\n7 5 3 1 2 1 5\n", "7\n1 6 4...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. O...
719_B. Anatoly and Cockroaches_2866
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room. Anatoly just made all his cockroaches to form a single line. As he is a perfectioni...
#http://codeforces.com/problemset/problem/719/B #solved n = int(input()) s = input() red_black = True swap_red = {"b": 0, "r": 0} swap_black = {"b": 0, "r": 0} for c in s: if red_black is True and c != "b": swap_black["r"] += 1 elif red_black is False and c != "r": swap_black["b"] += 1 ...
{ "input": [ "5\nrbbrr\n", "5\nbbbbb\n", "3\nrbr\n", "166\nrbbbbbbbbbbbbrbrrbbrbbbrbbbbbbbbbbrbbbbbbrbbbrbbbbbrbbbbbbbrbbbbbbbrbbrbbbbbbbbrbbbbbbbbbbbbbbrrbbbrbbbbbbbbbbbbbbrbrbbbbbbbbbbbrbbbbbbbbbbbbbbrbbbbbbbbbbbbbbbbbbbbbb\n", "7\nrrbrbrb\n", "1\nr\n", "2\nbb\n", "18\nrrrrrrrrrrrrrr...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and re...
787_A. The Monster_2874
A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b, b + a, b + 2a, b + 3a, ... and Morty screams at times d, d + c, d + 2c, d + 3c, .... <image> The Monster will catch them if at any point they scream at the same tim...
import math R=lambda:list(map(int,input().split())) a,b=R() c,d=R() if abs(b-d)%math.gcd(a,c)!=0: print(-1) exit(0) while b != d: if b<d: b+=a else: d+=c print(b)
{ "input": [ "20 2\n9 19\n", "2 1\n16 12\n", "84 82\n38 6\n", "2 3\n2 2\n", "2 3\n4 99\n", "3 7\n3 6\n", "11 81\n49 7\n", "1 1\n1 100\n", "6 10\n12 14\n", "2 10\n6 20\n", "97 2\n99 100\n", "17 19\n44 75\n", "39 52\n88 78\n", "4 2\n4 4\n", "1 1\n1 1\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b, b + a, b + 2a, b + 3a, ... and...
808_E. Selling Souvenirs_2878
After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market. This morning, as us...
def f(): n, m = map(int, input().split()) l = list(tuple(map(int, input().split())) for _ in range(n)) l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) last, r = [0] * 4, 0 for i, (w, c) in enumerate(l): if m < w: break m -= w r += c last[w] =...
{ "input": [ "4 3\n3 10\n2 7\n2 8\n1 1\n", "2 2\n1 3\n2 2\n", "1 1\n2 1\n", "52 102\n3 199\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract touris...
833_A. The Meaningless Game_2881
<image> Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting. The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's s...
import sys,os,io from sys import stdin from math import log, gcd, ceil from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify from bisect import bisect_left , bisect_right import math def ii(): return int(input()) def li(): return list(map(int,input().split())) if(os.p...
{ "input": [ "6\n2 4\n75 45\n8 8\n16 16\n247 994\n1000000000 1000000\n", "1\n1062961 1031\n", "1\n3331 11095561\n", "1\n2 3\n", "3\n1 1\n8 27\n1000 1331\n", "1\n90 72\n", "1\n12004 18012002\n", "1\n3 10\n", "1\n16 8\n", "1\n3 1\n", "1\n31159 970883281\n", "1\n6 12\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: <image> Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting. The game consists of multiple rounds. Its rules are very simple: in each r...
878_B. Teams Formation_2887
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai. Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in ...
import sys from collections import deque input=sys.stdin.readline n,k,m=map(int,input().split()) a=list(map(int,input().split())) r=a[0] flag=0 for i in range(n): if r!=a[i]: flag=1 break if flag==0: print((m*n)%k) sys.exit() if k>n: print(m*n) sys.exit() curr=a[0] tmp=1 que=deque([(a[0],1)]) for i in...
{ "input": [ "4 2 5\n1 2 3 1\n", "1 9 10\n1\n", "3 2 10\n1 2 1\n", "10 5 1000000000\n1 1 1 2 2 1 2 2 2 2\n", "5 3 3\n1 2 2 1 1\n", "2 4 1\n1 1\n", "1 1000000000 1000000000\n100000\n", "5 3 2\n1 1 2 1 1\n", "5 2 4\n1 2 3 2 1\n", "1 10 10\n1\n", "2 4 1\n1 0\n", "5 3 2\n1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a part...
901_D. Weighting a Tree_2890
You are given a connected undirected graph with n vertices and m edges. The vertices are enumerated from 1 to n. You are given n integers c1, c2, ..., cn, each of them is between - n and n, inclusive. It is also guaranteed that the parity of cv equals the parity of degree of vertex v. The degree of a vertex is the n...
def divisors(M): d=[] i=1 while M>=i**2: if M%i==0: d.append(i) if i**2!=M: d.append(M//i) i=i+1 return d def popcount(x): x = x - ((x >> 1) & 0x55555555) x = (x & 0x33333333) + ((x >> 2) & 0x33333333) x = (x + (x >> 4)) & 0x0f0f0f0f ...
{ "input": [ "6 6\n3 5 5 5 1 5\n1 4\n3 2\n4 3\n4 5\n3 5\n5 6\n", "4 4\n4 4 2 4\n1 2\n2 3\n3 4\n4 1\n", "3 3\n2 2 2\n1 2\n2 3\n1 3\n", "4 3\n-1 0 2 1\n1 2\n2 3\n3 4\n", "2 1\n1 1\n2 1\n", "8 12\n-6 2 -3 2 6 -7 1 -5\n2 1\n5 6\n1 6\n1 4\n8 2\n2 7\n2 5\n8 3\n8 4\n3 7\n7 6\n3 1\n", "10 13\n3 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 connected undirected graph with n vertices and m edges. The vertices are enumerated from 1 to n. You are given n integers c1, c2, ..., cn, each of them is between -...
924_A. Mystical Mosaic_2894
There is a rectangular grid of n rows of m initially-white cells each. Arkady performed a certain number (possibly zero) of operations on it. In the i-th operation, a non-empty subset of rows Ri and a non-empty subset of columns Ci are chosen. For each row r in Ri and each column c in Ci, the intersection of row r and...
def read_data(): n, m = map(int, input().strip().split()) a = [] for i in range(n): line = list(input().strip()) for j in range(len(line)): if line[j] == ".": line[j] = 0 else: line[j] = 1 a.append(line) return n, m, a def ...
{ "input": [ "5 9\n........#\n#........\n..##.#...\n.......#.\n....#.#.#\n", "5 5\n..#..\n..#..\n#####\n..#..\n..#..\n", "5 8\n.#.#..#.\n.....#..\n.#.#..#.\n#.#....#\n.....#..\n", "9 5\n.....\n#....\n..#..\n.....\n.#..#\n...#.\n.#...\n....#\n.....\n", "4 10\n..#......#\n.....##...\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 rectangular grid of n rows of m initially-white cells each. Arkady performed a certain number (possibly zero) of operations on it. In the i-th operation, a non-empty subse...
952_C. Ravioli Sort_2898
Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with...
n = int(input()) l = [int(x) for x in input().split()] while len(l) > 1: if len([i for i in range(len(l)-1) if abs(l[i] - l[i+1]) > 1 ])>0: print ('NO') exit(0) l.remove(max(l)) print('YES')
{ "input": [ "3\n3 1 2\n", "3\n1 2 3\n", "4\n90 91 90 91\n", "4\n54 54 54 55\n", "10\n55 39 93 42 97 40 36 38 11 97\n", "3\n90 39 98\n", "5\n21 57 40 94 17\n", "8\n46 4 30 85 52 6 84 13\n", "6\n47 100 96 2 96 43\n", "4\n61 28 3 81\n", "5\n14 91 91 91 84\n", "3\n22 23 24...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you rea...
979_E. Kuro and Topological Parity_2901
Kuro has recently won the "Most intelligent cat ever" contest. The three friends then decided to go to Katie's home to celebrate Kuro's winning. After a big meal, they took a small break then started playing games. Kuro challenged Katie to create a game with only a white paper, a pencil, a pair of scissors and a lot o...
n,p=map(int,input().split()) nums=[0]+list(map(int,input().split())) mod=10**9+7 f=[[[[0]*2 for _ in range(2)] for _ in range(2)] for _ in range(n+1)] _2=[0]*(n+1) _2[0]=1 for i in range(1,n+1): _2[i]=(_2[i-1]<<1)%mod f[0][0][0][0]=1 if nums[1]!=0: f[1][1][0][1]+=1 if nums[1]!=1: f[1][1][1][0]+=1 fo...
{ "input": [ "3 1\n-1 0 1\n", "2 1\n1 0\n", "1 1\n-1\n", "9 1\n0 1 -1 -1 -1 -1 1 1 1\n", "16 0\n-1 0 0 1 0 0 0 0 -1 -1 -1 -1 1 1 0 1\n", "36 0\n-1 0 0 -1 -1 -1 0 -1 0 1 -1 -1 1 1 -1 1 0 0 1 -1 1 1 -1 0 0 1 1 1 -1 1 1 -1 1 1 1 -1\n", "37 0\n0 -1 0 0 0 -1 0 1 0 0 -1 0 -1 -1 0 1 1 0 -1 -1 -1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Kuro has recently won the "Most intelligent cat ever" contest. The three friends then decided to go to Katie's home to celebrate Kuro's winning. After a big meal, they took a small br...
999_F. Cards and Joy_2905
There are n players sitting at the card table. Each player has a favorite number. The favorite number of the j-th player is f_j. There are k ⋅ n cards on the table. Each card contains a single integer: the i-th card contains number c_i. Also, you are given a sequence h_1, h_2, ..., h_k. Its meaning will be explained b...
def solve(): n, k = map(int, input().split()) c = list(map(int, input().split())) f = list(map(int, input().split())) h = list(map(int, input().split())) cnt = {} for i in c: cnt[i] = cnt.get(i, 0) + 1 likecolor = {} for i in range(n): likecolor.setdefault(f[i], []).appen...
{ "input": [ "4 3\n1 3 2 8 5 5 8 2 2 8 5 2\n1 2 2 5\n2 6 7\n", "3 3\n9 9 9 9 9 9 9 9 9\n1 2 3\n1 2 3\n", "1 1\n1\n2\n1\n", "1 1\n1\n1\n100000\n", "50 1\n995 1815 941 1716 725 1098 747 627 1728 1007 34 1001 679 1742 22 1495 1299 1696 507 631 1971 775 1052 1665 1035 203 1564 1329 1592 1295 983 177 7...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n players sitting at the card table. Each player has a favorite number. The favorite number of the j-th player is f_j. There are k ⋅ n cards on the table. Each card contain...
p02537 ACL Beginner Contest - Flat Subsequence_2918
You are given a sequence A_1, A_2, ..., A_N and an integer K. Print the maximum possible length of a sequence B that satisfies the following conditions: * B is a (not necessarily continuous) subsequence of A. * For each pair of adjacents elements of B, the absolute difference of the elements is at most K. Constraint...
import typing class SegTree: def __init__(self, op: typing.Callable[[typing.Any, typing.Any], typing.Any], e: typing.Any, v: typing.Union[int, typing.List[typing.Any]]): self._op = op self._e = e if isinstance(v, int): v = [e] *...
{ "input": [ "10 3\n1\n5\n4\n3\n8\n6\n9\n7\n2\n4", "10 3\n1\n5\n4\n3\n8\n6\n9\n7\n4\n4", "10 3\n1\n5\n4\n3\n6\n6\n9\n7\n4\n4", "10 3\n0\n1\n4\n3\n6\n6\n9\n7\n4\n4", "10 3\n1\n5\n4\n3\n8\n6\n9\n7\n3\n4", "10 3\n1\n5\n4\n3\n8\n11\n9\n7\n3\n4", "10 1\n0\n1\n4\n0\n5\n9\n9\n7\n8\n4", "10 1\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a sequence A_1, A_2, ..., A_N and an integer K. Print the maximum possible length of a sequence B that satisfies the following conditions: * B is a (not necessarily co...
p02797 Keyence Programming Contest 2020 - Subarray Sum_2924
Given are three integers N, K, and S. Find a sequence A_1, A_2, ..., A_N of N integers between 1 and 10^9 (inclusive) that satisfies the condition below. We can prove that, under the conditions in Constraints, such a sequence always exists. * There are exactly K pairs (l, r) of integers such that 1 \leq l \leq r \leq...
n, k, s = map(int, input().split()) if s == 10**9: ans = [10**9]*k+[1]*(n-k) else: ans = [s]*k+[s+1]*(n-k) print(*ans)
{ "input": [ "5 3 100", "4 2 3", "5 3 000", "5 0 100", "4 1 3", "5 3 010", "4 0 3", "10 3 010", "8 0 3", "10 5 010", "8 1 3", "10 5 000", "10 1 3", "10 9 000", "10 2 3", "17 9 000", "10 0 3", "10 9 001", "7 0 3", "10 6 001", "7 0 6", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given are three integers N, K, and S. Find a sequence A_1, A_2, ..., A_N of N integers between 1 and 10^9 (inclusive) that satisfies the condition below. We can prove that, under the...
p02933 AtCoder Beginner Contest 138 - Red or Not_2928
You will be given an integer a and a string s consisting of lowercase English letters as input. Write a program that prints s if a is not less than 3200 and prints `red` if a is less than 3200. Constraints * 2800 \leq a < 5000 * s is a string of length between 1 and 10 (inclusive). * Each character of s is a lowerca...
a,s = int(input()),input() print(s if a>=3200 else "red")
{ "input": [ "4049\nred", "3199\npink", "3200\npink", "4049\nrdd", "3199\nknip", "3200\npinl", "4049\nqdd", "3200\npiml", "4049\nqcd", "3200\nlmip", "4049\nqdc", "3200\nkmip", "4049\nddq", "3200\nknip", "4049\nddr", "3200\nkniq", "4049\ndrd", "32...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You will be given an integer a and a string s consisting of lowercase English letters as input. Write a program that prints s if a is not less than 3200 and prints `red` if a is less...
p03069 Tenka1 Programmer Beginner Contest 2019 - Stones_2932
There are N stones arranged in a row. Every stone is painted white or black. A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is `.`, and the stone is black if the character is `#`. Takahashi wants to change the colors of some stones to black or white so t...
n = int(input()) s = input() black = 0 ans = white = s.count('.') for i in range(n): if s[i] == "#": black += 1 else: white -= 1 ans = min(ans, black+white) print(ans)
{ "input": [ "3\n.#", "5\n.##.", "9\n.........", "3\n#.#", "5\n.#.#", "3\n.##", "3\n##.", "5\n..##", "5\n#..#", "3\n.# ", "3\n##.", "3\n.##", "5\n.#.#", "5\n..##", "5\n#..#", "3\n.# " ], "output": [ "1", "2", "0", "1", "1\n", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N stones arranged in a row. Every stone is painted white or black. A string S represents the color of the stones. The i-th stone from the left is white if the i-th character...
p03212 AtCoder Beginner Contest 114 - 755_2936
You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there? Here, a Shichi-Go-San number is a positive integer that satisfies the following condition: * When the number is written in base ten, each of the digits `7`, `5` ...
n = int(input()) def bfs(s): if int(s) > n: return 0 return ('3' in s and '5' in s and '7' in s) + bfs(s+'3') + bfs(s+'5') + bfs(s+'7') print(bfs('0'))
{ "input": [ "575", "3600", "999999999", "60", "1134", "656396080", "1260308870", "251112438", "3669", "4753", "3412", "5825", "471776498", "377370744", "563", "35191132", "26435227", "635", "5689978", "7251994", "2751855", "58545...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there? Here, a Shichi-Go-San num...
p03361 AtCoder Beginner Contest 096 - Grid Repainting 2_2940
We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j). Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, ...
import sys h, w = map(int, input().split()) s = [list(input()) for i in range(h)] for i in range(h): for j in range(w): if s[i][j] == "#" and (i == 0 or s[i-1][j] == ".") and (i == h-1 or s[i+1][j] == ".") and (j == 0 or s[i][j-1] == ".") and (j == w-1 or s[i][j+1] == "."): print("No") ...
{ "input": [ "3 3\n.#.\n###\n.#.", "5 5\n.#.#\n.#.#.\n.#.#\n.#.#.\n.#.#", "11 11\n...#####...\n.##.....##.\n..##.##..#\n..##.##..#\n.........#\n...###...#\n.#########.\n.#.#.#.#.#.\n.#.#.#.##\n..##.#.##..\n.##..#..##.", "3 3\n.#.\n\n.#.", "3 5\n.#.\n###\n.#.", "5 5\n.#.#\n.#.#.\n-#.#\n.#.#.\n....
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j). Initially, all the ...
p03519 CODE FESTIVAL 2017 Elimination Tournament Round 1 (Parallel) - Paired Parentheses_2943
You are given two sequences a and b, both of length 2N. The i-th elements in a and b are a_i and b_i, respectively. Using these sequences, Snuke is doing the job of calculating the beauty of pairs of balanced sequences of parentheses (defined below) of length 2N. The beauty of a pair (s,t) is calculated as follows: * ...
import sys from heapq import heappop, heappush, heapify def main(): input = sys.stdin.readline N, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) ls = [b-a for a, b in zip(A, B)] q_m = [-v for v in ls[1:-1] if v < 0] q_p = [v f...
{ "input": [ "2 2\n1 1 7 3\n4 2 3 3\n2 4 6\n3 2 5", "7 7\n34 -20 -27 42 44 29 9 11 20 44 27 19 -31 -29\n46 -50 -11 20 28 46 12 13 33 -22 -48 -27 35 -17\n7 27 34\n12 -2 22\n4 -50 -12\n3 -32 15\n8 -7 23\n3 -30 11\n4 -2 23", "2 2\n1 1 6 3\n4 2 3 3\n2 4 6\n3 2 5", "7 7\n34 -20 -27 42 44 29 9 11 20 44 27 1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two sequences a and b, both of length 2N. The i-th elements in a and b are a_i and b_i, respectively. Using these sequences, Snuke is doing the job of calculating the be...
p03684 AtCoder Regular Contest 076 - Built?_2947
There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates. You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of road...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 mod = 10**9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float...
{ "input": [ "6\n8 3\n4 9\n12 19\n18 1\n13 5\n7 6", "3\n1 5\n3 9\n7 8", "6\n8 3\n4 9\n12 19\n2 1\n13 5\n7 6", "3\n0 5\n3 9\n7 8", "6\n8 3\n4 9\n12 19\n2 1\n15 5\n7 6", "3\n0 8\n3 9\n7 8", "6\n8 3\n4 9\n12 19\n2 2\n15 5\n7 6", "6\n8 3\n4 9\n12 19\n2 4\n12 5\n7 6", "6\n8 3\n4 5\n12 1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates. You can build a road between two towns a...
p03837 AtCoder Beginner Contest 051 - Candidates of No Shortest Paths_2951
You are given an undirected connected weighted graph with N vertices and M edges that contains neither self-loops nor double edges. The i-th (1≤i≤M) edge connects vertex a_i and vertex b_i with a distance of c_i. Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b...
N, M = map(int, input().split()) inf = 10**6 abc = [] d = [[inf]*N for i in range(N)] for i in range(M): a, b, c = map(int, input().split()) d[a-1][b-1] = c d[b-1][a-1] = c abc.append((a-1,b-1,c)) for k in range(N): for i in range(N): for j in range(N): d[i][j] = min(d[i][k]+d[k][j], d[i][j]) ans = 0 for i i...
{ "input": [ "3 3\n1 2 1\n1 3 1\n2 3 3", "3 2\n1 2 1\n2 3 1", "3 3\n1 2 2\n1 3 1\n2 3 3", "4 3\n1 2 1\n1 3 1\n2 3 3", "3 2\n1 2 2\n2 3 1", "3 2\n1 3 2\n2 3 1", "3 2\n1 3 2\n2 0 1", "3 2\n1 2 2\n2 0 1", "3 2\n2 2 2\n2 0 1", "3 2\n1 1 1\n2 3 1", "3 3\n1 2 2\n1 3 1\n2 3 0", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an undirected connected weighted graph with N vertices and M edges that contains neither self-loops nor double edges. The i-th (1≤i≤M) edge connects vertex a_i and verte...
p00090 Overlaps of Seals_2957
Stick n circular stickers with a radius of 1 on a square origami paper with a side length of 10. The stickers can be stacked. Create a program that reads the coordinates of the position where the stickers are to be attached and outputs the number of stickers at the place where the stickers overlap most on the origami p...
import itertools x=[0.]*100;y=[0.]*100 for e in iter(input,'0'): a=0 n=int(e) for i in range(n):x[i],y[i]=map(float,input().split(',')) for i,j in itertools.product(range(100),range(100)): c=0 for k in range(n): if pow(x[k]-(i/10),2)+pow(y[k]-(j/10),2)<=1.01:c+=1 ...
{ "input": [ "15\n3.14979,8.51743\n2.39506,3.84915\n2.68432,5.39095\n5.61904,9.16332\n7.85653,4.75593\n2.84021,5.41511\n1.79500,8.59211\n7.55389,8.17604\n4.70665,4.66125\n1.63470,4.42538\n7.34959,4.61981\n5.09003,8.11122\n5.24373,1.30066\n0.13517,1.83659\n7.57313,1.58150\n0", "15\n3.13979,8.51743\n2.39506,3.8...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Stick n circular stickers with a radius of 1 on a square origami paper with a side length of 10. The stickers can be stacked. Create a program that reads the coordinates of the positi...
p00222 Prime Quadruplet_2960
A set of four prime numbers arranged like (a, a + 2, a + 6, a + 8) is called a quadruplet prime number. Of the four prime numbers that make up a quadruplet prime, the largest number is called the size of the quadruplet prime. For example, the smallest prime quadruplet is a set of (5, 7, 11, 13), which is 13 in size. Th...
# AOJ 0222 Prime Quadruplet # Python3 2018.6.23 bal4u # From Wekipedia (https://en.wikipedia.org/wiki/Prime_quadruplet) # All prime quadruplets except {5, 7, 11, 13} are of the form # {30n + 11, 30n + 13, 30n + 17, 30n + 19} for some integer n. MAX = 899 diff = (0,3,3,21,22,13,7,39,7,73, \ 126,119,88,3,11,66,29,17,53,...
{ "input": [ "13\n14\n15\n16\n17\n18\n19\n20\n10000\n0", "13\n14\n15\n16\n17\n18\n0\n20\n10000\n0", "13\n14\n15\n16\n17\n27\n0\n20\n10000\n0", "13\n14\n27\n16\n17\n27\n0\n-1\n11000\n0", "13\n14\n15\n16\n17\n18\n19\n20\n00000\n0", "13\n26\n15\n16\n17\n27\n0\n20\n10000\n0", "13\n14\n15\n16\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A set of four prime numbers arranged like (a, a + 2, a + 6, a + 8) is called a quadruplet prime number. Of the four prime numbers that make up a quadruplet prime, the largest number i...
p00600 Computation of Minimum Length of Pipeline_2965
The Aizu Wakamatsu city office decided to lay a hot water pipeline covering the whole area of the city to heat houses. The pipeline starts from some hot springs and connects every district in the city. The pipeline can fork at a hot spring or a district, but no cycle is allowed. The city office wants to minimize the le...
while(1): s,d=[int(x) for x in input().split()] A=[999 for x in range(d)] if s==0: break for j in range(s): l=[int(x) for x in input().split()] for i in range(d): if l[i]!=0: A[i]=min(A[i], l[i]) B=[[999 for x in range(d)] for y in range(d)] for j ...
{ "input": [ "3 5\n12 8 25 19 23\n9 13 16 0 17\n20 14 16 10 22\n17 27 18 16\n9 7 0\n19 5\n21\n0 0", "3 5\n12 8 25 19 44\n9 13 16 0 17\n20 14 16 10 22\n17 27 18 16\n9 7 0\n19 5\n21\n0 0", "3 5\n12 15 70 19 44\n9 13 16 0 17\n20 14 16 10 22\n17 27 18 16\n9 7 0\n19 5\n21\n0 0", "3 5\n12 8 25 19 23\n9 13 1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Aizu Wakamatsu city office decided to lay a hot water pipeline covering the whole area of the city to heat houses. The pipeline starts from some hot springs and connects every dis...
p00736 How can I satisfy thee? Let me count the ways..._2969
Three-valued logic is a logic system that has, in addition to "true" and "false", "unknown" as a valid value. In the following, logical values "false", "unknown" and "true" are represented by 0, 1 and 2 respectively. Let "-" be a unary operator (i.e. a symbol representing one argument function) and let both "*" and "+...
class N: def __init__(self, a): self.a = a def __mul__(self, b): if self.a == 0: return N(0) elif self.a == 1: return N((b.a >= 1) * 1) else: return b def __add__(self, b): if self.a == 0: return b elif self.a ...
{ "input": [ "(P*Q)\n(--R+(P*Q))\n(P*-P)\n2\n1\n(-1+(((---P+Q)*(--Q+---R))*(-R+-P)))\n.", "(P*Q)\n(--R+(P*R))\n(P*-P)\n2\n1\n(-1+(((---P+Q)*(--Q+---R))*(-R+-P)))\n.", "(P*Q)\n(--R+(P*Q))\n(P*-P)\n1\n1\n(-1+(((---P+Q)*(--Q+---R))*(-R+-P)))\n.", "(P*Q)\n(--R+(P*R))\n(P*-P)\n1\n0\n(-1+(((---P+Q)*(--Q+---...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Three-valued logic is a logic system that has, in addition to "true" and "false", "unknown" as a valid value. In the following, logical values "false", "unknown" and "true" are repres...
p00876 Swimming Jam_2972
Despite urging requests of the townspeople, the municipal office cannot afford to improve many of the apparently deficient city amenities under this recession. The city swimming pool is one of the typical examples. It has only two swimming lanes. The Municipal Fitness Agency, under this circumstances, settled usage rul...
from collections import deque while 1: N = int(input()) if N == 0: break T = [0]*N; C = [0]*N que0 = deque() que1 = deque() P = [list(map(int, input().split())) for i in range(N)] P.sort() for i in range(N): t, c = P[i] que0.append((i, t)) T[i] = t ...
{ "input": [ "2\n10 30\n15 20\n2\n10 240\n15 160\n3\n2 6\n7 2\n8 2\n4\n2 4\n7 2\n8 2\n18 1\n0", "2\n10 30\n15 20\n2\n10 240\n15 160\n3\n2 6\n7 4\n8 2\n4\n2 4\n7 2\n8 2\n18 1\n0", "2\n5 30\n15 20\n2\n10 240\n15 160\n3\n2 6\n7 2\n8 2\n4\n2 4\n7 2\n8 2\n18 1\n0", "2\n10 30\n15 17\n2\n10 240\n15 160\n3\n2...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Despite urging requests of the townspeople, the municipal office cannot afford to improve many of the apparently deficient city amenities under this recession. The city swimming pool ...
p01139 Surrounding Area_2978
Surrounding Area Land fence English text is not available in this practice contest. Two real estate agents were boarding a passenger ship to the southern island. Blue sky, refreshing breeze ... The two enjoyed a voyage with other passengers. However, one day a tornado suddenly sank a passenger ship. The other passen...
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10**6) def dfs(x, y, symbol,land): if land[y][x] != '.': return ([land[y][x]] if not str(land[y][x]).isnumeric() else False), 0 land[y][x] = symbol count = 1 dxdy = [(1,0),(-1,0),(0,1),(0,-1)] owner_list = [] ...
{ "input": [ "10 10\n.....W....\n....W.W...\n...W...W..\n....W...W.\n.....W...W\n......W.W.\nBBB....W..\n..B..BBBBB\n..B..B....\n..B..B..W.\n5 3\n...B.\n...BB\n.....\n1 1\n.\n0 0", "10 10\n.....W....\n....W.W...\n...W...W..\n....W...W.\n.....W...W\n......W.W.\nBBB....W..\n..B..BBBBB\n..B..B....\n..B..B..W.\n5...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Surrounding Area Land fence English text is not available in this practice contest. Two real estate agents were boarding a passenger ship to the southern island. Blue sky, refreshi...
p01278 Voronoi Island_2981
Some of you know an old story of Voronoi Island. There were N liege lords and they are always involved in territorial disputes. The residents of the island were despaired of the disputes. One day, a clever lord proposed to stop the disputes and divide the island fairly. His idea was to divide the island such that any ...
import sys readline = sys.stdin.readline write = sys.stdout.write def cross3(O, A, B): ox, oy = O; ax, ay = A; bx, by = B return (ax - ox) * (by - oy) - (bx - ox) * (ay - oy) def cross_point(p0, p1, q0, q1): x0, y0 = p0; x1, y1 = p1 x2, y2 = q0; x3, y3 = q1 dx0 = x1 - x0; dy0 = y1 - y0 dx1 = x3 ...
{ "input": [ "3 3\n0 0\n8 0\n0 8\n2 2\n4 2\n2 4\n0 0", "3 3\n0 0\n8 -1\n0 8\n2 2\n4 2\n2 4\n0 0", "3 3\n0 0\n8 -1\n-1 8\n2 2\n4 2\n2 4\n0 0", "3 3\n0 0\n8 0\n0 8\n2 2\n4 0\n2 4\n0 0", "3 3\n0 0\n8 -1\n0 2\n2 2\n4 2\n2 4\n0 0", "3 3\n0 0\n8 0\n0 8\n2 2\n7 0\n2 4\n0 0", "3 3\n0 0\n14 0\n0 8\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Some of you know an old story of Voronoi Island. There were N liege lords and they are always involved in territorial disputes. The residents of the island were despaired of the dispu...
p01448 A Way to Invite Friends_2985
From tomorrow, the long-awaited summer vacation will begin. So I decided to invite my friends to go out to the sea. However, many of my friends are shy. They would hate it if they knew that too many people would come with them. Besides, many of my friends want to stand out. They will probably hate it if they know tha...
n = int(input()) l = [0 for i in range(n+2)] for i in range(n): [a,b] = list(map(int,input().split())) if a <= n+1: l[a] += 1 if b+1 <= n+1: l[b+1] -= 1 ans = 0 for i in range(n+2): if i != 0: l[i] = l[i-1] + l[i] if l[i] >= i-1: ans = i-1 print(ans)
{ "input": [ "5\n8 100001\n7 100001\n12 100001\n8 100001\n3 100001", "4\n2 5\n4 7\n2 4\n3 6", "6\n2 9\n4 8\n6 7\n6 6\n5 7\n2 100001", "5\n8 100001\n14 100001\n12 100001\n8 100001\n3 100001", "4\n2 5\n4 7\n2 4\n5 6", "6\n2 9\n4 8\n6 7\n6 6\n5 4\n2 100001", "4\n2 1\n4 7\n2 4\n5 6", "5\n8...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: From tomorrow, the long-awaited summer vacation will begin. So I decided to invite my friends to go out to the sea. However, many of my friends are shy. They would hate it if they kn...
p01754 Dinner_2989
In the spring of 2014, a student successfully passed the university and started living alone. The problem here is what to do with the supper. He decided to plan a supper for the next N days. He wants to maximize the total happiness he gets in N days. Of course, the more delicious or favorite you eat, the higher your h...
tmp = list(map(int,input().split())) n,p,q = tmp[0],tmp[1],tmp[2] cs = [] for i in range(n): cs.append(int(input())) ans = [sum(cs)] difs = [p * (q-i) - cs[i] for i in range(n)] difs.sort(reverse = True) for i in range(n): ans.append(ans[-1] + 2 * i * p + difs[i]) print(max(ans))
{ "input": [ "1 1 1\n2", "3 2 1\n3\n3\n3", "3 1 -1\n2\n-3\n2", "3 1 -10\n-10\n-10\n-10", "1 0 1\n2", "3 2 0\n3\n3\n3", "3 1 -10\n-19\n-10\n-10", "0 0 1\n2", "3 2 0\n5\n3\n3", "2 1 -10\n-19\n-10\n-10", "3 2 1\n5\n3\n3", "1 0 0\n4", "3 2 0\n1\n3\n6", "2 1 -10\n-35...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the spring of 2014, a student successfully passed the university and started living alone. The problem here is what to do with the supper. He decided to plan a supper for the next ...
p02031 Parentheses Number_2994
B: Parentheses Number problem Define the correct parenthesis string as follows: * The empty string is the correct parenthesis string * For the correct parenthesis string S, `(` S `)` is the correct parenthesis string * For correct parentheses S, T ST is the correct parentheses Here, the permutations are associate...
n = int(input()) a = list(map(int,input().split())) cnt = 0 ans = '' st = list() for x in a: while cnt < x: ans += '(' cnt += 1 st.append(cnt) if st[-1] == x: st.pop() ans += ')' else: ans = ':(' break print(ans)
{ "input": [ "2\n2 1", "2\n1 2", "2\n2 -2", "2\n2 -4", "2\n2 -3", "2\n2 -6", "2\n2 -11", "2\n2 -5", "2\n2 -10", "2\n2 -7", "2\n2 -13", "2\n2 -16", "2\n2 -25", "2\n2 -20", "2\n2 -14", "2\n2 -12", "2\n2 -15", "2\n2 -36", "2\n2 -17", "2\n2 -...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: B: Parentheses Number problem Define the correct parenthesis string as follows: * The empty string is the correct parenthesis string * For the correct parenthesis string S, `(` S `...
p02174 Power Subsequences_2997
Problem Define a function $ f $ that starts with $ 1 $ and takes a sequence of finite lengths as an argument as follows. $ \ displaystyle f (\\ {a_1, a_2, \ ldots, a_n \\}) = \ sum_ {i = 1} ^ n {a_i} ^ i $ Given a sequence of length $ N $, $ X = \\ {x_1, x_2, \ ldots, x_N \\} $, $ f (X) for all subsequences $ X'$ ex...
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()]...
{ "input": [ "5\n100 200 300 400 500", "3\n1 2 3", "5\n100 325 300 400 500", "3\n1 4 3", "5\n101 325 300 400 500", "3\n2 4 3", "5\n101 325 404 400 500", "3\n3 4 3", "5\n101 608 404 400 500", "3\n4 4 3", "5\n101 608 404 772 500", "3\n4 4 1", "5\n101 608 404 1494 500"...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem Define a function $ f $ that starts with $ 1 $ and takes a sequence of finite lengths as an argument as follows. $ \ displaystyle f (\\ {a_1, a_2, \ ldots, a_n \\}) = \ sum_...
p02317 Longest Increasing Subsequence_3000
For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : ...
N = int(input()) A = [int(input()) for _ in range(N)] from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort def solve(N,A): lis = [] for i in range(N): ind = bisect_left(lis,A[i]) if ind == len(lis): lis.append(A[i]) else: lis[ind...
{ "input": [ "5\n5\n1\n3\n2\n4", "3\n1\n1\n1", "5\n5\n1\n3\n2\n6", "3\n1\n1\n2", "3\n1\n1\n0", "5\n0\n2\n1\n2\n11", "5\n5\n1\n3\n1\n6", "3\n0\n1\n2", "5\n5\n1\n2\n1\n6", "3\n0\n2\n2", "5\n5\n1\n2\n0\n6", "5\n5\n1\n2\n-1\n6", "5\n5\n1\n2\n-1\n10", "5\n5\n1\n0\n-1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1,...
p02462 Multi-Map_3003
For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have equivalent keys. * insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$. * get($key$): Print all values with th...
# -*- coding: utf-8 -*- """ Dictionary - Multi-Map http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_8_D&lang=jp """ from bisect import insort, bisect_right, bisect_left class Multi_map: def __init__(self): self.mm = dict() self.lr = [] def insert(self, x, y): if x in self...
{ "input": [ "10\n0 blue 6\n0 red 1\n0 blue 4\n0 white 5\n1 red\n1 blue\n2 red\n1 black\n1 red\n3 w z", "10\n0 blue 6\n0 red 1\n0 blue 4\n0 white 5\n1 red\n2 blue\n2 red\n1 black\n1 red\n3 w z", "10\n0 blue 6\n0 red 1\n0 blue 4\n0 white 10\n1 red\n1 blue\n2 red\n1 black\n1 red\n3 w z", "10\n0 blue 6\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that multiple elements can have e...
1003_E. Tree Constructing_3013
You are given three integers n, d and k. Your task is to construct an undirected tree on n vertices with diameter d and degree of each vertex at most k, or say that it is impossible. An undirected tree is a connected undirected graph with n - 1 edges. Diameter of a tree is the maximum length of a simple path (a path...
def main(): n, d, k = map(int, input().split()) r, odd = divmod(d, 2) k -= 1 cap = d + 1 if k == 1 else 1 if k > 1: cap = 2 * (k ** (r + 1) - 1) // (k - 1) if odd else 1 + (k + 1) * (k ** r - 1) // (k - 1) if n == 1 or k < 1 < n - 1 or k == 1 and d != n - 1 or d >= n or k > 1 and not d <...
{ "input": [ "8 5 3\n", "6 3 3\n", "10 4 3\n", "6 2 3\n", "400000 3 199999\n", "20 6 3\n", "5 5 3\n", "2 3 1\n", "10 9 1\n", "7 5 2\n", "2 1 4\n", "3 2 1\n", "1 1 1\n", "2 2 1\n", "2 3 3\n", "6 4 2\n", "7 4 3\n", "2 10 1\n", "6 5 3\n", "1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given three integers n, d and k. Your task is to construct an undirected tree on n vertices with diameter d and degree of each vertex at most k, or say that it is impossible....
1070_D. Garbage Disposal_3020
Enough is enough. Too many times it happened that Vasya forgot to dispose of garbage and his apartment stank afterwards. Now he wants to create a garbage disposal plan and stick to it. For each of next n days Vasya knows a_i — number of units of garbage he will produce on the i-th day. Each unit of garbage must be dis...
import sys n, k = tuple(int(i) for i in sys.stdin.readline().split()) a = tuple(int(i) for i in sys.stdin.readline().split()) assert len(a) == n bags = 0 r = 0 for ai in a: leftovers = (r > 0) q, r = divmod(ai + r, k) if q == 0: if leftovers: bags += 1 r = 0 else: ...
{ "input": [ "3 2\n1 0 1\n", "4 4\n2 8 4 1\n", "3 2\n3 2 1\n", "5 1\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "3 10\n5 5 0\n", "1 1\n1\n", "4 4\n3 6 2 3\n", "1 10\n1\n", "185 2\n0 14 2 11 5 17 9 11 8 12 8 17 17 0 4 15 4 17 8 1 2 10 8 0 8 16 1 7 10 4 0 18 2 15 2 1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Enough is enough. Too many times it happened that Vasya forgot to dispose of garbage and his apartment stank afterwards. Now he wants to create a garbage disposal plan and stick to it...
1140_C. Playlist_3028
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i — its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to...
import heapq n,k=map(int,input().split()) b_l=[] for _ in range(n): t,b=map(int,input().split()) b_l.append([b,t]) b_l.sort(reverse=True) ans=0 sum_fg=0 h=[] heapq.heapify(h) for i in range(n): sum_fg+=b_l[i][1] heapq.heappush(h,b_l[i][1]) while(len(h)>k): g=heapq.heappop(h) sum_fg-=...
{ "input": [ "5 3\n12 31\n112 4\n100 100\n13 55\n55 50\n", "4 3\n4 7\n15 1\n3 6\n6 8\n", "5 1\n11 3\n10 2\n7 4\n6 1\n9 1\n", "8 1\n769 58454\n48687 67740\n51730 90641\n39410 27719\n63633 23862\n39951 40870\n56520 69058\n15454 9033\n", "2 1\n1 2\n2 1\n", "2 2\n100 100\n100 100\n", "10 1\n74...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i — its length and beauty respectively. The pleasure of listening to set of songs is...
1159_A. A pile of stones_3032
Vasya has a pile, that consists of some number of stones. n times he either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation of taking one stone from the pile. You are given n operations which Vasya has made. Find the minimal possible number of stones that can b...
''' t= input() lng= len(t) li1=[]; li2=[] for i in range(lng): if t[i]!='a': li1.append(t[i]) elif t[i]=='a': li2.append(i) aa= ''.join(li1) if len(aa)==0: print(t); exit(0) if len(aa)%2==1: print(':('); exit(0) if len(aa)%2==0: #print(123) l= int(len(aa)/2); lp= l#; pri...
{ "input": [ "2\n-+\n", "3\n---\n", "5\n++-++\n", "4\n++++\n", "4\n-+--\n", "2\n++\n", "98\n----+-+--+-+-++-++++---+-++++-+--++-++++++-+++--+++-+-----+-+--+-----+++++--+-++-+++-+-+++-+---++-\n", "3\n+-+\n", "100\n---+-----------+-++---+----------++++++++++++++++++++++++++++++++++++...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has a pile, that consists of some number of stones. n times he either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation o...
1181_A. Chunga-Changa_3036
Soon after the Chunga-Changa island was discovered, it started to acquire some forms of civilization and even market economy. A new currency arose, colloquially called "chizhik". One has to pay in chizhiks to buy a coconut now. Sasha and Masha are about to buy some coconuts which are sold at price z chizhiks per cocon...
x,y,z=map(int,input().split()) sum=(x+y)//z i1=x//z i2=y//z x=x%z y=y%z current=i1+i2 nu=0 if current==sum: print(sum,end=' ') print(nu) else: if x>y: h=x else: h=y current=sum-current current=current*z nu=current-h print(sum, end=' ') print(nu)
{ "input": [ "6 8 2\n", "5 4 3\n", "5000000000000 6000000000000 10000000000000\n", "2778787205 1763925790 48326734\n", "582426543750017 789023129080207 4395672028302\n", "0 0 1\n", "1000000000000000000 999999999999999999 3\n", "7 14 5\n", "1 0 1\n", "132940 345844 5\n", "20...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Soon after the Chunga-Changa island was discovered, it started to acquire some forms of civilization and even market economy. A new currency arose, colloquially called "chizhik". One ...
1216_E1. Numerical Sequence (easy version)_3042
The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one — from 1 to 2...
def isqrt(x): if x < 0: raise ValueError('square root not defined for negative numbers') n = int(x) if n == 0: return 0 a, b = divmod(n.bit_length(), 2) x = 2**(a+b) while True: y = (x + n//x)//2 if y >= x: return x x = y p = [0, 45, 9045, 1395...
{ "input": [ "4\n2132\n506\n999999999\n1000000000\n", "5\n1\n3\n20\n38\n56\n", "1\n755045\n", "19\n1\n10\n100\n1000\n10000\n100000\n1000000\n10000000\n100000000\n1000000000\n10000000000\n100000000000\n1000000000000\n10000000000000\n100000000000000\n1000000000000000\n10000000000000000\n1000000000000000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all c...
125_A. Measuring Lengths in Baden_3047
Lengths are measures in Baden in inches and feet. To a length from centimeters it is enough to know that an inch equals three centimeters in Baden and one foot contains 12 inches. You are given a length equal to n centimeters. Your task is to convert it to feet and inches so that the number of feet was maximum. The re...
n=int(input()) e=(n+1)//36 n-=36*e print(e,(n+1)//3)
{ "input": [ "42\n", "5\n", "4\n", "71\n", "9999\n", "35\n", "199\n", "10000\n", "10\n", "2\n", "9876\n", "8\n", "120\n", "3\n", "1233\n", "501\n", "24\n", "12\n", "13\n", "1000\n", "1\n", "100\n", "7\n", "94\n", "1886...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Lengths are measures in Baden in inches and feet. To a length from centimeters it is enough to know that an inch equals three centimeters in Baden and one foot contains 12 inches. Yo...
1281_C. Cut and Paste_3051
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i. There is one cursor. The cursor's location ℓ is denoted by an integer in \{0, …, |s|\}, with the following meaning: * If ℓ = 0, then the cursor...
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ MAX=10**9+7 def solve(): x = int(input()) s = [int(i) for i in input()] ans=len(s) c = "" l = 0 while l < x: l += 1 k=len(s) if k < x: #s += s[l:]*(s[l - 1]-1) for i in ran...
{ "input": [ "4\n5\n231\n7\n2323\n6\n333\n24\n133321333\n", "9\n1500\n1212\n1500\n1221\n1500\n122\n1500\n12121\n1500\n22\n1500\n1111112111111112\n1500\n1111111111221111111\n1500\n111111122\n1500\n11111121111121111111\n", "1\n1000000\n22\n", "1\n941759\n1223231111\n", "1\n1000000\n2211\n", "1\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i. There is o...
1301_C. Ayoub's function_3055
Ayoub thinks that he is a very smart person, so he created a function f(s), where s is a binary string (a string which contains only symbols "0" and "1"). The function f(s) is equal to the number of substrings in the string s that contains at least one symbol, that is equal to "1". More formally, f(s) is equal to the ...
'''input 5 3 1 3 2 3 3 4 0 5 2 ''' import sys read = lambda: list(map(int,sys.stdin.readline().strip().split())) # try: sigma = lambda x:x*(x+1)//2 for _ in range(int(input())): n,m = read() k = n-m total = sigma(n) # if m==0 or m==n: # print(total) # continue if k>m: e,f =...
{ "input": [ "5\n3 1\n3 2\n3 3\n4 0\n5 2\n", "5\n3 1\n3 2\n3 3\n0 0\n5 2\n", "5\n3 1\n3 2\n3 3\n0 0\n10 2\n", "5\n3 0\n3 2\n3 3\n0 0\n10 2\n", "5\n3 0\n3 2\n3 3\n4 0\n5 2\n", "5\n4 1\n3 2\n3 3\n0 0\n5 2\n", "5\n3 0\n3 2\n3 3\n0 0\n9 2\n", "5\n3 0\n3 2\n3 3\n4 0\n8 2\n", "5\n3 1\n3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ayoub thinks that he is a very smart person, so he created a function f(s), where s is a binary string (a string which contains only symbols "0" and "1"). The function f(s) is equal t...
1325_C. Ehab and Path-etic MEXs_3059
You are given a tree consisting of n nodes. You want to write some labels on the tree's edges such that the following conditions hold: * Every label is an integer between 0 and n-2 inclusive. * All the written labels are distinct. * The largest value among MEX(u,v) over all pairs of nodes (u,v) is as small as ...
n = int(input()) occ = [0 for i in range(n)] graph = [[0,0] for i in range(n-1)] for i in range(n-1): x, y = map(int,input().split()) occ[x-1]+=1 occ[y-1]+=1 graph[i][0] = x-1 graph[i][1] = y-1 fin = [-1 for i in range(n-1)] for i in range(n): if occ[i] >= 3 : var = 0 for j ...
{ "input": [ "6\n1 2\n1 3\n2 4\n2 5\n5 6\n", "3\n1 2\n1 3\n", "6\n1 2\n2 3\n3 4\n3 5\n3 6\n", "7\n6 4\n3 7\n3 5\n1 3\n4 2\n7 4\n", "7\n3 2\n5 7\n4 2\n7 6\n6 3\n1 6\n", "2\n1 2\n", "13\n1 4\n2 4\n3 4\n4 5\n5 11\n11 12\n12 13\n13 6\n6 7\n7 8\n7 9\n7 10\n", "4\n2 3\n2 4\n2 1\n", "5\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a tree consisting of n nodes. You want to write some labels on the tree's edges such that the following conditions hold: * Every label is an integer between 0 and n-2...
1344_B. Monopole Magnets_3063
A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exist since real magnets have two poles, but this is a programming contest problem, so we don't care. There is an n× m grid. Initially, you may place some north magnets and some south magnets into the cells. You are allowe...
# -*- coding:utf-8 -*- """ created by shuangquan.huang at 2020/6/16 """ import collections import time import os import sys import bisect import heapq from typing import List def check(row): start, end = 0, len(row) - 1 while start < len(row) and row[start] == 0: start += 1 if start >...
{ "input": [ "3 3\n.#.\n###\n##.\n", "4 5\n....#\n####.\n.###.\n.#...\n", "4 2\n##\n.#\n.#\n##\n", "3 5\n.....\n.....\n.....\n", "2 1\n.\n#\n", "19 19\n##############.....\n.#############.....\n.###########.......\n.###########.......\n.....#######.......\n.....###...........\n...................
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exist since real magnets have two poles, but this is a programming contest problem, so...
1366_B. Shuffle_3067
You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≤ c, d ≤ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is poss...
from __future__ import division, print_function # import threading # threading.stack_size(2**27) # import sys # sys.setrecursionlimit(10**7) # sys.stdin = open('inpy.txt', 'r') # sys.stdout = open('outpy.txt', 'w') from sys import stdin, stdout import bisect #c++ upperbound import math import heapq i_m=92233...
{ "input": [ "3\n6 4 3\n1 6\n2 3\n5 5\n4 1 2\n2 4\n1 2\n3 3 2\n2 3\n1 2\n", "9\n100 50 6\n1 49\n51 100\n40 60\n30 70\n20 30\n70 80\n100 1 11\n1 1\n1 1\n1 1\n2 2\n2 100\n1 2\n2 3\n3 4\n5 6\n6 100\n4 5\n1 1 5\n1 1\n1 1\n1 1\n1 1\n1 1\n11 6 4\n6 6\n1 5\n7 11\n6 6\n3 3 2\n1 2\n2 3\n1000000000 1 1\n1 1000000000\n1...
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 consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation,...
1408_B. Arrays Sum_3073
You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≤ i ≤ m. * For all 1 ≤ j ≤ n, a_j = b_{1, j} + b_{2, j}...
import sys as _sys def main(): t = int(input()) for i_t in range(t): n, k = _read_ints() a = tuple(_read_ints()) try: result = find_min_m(a, k) except ValueError: result = -1 print(result) def _read_line(): result = _sys.stdin.readline() ...
{ "input": [ "6\n4 1\n0 0 0 1\n3 1\n3 3 3\n11 3\n0 1 2 2 3 3 3 4 4 4 4\n5 3\n1 2 3 4 5\n9 4\n2 2 3 5 7 11 13 13 17\n10 7\n0 1 1 2 3 3 4 5 5 6\n", "1\n3 2\n3 3 3\n", "1\n3 3\n1 1 1\n", "1\n5 5\n5 5 5 5 5\n", "1\n5 3\n1 1 1 1 1\n", "1\n5 2\n5 5 5 5 5\n", "1\n2 2\n3 3\n", "1\n4 2\n1 1 1 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative inte...
1428_E. Carrots for Rabbits_3077
There are some rabbits in Singapore Zoo. To feed them, Zookeeper bought n carrots with lengths a_1, a_2, a_3, …, a_n. However, rabbits are very fertile and multiply very quickly. Zookeeper now has k rabbits and does not have enough carrots to feed all of them. To solve this problem, Zookeeper decided to cut the carrots...
import sys from heapq import heapify, heappush, heappop def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c for j in range(b)] for i in range(a)] def list3d(a, b, c, d): return [[[d for k in range(c)] for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e for l in ra...
{ "input": [ "1 4\n19\n", "3 6\n5 3 1\n", "10 23\n343 984 238 758983 231 74 231 548 893 543\n", "1 1\n1\n", "29 99047\n206580 305496 61753 908376 272137 803885 675070 665109 995787 667887 164508 634877 994427 270698 931765 721679 518973 65009 804367 608526 535640 117656 342804 398273 369209 298745...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are some rabbits in Singapore Zoo. To feed them, Zookeeper bought n carrots with lengths a_1, a_2, a_3, …, a_n. However, rabbits are very fertile and multiply very quickly. Zook...
1451_F. Nullify The Matrix_3081
Jeel and Ashish play a game on an n × m matrix. The rows are numbered 1 to n from top to bottom and the columns are numbered 1 to m from left to right. They play turn by turn. Ashish goes first. Initially, each cell of the matrix contains a non-negative integer. Each turn, a player must perform all of the following ac...
def solve_case(): n, m = [int(x) for x in input().split()] a = [[int(x) for x in input().split()] for x in range(n)] xr = [0] * (n + m) for i in range(n): for j in range(m): xr[i + j] ^= a[i][j] return sum(xr) > 0 def main(): for _ in range(int(input())): print(['J...
{ "input": [ "4\n1 1\n0\n1 3\n0 0 5\n2 2\n0 1\n1 0\n3 3\n1 2 3\n4 5 6\n7 8 9\n", "3\n5 5\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n4 4\n0 0 3 0\n0 2 0 0\n2 0 0 0\n0 0 0 0\n5 5\n0 3 4 5 0\n7 8 9 10 11\n12 13 14 15 16\n17 18 19 20 21\n0 22 23 24 0\n", "5\n5 5\n0 126 30 105 44\n126 98 21 0 108\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Jeel and Ashish play a game on an n × m matrix. The rows are numbered 1 to n from top to bottom and the columns are numbered 1 to m from left to right. They play turn by turn. Ashish ...
1475_E. Advertising Agency_3085
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers. Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course...
import math def c (k, n): return (math.factorial(n) // (math.factorial(k) * math.factorial(n - k))) % (10**9 + 7) def solve(): n, k = map(int, input().split()) a = [] for i in input().split(): a.append(int(i)) a.sort() m = dict() for i in a: if(m.get(i, 0) == 0): m[i]...
{ "input": [ "3\n4 3\n1 3 1 2\n4 2\n1 1 1 1\n2 1\n1 2\n", "3\n4 3\n1 3 1 2\n4 2\n1 2 1 1\n2 1\n1 2\n", "3\n4 3\n1 3 1 2\n4 2\n1 1 1 1\n2 1\n2 2\n", "3\n4 3\n1 3 2 2\n4 2\n1 1 1 1\n2 1\n2 2\n", "3\n4 3\n1 3 2 2\n4 2\n1 1 2 1\n2 1\n2 2\n", "3\n4 3\n1 3 1 2\n4 2\n1 1 2 1\n2 1\n2 2\n", "3\n4 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. B...
1525_E. Assimilation IV_3091
Monocarp is playing a game "Assimilation IV". In this game he manages a great empire: builds cities and conquers new lands. Monocarp's empire has n cities. In order to conquer new lands he plans to build one Monument in each city. The game is turn-based and, since Monocarp is still amateur, he builds exactly one Monum...
ans = 0 n,m = map(int,input().split()) mod = 998244353 a = [list(map(int,input().split())) for i in range(n)] fac = 1 for i in range(1,n+1): fac *= i inv = pow(fac,mod-2,mod) for j in range(m): na = sorted([a[i][j] for i in range(n)]) now = 1 able = 0 for i in range(n): while len(na) > 0...
{ "input": [ "3 5\n1 4 4 3 4\n1 4 1 4 2\n1 4 4 4 3\n", "1 1\n1\n", "1 1\n2\n", "3 1\n1\n1\n1\n", "3 1\n1\n1\n2\n", "3 5\n1 4 4 3 4\n1 4 2 4 2\n1 4 4 4 3\n", "3 5\n1 4 4 3 4\n1 4 2 4 2\n1 3 4 4 3\n", "3 5\n1 4 4 3 4\n1 4 2 4 2\n1 3 4 4 4\n", "3 5\n1 4 4 3 4\n1 1 2 4 2\n1 3 4 4 4\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Monocarp is playing a game "Assimilation IV". In this game he manages a great empire: builds cities and conquers new lands. Monocarp's empire has n cities. In order to conquer new la...
157_B. Trace_3095
One day, as Sherlock Holmes was tracking down one very important criminal, he found a wonderful painting on the wall. This wall could be represented as a plane. The painting had several concentric circles that divided the wall into several parts. Some parts were painted red and all the other were painted blue. Besides,...
n=int(input()) s=0 X=[int(x) for x in input().split(" ")] X.sort(reverse=True) for i in range(n): s=s+(-1)**(i)*X[i]*X[i] print(3.1415926536*s)
{ "input": [ "1\n1\n", "3\n1 4 2\n", "6\n258 877 696 425 663 934\n", "37\n280 281 169 68 249 389 977 101 360 43 448 447 368 496 125 507 747 392 338 270 916 150 929 428 118 266 589 470 774 852 263 644 187 817 808 58 637\n", "100\n469 399 735 925 62 153 707 723 819 529 200 624 57 708 245 384 889 11 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One day, as Sherlock Holmes was tracking down one very important criminal, he found a wonderful painting on the wall. This wall could be represented as a plane. The painting had sever...
178_A2. Educational Game_3099
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequenc...
from math import log n=int(input()) s=list(map(int,input().split( ))) p=[] for i in range(0,n-1): m=(log(n-1-i,2))//1 p.append(i+2**m) for k in range(1,n): g=0 f=s[:] for i in range(0,k): g+=f[i] f[int(p[i])]+=f[i] print(g)
{ "input": [ "8\n1 2 3 4 5 6 7 8\n", "4\n1 0 1 2\n", "9\n13 13 7 11 3 9 3 5 5\n", "80\n72 66 82 46 44 22 63 92 71 65 5 30 45 84 29 73 9 90 25 19 26 15 12 29 33 19 85 92 91 66 83 39 100 53 20 99 11 81 26 41 36 51 21 72 28 100 34 3 24 58 11 85 73 18 4 45 90 99 42 85 26 71 58 49 76 32 88 13 40 98 57 95 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of ...
19_C. Deletion of Repeats_3102
Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. Bob didn't like that string, because it contained repeats: a repeat of length x is such a substring of length 2x, that its first half coin...
def pow(x, exp, mod): res = 1 while exp: if exp & 1: res = (res * x) % mod x = (x * x) % mod exp >>= 1 return res MOD = 2 ** 121 - 1 M = int(1e9) + 1 n = int(input()) vals = list(map(int, input().split())) groups = dict() for i in range(n): groups.setdefault(vals...
{ "input": [ "6\n1 2 3 1 2 3\n", "7\n4 5 6 5 6 7 7\n", "100\n8 43 55 27 70 81 66 10 49 67 24 61 53 0 2 76 96 16 16 1 78 1 69 69 4 59 6 87 98 14 98 20 12 48 20 41 67 90 20 96 44 8 77 94 84 2 61 27 90 42 66 84 5 19 68 13 5 53 13 87 70 41 48 40 19 61 72 31 4 4 59 100 50 64 84 10 96 16 73 3 63 85 67 91 74 63 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. B...
248_B. Chilly Willy_3108
Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up with a few games that were connected with them. Chilly Willy wants to find the minimum number of length n, such that it is simultaneously...
n = int(input()) if n < 3: print(-1) else: t = 10**(n-1) print(t+210-t%210)
{ "input": [ "5\n", "1\n", "99996\n", "99999\n", "88888\n", "99987\n", "7\n", "99987\n", "21\n", "31\n", "13\n", "99995\n", "123\n", "5000\n", "20\n", "10\n", "45332\n", "14\n", "8\n", "33\n", "9\n", "8824\n", "27324\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up w...
272_C. Dima and Staircase_3112
Dima's got a staircase that consists of n stairs. The first stair is at height a1, the second one is at a2, the last one is at an (1 ≤ a1 ≤ a2 ≤ ... ≤ an). Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The i-th box has width wi and height hi. Dima throws eac...
# https://codeforces.com/problemset/problem/272/C # we just check if the box dropped falls above the previous box or on stair number(1...w) # the box is dropped from the top and hence we need to check the heights from the top - # which height is suitable and higher m = int(input()) arr = list(map(int, input().split(...
{ "input": [ "1\n1\n5\n1 2\n1 10\n1 10\n1 10\n1 10\n", "3\n1 2 3\n2\n1 1\n3 1\n", "5\n1 2 3 6 6\n4\n1 1\n3 1\n1 1\n4 3\n", "2\n1 6\n5\n2 6\n1 2\n1 1\n1 2\n1 7\n", "3\n2 10 15\n1\n1 830\n", "3\n1 6 8\n5\n3 4\n3 9\n3 3\n1 2\n1 6\n", "7\n8 13 19 21 25 30 32\n3\n5 4\n6 5\n1 2\n", "1\n10000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dima's got a staircase that consists of n stairs. The first stair is at height a1, the second one is at a2, the last one is at an (1 ≤ a1 ≤ a2 ≤ ... ≤ an). Dima decided to play with...
319_C. Kalila and Dimna in the Logging Industry_3118
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut n trees with heights a1, a2, ..., an. They bought a chain saw from a shop. Each time they use the chain saw on the tree...
n = int(input()) a = [0] + [int(x) for x in input().split()] b = [0] + [int(x) for x in input().split()] q = [0 for i in range(n + 1)] f = [0 for _ in range(n + 1)] l, r, q[1] = 1, 1, 1 for i in range(2, n + 1): while l < r and f[q[l + 1]] - f[q[l]] < a[i] * (b[q[l]] - b[q[l + 1]]): l += 1 f[i] = f[q[l]...
{ "input": [ "5\n1 2 3 4 5\n5 4 3 2 0\n", "6\n1 2 3 10 20 30\n6 5 4 3 2 0\n", "6\n2 2 3 10 20 30\n6 5 4 3 2 0\n", "5\n1 4 3 4 5\n5 4 3 2 0\n", "6\n1 2 3 10 20 25\n6 5 4 3 2 0\n", "6\n1 1 2 10 20 25\n6 5 4 3 2 0\n", "5\n1 2 3 4 5\n9 4 3 2 0\n", "5\n1 2 3 4 5\n6 4 3 2 0\n", "6\n2 2 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to t...
343_C. Read Time_3122
Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read data in parallel. When viewed from the side, Mike's hard drive is an endless array of tracks. The tracks of the array are numbered from left to right with integers, starting with 1. In the...
import sys from itertools import * from math import * def solve(): n, m = map(int, input().split()) h = list(map(int, input().split())) p = list(map(int, input().split())) ss, ll = 0, int(2.2e10) while ss < ll: avg = (ss + ll) // 2 works = True hidx = 0 pidx = 0 ...
{ "input": [ "3 4\n2 5 6\n1 3 6 8\n", "3 3\n1 2 3\n1 2 3\n", "1 2\n165\n142 200\n", "10 11\n1 909090909 1818181817 2727272725 3636363633 4545454541 5454545449 6363636357 7272727265 8181818173\n454545455 1363636363 2272727271 3181818179 4090909087 4999999995 5909090903 6818181811 7727272719 8636363627 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read data in parallel. When viewed from the side, Mike's...
366_E. Dima and Magic Guitar_3125
Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with n strings and m frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the i-th string holding it on the j-th fret...
def solution() : # 最大的距离来自于角落附近的点 n,m,k,s = map(int, input().split()) dis = lambda a,b : abs(a[0] - b[0]) + abs(a[1] - b[1]) corner = [(0,0), (0,m-1), (n-1,0), (n-1,m-1)] vertex = [[(n,m), (n,-1), (-1,m), (-1,-1)] for _ in range(k+1)] for i in range(n) : for j,note in enumerate(map(int, input().split())) : ve...
{ "input": [ "4 6 5 7\n3 1 2 2 3 1\n3 2 2 2 5 5\n4 2 2 2 5 3\n3 2 2 1 4 3\n2 3 1 4 1 5 1\n", "4 4 9 5\n4 7 9 5\n1 2 1 7\n8 3 4 9\n5 7 7 2\n7 1 9 2 5\n", "10 1 9 5\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1\n1 1 9 2 3\n", "11 11 9 11\n1 1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 2 2 1\n1 1 1 1 1 1 1 1 3 1 1\n1 1 1 1 1 1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with n strings and m frets. Dima makes the guitar produce sounds like that: to play a note, he n...
38_D. Vasya the Architect_3129
Once Vasya played bricks. All the bricks in the set had regular cubical shape. Vasya vas a talented architect, however the tower he built kept falling apart. Let us consider the building process. Vasya takes a brick and puts it on top of the already built tower so that the sides of the brick are parallel to the sides ...
a=[list(map(int,input().split())) for i in range(int(input()))] n=len(a) z=n for i in range(n): if i!=0: c=[(a[i][0]+a[i][2])/2,(a[i][1]+a[i][3])/2] m=abs(a[i][0]-a[i][2])**3 for j in range(i-1,-1,-1): x1,y1,x2,y2=min(a[j][0],a[j][2]),min(a[j][1],a[j][3]),max(a[j][0],a[j][2]),max...
{ "input": [ "3\n0 0 3 3\n1 0 4 3\n2 0 5 3\n", "2\n0 0 3 3\n1 0 4 3\n", "2\n0 0 3 3\n2 0 5 3\n", "7\n4 6 1 9\n-1 4 6 11\n8 13 -3 2\n-5 0 45 50\n-2 50 45 3\n45 50 -4 1\n45 50 -1 4\n", "11\n12 12 -12 -12\n11 8 -11 -14\n-15 -11 -3 1\n-16 -3 -14 -1\n-7 -8 -21 6\n37 -6 -19 50\n-21 -12 41 50\n48 -13 -15...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once Vasya played bricks. All the bricks in the set had regular cubical shape. Vasya vas a talented architect, however the tower he built kept falling apart. Let us consider the buil...
40_C. Berland Square_3133
Last year the world's largest square was built in Berland. It is known that the square can be represented as an infinite plane with an introduced Cartesian system of coordinates. On that square two sets of concentric circles were painted. Let's call the set of concentric circles with radii 1, 2, ..., K and the center i...
def readGen(trans): while 1: for x in input().split(): yield(trans(x)) readint=readGen(int) [N,x,M,y]=(next(readint) for i in range(4)) d=abs(y-x) def interval(a,b): return range(a,b+1) def case1(N,M,d): # d>=N ans=0 for r in interval(1, min(M,d-N)): ans+=1 if (M<=d-N): return ans for r in interval(...
{ "input": [ "3 3 4 7\n", "1 0 1 2\n", "1 0 1 1\n", "99085 7738 98097 -6487\n", "1 2 2 1\n", "93430 5720 93581 -2371\n", "97530 -7728 92184 -755\n", "93564 -4520 99118 -52061\n", "100000 100 100000 70000\n", "98405 -62879 99461 -33688\n", "5 4 3 -2\n", "93564 8371 99118...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Last year the world's largest square was built in Berland. It is known that the square can be represented as an infinite plane with an introduced Cartesian system of coordinates. On t...
438_B. The Child and Zoo_3137
Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains ai animals in it. Also there are m roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo from any other area using the roads....
R = lambda:map(int, input().split()) n, m = R() a = list(R()) p, f, sz =[], [], [] e = [[] for i in range(n)] vis = [0] * n ans = 0 def find(u): if f[u] != u: f[u] = find(f[u]) return f[u] for i in range(n): p.append([a[i], i]) f.append(i) sz.append(1) p.sort() p.reverse() for i in range(m)...
{ "input": [ "7 8\n40 20 10 30 20 50 40\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n1 4\n5 7\n", "3 3\n10 20 30\n1 2\n2 3\n3 1\n", "4 3\n10 20 30 40\n1 3\n2 3\n4 3\n", "10 19\n13637 26970 19043 3616 12880 19387 12539 25190 2452 1261\n1 2\n1 3\n1 4\n2 5\n3 6\n6 7\n3 8\n5 9\n3 10\n4 10\n9 3\n2 8\n4 3\n2 3\n7 10\n7 8...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains ai animals in it. Also there are m roads in the zoo, and each ro...
45_I. TCMCF+++_3141
Vasya has gotten interested in programming contests in TCMCF+++ rules. On the contest n problems were suggested and every problem had a cost — a certain integral number of points (perhaps, negative or even equal to zero). According to TCMCF+++ rules, only accepted problems can earn points and the overall number of poin...
import sys input=sys.stdin.readline n=int(input()) s=list(map(int,input().split())) s.sort() s.reverse() num_manfi=0 num_sefr=0 flag=0 for i in s: if i>0: print(i,end=' ') flag=1 elif i==0: num_sefr+=1 else: num_manfi+=1 num_manfi-=(num_manfi%2) s.reverse() for i in ...
{ "input": [ "4\n-2 -2 -2 -2\n", "13\n100 100 100 100 100 100 100 100 100 100 100 100 100\n", "5\n1 2 -3 3 3\n", "100\n-93 -100 -99 -100 -99 -100 -99 -99 -99 -100 -99 -99 -100 -98 -98 -100 -99 -100 -95 -100 -100 -99 -98 -100 -96 -99 -99 -100 -97 -98 -100 -98 -98 -93 -100 -100 -99 -100 -100 -98 -100 -9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has gotten interested in programming contests in TCMCF+++ rules. On the contest n problems were suggested and every problem had a cost — a certain integral number of points (per...
483_C. Diverse Permutation_3145
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k d...
from collections import Counter import string import math import sys # sys.setrecursionlimit(10**6) from fractions import Fraction def array_int(): return [int(i) for i in sys.stdin.readline().split()] def vary(arrber_of_variables): if arrber_of_variables==1: return int(sys.stdin.readline()) if arr...
{ "input": [ "5 2\n", "3 1\n", "3 2\n", "29857 9843\n", "10 3\n", "2 1\n", "10931 8824\n", "4 2\n", "6591 407\n", "10 9\n", "5 4\n", "3222 311\n", "27687 4031\n", "23347 20494\n", "10 4\n", "7 5\n", "25517 1767\n", "4 1\n", "9 8\n", "1111...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn....
507_C. Guess Your Way Out!_3149
Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node. Let's index all the leaf nodes from the left to ...
import sys def main(): rdl = list(map(int,input().split())) obx(rdl[0],rdl[1],0,1) def obx(lvl, ind, kl, current): if lvl ==0: print(int(kl)) sys.exit() all = 0 for i in range(lvl+1): all += 2**i all -= 1 if ind > (2**(lvl))/2: if current == 1: kl...
{ "input": [ "1 2\n", "10 1024\n", "3 6\n", "2 3\n", "49 295606900104348\n", "1 1\n", "29 113463931\n", "40 615535158153\n", "50 905353992267944\n", "22 3343393\n", "20 589266\n", "19 12783\n", "30 414940886\n", "32 3786259360\n", "35 12080044014\n", "13...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially sta...
556_D. Case of Fugitive_3152
Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an archipelago of n narrow islands located in a row. For more comfort let's represent them as non-intersecting segments on a straight line: ...
#!/usr/bin/env python # 556D_fug.py - Codeforces.com 556D Fug quiz # # Copyright (C) 2015 Sergey # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2...
{ "input": [ "4 4\n1 4\n7 8\n9 10\n12 14\n4 5 3 8\n", "2 2\n11 14\n17 18\n2 9\n", "2 1\n1 1\n1000000000000000000 1000000000000000000\n999999999999999999\n", "2 1\n1 1\n100 100\n5\n", "3 2\n1000000000000000 1000000000000000\n3000000000000000 4000000000000000\n6000000000000000 7000000000000000\n2000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an ...
582_B. Once Again..._3156
You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≤ n ≤ 100, 1 ≤ T ≤ 107). The second li...
f = lambda: map(int, input().split()) n, m = f() t = list(f()) s = [0] * 301 d = s[:] for i in t: d[i] += 1 for i in t * min(m, 2 * n): s[i] = max(s[:i + 1]) + 1 print(max(s) + max((m - n * 2) * max(d), 0))
{ "input": [ "4 3\n3 1 4 2\n", "100 10000000\n1 2 2 1 2 2 1 1 2 2 1 2 1 1 1 1 1 2 2 2 1 2 1 2 1 2 1 2 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 2 2 2 2 2 2 1 1 2 2 1 1 2 1 1 2 1 2 1 1 2 1 2 2 2 1 1 2 2 1 2 1 1 2 2 1 1 1 2 1 2 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1\n", "99 10000000\n96 97 98 93 94 95 90 91 92 87 88 89 84 85 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing ...
604_B. More Cowbell_3160
Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single ...
n, k = map(int, input().split()) a = [int(s) for s in input().split()] p = n - k q = 2*k - n if n < k: q = n p = 0 ma = 0 for i in range ((n - q) // 2): ma = max((a[i] + a[n - q - i - 1]), ma) ma2 = 0 for i in range(n - q, n): ma2 = max(a[i], ma2) print(max(ma,ma2))
{ "input": [ "4 3\n2 3 5 9\n", "2 1\n2 5\n", "3 2\n3 5 7\n", "10 5\n15 15 20 28 38 44 46 52 69 94\n", "100 89\n474 532 759 772 803 965 1043 1325 1342 1401 1411 1452 1531 1707 1906 1928 2034 2222 2335 2606 2757 2968 2978 3211 3513 3734 3772 3778 3842 3948 3976 4038 4055 4113 4182 4267 4390 4408 447...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into ...
626_E. Simple Skewness_3163
Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a colle...
from itertools import accumulate from fractions import Fraction n = int(input()) A = [int(x) for x in input().split()] A.sort() B = list(accumulate([0] + A)) def condition(i, z): return (2*z - 1)*(A[i-z] + A[-z]) > 2*(B[i+1] - B[i-z+1] + B[-1] - B[-z]) def average(i, z): return Fraction((B[i+1] - B[i-z] + B[-1...
{ "input": [ "2\n1 2\n", "4\n1 1 2 2\n", "4\n1 2 3 12\n", "21\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712 1000000\n", "1\n412237\n", "3\n76427 184396 963319\n", "5\n741304 281944 258539 54430 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty s...
676_A. Nicholas and Permutation_3169
Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n. Nicholas want the minimum element (integer 1) and the maximum element (integer n) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance...
n = int(input()) a = list(map(int, input().split())) maxLocate = a.index(n) minLocate = a.index(1) distanceMax = len(a) - 1 - a.index(n) distanceMin = len(a) - 1 - a.index(1) print(max(maxLocate, minLocate, distanceMax, distanceMin))
{ "input": [ "6\n6 5 4 3 2 1\n", "7\n1 6 5 3 4 7 2\n", "5\n4 5 1 3 2\n", "76\n1 47 54 17 38 37 12 32 14 48 43 71 60 56 4 13 64 41 52 57 62 24 23 49 20 10 63 3 25 66 59 40 58 33 53 46 70 7 35 61 72 74 73 19 30 5 29 6 15 28 21 27 51 55 50 9 65 8 67 39 76 42 31 34 16 2 36 11 26 44 22 45 75 18 69 68\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n. Nicholas want the minimum element (integer 1) and the max...
69_A. Young Physicist_3173
A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasy...
sum1,sum2,sum3 = 0,0,0 n = int(input()) while n: n-=1 l = list(map(int, input().split())) sum1 += l[0] sum2 += l[1] sum3 += l[2] if sum1==0 and sum2 ==0 and sum3 ==0: print("YES") else: print("NO")
{ "input": [ "3\n3 -1 7\n-5 2 -4\n2 -1 -3\n", "3\n4 1 7\n-2 4 -1\n1 -5 -3\n", "14\n43 23 17\n4 17 44\n5 -5 -16\n-43 -7 -6\n47 -48 12\n50 47 -45\n2 14 43\n37 -30 15\n4 -17 -11\n17 9 -45\n-50 -3 -8\n-50 0 0\n-50 0 0\n-16 0 0\n", "3\n1 2 3\n3 2 1\n0 0 0\n", "44\n52 -54 -29\n-82 -5 -94\n-54 43 43\n91 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than ...
742_A. Arpa’s hard exam and Mehrdad’s naive cheat_3179
There exists an island called Arpa’s land, some beautiful girls live there, as ugly ones do. Mehrdad wants to become minister of Arpa’s land. Arpa has prepared an exam. Exam has only one question, given n, print the last digit of 1378n. <image> Mehrdad has become quite confused and wants you to help him. Please hel...
n = int(input()) if n == 0: print(1) if n % 4 == 0 and n!= 0 : print(6) if n % 4 == 1: print(8) if n % 4 == 2: print(4) if n % 4 == 3: print(2)
{ "input": [ "2\n", "1\n", "12\n", "1378\n", "51202278\n", "989898989\n", "4\n", "7\n", "9\n", "6\n", "8\n", "12193721\n", "999999995\n", "999999999\n", "11\n", "999999996\n", "999999998\n", "3\n", "14\n", "15\n", "10\n", "9999999...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There exists an island called Arpa’s land, some beautiful girls live there, as ugly ones do. Mehrdad wants to become minister of Arpa’s land. Arpa has prepared an exam. Exam has only...
765_E. Tree Folding_3183
Vanya wants to minimize a tree. He can perform the following operation multiple times: choose a vertex v, and two disjoint (except for v) paths of equal length a0 = v, a1, ..., ak, and b0 = v, b1, ..., bk. Additionally, vertices a1, ..., ak, b1, ..., bk must not have any neighbours in the tree other than adjacent verti...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time sys.setrecursionlimit(10**7) inf = 10**20 mod = 10**9 + 7 def LI(): return list(map(int, input().split())) def II(): return int(input()) def LS(): return input().split() def S(): return input() def main(): n = II() d = ...
{ "input": [ "6\n1 2\n2 3\n2 4\n4 5\n1 6\n", "7\n1 2\n1 3\n3 4\n1 5\n5 6\n6 7\n", "10\n4 2\n7 4\n2 6\n2 5\n4 8\n10 3\n2 9\n9 1\n5 10\n", "10\n5 10\n7 8\n8 3\n2 6\n3 2\n9 7\n4 5\n10 1\n6 4\n", "4\n4 1\n4 3\n4 2\n", "3\n3 1\n1 2\n", "9\n1 2\n1 3\n1 4\n1 5\n1 6\n6 7\n6 8\n8 9\n", "5\n1 5\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vanya wants to minimize a tree. He can perform the following operation multiple times: choose a vertex v, and two disjoint (except for v) paths of equal length a0 = v, a1, ..., ak, an...
789_B. Masha and geometric depression_3187
Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise. You are given geometric progression b defined by two integers b1 and q. Remind that a geometric progression is a sequence of integers b1, b2, b3, ..., where for each i > 1 the respective term satisfies the condition bi = ...
b1,q,l,m = map(int,input().split()) a = set(map(int,input().split())) if q==0: if 0 in a: if b1 in a: print(0) elif abs(b1)<=l: print(1) else: print(0) elif abs(b1)<=l: print("inf") else: print(0) elif q==1: if abs(b1)<=l and b1...
{ "input": [ "123 1 2143435 4\n123 11 -5453 141245\n", "3 2 30 4\n6 14 25 48\n", "123 1 2143435 4\n54343 -13 6 124\n", "-100 0 50 1\n0\n", "-1000 0 10 1\n5\n", "-3 1 100 1\n3\n", "1000000000 100000 1000000000 4\n5433 13 6 0\n", "5 1 5 5\n1 2 3 4 0\n", "2 -1 6 1\n2\n", "3 2 30 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise. You are given geometric progression b defined by two integers b1 and q. Remind that ...
80_D. Falling Anvils_3191
For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all. Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who ...
t = int(input()) for i in range(t): a, b = map(int, input().split()) denom = a * b * 2 if denom == 0: if b == 0: print('1') else: print('0.5') continue if a > 4 * b: nom = 2 * b * b else: nom = (b - a / 4.0 + b) / 2.0 * a pri...
{ "input": [ "2\n4 2\n1 2\n", "90\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n1 2\n2 2\n3 2\n4 2\n5 2\n6 2\n7 2\n8 2\n9 2\n1 3\n2 3\n3 3\n4 3\n5 3\n6 3\n7 3\n8 3\n9 3\n1 4\n2 4\n3 4\n4 4\n5 4\n6 4\n7 4\n8 4\n9 4\n1 5\n2 5\n3 5\n4 5\n5 5\n6 5\n7 5\n8 5\n9 5\n1 6\n2 6\n3 6\n4 6\n5 6\n6 6\n7 6\n8 6\n9 6\n1 7\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most o...
835_B. The number on the board_3195
Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change. You have to find the minimum number of digits in which these two numbers c...
""" Author : Arif Ahmad Date : Algo : Difficulty : """ from sys import stdin, stdout def main(): k = int(stdin.readline().strip()) n = stdin.readline().strip() n = sorted(n) total = 0 for c in n: d = ord(c) - 48 total += d ans = 0 for c in n: if total >= k: break d = ord(c) - 48 total ...
{ "input": [ "3\n11\n", "3\n99\n", "3\n21\n", "3\n111\n", "6\n33\n", "6\n222\n", "5\n5\n", "2\n11\n", "1\n1\n", "500\n1899337170458531693764539600958943248270674811247191310452938511077656066239840703432499357537079035\n", "900\n757042381727296702755308246486396202463521737...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with ot...
87_C. Interesting Game_3202
Two best friends Serozha and Gena play a game. Initially there is one pile consisting of n stones on the table. During one move one pile should be taken and divided into an arbitrary number of piles consisting of a1 > a2 > ... > ak > 0 stones. The piles should meet the condition a1 - a2 = a2 - a3 = ... = ak - 1 - ak =...
import math from collections import Counter n = int(input()) g = [0 for i in range(n + 1)] prefix_xor = g.copy() def in_range(d, k): if (2 * k - d * d + d) % (2 * d) != 0: return -1 x = (2 * k - d * d + d) / (2 * d) return int(x) if x > 0 else -1 def mex(arr): counter = Counter() for i ...
{ "input": [ "6\n", "100\n", "3\n", "56\n", "1\n", "46\n", "63478\n", "7578\n", "56056\n", "78\n", "66873\n", "8149\n", "35708\n", "81207\n", "41385\n", "37317\n", "74424\n", "67817\n", "684\n", "15\n", "96992\n", "38798\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Two best friends Serozha and Gena play a game. Initially there is one pile consisting of n stones on the table. During one move one pile should be taken and divided into an arbitrary...
903_E. Swapping Characters_3206
We had a string s consisting of n lowercase Latin letters. We made k copies of this string, thus obtaining k identical strings s1, s2, ..., sk. After that, in each of these strings we swapped exactly two characters (the characters we swapped could be identical, but they had different indices in the string). You are gi...
import collections def swapCharacters(strings, k, n): """ Time: O(n^2 * k) Space: O(1) """ if k == 1: s0 = list(strings[0]) s0[0], s0[1] = s0[1], s0[0] return ''.join(s0) # Initial check for validity freq = collections.Counter(strings[0]) canSame = (max(freq.values()) >= 2) # could swap two of the same...
{ "input": [ "5 4\nabcd\ndcba\nacbd\ndbca\nzzzz\n", "3 4\nabac\ncaab\nacba\n", "3 4\nkbbu\nkbub\nubkb\n", "3 4\nkbub\nkbbu\nubkb\n", "5 5\nzbibx\nzbbix\nzbibx\nxbibz\nxbibz\n", "100 2\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nqz\nq...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We had a string s consisting of n lowercase Latin letters. We made k copies of this string, thus obtaining k identical strings s1, s2, ..., sk. After that, in each of these strings we...
954_G. Castle Defense_3210
Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into n sections. At this moment there are exactly ai archers located at the...
import sys border = 1 << 30 def build(x=0): res = [0, 0] i = 0 while x and i < 30: if x & 1: res[1] += (1 << i) x >>= 1 i += 1 i = 0 while x: if x & 1: res[0] += (1 << i) x >>= 1 i += 1 return res n, r, k = map(int, ...
{ "input": [ "4 2 0\n1 2 3 4\n", "5 0 6\n5 4 3 4 9\n", "5 1 1\n2 1 2 1 2\n", "1 0 0\n1\n", "1 1 10\n23\n", "100 0 0\n607 169 477 518 368 54 15 471 604 138 982 836 563 886 466 118 366 670 257 37 868 142 14 666 670 624 427 281 995 133 914 413 356 852 63 894 430 310 634 253 281 938 975 272 821 24...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains an...
p02549 AtCoder Beginner Contest 179 - Leaping Tak_3226
There are N cells arranged in a row, numbered 1, 2, \ldots, N from left to right. Tak lives in these cells and is currently on Cell 1. He is trying to reach Cell N by using the procedure described below. You are given an integer K that is less than or equal to 10, and K non-intersecting segments [L_1, R_1], [L_2, R_2...
import sys input = sys.stdin.readline N,K=map(int,input().split()) K=[tuple(map(int,input().split())) for i in range(K)] mod=998244353 DP=[0]*(4*10**5+100) DP[1]=1 DP[2]=-1 for i in range(1,N+1): DP[i]+=DP[i-1] DP[i]%=mod for l,r in K: DP[i+l]+=DP[i] DP[i+l]%=mod DP[i+r+1]-=D...
{ "input": [ "5 2\n3 3\n5 5", "5 1\n1 2", "60 3\n5 8\n1 3\n10 15", "5 2\n1 1\n3 4", "5 2\n3 3\n5 9", "60 3\n5 4\n1 3\n10 15", "10 2\n1 1\n3 4", "60 3\n5 4\n1 3\n10 20", "50 3\n5 4\n1 3\n10 20", "5 1\n1 2\n5 9", "60 3\n5 8\n1 0\n10 15", "3 2\n1 1\n3 4", "60 3\n5 4\n1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N cells arranged in a row, numbered 1, 2, \ldots, N from left to right. Tak lives in these cells and is currently on Cell 1. He is trying to reach Cell N by using the proce...
p02680 AtCoder Beginner Contest 168 - . (Single Dot)_3229
There is a grass field that stretches infinitely. In this field, there is a negligibly small cow. Let (x, y) denote the point that is x\ \mathrm{cm} south and y\ \mathrm{cm} east of the point where the cow stands now. The cow itself is standing at (0, 0). There are also N north-south lines and M east-west lines drawn...
I=lambda:map(int,input().split());F=1e21;r=range;n,m=I();f=[-F,F];X=[0]+f;Y=X[:];R=[(f,F),(f,-F)];H=[(F,f),(-F,f)] for i in r(n):*a,c=I();R+=[(a,c)];Y+=c,;X+=a for i in r(m):a,*b=I();H+=[(a,b)];X+=a,;Y+=b def g(X):s=dict(enumerate(sorted(set(X))));return{s[i]:i for i in s},s,len(s)+1 h,s,K=g(X);w,t,L=g(Y);j=lambda:[[0]...
{ "input": [ "6 1\n-3 -1 -2\n-3 -1 1\n-2 -1 2\n1 4 -2\n1 4 -1\n1 4 1\n3 1 4", "5 6\n1 2 0\n0 1 1\n0 2 2\n-3 4 -1\n-2 6 3\n1 0 1\n0 1 2\n2 0 2\n-1 -4 5\n3 -2 4\n1 2 4", "6 1\n-3 -1 -4\n-3 -1 1\n-2 -1 2\n1 4 -2\n1 4 -1\n1 4 1\n3 1 4", "5 6\n1 2 0\n0 1 1\n0 2 2\n-3 4 -1\n-2 6 3\n1 0 1\n0 1 2\n2 0 2\n-1 -...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a grass field that stretches infinitely. In this field, there is a negligibly small cow. Let (x, y) denote the point that is x\ \mathrm{cm} south and y\ \mathrm{cm} east of ...
p02808 Dwango Programming Contest 6th - Cookie Distribution_3232
There are N children, numbered 1,2,\ldots,N. In the next K days, we will give them some cookies. In the i-th day, we will choose a_i children among the N with equal probability, and give one cookie to each child chosen. (We make these K choices independently.) Let us define the happiness of the children as c_1 \times ...
n,k = map(int,input().split()) a = tuple(map(int,input().split())) mod = 10**9+7 rng = 1001 fctr = [1] finv = [1] for i in range(1,rng): fctr.append(fctr[-1]*i%mod) finv.append(pow(fctr[i],mod-2,mod)) dp = [[0 for i in range(n+1)] for j in range(k+1)] dp[0][0] = 1 for i in range(1,k+1): x = a[i-1] for j in rang...
{ "input": [ "3 2\n3 2", "856 16\n399 263 665 432 206 61 784 548 422 313 848 478 827 26 398 63", "3 2\n1 2", "856 16\n399 491 665 432 206 61 784 548 422 313 848 478 827 26 398 63", "4 2\n1 2", "856 16\n399 491 665 432 206 0 784 548 422 313 848 478 827 26 398 63", "2 2\n1 2", "856 16\n3...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N children, numbered 1,2,\ldots,N. In the next K days, we will give them some cookies. In the i-th day, we will choose a_i children among the N with equal probability, and g...
p03081 ExaWizards 2019 - Snuke the Wizard_3238
There are N squares numbered 1 to N from left to right. Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square. Snuke cast Q spells to move the golems. The i-th spell consisted of two characters t_i and d_i, where d_i is `L` or `R`. When Snuke ca...
n, q = map(int, input().split()) s = input() o = [] for _ in range(q): t, d = input().split() o.append((t, d)) def check(x): cur = s[x] ind = x for i in range(q): if o[i][0] == cur: if o[i][1] == 'L': ind -= 1 if ind < 0: ret...
{ "input": [ "10 15\nSNCZWRCEWB\nB R\nR R\nE R\nW R\nZ L\nS R\nQ L\nW L\nB R\nC L\nA L\nN L\nE R\nZ L\nS L", "3 4\nABC\nA L\nB L\nB R\nA R", "8 3\nAABCBDBA\nA L\nB R\nA R", "10 15\nSNCZWRCEWB\nB R\nR R\nE R\nW R\nZ L\nS R\nQ L\nW L\nB R\nC L\nA L\nN L\nE R\nZ L\nR L", "3 4\nACB\nA L\nB L\nB R\nA R...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N squares numbered 1 to N from left to right. Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square....
p03226 Tenka1 Programmer Contest - Circular_3241
You are given a sequence of N integers: A_1,A_2,...,A_N. Find the number of permutations p_1,p_2,...,p_N of 1,2,...,N that can be changed to A_1,A_2,...,A_N by performing the following operation some number of times (possibly zero), modulo 998244353: * For each 1\leq i\leq N, let q_i=min(p_{i-1},p_{i}), where p_0=p_N...
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10 ** 7) from operator import itemgetter MOD = 998244353 N,*A = map(int,read().split()) T = A.count(1) if T == 0: print(0) exit() if T == 1: answer = 1 if len(set(A))...
{ "input": [ "3\n1\n2\n1", "8\n4\n4\n4\n1\n1\n1\n2\n2", "6\n1\n1\n6\n2\n2\n2", "5\n3\n1\n4\n1\n5", "8\n4\n4\n4\n1\n2\n1\n2\n2", "5\n3\n1\n4\n2\n5", "6\n4\n1\n1\n2\n2\n4", "6\n1\n1\n1\n2\n2\n2", "6\n1\n1\n1\n2\n2\n1", "6\n1\n1\n1\n1\n2\n1", "6\n1\n1\n1\n1\n1\n1", "3\n1\n...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a sequence of N integers: A_1,A_2,...,A_N. Find the number of permutations p_1,p_2,...,p_N of 1,2,...,N that can be changed to A_1,A_2,...,A_N by performing the followi...
p03374 AtCoder Regular Contest 096 - Static Sushi_3244
"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on...
N, C = map(int, input().split()) xv = [[int(i) for i in input().split()] for _ in range(N)] av = 0 fa1 = 0 bv = 0 fb1 = 0 fa = [] fb = [] ga = [] gb = [] for i in range(N): ax = xv[i][0] av += xv[i][1] bx = C - xv[-(i+1)][0] bv += xv[-(i+1)][1] fa1 = max(fa1, av-ax) fa.append(fa1) ga.append...
{ "input": [ "15 10000000000\n400000000 1000000000\n800000000 1000000000\n1900000000 1000000000\n2400000000 1000000000\n2900000000 1000000000\n3300000000 1000000000\n3700000000 1000000000\n3800000000 1000000000\n4000000000 1000000000\n4100000000 1000000000\n5200000000 1000000000\n6600000000 1000000000\n8000000000...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. ...
p03535 CODE FESTIVAL 2017 Final (Parallel) - Time Gap_3248
In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if t...
N = int(input()) D = [int(x) for x in input().split()] D.append(0) D.sort() ans = [] for i in range(N + 1): if i % 2 == 0: ans.append(D[i]) else: ans.append((24 - D[i]) % 24) ans.sort() print(min(min(ans[i + 1] - ans[i] for i in range(N)), 24 - ans[-1]))
{ "input": [ "2\n11 11", "3\n7 12 8", "1\n0", "2\n11 8", "3\n7 12 11", "2\n8 8", "2\n10 2", "2\n2 0", "3\n9 12 8", "2\n6 8", "2\n12 8", "2\n9 7", "1\n11", "1\n9", "1\n10", "3\n8 12 11", "2\n8 1", "2\n10 1", "2\n4 2", "2\n8 2", "2\n2 1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times ...
p03695 AtCoder Beginner Contest 064 - Colorful Leaderboard_3252
In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 280...
n,a=open(0) s=set();x=0 for i in map(int,a.split()): j=i//400 if j<8:s.add(j) else:x+=1 print(max(1,len(s)),len(s)+x)
{ "input": [ "4\n2100 2500 2700 2700", "20\n800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990", "5\n1100 1900 2800 3200 3200", "4\n2100 2500 4217 2700", "20\n800 810 820 830 840 850 860 870 880 890 900 1771 920 930 940 950 960 970 980 990", "5\n1100 3348 2800 3200...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating ...
p03850 AtCoder Regular Contest 066 - Addition and Subtraction Hard_3256
Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) in...
#!/usr/bin/env python3 def solve(n): s = input() sum_a = 0 sum_n = 0 sum_np = 0 min_sum_np = 10 ** 15 j = s.find('-') if j < 0: return sum(list(map(int, s.split(' + ')))) sum_a = sum(list(map(int, s[:j - 1].split(' + ')))) j += 2 s += ' ' len_s = len(s) sign ...
{ "input": [ "5\n1 - 2 + 3 - 4 + 5", "3\n5 - 1 - 3", "5\n1 - 20 - 13 + 14 - 5", "5\n1 - 2 + 2 - 4 + 5", "5\n1 - 38 - 13 + 14 - 5", "5\n1 - 45 - 13 + 14 - 5", "5\n2 - 45 - 13 + 14 - 5", "5\n2 - 80 - 13 + 14 - 5", "5\n2 - 57 - 13 + 14 - 5", "5\n2 - 39 - 13 + 9 - 5", "5\n1 - 2...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numb...
p04017 AtCoder Regular Contest 060 - Tak and Hotels_3260
N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are ...
import bisect N = int(input()) x = list(map(int, input().split())) L = int(input()) dest = [] dest_1 = [0] * N for i in range(N): dest_1[i] = bisect.bisect_left(x, x[i] + L + 0.5) - 1 dest.append(dest_1) for i in range(1, len(bin(N - 1)) - 1): dest_prev = dest[i - 1] dest_i = [0] * N for j in range(N)...
{ "input": [ "9\n1 3 6 13 15 18 19 29 31\n10\n4\n1 8\n7 3\n6 7\n8 5", "9\n1 3 6 13 15 18 19 29 31\n10\n2\n1 8\n7 3\n6 7\n8 5", "9\n1 6 6 13 23 18 19 29 31\n10\n3\n1 8\n7 3\n6 7\n8 10", "9\n1 3 6 13 23 18 19 29 31\n10\n2\n1 8\n7 5\n6 7\n8 5", "9\n1 3 6 13 23 18 19 29 31\n10\n2\n1 9\n7 3\n6 7\n8 10"...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a ...
p00101 Aizu PR_3264
An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino...
[print(input().replace("Hoshino","Hoshina")) for i in range(int(input()))]
{ "input": [ "3\nHoshino\nHashino\nMasayuki Hoshino was the grandson of Ieyasu Tokugawa.", "3\nHoshimo\nHashino\nMasayuki Hoshino was the grandson of Ieyasu Tokugawa.", "3\nHoshimo\nHashino\nMasayuki Hoshino was the grandspn of Ieyasu Tokugawa.", "3\nHoshimo\nHashino\nMasayuki Hoshino was the grandspn...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the...
p00234 Aizu Buried Treasure_3267
Aizu has an ancient legend of buried treasure. You have finally found the place where the buried treasure is buried. Since we know the depth of the buried treasure and the condition of the strata to be dug, we can reach the buried treasure at the lowest cost with careful planning. So you decided to create a program tha...
import sys sys.setrecursionlimit(1000000) INF = 10 ** 20 def update_state(state, newx): tmp = list(state) tmp[newx] = 1 return tuple(tmp) def get_co(x, y): dc = do = 0 score = mp[y][x] if score < 0: dc = -score else: do = score return dc, do def minimum_cost(x, y, state, ox, goal, dic, w, m):...
{ "input": [ "3 3\n100 10 10\n-100 -20 -100\n-100 -20 -100\n-100 -20 -100\n3 3\n100 10 10\n-100 -20 -100\n-100 -20 -20\n-100 -60 -20\n3 3\n100 10 3\n-100 -20 -100\n-20 -20 -20\n-20 -100 -20\n3 3\n100 3 3\n-100 -20 -30\n-100 -20 2\n-100 -20 -20\n4 5\n1500 5 4\n-10 -380 -250 -250\n-90 2 -80 8\n-250 -130 -330 -120\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Aizu has an ancient legend of buried treasure. You have finally found the place where the buried treasure is buried. Since we know the depth of the buried treasure and the condition o...
p00396 Playing With Stones_3270
Playing with Stones Koshiro and Ukiko are playing a game with black and white stones. The rules of the game are as follows: 1. Before starting the game, they define some small areas and place "one or more black stones and one or more white stones" in each of the areas. 2. Koshiro and Ukiko alternately select an area ...
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdi...
{ "input": [ "4\n24 99\n15 68\n12 90\n95 79", "3\n2 46\n94 8\n46 57", "4\n24 99\n15 68\n12 90\n95 66", "3\n0 46\n94 8\n46 57", "4\n24 99\n15 44\n12 90\n95 66", "3\n0 46\n9 8\n46 57", "4\n18 99\n15 44\n12 90\n95 66", "3\n1 46\n9 8\n46 57", "4\n18 99\n15 44\n3 90\n95 66", "3\n1 6...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Playing with Stones Koshiro and Ukiko are playing a game with black and white stones. The rules of the game are as follows: 1. Before starting the game, they define some small areas...
p00749 Off Balance_3274
You are working for an administration office of the International Center for Picassonian Cubism (ICPC), which plans to build a new art gallery for young artists. The center is organizing an architectural design competition to find the best design for the new building. Submitted designs will look like a screenshot of a...
while True: w, h = map(int, input().split()) if w == 0: break mp = [[0] + list(input()) + [0] for _ in range(h)] mp.insert(0, [0] * (w + 2)) mp.append([0] * (w + 2)) for y in range(1, h + 1): for x in range(1, w + 1): if mp[y][x] == ".": mp[y][x] = 0 else: mp[y][x] = int...
{ "input": [ "4 5\n..33\n..33\n2222\n..1.\n.111\n5 7\n....1\n.1..1\n.1..1\n11..1\n.2222\n..111\n...1.\n3 6\n.3.\n233\n23.\n22.\n.11\n.11\n4 3\n2222\n..11\n..11\n4 5\n.3..\n33..\n322.\n2211\n.11.\n3 3\n222\n2.1\n111\n3 4\n11.\n11.\n.2.\n222\n3 4\n.11\n.11\n.2.\n222\n2 7\n11\n.1\n21\n22\n23\n.3\n33\n2 3\n1.\n11\n.1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are working for an administration office of the International Center for Picassonian Cubism (ICPC), which plans to build a new art gallery for young artists. The center is organiz...
p00887 Awkward Lights_3277
You are working as a night watchman in an office building. Your task is to check whether all the lights in the building are turned off after all the office workers in the building have left the office. If there are lights that are turned on, you must turn off those lights. This task is not as easy as it sounds to be be...
import sys,queue,math,copy,itertools LI = lambda : [int(x) for x in sys.stdin.readline().split()] while True: M,N,D = LI() if N == 0 : break S = [] for _ in range(N): S.extend(LI()) x = [] for i in range(N): for j in range(M): y = [0 for _ in range(N*M)] ...
{ "input": [ "1 1 1\n1\n2 2 1\n1 1\n1 1\n3 2 1\n1 0 1\n0 1 0\n3 3 1\n1 0 1\n0 1 0\n1 0 1\n4 4 2\n1 1 0 1\n0 0 0 1\n1 0 1 1\n1 0 0 0\n5 5 1\n1 1 1 0 1\n0 1 0 1 0\n1 0 1 0 1\n0 1 0 1 0\n1 0 1 0 1\n5 5 2\n0 0 0 0 0\n0 0 0 0 0\n0 0 1 0 0\n0 0 0 0 0\n0 0 0 0 0\n11 11 3\n0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0\n0 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are working as a night watchman in an office building. Your task is to check whether all the lights in the building are turned off after all the office workers in the building hav...
p01018 Warping Girl_3280
Problem Aizu Magic School is a school where people who can use magic gather. Haruka, one of the students of that school, can use the magic of warp on the magic team. From her house to the school, there is a straight road of length L. There are also magic circles on this road. She uses this road every day to go to sch...
INF = 10 ** 20 l, n = map(int, input().split()) lst = [list(map(int, input().split())) for _ in range(n)] + [[l, 0, INF]] lst.sort() score = {} for p, _, _ in lst: score[p] = p for i in range(n + 1): p, d, t = lst[i] for j in range(i + 1, n + 1): to_p, _, _ = lst[j] if to_p >= p + d: score[to_p] = m...
{ "input": [ "10 2\n2 3 1\n3 5 1", "1 0", "10 3\n2 2 1\n4 2 1\n8 1 1", "10 2\n2 3 1\n1 5 1", "10 2\n2 3 1\n1 5 0", "10 0\n2 3 1\n1 5 1", "18 0\n2 3 1\n1 5 1", "0 0\n2 -1 1\n0 1 1", "1 0\n2 0 2\n1 2 1", "2 1\n1 0 7\n4 -1 0", "10 3\n2 0 1\n4 2 1\n8 1 1", "34 0\n2 3 1\n1 5...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem Aizu Magic School is a school where people who can use magic gather. Haruka, one of the students of that school, can use the magic of warp on the magic team. From her house ...
p01290 Queen's Case_3284
A small country called Maltius was governed by a queen. The queen was known as an oppressive ruler. People in the country suffered from heavy taxes and forced labor. So some young people decided to form a revolutionary army and fight against the queen. Now, they besieged the palace and have just rushed into the entranc...
from collections import deque, defaultdict import sys readline = sys.stdin.readline write = sys.stdout.write dd = ((-1, 0), (0, -1), (1, 0), (0, 1), (0, 0)) def solve(): W, H = map(int, readline().split()) if W == H == 0: return False S = [readline().strip() for i in range(H)] px = py = qx = qy ...
{ "input": [ "2 2\nQE\nEA\n3 1\nQAE\n3 1\nAQE\n5 5\n..E..\n.###.\nA###Q\n.###.\n..E..\n5 1\nA.E.Q\n5 5\nA....\n.\n..E..\n.####\n....Q\n0 0", "2 2\nQE\nEA\n3 1\nQAE\n3 1\nAQE\n5 5\n..E..\n.###.\nA###Q\n.###.\n..E..\n5 1\nA.E.Q\n5 5\nA....\n####.\n..E..\n.####\n....Q\n0 0", "2 2\nQE\nEA\n3 1\nQAE\n3 1\nAQE\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A small country called Maltius was governed by a queen. The queen was known as an oppressive ruler. People in the country suffered from heavy taxes and forced labor. So some young peo...