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
p01831 Line Gimmick_21259
Example Input 7 >> Output 7
import re n = int(input()) s = input() res = 10**10 res = min(res, len(re.search(r'^<*', s).group())) res = min(res, len(re.search(r'>*$', s).group())) print(n-res)
{ "input": [ "7\n>>", "7\n>=", "7\n=>", "7\n?=", "7\n=?", "7\n<?", "7\n?<", "7\n><", "7\n>?", "7\n?>", "7\n?;", "7\n@=", "7\n;?", "7\n:?", "7\n<=", "7\n=<", "7\n@>", "7\n=@", "7\n@;", "7\n==", "7\n@:", "7\n:>", "7\n;=", "7...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Example Input 7 >> Output 7 ### Input: 7 >> ### Output: 7 ### Input: 7 >= ### Output: 7 ### Code: import re n = int(input()) s = input() res = 10**10 res = min(res, len(re.se...
p02113 Palindrome_21263
Problem Taro has N character strings, each of which is L in length. Taro loves palindromes, so I want to make palindromes as long as possible by selecting some of the N character strings and arranging them in any order. Find the longest palindrome that Taro can make. If there are more than one, output the smallest on...
from functools import cmp_to_key n, l = map(int, input().split()) ss = {} for i in range(n): s = input() ss[s] = ss.get(s, 0) + 1 ans = "" ma = "" for s in sorted(ss): if s[::-1] == s: ans += s * (ss[s]//2) ss[s] -= 2*(ss[s]//2) if ss[s] > 0 and len(ma) < len(s): ma = s ...
{ "input": [ "5 1\na\nb\nc\na\nc", "3 3\nuki\nuku\nuke", "4 2\noi\nio\nrr\nrr", "5 2\na\nb\nc\na\nc", "3 2\nuki\nuku\nuke", "4 2\npi\nio\nrr\nrr", "4 2\npi\nio\nrr\nqr", "7 2\na\nb\nc\n`\nc", "2 3\na\nb\nc\n`\nc", "5 1\na\nb\nc\nb\nc", "4 2\noi\nio\nrr\nsr", "5 2\na\na\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem Taro has N character strings, each of which is L in length. Taro loves palindromes, so I want to make palindromes as long as possible by selecting some of the N character str...
p02253 Activity Selection Problem_21266
There are $ n $ acitivities with start times $ \\ {s_i \\} $ and finish times $ \\ {t_i \\} $. Assuming that a person can only work on a single activity at a time, find the maximum number of activities that can be performed by a single person. Constraints * $ 1 \ le n \ le 10 ^ 5 $ * $ 1 \ le s_i \ lt t_i \ le 10 ^ 9...
n = int(input()) A = [[int(i) for i in input().split()] for _ in range(n)] A.sort(key = lambda x:x[1]) t = 0 ans = 0 for i in A: if t < i[0]: ans += 1 t = i[1] print(ans)
{ "input": [ "5\n1 2\n3 9\n3 5\n5 9\n6 8", "3\n1 2\n2 3\n3 4", "3\n1 5\n3 4\n2 5", "5\n2 2\n3 9\n3 5\n5 9\n6 8", "3\n1 1\n2 3\n3 4", "3\n0 5\n3 4\n2 5", "5\n3 2\n5 5\n4 3\n0 3\n6 8", "5\n2 2\n3 9\n4 5\n5 9\n6 8", "3\n1 1\n2 4\n3 4", "3\n-1 5\n3 4\n2 5", "5\n2 2\n3 9\n4 5\n1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are $ n $ acitivities with start times $ \\ {s_i \\} $ and finish times $ \\ {t_i \\} $. Assuming that a person can only work on a single activity at a time, find the maximum nu...
p02401 Simple Calculator_21270
Write a program which reads two integers a, b and an operator op, and then prints the value of a op b. The operator op is '+', '-', '*' or '/' (sum, difference, product or quotient). The division should truncate any fractional part. Constraints * 0 ≤ a, b ≤ 20000 * No divisions by zero are given. Input The input c...
while(1): a,op,b = input().split() a ,b = int(a), int(b) if(op == '+'): print(a+b) if(op == '-'): print(a-b) if(op == '*'): print(a*b) if(op == '/'): print(int(a/b)) if(op == '?'): break
{ "input": [ "1 + 2\n56 - 18\n13 * 2\n100 / 10\n27 + 81\n0 ? 0", "1 + 2\n56 - 18\n13 * 2\n100 / 10\n27 + 81\n-1 ? 0", "1 + 2\n56 - 18\n13 * 2\n100 / 10\n27 , 81\n-1 ? 0", "1 + 2\n56 - 18\n21 * 2\n100 / 10\n27 , 81\n-1 ? 0", "1 + 2\n56 - 18\n13 * 2\n100 / 10\n27 + 136\n-1 ? 0", "1 + 2\n56 - 18\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Write a program which reads two integers a, b and an operator op, and then prints the value of a op b. The operator op is '+', '-', '*' or '/' (sum, difference, product or quotient)....
1028_E. Restore Array_21282
While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers a_1, a_2, …, a_n. Since the talk was long and not promising, Kostya created a new cyclic array b_1, b_2, …, b_{n} so that b_i = (a_i mod a_{i + 1}), where we take a_{n+1} = a_1. Here mod is the [modulo operati...
####################################### Define maxN = int(2e5 + 10) n = 0 b = [0] * maxN ans = [0] * maxN ####################################### Functions ####################################### Main n = int(input()) inp = input().split() flag = False for i in range(1, n + 1) : b[i] = int(inp[i - 1]) if i > 1...
{ "input": [ "2\n4 4\n", "4\n1 3 1 0\n", "100\n57 5 28 44 99 10 66 93 76 32 67 92 67 81 33 3 6 6 67 10 41 72 5 71 27 22 21 54 21 59 36 62 43 39 28 49 55 65 21 73 87 40 0 62 67 59 40 18 56 71 15 97 73 73 2 61 54 44 6 52 25 34 13 20 18 13 25 51 19 66 63 87 50 63 82 60 11 11 54 58 88 20 33 40 85 68 13 74 37 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers a_1, a_2, …, a_n. Since the talk was long and not promising, Kostya crea...
1047_A. Little C Loves 3 I_21286
Little C loves number «3» very much. He loves all things about it. Now he has a positive integer n. He wants to split n into 3 positive integers a,b,c, such that a+b+c=n and none of the 3 integers is a multiple of 3. Help him to find a solution. Input A single line containing one integer n (3 ≤ n ≤ 10^9) — the integ...
def main(): n = int(input()) if (n % 3 == 0) or (n % 3 == 1): print(1, 1, n - 2) else: print(1, 2, n - 3) if __name__ == '__main__': main()
{ "input": [ "233", "3\n", "6\n", "348050648\n", "8\n", "288887657\n", "417896689\n", "21\n", "25\n", "240248899\n", "238160779\n", "101\n", "463282042\n", "484489325\n", "16\n", "762246473\n", "387420489\n", "597632599\n", "7\n", "929415...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little C loves number «3» very much. He loves all things about it. Now he has a positive integer n. He wants to split n into 3 positive integers a,b,c, such that a+b+c=n and none of ...
1092_E. Minimal Diameter Forest_21293
You are given a forest — an undirected graph with n vertices such that each its connected component is a tree. The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices. You task is to add some edges (possibly zero) ...
from collections import deque ##make the tree n,m=[int(x) for x in input().split()] tree={} for i in range(m): a,b=[int(x) for x in input().split()] if a not in tree: tree[a]=[b] else: tree[a].append(b) if b not in tree: tree[b]=[a] else: tree[b].append(a) for i in ra...
{ "input": [ "3 2\n1 3\n2 3\n", "2 0\n", "4 2\n1 2\n2 3\n", "1000 0\n", "5 4\n5 2\n3 2\n5 4\n3 1\n", "5 3\n3 2\n1 3\n5 4\n", "5 0\n", "100 0\n", "100 50\n2 1\n3 4\n6 5\n7 8\n9 10\n11 12\n13 14\n12 13\n18 17\n21 22\n20 23\n20 21\n25 26\n28 27\n30 29\n32 31\n34 35\n33 35\n34 32\n37 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a forest — an undirected graph with n vertices such that each its connected component is a tree. The diameter (aka "longest shortest path") of a connected undirected gr...
1111_D. Destroy the Colony_21296
There is a colony of villains with several holes aligned in a row, where each hole contains exactly one villain. Each colony arrangement can be expressed as a string of even length, where the i-th character of the string represents the type of villain in the i-th hole. Iron Man can destroy a colony only if the colon...
from sys import stdin MOD = 1000000007 s = stdin.readline().strip() n = len(s) buc = [0] * 101 fac = [0] * (n + 1) inv = [0] * (n + 1) dp = [0] * (n + 1) # temp_dp = [0] * (n+1) ans = [[0] * 55 for _ in range(55)] def find(c: 'str') -> 'int': if 'A' <= c <= 'Z': return ord(c) - ord('A') + 26 else: ...
{ "input": [ "abcd\n1\n1 3\n", "abba\n2\n1 4\n1 2\n", "AAaa\n2\n1 2\n1 3\n", "sYDMlMKcqjyzmIKKjjcNyUgaixfaNUJYXjHXRHiRzNyHysnxTt\n68\n19 15\n46 34\n38 30\n50 39\n22 11\n4 14\n23 2\n10 47\n42 15\n9 5\n38 40\n43 16\n11 46\n29 46\n49 5\n28 1\n33 22\n48 8\n5 33\n8 34\n31 4\n49 3\n22 21\n31 22\n13 48\n17 5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a colony of villains with several holes aligned in a row, where each hole contains exactly one villain. Each colony arrangement can be expressed as a string of even length, ...
1141_B. Maximal Continuous Rest_21300
Each day in Berland consists of n hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence a_1, a_2, ..., a_n (each a_i is either 0 or 1), where a_i=0 if Polycarp works during the i-th hour of the day and a_i=1 if Polycarp rests during the i-th hour of the day. Days go ...
n=int(input()) ans=0 curr=0 l=list(map(int,input().split())) for i in range(2*n): if(l[i%n]==1): curr+=1 else: curr=0 ans=max(ans,curr) print(ans)
{ "input": [ "5\n1 0 1 0 1\n", "6\n0 1 0 1 1 0\n", "3\n0 0 0\n", "7\n1 0 1 1 1 0 1\n", "3\n0 1 1\n", "3\n1 0 0\n", "3\n1 0 1\n", "2\n1 0\n", "23\n1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n", "3\n0 0 1\n", "4\n1 1 0 1\n", "2\n0 0\n", "2\n0 1\n", "21\n1 0 1 0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Each day in Berland consists of n hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence a_1, a_2, ..., a_n (each a_i is either 0 or...
115_A. Party_21304
A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manag...
import sys;readline = sys.stdin.buffer.readline def i1(): return int(readline()) def nl(): return [int(s) for s in readline().split()] def nn(n): return [int(readline()) for i in range(n)] def nlp(x): return [int(s)+x for s in readline().split()] def nll(n): return [[int(s) for s in readline().split()] for i in range(n...
{ "input": [ "5\n-1\n1\n2\n1\n-1\n", "3\n2\n-1\n1\n", "77\n53\n-1\n48\n-1\n51\n-1\n54\n43\n70\n19\n64\n31\n45\n58\n6\n61\n41\n56\n34\n-1\n54\n-1\n66\n65\n74\n16\n38\n1\n2\n61\n-1\n34\n-1\n-1\n7\n19\n57\n13\n61\n42\n-1\n34\n65\n16\n31\n61\n6\n5\n34\n15\n-1\n13\n4\n22\n74\n7\n-1\n70\n38\n61\n45\n53\n46\n22\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An emp...
1182_A. Filling Shapes_21308
You have a given integer n. Find the number of ways to fill all 3 × n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap. <image> This picture describes when n = 4. The left one is the shape and the right one is 3 × n tiles. Input The only line cont...
import sys from random import choice,randint inp=sys.stdin.readline out=sys.stdout.write flsh=sys.stdout.flush sys.setrecursionlimit(10**9) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def MI(): return map(int, inp().str...
{ "input": [ "1\n", "4\n", "7\n", "60\n", "36\n", "18\n", "8\n", "44\n", "39\n", "3\n", "57\n", "5\n", "12\n", "17\n", "19\n", "11\n", "59\n", "47\n", "56\n", "27\n", "6\n", "2\n", "9\n", "24\n", "32\n", "14\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have a given integer n. Find the number of ways to fill all 3 × n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot ove...
11_A. Increasing Sequence_21312
A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasin...
n,d=map(int,input().split()) l=[int(i) for i in input().split()] s=0 import math for i in range(1,n): if l[i]>l[i-1]: continue diff=l[i-1]-l[i]+1 s+=math.ceil(diff/d) l[i]+=d*math.ceil(diff/d) print(s)
{ "input": [ "4 2\n1 3 3 2\n", "2 1\n1 2\n", "3 3\n18 1 9\n", "2 7\n10 20\n", "10 3\n10 24 13 15 18 14 15 26 33 35\n", "2 1\n1 1\n", "10 3\n6 11 4 12 22 15 23 26 24 26\n", "100 3\n529 178 280 403 326 531 671 427 188 866 669 646 421 804 494 609 53 1012 211 243 887 833 900 543 226 42 859...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may ...
1217_D. Coloring Edges_21316
You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given d...
import sys input = sys.stdin.readline def dfs(cur_node, childs, vis, cur_dfs): if cur_node in cur_dfs: return True if vis[cur_node]: return False vis[cur_node] = True cur_dfs.add(cur_node) for ele in childs[cur_node]: if dfs(ele, childs, vis, cur_dfs): return Tr...
{ "input": [ "3 3\n1 2\n2 3\n3 1\n", "4 5\n1 2\n1 3\n3 4\n2 4\n1 4\n", "3 5\n1 3\n1 2\n3 2\n3 1\n2 3\n", "3 4\n3 1\n1 3\n2 3\n3 2\n", "3 6\n1 2\n2 3\n3 1\n2 1\n3 2\n1 3\n", "10 2\n8 7\n10 5\n", "3 6\n1 2\n2 3\n3 1\n2 1\n1 3\n3 2\n", "8 6\n3 5\n8 3\n3 6\n8 5\n4 6\n2 5\n", "12 7\n11 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge i...
1240_A. Save the Nature_21320
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of t...
t = int(input()) for case_num in range(t): n = int(input()) p = list(map(int, input().split(' '))) x, a = map(int, input().split(' ')) y, b = map(int, input().split(' ')) k = int(input()) p.sort() p.reverse() sum = [0] for i in range(n): sum.append(sum[-1] + p[i]) if x < ...
{ "input": [ "4\n1\n100\n50 1\n49 1\n100\n8\n100 200 100 200 100 200 100 100\n10 2\n15 3\n107\n3\n1000000000 1000000000 1000000000\n50 1\n50 1\n3000000000\n5\n200 100 100 100 100\n69 5\n31 2\n90\n", "3\n5\n5000 1000 2000 3400 4300\n1 1\n99 2\n50\n5\n5000 1000 2000 3400 4300\n1 1\n99 2\n51\n10\n100 100 100 100...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of t...
1260_B. Obtain Two Zeroes_21324
You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to mak...
t = int(input()) for i in range(t): a, b = map(int, input().split()) if a > b: [a, b] = [b, a] if (a * 2 >= b and (a + b) % 3 == 0): print("YES") else: print("NO")
{ "input": [ "3\n6 9\n1 1\n1 2\n", "1\n69 69\n", "1\n69 16\n", "3\n6 2\n1 1\n1 2\n", "3\n6 2\n1 1\n1 0\n", "1\n29 16\n", "3\n5 2\n1 2\n2 0\n", "3\n6 9\n1 1\n1 0\n", "3\n6 2\n0 0\n1 2\n", "3\n2 4\n0 1\n0 0\n", "3\n0 0\n2 1\n2 -1\n", "1\n22 16\n", "3\n5 2\n1 1\n1 0\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - ...
1282_B2. K for the Price of One (Hard Version)_21328
This is the hard version of this problem. The only difference is the constraint on k — the number of gifts in the offer. In this version: 2 ≤ k ≤ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "k of goods for the price of one" is held in...
T=int(input()) for _ in range(T): n,amt,k=map(int,input().split()) l=list(map(int,input().split())) l=sorted(l);dp=[0]*(n+1);ans=0; for i in range(n): if i-k>=-1:dp[i]=dp[i-k]+l[i] else:dp[i]=l[i]+dp[i-1] #print(dp) for i in range(n): if dp[i]<=amt:ans=i+1; print(ans)
{ "input": [ "8\n5 6 2\n2 4 3 5 7\n5 11 2\n2 4 3 5 7\n3 2 3\n4 2 6\n5 2 3\n10 1 3 9 2\n2 10000 2\n10000 10000\n2 9999 2\n10000 10000\n4 6 4\n3 2 3 2\n5 5 3\n1 2 2 1 2\n", "2\n2 1 2\n1 1\n2 2000000000 2\n1 1\n", "8\n5 6 2\n2 4 3 5 7\n5 4 2\n2 4 3 5 7\n3 2 3\n4 2 6\n5 2 3\n10 1 3 9 2\n2 10000 2\n10000 10000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This is the hard version of this problem. The only difference is the constraint on k — the number of gifts in the offer. In this version: 2 ≤ k ≤ n. Vasya came to the store to buy go...
1326_C. Permutation Partitions_21332
You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≤ k ≤ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_...
import sys input = sys.stdin.readline if __name__ == "__main__": n, k = map(int, input().split()) p = list(map(int, input().split())) max_val = sum(range(n, n - k, -1)) num_perm = 1 last_idx = -1 for i in range(n): if p[i] > n - k: if last_idx != -1: num_pe...
{ "input": [ "5 5\n2 1 5 3 4\n", "3 2\n2 1 3\n", "7 3\n2 7 3 1 5 4 6\n", "1 1\n1\n", "10 3\n4 6 7 8 9 1 10 3 5 2\n", "2 1\n1 2\n", "3 2\n3 2 1\n", "2 2\n2 1\n", "100 77\n59 92 18 16 45 82 63 43 50 68 19 13 53 79 48 28 94 49 25 77 54 8 61 66 40 100 99 20 35 14 52 56 22 17 57 36 23 9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≤ k ≤ n. A permutation means that every number from 1 to n is contained in p exactly...
1345_B. Card Constructions_21336
A card pyramid of height 1 is constructed by resting two cards against each other. For h>1, a card pyramid of height h is constructed by placing a card pyramid of height h-1 onto a base. A base consists of h pyramids of height 1, and h-1 cards on top. For example, card pyramids of heights 1, 2, and 3 look as follows: ...
import os import bisect def get_sizes(): s = [0] i = 1 while s[-1] <= int(1e9): s.append(s[-1] + (i - 1) * 3 + 2) i += 1 return s SIZES = get_sizes()[1:] def g(n, p = 0): if n < 2: return p b = SIZES[bisect.bisect_right(SIZES, n) - 1] return g(n - b, p + 1) ...
{ "input": [ "5\n3\n14\n15\n24\n1\n", "50\n11833308\n5211498\n64660\n9077929\n2699416\n2432035\n234901\n1337830\n8457191\n10523566\n5722429\n5452712\n5511654\n8587879\n348160\n3141182\n2661936\n395300\n6950223\n2119926\n4407089\n3559060\n10228325\n5752720\n1607500\n647047\n8904557\n1954681\n698061\n9852830\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A card pyramid of height 1 is constructed by resting two cards against each other. For h>1, a card pyramid of height h is constructed by placing a card pyramid of height h-1 onto a ba...
1367_A. Short Substrings_21340
Alice guesses the strings that Bob made for her. At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a. Bob builds b from a...
t=int(input()) for i in range (t): a=input() print(a[0::2]+a[-1])
{ "input": [ "4\nabbaac\nac\nbccddaaf\nzzzzzzzzzz\n", "6\nabbaac\nabbaac\nabbaac\nac\nbccddaaf\nzzzzzzzzzz\n", "10\nabbaac\nac\nbccddaaf\nzzzzzzzzzz\nabbaac\nac\nbccddaaf\nzzzzzzzzzz\nbccddaaf\nzzzzzzzzzz\n", "1\nassaad\n", "11\nabbaac\nabbaac\nabbaac\nabbaac\nabbaac\nabbaac\nabbaac\nabbaac\nac\nb...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice guesses the strings that Bob made for her. At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more charac...
1388_A. Captain Flint and Crew Recruitment_21344
Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task. Recently, out of blue Captain Flint has been interested in ma...
# import sys # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') t = 1 t = int(input()) while t: t -= 1 n = int(input()) # s = input() if n > 30: if (n-30) in [6, 10, 14]: if n > 31: print('YES') print(6,10,15,n-31) ...
{ "input": [ "7\n7\n23\n31\n36\n44\n100\n258\n", "1\n78788\n", "1\n15239\n", "7\n7\n23\n31\n36\n44\n100\n258\n", "1\n52340\n", "1\n8968\n", "7\n7\n23\n31\n36\n73\n100\n258\n", "7\n7\n23\n30\n36\n44\n100\n258\n", "1\n33480\n", "1\n6790\n", "1\n65901\n", "1\n914\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful...
142_B. Help General_21350
Once upon a time in the Kingdom of Far Far Away lived Sir Lancelot, the chief Royal General. He was very proud of his men and he liked to invite the King to come and watch drill exercises which demonstrated the fighting techniques and tactics of the squad he was in charge of. But time went by and one day Sir Lancelot h...
n, m = sorted(map(int, input().split())) k = 4 * (m >> 2) print(m if n == 1 else k + 2 * min(2, m - k) if n == 2 else (m * n + 1 >> 1))
{ "input": [ "2 4\n", "3 4\n", "915 72\n", "967 4\n", "997 997\n", "998 999\n", "1 991\n", "757 210\n", "1 865\n", "995 2\n", "862 330\n", "1 999\n", "953 874\n", "2 348\n", "998 998\n", "1 1000\n", "555 349\n", "993 2\n", "936 759\n", "4...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once upon a time in the Kingdom of Far Far Away lived Sir Lancelot, the chief Royal General. He was very proud of his men and he liked to invite the King to come and watch drill exerc...
1476_D. Journey_21357
There are n + 1 cities, numbered from 0 to n. n roads connect these cities, the i-th road connects cities i - 1 and i (i ∈ [1, n]). Each road has a direction. The directions are given by a string of n characters such that each character is either L or R. If the i-th character is L, it means that the i-th road initiall...
from sys import stdin, exit from bisect import bisect_left as bl, bisect_right as br from itertools import accumulate input = lambda: stdin.readline()[:-1] intput = lambda: int(input()) sinput = lambda: input().split() intsput = lambda: map(int, sinput()) def dprint(*args, **kwargs): if debugging: print(...
{ "input": [ "2\n6\nLRRRLL\n3\nLRL\n", "2\n6\nLLRRRL\n3\nLRL\n", "2\n6\nLLRRRL\n3\nRLL\n", "2\n6\nLRRRLL\n3\nRLL\n", "2\n6\nLLRRRL\n3\nLLR\n", "2\n6\nLRRRLL\n3\nLLR\n", "2\n6\nLRLRRL\n3\nLRL\n", "2\n6\nRRLRLL\n3\nRLL\n", "2\n6\nRRRLLL\n3\nLLR\n", "2\n6\nRLRRLL\n3\nLLR\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n + 1 cities, numbered from 0 to n. n roads connect these cities, the i-th road connects cities i - 1 and i (i ∈ [1, n]). Each road has a direction. The directions are give...
1503_A. Balance the Bits_21361
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string s of length n. Construct two balanced bracket sequences ...
T = int(input()) for ts in range(T): N = int(input()) s = input() if s[0] == '0' or s[-1] == '0': print("NO") continue a = ['('] b = ['('] # y_n = 0 # y = jie(1, 1, 1) # if y == 1: # print("YES") # print(''.join(a)) # print(''.join(b)) # else: ...
{ "input": [ "3\n6\n101101\n10\n1001101101\n4\n1100\n", "3\n6\n101101\n10\n1001101101\n4\n1100\n", "3\n6\n100101\n10\n1001101101\n4\n1100\n", "3\n6\n101101\n10\n1001001101\n4\n1100\n", "3\n6\n101101\n10\n1001101101\n4\n0100\n", "3\n6\n101101\n10\n1001000101\n4\n1100\n", "3\n6\n001101\n10\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are b...
1526_E. Oolimry and Suffix Array_21365
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array. More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array. Let s be a string of length n. Then the i-th suffix of s is the subst...
MOD = 998244353 n, k = map(int, input().split()) arr = list(map(int, input().split())) pos = [-1] * (n+1) for i, a in enumerate(arr): pos[a] = i pos[-1] = -1 for i in range(n-1): if pos[arr[i]+1] < pos[arr[i+1]+1]: k += 1 if k < n: print(0) else: fact = [1] for i in range(1, k+1): ...
{ "input": [ "6 200000\n0 1 2 3 4 5\n", "3 2\n0 2 1\n", "5 1\n0 1 2 3 4\n", "7 6\n3 2 4 1 0 5 6\n", "1 69258\n0\n", "1 195078\n0\n", "2 132707\n1 0\n", "1 1\n0\n", "7 129714\n5 6 0 2 1 4 3\n", "4 199473\n1 3 2 0\n", "73 95\n4 53 37 9 45 54 16 34 12 10 13 24 2 36 33 48 50 43...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array. More formally, given a suffix array of length n and having an alphabet size...
158_C. Cd and pwd commands_21369
Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory). Directories in Vasya's operating system form a traditional hierarchical tree structure...
#!/usr/bin/env python3 def pwd(cur): if len(cur) == 0: print('/') else: print('/' + '/'.join(cur) + '/') def process(cmd, cur): cmd = cmd[3:] ds = cmd.split('/') if cmd[0] == '/': cur = [] for d in ds: if d == '..': cur.pop() elif len(d) ...
{ "input": [ "7\npwd\ncd /home/vasya\npwd\ncd ..\npwd\ncd vasya/../petya\npwd\n", "4\ncd /a/b\npwd\ncd ../a/b\npwd\n", "17\npwd\ncd denwxe/../jhj/rxit/ie\npwd\ncd /tmyuylvul/qev/ezqit\npwd\ncd ../gxsfgyuspg/irleht\npwd\ncd wq/pqyz/tjotsmdzja\npwd\ncd ia/hs/../u/nemol/ffhf\npwd\ncd /lrdm/mvwxwb/llib\npwd\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current di...
224_E. Partial Sums_21377
You've got an array a, consisting of n integers. The array elements are indexed from 1 to n. Let's determine a two step operation like that: 1. First we build by the array a an array s of partial sums, consisting of n elements. Element number i (1 ≤ i ≤ n) of array s equals <image>. The operation x mod y means that ...
def ncr(n, r, p): # initialize numerator # and denominator num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p p=10**9+7 n,k=map(int,input().split()) b=list(map(int,input().split())) if k==...
{ "input": [ "3 1\n1 2 3\n", "5 0\n3 14 15 92 6\n", "1 1\n0\n", "17 239\n663 360 509 307 311 501 523 370 302 601 541 42 328 200 196 110 573\n", "1 1\n3\n", "1 0\n123\n", "5 20\n11 5 6 8 11\n", "10 1000000\n1 2 3 4 84 5 6 7 8 9\n", "13 666\n84 89 29 103 128 233 190 122 117 208 119 9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You've got an array a, consisting of n integers. The array elements are indexed from 1 to n. Let's determine a two step operation like that: 1. First we build by the array a an arr...
297_A. Parity Game_21385
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: * Write parity(a...
print('YES' if input().count('1')+1>>1<<1 >= input().count('1') else 'NO')
{ "input": [ "01011\n0110\n", "0011\n1110\n", "11111\n111111\n", "1\n11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (s...
31_D. Chocolate_21389
Bob has a rectangular chocolate bar of the size W × H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parall...
w,h,n=list(map(int,input().split())) a=[[0 for i in range(2*w-1)] for j in range(2*h-1)] for i in range(1,2*h-1,2): for j in range(1,2*w-1,2): a[i][j]=' ' for i in range(n): x1,y1,x2,y2=list(map(int,input().split())) if x1==x2: if x1!=0 and x1!=w: for j in range(min(y1,y2),max(y1...
{ "input": [ "2 4 2\n0 1 2 1\n0 3 2 3\n", "2 2 3\n1 0 1 2\n0 1 1 1\n1 1 2 1\n", "2 2 2\n1 0 1 2\n0 1 1 1\n", "10 10 4\n9 0 9 10\n4 0 4 10\n1 0 1 10\n1 4 4 4\n", "5 5 3\n2 1 2 5\n0 1 5 1\n4 0 4 1\n", "100 100 1\n0 14 100 14\n", "9 8 5\n4 3 4 4\n0 4 9 4\n5 4 5 8\n0 3 9 3\n1 4 1 8\n", "2 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bob has a rectangular chocolate bar of the size W × H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the po...
344_D. Alternating Current_21393
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended u...
class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): ...
{ "input": [ "++\n", "+-\n", "-++-\n", "-\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: Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in t...
368_A. Sereja and Coat Rack_21397
Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook. Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to...
racks, fine = map(int, input().split()) cost = list(map(int, input().split())) cost.sort() guests = int(input()) if guests > racks: print (sum(cost) - ((guests - racks) * fine)) else: print (sum(cost[:guests]))
{ "input": [ "2 1\n2 1\n2\n", "2 1\n2 1\n10\n", "34 30\n59 23 47 93 38 26 48 59 3 8 99 31 93 1 79 100 53 49 83 41 16 76 63 68 37 98 19 98 29 52 17 31 50 26\n59\n", "4 39\n28 9 46 9\n86\n", "5 72\n4 22 64 7 64\n11\n", "53 75\n74 53 95 77 27 97 73 50 41 75 20 44 12 42 90 20 66 6 86 17 51 16 10 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai...
390_B. Inna, Dima and Song_21401
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much. A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at vo...
num = int(input()) arr_a = list(map(int,(input()).split())) arr_b = list(map(int,(input()).split())) joy = 0 for i in range(num): if arr_b[i] == 1: joy -= 1 continue if arr_a[i] * 2 >= arr_b[i]: temp = arr_b[i] // 2 joy = joy + temp * (arr_b[i] - temp) else: joy -= 1 ...
{ "input": [ "3\n1 1 2\n2 2 3\n", "1\n2\n5\n", "10\n1 2 3 4 5 6 7 8 9 10\n1 2 3 4 5 6 7 8 9 10\n", "2\n1 1\n2 1\n", "10\n2 2 3 4 5 6 7 8 9 10\n2 2 3 4 5 6 7 8 9 10\n", "3\n10000 10000 10000\n5000 5000 1\n", "10\n1 2 3 4 5 6 7 16 9 10\n1 2 3 4 5 6 7 8 9 10\n", "10\n2 2 3 4 5 5 7 8 9 10\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very...
412_A. Poster_21405
The R1 company has recently bought a high rise building in the centre of Moscow for its main office. It's time to decorate the new office, and the first thing to do is to write the company's slogan above the main entrance to the building. The slogan of the company consists of n characters, so the decorators hung a lar...
n, k = map(int, input().split()) s = input() def goRight(): i = k - 1 while i < n - 1: print("RIGHT") i += 1 i = n - 1 while i >= 0: print("PRINT", s[i]) if i: print("LEFT") i -= 1 def goLeft(): i = k - 1 while i > 0: print("LEFT") i -= 1 i = 0 while i < n: print(...
{ "input": [ "2 1\nR1\n", "2 2\nR1\n", "6 4\nGO?GO!\n", "7 3\nME,YOU.\n", "100 100\nE?F,W.,,O51!!G13ZWP?YHWRT69?RQPW7,V,EM3336F1YAIKJIME1M45?LJM42?45V7221?P.DIO9FK245LXKMR4ALKPDLA5YI2Y\n", "100 79\nF2.58O.L4A!QX!,.,YQUE.RZW.ENQCZKUFNG?.J6FT?L59BIHKFB?,44MAHSTD8?Z.UP3N!76YW6KVI?4AKWDPP0?3HPERM3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The R1 company has recently bought a high rise building in the centre of Moscow for its main office. It's time to decorate the new office, and the first thing to do is to write the co...
439_C. Devu and Partitioning of the Array_21409
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into k disjoint non-empty part...
def solve(): n,k,p=map(int,input().split()) a=list(map(int,input().split())) even=list(filter(lambda x: x%2==0, a)) odd=list(filter(lambda x:x%2==1, a)) if (len(odd)-(k-p))%2!=0: print("NO"); return ans=[[] for _ in range(k)] for i in range(k-p): if odd: ...
{ "input": [ "5 5 3\n7 14 2 9 5\n", "5 3 1\n1 2 3 7 5\n", "5 5 3\n2 6 10 5 9\n", "2 2 2\n2 4\n", "4 1 1\n266639563 36517 172287193 166673809\n", "2 2 1\n3 2\n", "1 1 1\n2\n", "3 2 0\n1 3 2\n", "1 1 0\n2\n", "2 2 2\n2 3\n", "30 24 12\n459253071 24156 64054 270713791 73479661...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solv...
484_D. Kindergarten_21416
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maxim...
#import sys #input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) dp = [0] * n pos, neg = -10 ** 18, -10 ** 18 for i, x in enumerate(a): prv = 0 if i == 0 else dp[i - 1] dp[i] = max(neg + x, pos - x, prv) pos = max(pos, prv + x) neg = max(neg, prv - x) print(dp[-1])
{ "input": [ "3\n3 3 3\n", "5\n1 2 3 1 2\n", "1\n0\n", "4\n1 4 2 3\n", "4\n23 7 5 1\n", "8\n4 5 3 6 2 7 1 8\n", "8\n23 2 7 5 15 8 4 10\n", "4\n23 5 7 1\n", "2\n-1000000000 1000000000\n", "4\n2 4 2 3\n", "4\n23 7 1 1\n", "8\n23 2 7 5 15 8 4 19\n", "4\n15 5 7 1\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child shou...
508_D. Tanya and Password_21420
While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of n + 2 characters. She has written all the possible n three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the password ...
from collections import Counter, defaultdict def main(): ht = defaultdict(list) cnt = Counter() n = int(input()) for _ in range(n): s = input() h, t = s[:2], s[1:] ht[h].append(t) cnt[h] += 1 cnt[t] -= 1 st = nd = flag = True for t, i in cnt.items(): ...
{ "input": [ "4\nabc\nbCb\ncb1\nb13\n", "7\naaa\naaa\naaa\naaa\naaa\naaa\naaa\n", "5\naca\naba\naba\ncab\nbac\n", "1\nabc\n", "5\n123\n234\n345\n456\n567\n", "8\nzHW\ncwx\nxmo\nWcw\nox1\nwxm\nmox\nHWc\n", "2\nbba\nabb\n", "99\naaJ\nJx9\naLL\nrCx\nllJ\naja\nxxr\nLal\nv9C\njrL\nLvL\nJLl\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of n + 2 characters. She has written all t...
583_C. GCD Table_21428
The GCD table G of size n × n for an array of positive integers a of length n is defined by formula <image> Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both x and y, it is denoted as <image>. For example, for array a = {4, 3, 6,...
import collections as cc import math as mt I=lambda:list(map(int,input().split())) n,=I() l=I() f=cc.Counter(l) ans=[] for i in range(n): now=max(f) f[now]-=1 for j in ans: f[mt.gcd(now,j)]-=2 ans.append(now) f+=cc.Counter() print(*ans)
{ "input": [ "2\n1 1 1 1\n", "1\n42\n", "4\n2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2\n", "5\n4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1\n", "4\n1 1 1 1 1 1 1 1 1 1 1 1 3 3 3 3\n", "5\n27 675 1 1 347621274 5 2 13 189 738040275 5 1 189 13 1 959752125 770516962 769220855 5 5 2 675 1 1 27\n", "3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The GCD table G of size n × n for an array of positive integers a of length n is defined by formula <image> Let us remind you that the greatest common divisor (GCD) of two positive...
605_C. Freelancer's Dreams_21431
Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired flat in Moscow costs q dollars. Mikhail is determined to follow his dreams and registered at a freelance site. He has suggestions to wo...
def get_bounds(points): if len(points) == 1: return points[:] points.sort() bounds = [points[0], points[1]] for xi, yi in points[2:]: while len(bounds) > 1 and not is_convex(bounds, xi, yi): del bounds[-1] bounds.append((xi, yi)) return bounds def is...
{ "input": [ "3 20 20\n6 2\n1 3\n2 6\n", "4 1 1\n2 3\n3 2\n2 3\n3 2\n", "10 18 25\n4 8\n16 27\n16 13\n1 26\n8 13\n2 14\n24 8\n4 29\n3 19\n19 20\n", "2 2 4\n2 4\n5 2\n", "2 4 1\n2 4\n5 2\n", "10 468662 93838\n589910 727627\n279516 867207\n470524 533177\n467834 784167\n295605 512137\n104422 6298...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired...
627_D. Preorder Test_21434
For his computer science class, Jacob builds a model tree with sticks and balls containing n nodes in the shape of a tree. Jacob has spent ai minutes building the i-th ball in the tree. Jacob's teacher will evaluate his model and grade Jacob based on the effort he has put in. However, she does not have enough time to ...
import sys input = sys.stdin.readline n, k = map(int, input().split()) a = [int(i) for i in input().split()] g = [[] for _ in range(n)] for i in range(n - 1): u, v = map(int, input().split()) g[u-1].append(v-1) g[v-1].append(u-1) stack = [0] done = [False] * n par = [0] * n order = [] while len(stack) > 0: x = sta...
{ "input": [ "4 2\n1 5 5 5\n1 2\n1 3\n1 4\n", "5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3\n", "2 1\n1 100000\n2 1\n", "10 10\n794273 814140 758469 932911 607860 683826 987442 652494 952171 698608\n1 3\n3 8\n2 7\n2 1\n2 9\n3 10\n6 4\n9 6\n3 5\n", "2 2\n1 1000000\n1 2\n", "10 3\n703660 186846 317819 628...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For his computer science class, Jacob builds a model tree with sticks and balls containing n nodes in the shape of a tree. Jacob has spent ai minutes building the i-th ball in the tre...
651_D. Image Preview_21438
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a sec...
import bisect def preview(n, a, b, t, S): t -= b+1 if S[0] else 1 S[0] = False if t < 0: return 0 R = [] s = 0 for i in range(1, n): s += a + (b+1 if S[i] else 1) if s > t: break R.append(s) else: return n L = [] s = 0 for i...
{ "input": [ "5 2 4 1000\nhhwhh\n", "5 2 4 13\nhhwhh\n", "4 2 3 10\nwwhw\n", "3 1 100 10\nwhw\n", "100 20 100 10202\nwwwwhhwhhwhhwhhhhhwwwhhhwwwhwwhwhhwwhhwwwhwwhwwwhwhwhwwhhhwhwhhwhwwhhwhwhwwwhwwwwhwhwwwwhwhhhwhwhwww\n", "7 1 100 8\nhhhwwwh\n", "5 2 4 12\nhhhwh\n", "12 10 10 1\nwhwhwh...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If ...
677_B. Vanya and Food Processor_21442
Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato. Vanya h...
n, h, k = map(int, input().split()) pots = map(int, input().split()) comb = [0] for pot in pots: if (comb[-1] % k + pot <= h): comb[-1] += pot else: comb.append(pot) sec = lambda x : x // k + int(x % k > 0) ans = sum(map(sec, comb)) print(ans)
{ "input": [ "5 6 3\n5 5 5 5 5\n", "5 6 3\n1 2 1 1 1\n", "5 6 3\n5 4 3 2 1\n", "4 1000000000 1\n1000000000 1000000000 1000000000 1000000000\n", "25 1000000000 1\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato ...
6_B. President's Office_21446
President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all ...
n, m, c = input().split() n, m = int(n), int(m) lst = [] for i in range(n): lst.append(input()) N = set() for i in range(n): for j in range(m): if lst[i][j] == c: if j != 0 and lst[i][j-1] != c and lst[i][j-1] != ".": N.add(lst[i][j-1]) if i != 0 and lst[i-1][j] != c and lst[i-1][j] != ".": N.add(lst...
{ "input": [ "3 4 R\nG.B.\n.RR.\nTTT.\n", "3 3 Z\n...\n.H.\n..Z\n", "3 2 W\nOO\nWW\nWW\n", "10 10 T\nCCEEEKKKHJ\nCCRRRRRRHJ\nCC..XFFOOO\nZZZZZFFOOO\n..PTTFFOOO\nAAATTFFOOO\nAAATTYYYYY\nAAATTYYYYY\nAAAMMYYYYY\nAAA..YYYYY\n", "2 2 W\nKW\nKW\n", "1 1 C\nC\n", "8 10 B\n..BBBBEEEE\n..BBBBEEEE\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Ea...
721_D. Maxim and Array_21450
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n) and replaces the i-th element of ...
import heapq as hq from math import ceil n, k, x = [int(i) for i in input().strip().split(' ')] arr = [int(i) for i in input().strip().split(' ')] is_neg = False for i in arr: if i < 0: is_neg = True if is_neg == False else False narr = [[abs(i), pos, i < 0] for pos, i in enumerate(arr)] hq.heapify(narr) i...
{ "input": [ "3 2 7\n5 4 2\n", "5 3 1\n5 4 4 5 5\n", "5 3 1\n5 4 3 5 2\n", "5 3 1\n5 4 3 5 5\n", "100 21063 1\n-2 -1 -1 -2 -1 -2 -1 0 0 -2 0 0 0 0 0 0 0 -1 -1 -2 2 2 0 2 2 2 2 0 1 1 2 1 2 1 0 1 2 2 0 2 0 2 1 1 2 1 0 1 1 0 1 0 0 0 0 0 0 2 1 0 2 2 0 2 0 1 1 2 1 2 2 1 1 1 1 1 0 2 2 2 2 2 2 0 0 0 0 0 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it fr...
743_B. Chloe and the sequence _21454
Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems to get to the olympiad like Vladik, but she was confused by the task proposed on the olympiad. Let's consider the following algorithm of generating a sequence of integers. Initially we have a sequence consisting of a single element eq...
n, k = list(map(int, input().split())) finish = False length = 1 for i in range(n-1): length = length * 2 + 1 while n!=1: gran = length // 2 + 1 if k == gran: print(n) finish = True break if k < gran: length = length // 2 n -= 1 if k > gran: k -= (...
{ "input": [ "4 8\n", "3 2\n", "50 1\n", "50 844424930131968\n", "16 49152\n", "4 12\n", "1 1\n", "49 351843720888320\n", "50 704239287953456\n", "13 1368\n", "19 1024\n", "46 34394312982528\n", "27 6586544\n", "48 133865540681728\n", "50 1125899906842623\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems to get to the olympiad like Vladik, but she was confused by the task proposed on the olympiad. Le...
766_D. Mahmoud and a Dictionary_21458
Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discovers a new relation between two words. He know that if two words have ...
class Union: def __init__(self, n): self.p = {i:i for i in range(n)} self.rank = {i:1 for i in range(n)} def find(self, x): if x < 0: return x if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] def union(self, x, y)...
{ "input": [ "8 6 5\nhi welcome hello ihateyou goaway dog cat rat\n1 hi welcome\n1 ihateyou goaway\n2 hello ihateyou\n2 hi goaway\n2 hi hello\n1 hi hello\ndog cat\ndog hi\nhi hello\nihateyou goaway\nwelcome ihateyou\n", "3 3 4\nhate love like\n1 love like\n2 love hate\n1 hate like\nlove like\nlove hate\nlike ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (...
78_C. Beaver Game_21462
Two beavers, Timur and Marsel, play the following game. There are n logs, each of exactly m meters in length. The beavers move in turns. For each move a beaver chooses a log and gnaws it into some number (more than one) of equal parts, the length of each one is expressed by an integer and is no less than k meters. Eac...
from sys import stdin, stdout def check(m, k): for i in range(2, int(m ** 0.5) + 1): if not m % i and (i >= k or m // i >= k): return 1 else: return 0 n, m, k = map(int, stdin.readline().split()) if m < 2 * k or (k != 1 and not check(m, k)): stdout.write('Marse...
{ "input": [ "1 15 4\n", "4 9 5\n", "14 30 9\n", "911637544 536870912 134217728\n", "114514534 999950443 31601\n", "907605112 935821597 11274959\n", "1 9 3\n", "81 180 53\n", "1 999999938 999999937\n", "623425842 999002449 10\n", "327082339 935821597 11274959\n", "76367...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Two beavers, Timur and Marsel, play the following game. There are n logs, each of exactly m meters in length. The beavers move in turns. For each move a beaver chooses a log and gnaw...
837_B. Flag of Berland_21468
The flag of Berland is such rectangular field n × m that satisfies following conditions: * Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. * Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. ...
n, m = map(int, input().split()) f = [0 for _ in range(n)] for i in range(n): f[i] = input() hor = True if n % 3 != 0: hor = False else: c = "RGB" used = {"R":False, "G":False, "B":False} used[f[0][0]] = True cnt = 0 if [f[0][0] * m for i in range(n // 3)] == \ f[:n // 3]: ...
{ "input": [ "6 5\nRRRRR\nRRRRR\nBBBBB\nBBBBB\nGGGGG\nGGGGG\n", "4 4\nRRRR\nRRRR\nBBBB\nGGGG\n", "6 7\nRRRGGGG\nRRRGGGG\nRRRGGGG\nRRRBBBB\nRRRBBBB\nRRRBBBB\n", "4 3\nBRG\nBRG\nBRG\nBRG\n", "2 4\nRGBB\nRRGB\n", "3 1\nB\nR\nB\n", "1 2\nRG\n", "3 4\nRRRR\nGGGG\nRRRR\n", "6 3\nRRR\nGGG...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The flag of Berland is such rectangular field n × m that satisfies following conditions: * Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. * Flag con...
926_F. Mobile Communications_21477
A sum of p rubles is charged from Arkady's mobile phone account every day in the morning. Among the following m days, there are n days when Arkady will top up the account: in the day di he will deposit ti rubles on his mobile phone account. Arkady will always top up the account before the daily payment will be done. Th...
n,p,m=map(int,input().split()) flag,t_neg,t_in,d,tot=0,0,0,1,0 for i in range (n): ini_d=d if flag==1: tot+=(t-p) if tot<0: t_neg+=1 d,t=map(int,input().split()) if flag==0: t_neg=(d-ini_d) tot=t_neg*-p flag=1 else: tot+=(((d-1)-ini_d)*-p)...
{ "input": [ "3 6 7\n2 13\n4 20\n7 9\n", "5 4 100\n10 70\n15 76\n21 12\n30 100\n67 85\n", "1 1 1\n1 1\n", "1 1000000000 1\n1 1000000000\n", "14 4115 100\n18 85238\n20 1\n28 38150\n32 16316\n33 269\n42 47018\n48 18222\n53 22346\n57 13571\n58 1717\n65 40035\n90 93955\n93 10747\n96 15415\n", "8 5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A sum of p rubles is charged from Arkady's mobile phone account every day in the morning. Among the following m days, there are n days when Arkady will top up the account: in the day ...
p02555 AtCoder Beginner Contest 178 - Redistribution_21496
Given is an integer S. Find how many sequences there are whose terms are all integers greater than or equal to 3, and whose sum is equal to S. The answer can be very large, so output it modulo 10^9 + 7. Constraints * 1 \leq S \leq 2000 * All values in input are integers. Input Input is given from Standard Input in ...
n=int(input()) p=10**9+7 l=[1]*(2001) l[1],l[2]=0,0 s=1 for i in range(6,2001): l[i]+=s l[i]%=p s+=l[i-2] print(l[n])
{ "input": [ "7", "1729", "2", "13", "3", "26", "1", "35", "20", "11", "15", "8", "10", "70", "22", "25", "14", "9", "19", "21", "27", "6", "1175", "23", "66", "37", "29", "41", "39", "48", "12"...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given is an integer S. Find how many sequences there are whose terms are all integers greater than or equal to 3, and whose sum is equal to S. The answer can be very large, so output ...
p02686 AtCoder Beginner Contest 167 - Bracket Sequencing_21500
A bracket sequence is a string that is one of the following: 1. An empty string; 2. The concatenation of `(`, A, and `)` in this order, for some bracket sequence A ; 3. The concatenation of A and B in this order, for some non-empty bracket sequences A and B / Given are N strings S_i. Can a bracket sequence be forme...
import sys input = sys.stdin.readline N = int(input()) S = [input()[:-1] for _ in range(N)] rl1, rl2 = [], [] for Si in S: l, r = 0, 0 for i in range(len(Si)): if Si[i]=='(': l += 1 else: if l==0: r += 1 else: l -= 1 ...
{ "input": [ "3\n(((\n)\n)", "2\n)\n(()", "4\n((()))\n((((((\n))))))\n()()()", "2\n)(\n()", "3\n()(\n)\n)", "2\n)\n()(", "4\n((()))\n((((()\n))))))\n()()()", "2\n)(\n)(", "3\n()(\n)\n(", "2\n((\n)(", "3\n())\n)\n(", "2\n((\n()", "3\n(((\n)\n(", "2\n)\n(((", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A bracket sequence is a string that is one of the following: 1. An empty string; 2. The concatenation of `(`, A, and `)` in this order, for some bracket sequence A ; 3. The concatena...
p02815 AtCoder Beginner Contest 150 - Change a Little Bit_21504
For two sequences S and T of length N consisting of 0 and 1, let us define f(S, T) as follows: * Consider repeating the following operation on S so that S will be equal to T. f(S, T) is the minimum possible total cost of those operations. * Change S_i (from 0 to 1 or vice versa). The cost of this operation is D \time...
n = int(input()) a = list(map(int,input().split())) a.sort(reverse=True) mod = 10 ** 9 + 7 ans = 1 for _ in range(2*n-2): ans *= 2 ans %= mod cef = [i+2 for i in range(n)] for i in range(n): a[i] *= cef[i] ans *= sum(a) ans %= mod print(ans)
{ "input": [ "2\n5 8", "1\n1000000000", "5\n52 67 72 25 79", "2\n8 8", "1\n1000010000", "5\n20 67 72 25 79", "2\n8 6", "1\n1000000010", "5\n2 67 72 25 79", "1\n1000000110", "5\n2 67 72 25 55", "2\n11 0", "1\n1000000111", "5\n2 67 16 25 55", "1\n1000000101", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For two sequences S and T of length N consisting of 0 and 1, let us define f(S, T) as follows: * Consider repeating the following operation on S so that S will be equal to T. f(S, T)...
p02951 AtCoder Beginner Contest 136 - Transfer_21508
We have two bottles for holding water. Bottle 1 can hold up to A milliliters of water, and now it contains B milliliters of water. Bottle 2 contains C milliliters of water. We will transfer water from Bottle 2 to Bottle 1 as much as possible. How much amount of water will remain in Bottle 2? Constraints * All val...
A, B, C = list(map(int,input().split())) print(max((B+C-A),0))
{ "input": [ "12 3 7", "8 3 9", "6 4 3", "12 0 7", "8 3 16", "8 6 16", "1 6 16", "0 2 0", "0 6 16", "0 4 0", "0 6 4", "1 4 0", "1 7 0", "-1 6 0", "3 -1 5", "-1 1 3", "-4 4 0", "-7 1 1", "-12 0 1", "-12 -1 1", "-11 1 3", "-11 1 5",...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have two bottles for holding water. Bottle 1 can hold up to A milliliters of water, and now it contains B milliliters of water. Bottle 2 contains C milliliters of water. We will...
p03087 AtCoder Beginner Contest 122 - GeT AC_21512
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many t...
import sys input=sys.stdin.readline n,q=map(int,input().split()) s=input();a=[0]*2 for i in range(2,n+1): a+=[a[i-1]+s[i-2:i].count("AC")] for _ in range(q): l,r=map(int,input().split()) print(a[r]-a[l])
{ "input": [ "8 3\nACACTACG\n3 7\n2 3\n1 8", "8 3\nACACTACG\n3 7\n2 3\n1 6", "8 3\nGCATCACA\n3 7\n2 3\n1 8", "8 3\nACBCTADG\n3 7\n2 3\n1 6", "8 3\nACACTACG\n3 7\n2 3\n1 2", "8 3\nACACTACG\n3 7\n4 3\n1 2", "8 3\nGAATCCCA\n3 7\n3 3\n2 8", "8 3\nACACTADG\n3 7\n2 4\n1 6", "8 2\nACBCTAC...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries: * Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq ...
p03232 AtCoder Grand Contest 028 - Removing Blocks_21516
There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of t...
MOD = 10**9+7 def main(): N = int(input()) A = list(map(int, input().split())) prodasc = [1] * (N+1) prodesc = [1] * (N+1) for i in range(N): prodasc[i+1] = (prodasc[i] * (i+1)) % MOD prodesc[i+1] = (prodesc[i] * (N-i)) % MOD inv = [1] * (N+1) for i in range(1, N+1): ...
{ "input": [ "10\n1 2 4 8 16 32 64 128 256 512", "2\n1 2", "4\n1 1 1 1", "10\n1 2 4 8 16 32 64 128 22 512", "2\n1 1", "4\n1 2 1 1", "10\n1 2 4 8 16 32 64 128 21 512", "2\n2 1", "4\n1 2 2 1", "10\n1 2 4 8 20 32 64 128 21 512", "2\n2 2", "4\n1 2 2 2", "10\n1 2 4 8 20 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on thes...
p03383 AtCoder Regular Contest 095 - Symmetric Grid_21520
There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i. Snuke can apply the following operation to this grid any number of times: * Choose two...
from collections import Counter n,m = map(int,input().split()) grid = [list(input()) for i in range(n)] def jc(a,b): l = len(a) used = [0]*l for i in range(l): if used[i]: continue for j in range(i+1,l): if used[j]: continue if a[i] == b[j] and b[i] == a[j]: used[i] = 1 ...
{ "input": [ "3 7\natcoder\nregular\ncontest", "12 12\nbimonigaloaf\nfaurwlkbleht\ndexwimqxzxbb\nlxdgyoifcxid\nydxiliocfdgx\nnfoabgilamoi\nibxbdqmzxxwe\npqirylfrcrnf\nwtehfkllbura\nyfrnpflcrirq\nwvcclwgiubrk\nlkbrwgwuiccv", "2 3\narc\nrac", "3 7\natcocer\nregular\ncontest", "2 3\nacr\ncar", "1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column i...
p03547 AtCoder Beginner Contest 078 - HEX_21524
In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represent the values 10, 11, 12, 13, 14 and 15, respectively. In this problem, you are given two letters X and Y. Each X and Y is `A`, `B`, `C`,...
x,y = map(str, input().split()) print(">" if x > y else "<" if x < y else "=")
{ "input": [ "F F", "A B", "E C", "E F", "A A", "E B", "D F", "A C", "E A", "E E", "A D", "D B", "F E", "A E", "D C", "F D", "A F", "E D", "F C", "B E", "G E", "G D", "C E", "H E", "D D", "C D", "H D", "F B...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In programming, hexadecimal notation is often used. In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters `A`, `B`, `C`, `D`, `E` and `F` are used to represen...
p03703 AtCoder Regular Contest 075 - Meaningful Mean_21528
You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 ...
def examC(): N = I() S = [I() for _ in range(N)] S1 = [] for s in S: if s%10==0: continue S1.append(s) if S1==[]: print(0) return S1.sort() ans = sum(S) if ans%10==0: ans -= S1[0] print(ans) return def examD(): N, A, B = LI...
{ "input": [ "1 2\n1", "3 6\n7\n5\n7", "7 26\n10\n20\n30\n40\n30\n20\n10", "1 4\n1", "3 6\n7\n5\n10", "7 26\n10\n20\n30\n43\n30\n20\n10", "1 6\n10\n20\n27\n12\n30\n20\n12", "2 1\n1\n12\n34\n45\n2\n9\n1", "2 1\n0\n18\n19\n56\n0\n4\n-4", "3 5\n7\n5\n7", "7 26\n10\n20\n30\n40\...
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 sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Amo...
p03858 AtCoder Regular Contest 065 - Manhattan Compass_21531
There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i). We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|). You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs ...
import sys input = sys.stdin.readline from collections import defaultdict import bisect INF = 10 ** 12 N,a,b = map(int,input().split()) X_to_Y = defaultdict(lambda: [-INF,INF]) Y_to_X = defaultdict(lambda: [-INF,INF]) for i in range(1,N+1): x,y = map(int,input().split()) x,y = x+y,x-y X_to_Y[x].append(y) ...
{ "input": [ "6 2 3\n1 3\n5 3\n3 5\n8 4\n4 7\n2 5", "5 1 2\n1 1\n4 3\n6 1\n5 5\n4 8", "8 1 2\n1 5\n4 3\n8 2\n4 7\n8 8\n3 3\n6 6\n4 8", "6 2 3\n1 3\n5 3\n3 5\n8 4\n4 10\n2 5", "5 1 2\n1 1\n4 1\n6 1\n5 5\n4 8", "8 1 2\n1 5\n4 3\n8 2\n4 7\n7 8\n3 3\n6 6\n4 8", "6 2 3\n1 3\n5 2\n3 5\n8 4\n4 10...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i). We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j...
p04023 AtCoder Grand Contest 003 - Sequential operations on Sequence_21534
Snuke got an integer sequence from his mother, as a birthday present. The sequence has N elements, and the i-th of them is i. Snuke performs the following Q operations on this sequence. The i-th operation, described by a parameter q_i, is as follows: * Take the first q_i elements from the sequence obtained by concaten...
import bisect,sys input=sys.stdin.readline N,Q=map(int,input().split()) que=[(N,0)] for i in range(Q): q=int(input()) que.append((q,i+1)) que.sort(reverse=True) ext=[] while que: q,id=que.pop() if not ext: ext.append((q,id)) else: if ext[-1][1]<id: if ext[-1][0]==q: ...
{ "input": [ "10 10\n9\n13\n18\n8\n10\n10\n9\n19\n22\n27", "5 3\n6\n4\n11", "10 10\n9\n13\n18\n8\n10\n10\n9\n19\n33\n27", "10 10\n9\n13\n18\n8\n10\n1\n9\n19\n33\n27", "10 8\n9\n13\n18\n8\n10\n1\n9\n19\n33\n27", "10 10\n9\n13\n18\n7\n10\n10\n9\n19\n22\n27", "5 3\n6\n4\n6", "20 10\n9\n13...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke got an integer sequence from his mother, as a birthday present. The sequence has N elements, and the i-th of them is i. Snuke performs the following Q operations on this sequenc...
p00107 Carry a Cheese_21538
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you he...
while True: d, w, h = sorted(map(int, input().split())) if d == w == h == 0: break dw = (d**2 + w**2) n = int(input()) for i in range(n): y = int(input()) if (2*y)**2 > dw: print("OK") else: print("NA")
{ "input": [ "10 6 8\n5\n4\n8\n6\n2\n5\n0 0 0", "10 6 8\n5\n4\n1\n6\n2\n5\n0 0 0", "10 6 8\n5\n4\n0\n0\n2\n5\n0 0 0", "10 6 8\n5\n4\n0\n0\n2\n7\n0 0 0", "10 6 8\n5\n1\n8\n6\n2\n5\n0 0 0", "10 6 4\n5\n4\n0\n-1\n2\n7\n0 0 0", "10 6 4\n5\n4\n0\n-1\n4\n7\n0 0 0", "10 3 8\n5\n4\n0\n6\n2\n5\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to t...
p00241 Quaternion Multiplication_21542
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $...
while True : n = int(input()) if n == 0 : break for i in range(n) : x1, y1, z1, w1, x2, y2, z2, w2 = map(int, input().split()) print((x1*x2 - y1*y2 - z1*z2 - w1*w2), (x1*y2 + x2*y1 + z1*w2 - z2*w1), (x1*z2 - y1*w2 + x2*z1 + y2*w1), (x1*w2 + y1*z2 - y2*z1 + x2*w1))
{ "input": [ "2\n1 2 3 4 7 6 7 8\n5 6 7 8 3 2 3 4\n0", "2\n1 2 3 4 7 6 7 8\n5 6 4 8 3 2 3 4\n0", "2\n1 2 3 4 7 6 14 8\n5 6 4 8 3 2 3 4\n0", "2\n1 3 3 4 7 6 14 8\n5 6 4 8 3 2 3 4\n0", "2\n1 3 3 4 7 6 14 8\n5 11 4 8 3 2 3 4\n0", "2\n1 3 3 7 7 6 14 8\n5 11 4 8 3 2 3 4\n0", "2\n1 3 3 7 7 6 14 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation ...
p00617 Simple GUI Application_21546
Advanced Creative Mobile (ACM) has decided to develop a GUI application to be installed in a new portable computer. As shown in the figure below, the GUI has a nested structure in which it has one main panel, several panels are arranged on it, and some panels are arranged on top of those panels. .. All panels are rect...
class Panel: def __init__(self, name, points, children): self.name = name self.x1 = points[0] self.x2 = points[2] self.y1 = points[1] self.y2 = points[3] self.children = children self.child_cnt = len(children) def search(self, x, y): if not (self....
{ "input": [ "5\n<main>10,10,190,150<menu>20,20,70,140</menu><primary>80,20,180,140</primary></main>\n10 10\n15 15\n40 60\n2 3\n130 80\n0", "5\n<main>10,10,190,150<menu>20,20,70,140</menu><primary>80,20,180,140</primary></main>\n10 10\n15 15\n40 60\n1 3\n130 80\n0", "5\n<main>10,10,190,150<menu>20,20,70,1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Advanced Creative Mobile (ACM) has decided to develop a GUI application to be installed in a new portable computer. As shown in the figure below, the GUI has a nested structure in wh...
p00761 Recurring Decimals_21550
A decimal representation of an integer can be transformed to another integer by rearranging the order of digits. Let us make a sequence using this fact. A non-negative integer a0 and the number of digits L are given first. Applying the following rules, we obtain ai+1 from ai. 1. Express the integer ai in decimal nota...
while True: list_a = [] a, L = input().split(" ") if a=='0' and L=='0': break L = int(L) list_a.append(int(a)) while True: if len(a) < L: a += '0'*(L-len(a)) an = int(''.join(sorted(a, reverse=True))) - int(''.join(sorted(a))) if an in list_a: idx = list_a.in...
{ "input": [ "2012 4\n83268 6\n1112 4\n0 1\n99 2\n0 0", "2012 4\n83268 3\n1112 4\n0 1\n99 2\n0 0", "2012 4\n83268 6\n1112 4\n0 1\n99 1\n0 0", "2012 4\n83268 3\n1112 4\n0 1\n17 2\n0 0", "2012 4\n83268 6\n1112 3\n0 1\n99 1\n0 0", "2012 4\n83268 6\n1112 4\n0 0\n99 1\n0 0", "2012 4\n83268 3\n1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A decimal representation of an integer can be transformed to another integer by rearranging the order of digits. Let us make a sequence using this fact. A non-negative integer a0 and...
p00893 Matrix Calculator_21553
Dr. Jimbo, an applied mathematician, needs to calculate matrices all day for solving his own problems. In his laboratory, he uses an excellent application program for manipulating matrix expressions, however, he cannot use it outside his laboratory because the software consumes much of resources. He wants to manipulate...
from string import digits import sys readline = sys.stdin.readline write = sys.stdout.write def parse(S, mp): M = 32768 cur = 0 def transpose(m): p, q, v = m res = [[0]*p for i in range(q)] for i in range(q): for j in range(p): res[i][j] = v[j][i] ...
{ "input": [ "1\nA=[1 2 3;4 5 6].\n1\nA=[[1 2 3;4 5 6] [7 8;9 10] [11;12];13 14 15 16 17 18].\n3\nB=[3 -2 1;-9 8 7].\nC=([1 2 3;4 5 6]+B)(2,3).\nD=([1 2 3;4 5 6]+B)([1 2],[2 3]).\n5\nA=2*[1 2;-3 4]'.\nB=A([2 1 2],[2 1]).\nA=[1 2;3 4]*3.\nA=[2]*[1 2;3 4].\nA=[1 2;3 4]*[3].\n2\nA=[11 12 13;0 22 23;0 0 33].\nA=[A A'...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dr. Jimbo, an applied mathematician, needs to calculate matrices all day for solving his own problems. In his laboratory, he uses an excellent application program for manipulating mat...
p01296 Futon_21559
The sales department of Japanese Ancient Giant Corp. is visiting a hot spring resort for their recreational trip. For deepening their friendships, they are staying in one large room of a Japanese-style hotel called a ryokan. In the ryokan, people sleep in Japanese-style beds called futons. They all have put their futo...
#!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": [ "4\n0 0 x\n2 0 x\n0 1 x\n2 1 x\n4\n1 0 x\n0 1 x\n2 1 x\n1 2 x\n4\n0 0 x\n2 0 y\n0 1 y\n1 2 x\n0", "4\n0 0 x\n2 0 x\n0 1 x\n2 1 x\n4\n1 0 x\n0 1 x\n2 1 x\n1 2 x\n4\n0 0 x\n2 0 y\n1 1 y\n1 2 x\n0", "4\n0 0 x\n2 0 x\n0 1 x\n2 1 x\n4\n1 0 w\n0 1 x\n2 1 x\n1 2 x\n4\n0 0 x\n2 0 y\n0 1 y\n1 2 x\n0",...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The sales department of Japanese Ancient Giant Corp. is visiting a hot spring resort for their recreational trip. For deepening their friendships, they are staying in one large room o...
p01911 Rainy Bus Stops_21565
problem AOR Ika is at the $ S $ th bus stop at time $ 0 $ and wants to go from there to the $ G $ th bus stop. The number of bus stops $ N $ and $ M $ routes (*) connecting different bus stops are given. The bus stops are numbered $ 1, \ dots, and N $, respectively. Each route consists of $ 4 $ values: origin $ u $, d...
from heapq import heappush, heappop def main(): n, m, s, g = map(int, input().split()) s -= 1 g -= 1 edges = [[] for _ in range(n)] for _ in range(m): u, v, t, c = map(int, input().split()) u -= 1 v -= 1 edges[u].append((t, t + c, v)) score = {} score[(0, 0)] = 0 que = [] heappush(...
{ "input": [ "2 2 1 2\n1 2 10 100\n1 2 5 500", "2 1 1 2\n1 2 10 100\n1 2 5 500", "2 2 1 2\n1 2 10 101\n1 2 5 500", "2 1 1 2\n1 2 13 011\n2 2 5 500", "2 1 1 2\n1 2 19 011\n2 2 5 500", "2 2 1 2\n1 2 10 100\n1 2 7 893", "2 1 1 2\n1 2 34 011\n2 2 5 500", "2 1 1 2\n1 2 9 011\n2 2 5 500", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: problem AOR Ika is at the $ S $ th bus stop at time $ 0 $ and wants to go from there to the $ G $ th bus stop. The number of bus stops $ N $ and $ M $ routes (*) connecting different...
p02346 Range Sum Query (RSQ)_21571
Write a program which manipulates a sequence A = {a1, a2, . . . , an} with the following operations: * add(i, x): add x to ai. * getSum(s, t): print the sum of as, as+1,...,at. Note that the initial values of ai (i = 1, 2, . . . , n) are 0. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * If comi is 0, then 1 ≤ xi...
from array import array import bisect import collections import heapq import itertools import operator import sys class BIT: def __init__(self, n): self.n = n self.arr = array("l", [0] * (n + 1)) def __str__(self): return str(self.arr.tolist()) def add(self, i, x): while ...
{ "input": [ "3 5\n0 1 1\n0 2 2\n0 3 3\n1 1 2\n1 2 2", "4 5\n0 1 1\n0 2 2\n0 3 3\n1 1 2\n1 2 2", "4 5\n0 1 1\n0 2 2\n0 1 5\n1 1 2\n1 2 2", "4 5\n0 1 0\n0 2 2\n0 1 5\n1 1 2\n1 2 2", "4 5\n0 1 1\n0 2 4\n0 3 3\n1 1 2\n1 2 2", "4 5\n0 1 1\n0 4 4\n0 3 3\n1 1 2\n1 2 2", "4 5\n0 1 1\n0 2 2\n0 3 9...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Write a program which manipulates a sequence A = {a1, a2, . . . , an} with the following operations: * add(i, x): add x to ai. * getSum(s, t): print the sum of as, as+1,...,at. No...
1023_E. Down or Right_21581
This is an interactive problem. Bob lives in a square grid of size n × n, with rows numbered 1 through n from top to bottom, and columns numbered 1 through n from left to right. Every cell is either allowed or blocked, but you don't know the exact description of the grid. You are given only an integer n. Bob can move...
from sys import stdout n=int(input()) x=1 y=1 s='! ' s2='' for i in range (n-1): print('? '+str(x+1)+' '+str(y)+' '+str(n)+' '+str(n)) stdout.flush() b=input() if b=='YES': s=s+'D' x+=1 else: s=s+'R' y+=1 x1=x y1=y x=n y=n for i in range (n-1): if y!=y1: p...
{ "input": [ "4\n \nYES\n \nNO\n \nYES\n \nYES\n \n", "10\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n", "5\n..#..\n.#...\n.....\n#..#.\n#....\n", "10\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: This is an interactive problem. Bob lives in a square grid of size n × n, with rows numbered 1 through n from top to bottom, and columns numbered 1 through n from left to right. Ever...
108_C. Dorm Water Supply_21589
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle). For each house, the...
n, m = map(int, input().split()) t, p = [], [[0, 0, 0] for i in range(n + 1)] for i in range(m): a, b, d = map(int, input().split()) p[b][1], p[a][0], p[a][2] = a, b, d for a in range(1, n + 1): if p[a][1] == 0: b, c, d = a, p[a][0], p[a][2] if not c: continue while c: ...
{ "input": [ "3 3\n1 2 20\n2 3 10\n3 1 5\n", "4 2\n1 2 60\n3 4 50\n", "3 2\n1 2 10\n2 3 20\n", "1 0\n", "9 6\n7 4 98\n5 9 72\n4 6 10\n2 8 22\n9 7 17\n3 1 66\n", "44 42\n4 37 166\n34 25 47\n28 19 367\n20 14 811\n8 3 878\n39 1 925\n35 9 206\n32 18 841\n16 44 503\n5 20 426\n22 34 896\n44 43 471\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only ...
1109_B. Sasha and One More Name_21593
Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in t...
s = input() n = len(s) def check(name): return name == name[::-1] and name != s for i in range(1, n): new_name = s[i:] + s[:i] if check(new_name): print(1) exit() for i in range(1, n // 2 + 1): new_name = s[-i:] + s[i:-i] + s[:i] if check(new_name): print(2) exit() ...
{ "input": [ "otto\n", "qqqq\n", "nolon\n", "kinnikkinnik\n", "aaabbbaaa\n", "abbba\n", "axalaxa\n", "abacaba\n", "abacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacaba\n", "lllhhlhhllhhlllhlhhhhlllllhhhhlllllllhhlhhllhhlllhlhhhhlllllhhhhllllllllhhhhlllllhhhhlhlllhhll...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my ...
1156_G. Optimizer_21599
Let's analyze a program written on some strange programming language. The variables in this language have names consisting of 1 to 4 characters, and each character is a lowercase or an uppercase Latin letter, or a digit. There is an extra constraint that the first character should not be a digit. There are four types ...
from random import seed, randint import sys sys.setrecursionlimit(10000) opr = ['#', '^', '&', '$'] namespace = { "res" : (False, "res") } rules = dict() lookup = dict() cnt = -1 def get_tag(var): if var in namespace: return namespace[var][1] else: return var N = int(input()) for _ in range(N...
{ "input": [ "4\nc=aa#bb\nd12=c\nres=c^d12\ntmp=aa$c\n", "2\nmax=aaaa$bbbb\nmin=bbbb^aaaa\n", "9\nc111=a&b\nd=b^a\ne=c111#d\nd=d\nf=f\nf=c111$d\ng=e$f\nh=e#f\nres=g^h\n", "3\na=res\nres=x^y\nres=a\n", "1\nres=res\n", "4\nc=a\na=b\nb=c\nres=a&b\n", "5\nres=a\nb=c&d\ne=res&a\nx=y\nres=c$x\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's analyze a program written on some strange programming language. The variables in this language have names consisting of 1 to 4 characters, and each character is a lowercase or a...
1179_A. Valeriy and Deque_21603
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A >...
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.wri...
{ "input": [ "5 3\n1 2 3 4 5\n1\n2\n10\n", "2 0\n0 0\n", "71 57\n9 26 80 10 65 60 63 1 15 85 71 1 58 27 41 97 42 15 42 56 87 22 10 28 34 90 13 70 71 56 65 21 0 78 47 96 56 77 32 83 28 16 10 41 0 18 78 12 27 58 4 67 21 41 99 20 21 52 74 10 83 45 43 65 2 15 1 63 46 97 72\n81\n21\n81\n81\n5\n9\n41\n76\n81\n9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gr...
1197_C. Array Splitting_21607
You are given a sorted array a_1, a_2, ..., a_n (for each index i > 1 condition a_i ≥ a_{i-1} holds) and an integer k. You are asked to divide this array into k non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let max(i) be equal to the maximum in the i-th subar...
n, k = map(int, input().split()) l = list(map(int, input().split())) ll = [] for i in range(n-1): ll.append(l[i+1]-l[i]) ll.sort() print(sum(ll[:n-k]))
{ "input": [ "4 4\n1 3 3 7\n", "6 3\n4 8 15 16 23 42\n", "8 1\n1 1 2 3 5 8 13 21\n", "1 1\n1\n", "5 2\n-1 -2 5 -4 8\n", "5 2\n3 3 4 5 6\n", "7 6\n-3 0 -1 -2 -2 -4 -1\n", "1 1\n4\n", "1 1\n2\n", "4 1\n3 -1 6 0\n", "1 1\n56\n", "1 1\n20\n", "5 3\n99 101 103 105 107\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a sorted array a_1, a_2, ..., a_n (for each index i > 1 condition a_i ≥ a_{i-1} holds) and an integer k. You are asked to divide this array into k non-empty consecutive...
1214_D. Treasure Island_21611
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure Island may be represented as a rectangular table n × m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from...
import sys from io import BytesIO, IOBase from collections import defaultdict, deque class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file...
{ "input": [ "4 4\n....\n#.#.\n....\n.#..\n", "2 2\n..\n..\n", "3 4\n....\n.##.\n....\n", "6 3\n.##\n###\n.##\n.##\n#.#\n#..\n", "20 1\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n", "3 6\n.#####\n######\n#####.\n", "3 6\n.....#\n.....#\n......\n", "4 2\n..\n#.\n..\n..\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure Island may be represented as a rectangular table n × m which is surrounded by the o...
1255_E2. Send Boxes to Alice (Hard Version)_21617
This is the harder version of the problem. In this version, 1 ≤ n ≤ 10^6 and 0 ≤ a_i ≤ 10^6. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend...
import sys # inf = open('input.txt', 'r') # input = inf.readline input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) def primeDivisors(num): sqrtNum = int(num ** 0.5) + 1 primes = [] if not num & 1: primes.append(2) while not num & 1: num >>= 1 ...
{ "input": [ "1\n1\n", "3\n4 8 5\n", "5\n3 10 2 1 5\n", "4\n0 5 15 10\n", "3\n0 0 17\n", "15\n1 1 1 0 0 0 1 1 1 0 0 0 1 1 1\n", "11\n0 3 3 5 5 5 4 3 6 4 1\n", "1\n2103\n", "3\n0 0 1\n", "15\n17 7 1 8 2 2 0 18 0 12 11 0 12 12 5\n", "16\n12 0 9 14 12 26 21 24 3 13 7 4 26 1 17...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This is the harder version of the problem. In this version, 1 ≤ n ≤ 10^6 and 0 ≤ a_i ≤ 10^6. You can hack this problem if you locked it. But you can hack the previous problem only if ...
1279_E. New Year Permutations_21621
Yeah, we failed to make up a New Year legend for this problem. A permutation of length n is an array of n integers such that every integer from 1 to n appears in it exactly once. An element y of permutation p is reachable from element x if x = y, or p_x = y, or p_{p_x} = y, and so on. The decomposition of a permut...
# 1279E - New Year Permutations # Idea: # dp(i) --> # of good permutations of [i,n] # cycles(m) --> # of blocks of len m ( == (m-2)! as first element is fixed) # It is easy to test for impossibility ie. k > dp(1) # Another subproblem: how to compute jth smallest cycle # getGoodPerm(n,k) uses both getCycle(b, (k/dp[n-...
{ "input": [ "5\n3 3\n5 15\n4 13\n6 8\n4 2\n", "5\n3 3\n5 25\n4 13\n6 8\n4 2\n", "5\n3 3\n5 25\n4 13\n6 8\n4 1\n", "5\n3 6\n5 25\n4 13\n6 8\n4 1\n", "5\n3 3\n5 15\n4 18\n6 8\n4 2\n", "5\n2 3\n5 25\n4 13\n6 8\n4 2\n", "5\n3 2\n5 25\n4 13\n6 8\n4 1\n", "5\n3 6\n5 25\n4 13\n6 8\n8 1\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Yeah, we failed to make up a New Year legend for this problem. A permutation of length n is an array of n integers such that every integer from 1 to n appears in it exactly once. A...
129_C. Statues_21625
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 × 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains...
from sys import stdin from collections import * from copy import deepcopy def valid(i, j): n, m = [8] * 2 return i > -1 and i < n and j > -1 and j < m def str_inp(n): return list(reversed(list((list(stdin.readline()[:-1]) for x in range(n))))) def check(x, y, step): if step + 1 < 8 and all[step + ...
{ "input": [ ".......A\n........\n........\n........\n........\n........\n........\nM.......\n", ".......A\n........\n........\n........\n........\n........\nSS......\nM.......\n", ".......A\n........\n........\n........\n........\n.S......\nS.......\nMS......\n", ".SSSSSSA\n.SSSSSSS\n.SSSSSSS\n.SSSSS...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 × 8): Anna is in the upper right corner, and Maria ...
1341_E. Nastya and Unexpected Guest_21629
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya. On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place s...
import sys input = sys.stdin.readline from collections import deque from typing import List ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(...
{ "input": [ "15 5\n0 3 7 14 15\n11 11\n", "13 4\n0 3 7 13\n9 9\n", "15 5\n15 3 7 0 14\n11 11\n", "4 5\n0 3 1 2 4\n1 1\n", "4 5\n0 3 1 2 4\n2 1\n", "30 8\n0 1 9 10 23 24 26 30\n40 7\n", "15 5\n15 14 7 3 0\n11 11\n", "100 5\n3 100 7 13 0\n99 5\n", "4 3\n0 4 2\n2 2\n", "15 5\n15 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya. On the way from Denis's house to the gi...
1363_E. Tree Shuffling_21633
Ashish has a tree consisting of n nodes numbered 1 to n rooted at node 1. The i-th node in the tree has a cost a_i, and binary digit b_i is written in it. He wants to have binary digit c_i written in the i-th node in the end. To achieve this, he can perform the following operation any number of times: * Select any...
import sys data = [list(map(int, line.rstrip().split())) for line in sys.stdin.readlines()] n = data[0][0] dic = [[] for _ in range(n+1)] dic[1].append(0) for u,v in data[n+1:]: dic[u].append(v) dic[v].append(u) cost = [0]*(n+1) cost[1] = 1000000001 father = [0]*(n+1) now = [1] leaf = [] gress = [0]*(n+1) whil...
{ "input": [ "5\n10000 0 1\n2000 1 0\n300 0 1\n40 0 0\n1 1 0\n1 2\n2 3\n2 4\n1 5\n", "5\n1 0 1\n20 1 0\n300 0 1\n4000 0 0\n50000 1 0\n1 2\n2 3\n2 4\n1 5\n", "2\n109 0 1\n205 0 1\n1 2\n", "3\n57 1 0\n6 0 1\n76 1 1\n1 3\n3 2\n", "9\n71 1 1\n59 0 0\n87 0 0\n9 0 1\n39 1 1\n3 1 1\n72 0 0\n96 1 0\n100 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ashish has a tree consisting of n nodes numbered 1 to n rooted at node 1. The i-th node in the tree has a cost a_i, and binary digit b_i is written in it. He wants to have binary digi...
1384_A. Common Prefixes_21637
The length of the longest common prefix of two strings s = s_1 s_2 … s_n and t = t_1 t_2 … t_m is defined as the maximum integer k (0 ≤ k ≤ min(n,m)) such that s_1 s_2 … s_k equals t_1 t_2 … t_k. Koa the Koala initially has n+1 strings s_1, s_2, ..., s_{n+1}. For each i (1 ≤ i ≤ n) she calculated a_i — the length of ...
T = int(input()) def solve(): N = int(input()) A = list(map(int,input().split())) def next_char(c): if c == 'a': return 'b' return 'a' ans = [] ans.append('a'*101) for a in A: lst = ans[-1] lst = lst[:a] + next_char(lst[a]) + lst[a+1:] ans.a...
{ "input": [ "4\n4\n1 2 4 2\n2\n5 3\n3\n1 3 1\n3\n0 0 0\n", "1\n100\n0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 0 0 1 0 1 0 1 0 1 0 0 0 0 1 1 1 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 1 0 1 0 1 0 1 0 0 1 1 0\n", "4\n4\n1 2 4 2\n2\n5 3\n3\n1 3 1\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The length of the longest common prefix of two strings s = s_1 s_2 … s_n and t = t_1 t_2 … t_m is defined as the maximum integer k (0 ≤ k ≤ min(n,m)) such that s_1 s_2 … s_k equals t_...
1405_B. Array Cancellation_21641
You're given an array a of n integers, such that a_1 + a_2 + ⋅⋅⋅ + a_n = 0. In one operation, you can choose two different indices i and j (1 ≤ i, j ≤ n), decrement a_i by one and increment a_j by one. If i < j this operation is free, otherwise it costs one coin. How many coins do you have to spend in order to make a...
def main(): t = int(input()) for _ in range(t): n = int(input()) alst = list(map(int, input().split())) ans = 0 total = 0 for a in alst: total -= a ans = max(ans, total) print(ans) main()
{ "input": [ "7\n4\n-3 5 -3 1\n2\n1 -1\n4\n-3 2 -3 4\n4\n-1 1 1 -1\n7\n-5 7 -6 -4 17 -13 4\n6\n-1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000\n1\n0\n", "1\n6\n5 -5 -3 1 1 1\n", "1\n6\n5 -6 -3 2 1 1\n", "1\n6\n5 -4 -4 1 1 1\n", "1\n6\n5 -6 -4 2 2 1\n", "1\n6\n6 -5 -3 1 0 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You're given an array a of n integers, such that a_1 + a_2 + ⋅⋅⋅ + a_n = 0. In one operation, you can choose two different indices i and j (1 ≤ i, j ≤ n), decrement a_i by one and in...
1426_D. Non-zero Segments_21645
Kolya got an integer array a_1, a_2, ..., a_n. The array can contain both positive and negative integers, but Kolya doesn't like 0, so the array doesn't contain any zeros. Kolya doesn't like that the sum of some subsegments of his array can be 0. The subsegment is some consecutive segment of elements of the array. Y...
#!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): pass # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not ...
{ "input": [ "5\n4 -2 3 -9 2\n", "9\n-1 1 -1 1 -1 1 1 -1 -1\n", "8\n16 -5 -11 -15 10 5 4 -4\n", "4\n1 -5 3 2\n", "3\n1 2 -3\n", "8\n4 -3 -1 -2 6 -6 6 -6\n", "15\n1 -1 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -12...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Kolya got an integer array a_1, a_2, ..., a_n. The array can contain both positive and negative integers, but Kolya doesn't like 0, so the array doesn't contain any zeros. Kolya does...
144_C. Anagram Search_21649
A string t is called an anagram of the string s, if it is possible to rearrange letters in t so that it is identical to the string s. For example, the string "aab" is an anagram of the string "aba" and the string "aaa" is not. The string t is called a substring of the string s if it can be read starting from some posi...
s = input() p = input() fit = 0 cnts = {} cntp = {} for c in s: cnts[c] = cntp[c] = 0 for c in p: cnts[c] = cntp[c] = 0 for c in p: cntp[c] += 1 def extend(c): if c == '?': return global fit if cnts[c] == cntp[c]: fit += 1 cnts[c] += 1 def remove(c): if c == '?': ...
{ "input": [ "bb??x???\naab\n", "ab?c\nacb\n", "?\na\n", "aaaaa?ab??\naab\n", "??fb???a??\ndeeefc\n", "?b?aaabaa?\naaa\n", "a\naa\n", "aaaaa??a?a\naaa\n", "?bba?b?aaa\nabb\n", "?bab?a?aab\naaa\n", "ccaac\ncbcbca\n", "?\ncb\n", "?bba?\nbba\n", "a\na\n", "aaaa...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so that it is identical to the string s. For example, the string "aab" is an anagram of th...
1473_C. No More Inversions_21653
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k ≤ n < 2k). Let's call as inversion in a a pair of indices i < j such that a[i] > a[j]. Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]]. Your goal...
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": [ "4\n1 1\n2 2\n3 2\n4 3\n", "4\n1 1\n3 2\n3 2\n4 3\n", "4\n1 1\n3 2\n3 2\n5 3\n", "4\n1 1\n2 2\n3 3\n4 3\n", "4\n1 1\n3 2\n3 2\n3 3\n", "4\n1 1\n3 2\n3 2\n5 5\n", "4\n1 1\n2 2\n5 3\n4 3\n", "4\n1 1\n3 2\n2 2\n3 3\n", "4\n1 1\n2 2\n5 3\n5 3\n", "4\n1 1\n2 2\n5 5\n4 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k ≤ n < 2k). Let's call as inversion in a a pair of indices i < j such that a[i] > a[j]....
1499_E. Chaotic Merge_21656
You are given two strings x and y, both consist only of lowercase Latin letters. Let |s| be the length of string s. Let's call a sequence a a merging sequence if it consists of exactly |x| zeros and exactly |y| ones in some order. A merge z is produced from a sequence a by the following rules: * if a_i=0, then re...
from bisect import * from collections import * from math import gcd,ceil,sqrt,floor,inf from heapq import * from itertools import * from operator import add,mul,sub,xor,truediv,floordiv from functools import * #------------------------------------------------------------------------ import os import sys from io impo...
{ "input": [ "code\nforces\n", "aaa\nbb\n", "aaaaa\naaa\n", "justamassivetesttocheck\nhowwellyouhandlemodulooperations\n", "aaaccabcbc\nabcbabbbaa\n", "nclmevhtio\nefnepwqphvwxiyguotuk\n", "zkccmpbrfadhkukfvkci\nfopwpsbztb\n", "abbbaabbba\nbaaaababbb\n", "aaaccabcbc\nacbbabbbaa\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two strings x and y, both consist only of lowercase Latin letters. Let |s| be the length of string s. Let's call a sequence a a merging sequence if it consists of exact...
1521_C. Nastia and a Hidden Permutation_21659
This is an interactive problem! Nastia has a hidden permutation p of length n consisting of integers from 1 to n. You, for some reason, want to figure out the permutation. To do that, you can give her an integer t (1 ≤ t ≤ 2), two different indices i and j (1 ≤ i, j ≤ n, i ≠ j), and an integer x (1 ≤ x ≤ n - 1). Dep...
import math,sys ## from itertools import permutations, combinations;import heapq,random; from collections import defaultdict,deque import bisect as bi def yes():print('YES') def no():print('NO') # sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w'); def I():return (int(sys.stdin.readline())) def In():retu...
{ "input": [ "2\n4\n\n3\n\n2\n\n5\n\n3", "2\n4\n3 1 4 2\n5\n2 5 3 4 1\n", "2\n4\n3 1 4 2\n5\n2 5 3 4 1\n", "24\n4\n3 2 1 4\n4\n4 2 1 3\n4\n1 2 3 4\n4\n4 3 1 2\n4\n1 3 4 2\n4\n3 1 2 4\n4\n1 3 2 4\n4\n2 1 4 3\n4\n4 2 3 1\n4\n4 1 2 3\n4\n1 2 4 3\n4\n1 4 2 3\n4\n3 4 2 1\n4\n2 4 1 3\n4\n1 4 3 2\n4\n2 4 3 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This is an interactive problem! Nastia has a hidden permutation p of length n consisting of integers from 1 to n. You, for some reason, want to figure out the permutation. To do that...
1550_C. Manhattan Subarrays_21663
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|. Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r). Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose...
A=[] t=int(input()) def isg(l,r): for i in range(l,r+1): for j in range(i+1,r+1): for k in range(j+1,r+1): if(A[i]>=A[j] and A[j]>=A[k]): return False if(A[i]<=A[j] and A[j]<=A[k]): return False return True for i in rang...
{ "input": [ "3\n4\n2 4 1 3\n5\n6 9 1 9 6\n2\n13 37\n", "3\n4\n2 4 1 3\n5\n6 9 1 18 6\n2\n13 37\n", "3\n4\n2 4 1 5\n5\n6 9 1 18 6\n2\n13 37\n", "3\n4\n2 4 1 3\n5\n6 9 1 9 6\n2\n8 37\n", "3\n4\n2 7 2 5\n5\n6 9 1 6 6\n2\n13 73\n", "3\n4\n0 0 1 3\n5\n6 9 -1 2 6\n2\n8 37\n", "3\n4\n2 4 1 3\n5\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|. Let's say that three points p,...
177_A1. Good Matrix Elements_21667
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: * Elements of the main diagonal. * Elements of the secondary diagonal. * Elements of the "middle" row — the row which has exac...
n = int(input()) gh = [0 for _ in range(n)] arr = [gh[:] for _ in range(n) ] visited = [gh[:] for _ in range(n)] for i in range(n): a = input() a1 = a.split() for j in range(n): arr[i][j] = int(a1[j]) if n == 1: print( a) else: middleR = (n - 1) // 2 middleC = (n - 1 ) // 2 to...
{ "input": [ "3\n1 2 3\n4 5 6\n7 8 9\n", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n", "11\n0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 ...
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 got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:...
197_C. Lexicographically Maximum Subsequence_21671
You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence. We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 ≤ p1 < p2 < ... < pk ≤ |s|) a subsequence of string s = s1s2... s|s|. String x = x1x2... x|x| is lexicographically larger than string y = y1y...
from collections import Counter s = input() c = Counter(s) cur_char = 'z' for ch in s: while c[cur_char] == 0: cur_char = chr(ord(cur_char) - 1) if ch == cur_char: print(cur_char, end='') c[ch] -= 1
{ "input": [ "abbcbccacbbcbaaba\n", "ababba\n", "aa\n", "thankstosamarasauteddybearsforthiscontest\n", "abcdeabcd\n", "arepretestsstrongforthisproblem\n", "zyxzxzwyxywxxwabcdabdacdbcacdbcacabaaba\n", "aababb\n", "a\n", "y\n", "whyareyoulookingfortestsdoyouhavewa\n", "z\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence. We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 ≤ p1 ...
26_C. Parquet_21679
Once Bob decided to lay a parquet floor in his living room. The living room is of size n × m metres. Bob had planks of three types: a planks 1 × 2 meters, b planks 2 × 1 meters, and c planks 2 × 2 meters. Help Bob find out, if it is possible to parquet the living room with such a set of planks, and if it is possible, f...
n, m, a, b, c = map(int, input().split()) mat = [0]*n for i in range(n): mat[i] = [0] * m n1 = [min(n*m//4, c), min(m//2 * n, a), min(n//2 * m, b)] p = ['c', 'a', 'b'] z = ['d', 'e', 'f', 'g'] z1 = ['m', 'n', 'u', 'o'] z2 = ['p', 'q', 'r', 'w'] #print(n1) def fill(h, w): if h <= 0 or w <= 0: return True...
{ "input": [ "2 6 2 2 1\n", "4 4 10 10 10\n", "1 1 100 100 100\n", "100 100 10000 10000 10000\n", "100 100 0 0 0\n", "100 100 2498 2498 1\n", "1 2 0 1 0\n", "100 100 2499 2499 0\n", "90 100 1488 2992 10\n", "2 1 1 0 0\n", "97 100 58 172 2310\n", "100 100 2474 270 1128\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once Bob decided to lay a parquet floor in his living room. The living room is of size n × m metres. Bob had planks of three types: a planks 1 × 2 meters, b planks 2 × 1 meters, and c...
292_E. Copying Data_21683
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly. More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of...
import sys ''' SEGMENT TREE Assign ''' class SegmTree(): ''' - modify elements on interval - get single element ''' def __init__(self, size): N = 1 while N < size: N <<= 1 self.N = N self.tree = [0] * (2*N) def modify_range(self, l, r, value): ...
{ "input": [ "5 10\n1 2 0 -1 3\n3 1 5 -2 0\n2 5\n1 3 3 3\n2 5\n2 4\n2 1\n1 2 1 4\n2 1\n2 4\n1 4 2 1\n2 2\n", "10 10\n-1 1 -1 2 -2 2 1 2 -1 0\n-1 -2 2 0 1 -1 -1 2 -2 1\n2 1\n2 2\n2 8\n1 6 8 1\n2 5\n2 9\n1 1 7 4\n2 5\n2 2\n2 3\n", "3 6\n4 -3 0\n1 3 -5\n2 2\n2 3\n1 2 1 2\n1 2 1 2\n2 2\n2 2\n", "2 5\n-3 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some p...
316_G1. Good Substrings_21687
Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string s. To determine if a string is good or not the game uses rules. Overall there are n rules. Each rule is described by a group of three (p, l, r), where p is a string and l and r (...
def count(p, s): start = 0 c = 0 while True: try: pos = s.index(p, start) c += 1 start = pos + 1 except ValueError: return c s = input() n = int(input()) pravs = [] for i in range(n): p, l, r = input().split() l = int(l); r = int(r) ...
{ "input": [ "aaab\n2\naa 0 0\naab 1 1\n", "a\n0\n", "ltntlnen\n3\nn 0 0\nttlneenl 1 4\nlelllt 1 1\n", "hrsszhjvrbsszvqesessiqbzeqijjerqirhhjsehiehezvzihzssivhvihrivrhizeqvvhejvibhqrbbqjrjsessvrhijzsrqirvsqqbibzhrzseijesszsrzhjjbqsrjrsrizivvhsjvqz\n3\nrzzevhiibziqiirhsebebrvrvvjihsjhvjjrejvqezireqevqe...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string s. To determine if a string is good or no...
340_C. Tourist Problem_21691
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents t...
import math n = int(input()) l = [int(x) for x in input().split()] a1 = sum(l) a2 = n a3 = 0 temp = 0 l.sort() for i in range(n): temp += l[n-i-1] a3-=(a1-temp) a3+=(n-i-1)*(l[n-i-1]) a1 = a1+a3+a3 a4 = math.gcd(a1, a2) print(a1//a4, a2//a4)
{ "input": [ "3\n2 3 5\n", "3\n4663018 4080080 6848370\n", "2\n5555 1242323\n", "6\n3835302 971984 8706888 1080445 2224695 1093317\n", "50\n3987477 8934938 4067156 6712855 7269334 5039822 9533601 9335400 5992073 2619268 438734 8620973 4347619 4307947 2249161 815221 7615258 8244100 8341666 5908546 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the exc...
387_D. George and Interesting Graph_21696
George loves graphs. Most of all, he loves interesting graphs. We will assume that a directed graph is interesting, if it meets the following criteria: * The graph doesn't contain any multiple arcs; * There is vertex v (we'll call her the center), such that for any vertex of graph u, the graph contains arcs (u, ...
# by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def matching(n,m,path): # Hopkrocft Karp O(EV^0.5) match1 = [-1]*n match2 = [-1]*m for node in range(n): for nei in path[node]: if match2[nei] == -1: match1[nod...
{ "input": [ "3 1\n2 2\n", "3 6\n1 1\n2 2\n3 1\n3 2\n2 3\n3 3\n", "3 7\n1 1\n2 2\n3 1\n1 3\n3 2\n2 3\n3 3\n", "500 1\n182 425\n", "3 1\n1 3\n", "6 1\n1 4\n", "499 1\n372 498\n", "8 12\n4 2\n1 5\n3 7\n4 3\n8 2\n5 4\n4 1\n6 5\n6 7\n5 2\n1 4\n8 3\n", "5 25\n4 2\n1 4\n5 2\n2 2\n3 1\n4 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: George loves graphs. Most of all, he loves interesting graphs. We will assume that a directed graph is interesting, if it meets the following criteria: * The graph doesn't contain...
408_B. Garland_21700
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter. The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece s...
#!/usr/bin/python import re import inspect from sys import argv, exit def rstr(): return input() def rstrs(splitchar=' '): return [i for i in input().split(splitchar)] def rint(): return int(input()) def rints(splitchar=' '): return [int(i) for i in rstrs(splitchar)] def varnames(obj, namespace=glo...
{ "input": [ "aaabbac\naabbccac\n", "a\nz\n", "zbvwnlgkshqerxptyod\nz\n", "lccfdfnfflncddlksndcwnfcllnnaswcdnldafcalssfcdnkkaklwnnacsncfwanwnwfadawcsdcfwflnnlfsfclkfknlaldna\nuaaldlllhedgugugueahddhedbuddaedhaaeugdubaealbgbagedldbl\n", "bbzmzqazmbambnmzaabznmbabzqnaabmabmnnabbmnzaanzzezebzabqaabzq...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper,...
435_C. Cardiogram_21704
In this problem, your task is to use ASCII graphics to paint a cardiogram. A cardiogram is a polyline with the following corners: <image> That is, a cardiogram is fully defined by a sequence of positive integers a1, a2, ..., an. Your task is to paint a cardiogram by given sequence ai. Input The first line contai...
n = int(input()) a_list = list(map(int, list(str(input()).split(" ")))) out = [' '*(sum(a_list))]*2000 cur,pref_size = 1000, 0 cur_max, cur_min = 1000,1000 for k in range(n): for i in range(a_list[k]): ind = pref_size + i if k % 2 == 0: out[cur] = out[cur][:ind] + '/' + out[cur][ind+1:]...
{ "input": [ "5\n3 1 2 5 1\n", "3\n1 5 1\n", "3\n1 2 1\n", "4\n1 1 1 1\n", "9\n96 114 117 124 114 107 126 95 105\n", "5\n1 2 1 1 1\n", "5\n2 2 1 2 2\n", "3\n1 2 2\n", "5\n1 1 1 1 1\n", "4\n1 1 2 1\n", "4\n1 1 2 2\n", "3\n2 2 2\n", "3\n1 1 1\n", "4\n2 2 2 2\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In this problem, your task is to use ASCII graphics to paint a cardiogram. A cardiogram is a polyline with the following corners: <image> That is, a cardiogram is fully defined by...
47_C. Crossword_21709
Vasya trains to compose crossword puzzles. He can only compose crosswords of a very simplе type so far. All of them consist of exactly six words; the words can be read only from top to bottom vertically and from the left to the right horizontally. The words are arranged in the form of a rectangular "eight" or infinity ...
from itertools import permutations v = [] for p in permutations(input() for i in range(6)): if len(p[1]) != len(p[0]) + len(p[2]) - 1 or len(p[4]) != len(p[3]) + len(p[5]) - 1: continue elif p[0][0] != p[3][0] or p[0][-1] != p[4][0]: continue elif p[1][0] != p[3][-1] or p[1][len(p[0]) - 1] !...
{ "input": [ "NOD\nBAA\nYARD\nAIRWAY\nNEWTON\nBURN\n", "PTC\nJYNYFDSGI\nZGPPC\nIXEJNDOP\nJJFS\nSSXXQOFGJUZ\n", "AAA\nAAA\nAAAAA\nAAA\nAAA\nAAAAA\n", "SAYRFLLIJHFTHVXAJUSAE\nAMSRRV\nVKKRTVWHZLKVIURJMOAZNUIQAZ\nVCEDJVWS\nAEKQPV\nZWE\n", "ABBAAAABBBB\nBBABAAAABAABAB\nBAABABBBABBBBBAAA\nBABABAAABBAAAB...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya trains to compose crossword puzzles. He can only compose crosswords of a very simplе type so far. All of them consist of exactly six words; the words can be read only from top t...
528_A. Glass Carving_21715
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm × h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how. In order not to waste time, he decided to practice t...
# import itertools # import bisect # import math from collections import defaultdict, Counter import os import sys from io import BytesIO, IOBase # sys.setrecursionlimit(10 ** 5) ii = lambda: int(input()) lmii = lambda: list(map(int, input().split())) slmii = lambda: sorted(map(int, input().split())) li = lambda: list...
{ "input": [ "4 3 4\nH 2\nV 2\nV 3\nV 1\n", "7 6 5\nH 4\nV 3\nV 5\nH 2\nV 1\n", "2 2 1\nH 1\n", "200000 200000 1\nH 1\n", "2 2 2\nH 1\nV 1\n", "10 10 10\nV 6\nH 8\nV 4\nV 8\nH 2\nH 5\nV 9\nH 7\nH 3\nV 7\n", "2 2 1\nV 1\n", "2 2 2\nV 1\nH 1\n", "2 4 1\nH 2\n", "2 3 1\nH 1\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm × h mm sheet of glass, a diamond glass cutt...
553_D. Nudist Beach_21718
Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Life Fibers. There are n cities, labeled from 1 to n, and m bidirectional roads between them. Currently, there are Life Fibers in every ci...
import heapq def read_data(): ''' n: number of cities m: number of roads k: initial numuber of fortresses of Life Fibers Es: list of edges fs: fs[i] = True -> city i is under control of Life Fibers gs: gs[i] number of edges connected to city i hs: hs[i] number of adjacent cities under c...
{ "input": [ "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4\n", "9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9\n", "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4\n", "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4\n", "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Li...
57_C. Array_21722
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful...
#love python :) #medo journy to icpc n = int(input()) m = int(1e9 + 7) p = 1 for i in range(1, n + 1): p *= 2 * n - i p *= pow(i, m - 2, m) p %= m print((2 * p - n) % m)
{ "input": [ "2\n", "3\n", "82\n", "691\n", "61043\n", "99925\n", "12\n", "42\n", "54904\n", "2197\n", "1300\n", "57500\n", "87440\n", "94962\n", "52402\n", "1\n", "47504\n", "49948\n", "67828\n", "38\n", "74977\n", "17813\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not ...
601_B. Lipshitz Sequence_21725
A function <image> is called Lipschitz continuous if there is a real constant K such that the inequality |f(x) - f(y)| ≤ K·|x - y| holds for all <image>. We'll deal with a more... discrete version of this term. For an array <image>, we define it's Lipschitz constant <image> as follows: * if n < 2, <image> * if n ...
f = lambda: map(int, input().split()) n, m = f() t = list(f()) p = [1e9] + [abs(b - a) for a, b in zip(t, t[1:])] + [1e9] L, R = [0] * n, [0] * n for i in range(1, n): j = n - i x, y = i - 1, j + 1 a, b = p[i], p[j] while a > p[x]: x = L[x] while b >= p[y]: y = R[y] L[i], R[j] = x, y for k in ra...
{ "input": [ "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5\n", "10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9\n", "2 2\n0 0\n1 2\n1 2\n", "2 2\n0 100000000\n1 2\n1 2\n", "3 6\n10 20 30\n1 2\n1 3\n2 3\n1 2\n2 3\n1 3\n", "3 6\n100000000 99999999 0\n1 2\n1 3\n2 3\n1 2\n2 3\n1 3\n", "3 6\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A function <image> is called Lipschitz continuous if there is a real constant K such that the inequality |f(x) - f(y)| ≤ K·|x - y| holds for all <image>. We'll deal with a more... dis...