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
p00232 Life Game_1434
Taro went to a toy store to buy a game of life made by Aizu Hobby. Life games are played using a board with squares and roulette. As shown in the figure, the board has one start point and one goal point, which are connected by a single grid. First, the pieces are placed in the square at the starting point, and the piec...
from heapq import heappush, heappop def main(): while True: x, y, z = map(int, input().split()) if x == 0:break vlst = list(map(int, input().split())) events = {} for _ in range(z): n, e, a = map(int, input().split()) events[n] = (e, a) que = [] heappush(que, (0, 0)) ...
{ "input": [ "1 2 0\n1\n1 2 1\n1\n1 2 100\n1 2 1\n2\n1 2 100\n2 2 1\n1 2\n1 2 100\n4 5 3\n1 2 3 4\n1 1 2\n2 2 100\n4 3 60\n0 0 0", "1 2 0\n1\n1 2 1\n1\n1 2 101\n1 2 1\n2\n1 2 100\n2 2 1\n1 2\n1 2 100\n4 5 3\n1 2 3 4\n1 1 2\n2 2 100\n4 3 60\n0 0 0", "1 2 0\n1\n1 2 1\n1\n1 2 101\n1 2 1\n2\n1 2 100\n2 2 1\n1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Taro went to a toy store to buy a game of life made by Aizu Hobby. Life games are played using a board with squares and roulette. As shown in the figure, the board has one start point...
p00610 Cleaning Robot 2_1439
Dr. Asimov, a robotics researcher, released cleaning robots he developed (see Problem B). His robots soon became very popular and he got much income. Now he is pretty rich. Wonderful. First, he renovated his house. Once his house had 9 rooms that were arranged in a square, but now his house has N × N rooms arranged in...
# AOJ 1024 Cleaning Robot 2.0 # Python3 2018.7.5 bal4u mv = ((-1,0), (0,1), (1,0), (0,-1)) d2c = {0:'.', 1:'E'} while True: n, k = map(int, input().split()) if n == 0: break k, n1 = k-1, n-1 if (n & 1) or k >= (1<<(n>>1)): print("No\n") continue arr = [[-1 for c in range(n)] for r in range(n)] for c in rang...
{ "input": [ "2 1\n2 3\n6 4\n0 0", "3 1\n2 3\n6 4\n0 0", "4 1\n2 3\n6 4\n0 0", "3 1\n4 3\n6 4\n0 0", "4 1\n1 3\n6 6\n0 0", "5 1\n4 3\n6 6\n0 0", "8 1\n1 3\n6 6\n0 0", "5 1\n4 3\n12 4\n0 0", "8 1\n1 3\n2 6\n0 0", "5 1\n4 3\n23 4\n0 0", "16 1\n1 3\n2 6\n0 0", "19 1\n1 3\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dr. Asimov, a robotics researcher, released cleaning robots he developed (see Problem B). His robots soon became very popular and he got much income. Now he is pretty rich. Wonderful....
p00748 Pollock's conjecture_1442
The nth triangular number is defined as the sum of the first n positive integers. The nth tetrahedral number is defined as the sum of the first n triangular numbers. It is easy to show that the nth tetrahedral number is equal to n(n+1)(n+2) ⁄ 6. For example, the 5th tetrahedral number is 1+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+...
def main(): INIT = 100 query = [] ans = [] while True: q = int(input()) if q == 0: break query.append(q) MAX = max(query) table = [INIT] * (MAX + 1) table[0] = 0 all_item = [i * (i + 1) * (i + 2) // 6 for i in range(1, 181)] odd_item = [i for i in all_item if i % 2] eve_item ...
{ "input": [ "40\n14\n5\n165\n120\n103\n106\n139\n0", "40\n14\n5\n165\n120\n133\n106\n139\n0", "40\n14\n5\n98\n120\n133\n106\n139\n0", "40\n14\n5\n98\n120\n133\n106\n150\n0", "40\n14\n5\n70\n120\n133\n106\n150\n0", "40\n14\n9\n70\n120\n133\n106\n150\n0", "40\n14\n9\n70\n72\n133\n106\n150\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The nth triangular number is defined as the sum of the first n positive integers. The nth tetrahedral number is defined as the sum of the first n triangular numbers. It is easy to sho...
p01017 Yu-kun Likes Rectangles_1447
Background The kindergarten attached to the University of Aizu is a kindergarten where children who love programming gather. Yu, one of the kindergarten children, loves rectangles as much as programming. Yu-kun decided to write a program to calculate the maximum score that can be obtained, thinking of a new play to ge...
H, W = map(int, input().split()) a_mp = [list(map(int, input().split())) for _ in range(H)] b_mp = [list(map(int, input().split())) for _ in range(H)] h, w = map(int, input().split()) c_mp = [list(map(int, input().split())) for _ in range(h)] INF = 10 ** 20 def check(x, y): ret = 0 for dy in range(h): for dx ...
{ "input": [ "4 4\n10 2 -1 6\n8 1 -100 41\n22 47 32 11\n-41 99 12 -8\n1 0 0 1\n0 0 1 0\n0 1 0 1\n0 0 1 0\n2 3\n1 0 1\n0 1 0", "3 4\n4 1 9 1\n9 1 -1 3\n2 -4 1 10\n1 1 0 1\n0 1 1 1\n1 1 0 0\n1 4\n1 1 1 1", "3 3\n5 1 3\n2 5 9\n0 1 5\n1 0 0\n0 1 1\n0 1 1\n1 1\n1", "4 4\n10 2 -1 6\n8 1 -100 41\n22 47 32 11...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Background The kindergarten attached to the University of Aizu is a kindergarten where children who love programming gather. Yu, one of the kindergarten children, loves rectangles as...
p01289 Strange Couple_1451
Alice and Bob are going to drive from their home to a theater for a date. They are very challenging - they have no maps with them even though they don’t know the route at all (since they have just moved to their new home). Yes, they will be going just by their feeling. The town they drive can be considered as an undir...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import time,random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 mod2 = 998244353 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return lis...
{ "input": [ "5 1 5\n1 0 1 0 0\n0 2 1 0 0\n2 0 0 1 0\n1 0 0 1 0\n0 1 1 0 1\n0 0 0 1 0\n0 0 0", "5 1 5\n0 0 1 0 0\n0 2 1 0 0\n2 0 0 1 0\n1 0 0 1 0\n0 1 1 0 1\n0 0 0 1 0\n0 0 0", "5 1 5\n1 0 1 0 1\n0 2 1 0 0\n2 0 0 1 0\n1 0 0 1 0\n0 1 1 0 1\n0 0 0 1 0\n0 0 0", "5 1 5\n1 0 1 0 1\n0 2 1 0 0\n2 0 0 1 0\n1 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice and Bob are going to drive from their home to a theater for a date. They are very challenging - they have no maps with them even though they don’t know the route at all (since t...
p01770 Arojam's Mask_1458
Problem statement You are a hero. The world in which the hero is traveling consists of N cities and M roads connecting different cities. Road i connects town a_i and town b_i and can move in both directions. The purpose of the brave is to start from town S and move to town T. S and T are different cities. The hero ca...
from collections import deque n, m, e, s, t, r = map(int, input().split()) edges = [[] for _ in range(n * 2 ** e)] for _ in range(m): a, b = map(int, input().split()) for i in range(2 ** e): edges[a + n * i].append(b + n * i) edges[b + n * i].append(a + n * i) start = s slide = {} for i in range(e): a, ...
{ "input": [ "8 5 2 0 5 5\n0 1\n0 3\n0 6\n1 2\n6 7\n3 4 7\n4 5 2", "8 5 2 0 5 5\n0 1\n0 3\n0 6\n1 2\n6 7\n1 4 7\n4 5 2", "8 5 2 0 5 5\n0 1\n0 3\n0 2\n1 2\n6 7\n3 4 7\n4 5 2", "8 5 2 0 5 5\n0 1\n0 3\n0 6\n1 2\n6 7\n1 4 7\n4 5 4", "8 5 2 0 5 5\n0 2\n0 3\n0 6\n1 2\n6 7\n1 4 7\n4 5 4", "8 5 2 0 3 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem statement You are a hero. The world in which the hero is traveling consists of N cities and M roads connecting different cities. Road i connects town a_i and town b_i and can...
p02185 Many Decimal Integers_1464
D: Many Decimal Integers problem Given a string S consisting only of numbers (0-9) and a string T consisting only of numbers and `?`. S and T are the same length. Consider changing each `?` That exists in T to one of the numbers from 0 to 9 to create the string T'consisting of only numbers. At this time, it must be ...
#!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": [ "73\n6?", "73\n?6", "73\n@6", "73\n5?", "73\n?5", "73\n4?", "73\n?4", "73\n?8", "73\n?7", "73\n7?", "73\n?3", "73\n3?", "73\n?9", "73\n?2", "73\n70", "73\n1?", "73\n27", "73\n?1", "73\n49", "73\n72", "73\n59", "73\n?0...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: D: Many Decimal Integers problem Given a string S consisting only of numbers (0-9) and a string T consisting only of numbers and `?`. S and T are the same length. Consider changing...
p02327 Largest Rectangle_1467
Given a matrix (H × W) which contains only 1 and 0, find the area of the largest rectangle which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following...
H, W = map(int, input().split()) height = W * [0] max_area = 0 for _ in range(H): height = [0 if t else height[j]+1 for j, t in enumerate(map(int, input().split()))] height.append(0) stack = [] for i, h in enumerate(height): if stack: if stack[-1][1] == h: continue ...
{ "input": [ "4 5\n0 0 1 0 0\n1 0 0 0 0\n0 0 0 1 0\n0 0 0 1 0", "4 5\n0 0 1 0 0\n1 0 0 0 0\n0 0 0 0 0\n0 0 0 1 0", "4 5\n0 0 1 0 0\n1 0 0 0 0\n0 0 0 0 1\n0 0 0 1 0", "1 5\n0 0 1 0 0\n1 0 0 0 1\n0 0 0 1 0\n0 0 0 1 0", "0 5\n0 0 1 -1 0\n1 0 0 0 0\n0 0 0 0 0\n0 0 0 1 0", "1 5\n1 0 1 0 1\n2 0 -1 0...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given a matrix (H × W) which contains only 1 and 0, find the area of the largest rectangle which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,...
p02472 Addition of Big Integers_1471
Addition of Big Integers Given two integers $A$ and $B$, compute the sum, $A + B$. Input Two integers $A$ and $B$ separated by a space character are given in a line. Output Print the sum in a line. Constraints * $-1 \times 10^{100000} \leq A, B \leq 10^{100000}$ Sample Input 1 5 8 Sample Output 1 13 S...
n, m = map(int, input().split()) print(n + m)
{ "input": [ "5 8", "9 8", "17 8", "17 12", "17 16", "21 16", "41 16", "58 16", "58 30", "109 30", "195 30", "302 30", "552 30", "47 30", "87 30", "87 37", "139 37", "139 39", "107 39", "107 73", "107 60", "107 79", "107 51", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Addition of Big Integers Given two integers $A$ and $B$, compute the sum, $A + B$. Input Two integers $A$ and $B$ separated by a space character are given in a line. Output Print...
1011_A. Stages_1481
Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string — concatenation of letters, which correspond to the stages. There are n stages available. The rock...
n,k = list(map(int,input().split())) data = sorted(list(input())) data = list(map(lambda x:ord(x)-ord('a')+1,data)) result = 0 used = 0 idx =0 prev = -2 # print(data) for d in data: if d > prev+1: result+= d prev = d used += 1 if used == k: break if used < k: print(-...
{ "input": [ "7 4\nproblem\n", "2 2\nab\n", "5 3\nxyabd\n", "12 1\nabaabbaaabbb\n", "50 1\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", "2 1\nxz\n", "2 2\nac\n", "50 13\nqwertyuiopasdfghjklzxcvbnmaaaaaaaaaaaaaaaaaaaaaaaa\n", "50 2\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the ...
1036_B. Diagonal Walking v.2_1485
Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move: * (1, 0); * (1, 1); * (0, 1); * (-1, 1); * (-1, 0); * (-1, -1);...
q = int(input()) for i in range(q): n, m, k = map(int, input().split()) if m >k or n > k: print(-1) else: print(k - (k-n)%2 - (k-m)%2)
{ "input": [ "3\n2 2 3\n4 3 7\n10 1 9\n", "3\n2 2 3\n4 3 7\n7 1 9\n", "3\n2 2 3\n4 3 9\n7 1 9\n", "3\n2 2 3\n6 1 9\n7 1 1\n", "3\n2 1 3\n6 1 9\n7 0 2\n", "3\n2 0 3\n3 1 9\n13 0 2\n", "3\n4 0 3\n3 1 9\n13 0 2\n", "3\n7 0 0\n3 1 5\n22 0 2\n", "3\n1 0 0\n5 0 5\n22 0 2\n", "3\n1 0 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0),...
1059_A. Cashier_1489
Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is...
n,l,a = map(int,input().split()) b =[] for i in range(n): b.append([int(e) for e in input().split()]) ans = 0 for i in range(n-1): ans += (b[i+1][0] - b[i][1] - b[i][0])//a if(n > 0): ans += b[0][0]//a ans += (l - b[n-1][1] - b[n-1][0])//a else: ans += l//a print(ans)
{ "input": [ "2 11 3\n0 1\n1 1\n", "0 5 2\n", "1 3 2\n1 2\n", "0 1 1\n", "1 5 3\n3 1\n", "0 1000000000 1\n", "3 21 3\n3 3\n9 3\n15 3\n", "0 1000000000 1000000000\n", "1 1 1\n0 1\n", "2 10 3\n0 1\n9 1\n", "1 1000000000 1\n0 1000000000\n", "0 1000000000 500000000\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} min...
1080_C. Masha and two friends_1493
Recently, Masha was presented with a chessboard with a height of n and a width of m. The rows on the chessboard are numbered from 1 to n from bottom to top. The columns are numbered from 1 to m from left to right. Therefore, each cell can be specified with the coordinates (x,y), where x is the column number, and y is ...
def num_sq(x,y,x2,y2): # b, w a = (abs(x2-x)+1) b = (abs(y2-y)+1) if a % 2 == 0 or b % 2 == 0: return (a*b // 2, a*b // 2) if (x+y) % 2 == 0: num_b = a * b // 2 return (num_b, a * b - num_b) num_w = a * b // 2 return (a * b - num_w, num_w) def pt_in(p1, r1, r2): r...
{ "input": [ "5\n2 2\n1 1 2 2\n1 1 2 2\n3 4\n2 2 3 2\n3 1 4 3\n1 5\n1 1 5 1\n3 1 5 1\n4 4\n1 1 4 2\n1 3 4 4\n3 4\n1 2 4 2\n2 1 3 3\n", "1\n3456 4567\n1 234 678 1000\n346 903 1005 2003\n", "5\n2 2\n1 1 2 2\n1 1 2 2\n3 5\n2 2 3 2\n3 1 4 3\n1 5\n1 1 5 1\n3 1 5 1\n4 4\n1 1 4 2\n1 3 4 4\n3 4\n1 2 4 2\n2 1 3 3\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently, Masha was presented with a chessboard with a height of n and a width of m. The rows on the chessboard are numbered from 1 to n from bottom to top. The columns are numbered ...
10_B. Cinema Cashier_1497
All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the respon...
__author__ = 'Darren' def solve(): n, k = map(int, input().split()) group = map(int, input().split()) available = [[k, 1][:] for _ in range(k+1)] center = (k + 1) // 2 for m in group: closest, best_row, best_col = 10000, -1, -1 for row in range(1, k+1): col = 0 ...
{ "input": [ "4 3\n1 2 3 1\n", "2 1\n1 1\n", "2 3\n3 3\n", "80 29\n19 15 15 27 2 25 2 5 29 11 6 4 20 11 27 16 6 6 10 2 5 12 8 23 11 7 11 13 19 29 8 4 9 13 14 22 16 29 7 12 17 5 17 14 6 15 8 25 11 16 14 4 3 7 25 2 5 2 12 12 22 18 14 16 5 19 25 4 21 24 7 11 21 27 10 16 21 17 19 13\n", "1 3\n1\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box...
1121_A. Technogoblet of Fire_1501
Everybody knows that the m-coder Tournament will happen soon. m schools participate in the tournament, and only one student from each school participates. There are a total of n students in those schools. Before the tournament, all students put their names and the names of their schools into the Technogoblet of Fire. ...
n,m,k=map(int,input().split()) p=list(map(int,input().split())) s=list(map(int,input().split())) c=set(map(int,input().split())) d={} for i in range(n): if s[i] not in d: d[s[i]]=[-1] if p[i]>d[s[i]][0]: d[s[i]]=(p[i],i) st=set() for i in d: st.add(d[i][1]+1) #print(c,st) c=c.difference(st) ...
{ "input": [ "8 4 4\n1 2 3 4 5 6 7 8\n4 3 2 1 4 3 2 1\n3 4 5 6\n", "7 3 1\n1 5 3 4 6 7 2\n1 3 1 2 1 2 3\n3\n", "2 1 1\n1 2\n1 1\n1\n", "2 1 1\n1 2\n1 1\n2\n", "1 1 1\n1\n1\n1\n", "10 5 4\n4 2 1 7 10 9 6 3 5 8\n3 2 1 4 5 1 4 2 4 2\n9 3 2 6\n", "10 1 10\n9 1 2 3 5 7 4 10 6 8\n1 1 1 1 1 1 1 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Everybody knows that the m-coder Tournament will happen soon. m schools participate in the tournament, and only one student from each school participates. There are a total of n stud...
1148_C. Crazy Diamond_1505
You are given a permutation p of integers from 1 to n, where n is an even number. Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type: * take two indices i and j such that 2 ⋅ |i - j| ≥ n and swap p_i and p_j. There is no need to minimize the number of...
n = int(input()) p = [*map(int, input().split())] p = [i - 1 for i in p] #print(p) pos = {} ans = [] for i, j in enumerate(p): pos[j] = i def swap(i, j): ans.append((i + 1, j + 1)) pos[p[i]], pos[p[j]] = pos[p[j]], pos[p[i]] p[i], p[j] = p[j], p[i] def do(i): j=pos[i] if j<n//2: ...
{ "input": [ "2\n2 1\n", "4\n3 4 1 2\n", "6\n2 5 3 1 4 6\n", "10\n6 1 2 9 3 5 4 10 7 8\n", "100\n47 54 37 8 33 97 74 80 35 96 55 39 15 67 85 92 49 50 1 25 70 53 48 19 86 16 14 68 60 41 40 87 29 27 34 94 26 42 17 38 45 4 22 13 31 57 23 99 73 90 44 88 3 63 59 93 52 30 20 21 18 7 12 58 56 81 32 82 84...
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 of integers from 1 to n, where n is an even number. Your goal is to sort the permutation. To do so, you can perform zero or more operations of the foll...
1206_F. Almost All_1512
You are given a tree with n nodes. You have to write non-negative integers on its edges so that the following condition would be satisfied: For every two nodes i, j, look at the path between them and count the sum of numbers on the edges of this path. Write all obtained sums on the blackboard. Then every integer from ...
import math n = int(input()) if n == 1: print() else: edge = [list(map(int, input().split())) for i in range(1, n) ] g = {} for x, y in edge: if x not in g: g[x] = [] if y not in g: g[y] = [] g[x].append(y) g[y].append(x) ...
{ "input": [ "4\n2 4\n2 3\n2 1\n", "3\n2 3\n2 1\n", "5\n1 2\n1 3\n1 4\n2 5\n", "43\n7 29\n19 18\n32 4\n39 40\n21 11\n34 28\n30 23\n13 19\n33 39\n13 35\n37 21\n29 8\n40 1\n7 9\n8 38\n7 31\n8 12\n7 42\n33 20\n5 36\n17 32\n30 43\n39 34\n40 3\n8 24\n13 37\n35 16\n23 22\n7 41\n35 30\n9 14\n13 2\n7 27\n10 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a tree with n nodes. You have to write non-negative integers on its edges so that the following condition would be satisfied: For every two nodes i, j, look at the path...
1225_B1. TV Subscriptions (Easy Version)_1515
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ k), where a_i is the show, the episode of which will be shown in i-th day. The...
x = int(input()) for i in range(x): n, k, d = map(int, input().split(' ')) l = map(int, input().split(' ')) l = list(l) ar = [] for j in range(n-d+1): ar.append(len(set(l[j:(j+d)]))) print(min(ar))
{ "input": [ "4\n5 2 2\n1 2 1 2 1\n9 3 3\n3 3 3 2 2 2 1 1 1\n4 10 4\n10 8 6 4\n16 9 8\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3\n", "1\n100 20 1\n10 17 9 8 11 8 8 3 7 20 11 10 7 7 2 13 10 7 7 10 7 8 4 17 20 9 5 18 12 8 3 5 19 1 18 14 9 11 12 11 11 12 12 9 13 8 20 3 8 1 15 20 6 18 8 1 13 11 4 17 12 15 4 4 11 19 18 5 2 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n d...
1249_D1. Too Many Segments (easy version)_1519
The only difference between easy and hard versions is constraints. You are given n segments on the coordinate axis OX. Segments can intersect, lie inside each other and even coincide. The i-th segment is [l_i; r_i] (l_i ≤ r_i) and it covers all integer points j such that l_i ≤ j ≤ r_i. The integer point is called bad...
import sys from heapq import * #sys.stdin = open('in', 'r') #n = int(input()) n,k = map(int, input().split()) seg = [] for i in range(n): l, r = map(int, input().split()) seg.append((l, r, i+1)) seg.sort() c = 0 res = [] i = 0 hmin = [] hmax = [] rem = set() while i < n: l,r,si = seg[i] while i < n and ...
{ "input": [ "5 1\n29 30\n30 30\n29 29\n28 30\n30 30\n", "7 2\n11 11\n9 11\n7 8\n8 9\n7 8\n9 11\n7 9\n", "6 1\n2 3\n3 3\n2 3\n2 2\n2 3\n2 3\n", "15 2\n4 4\n16 18\n16 18\n6 9\n3 14\n8 11\n3 13\n3 6\n14 16\n5 17\n7 14\n11 14\n4 4\n10 11\n13 17\n", "16 3\n181 182\n183 184\n179 181\n178 178\n182 185\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The only difference between easy and hard versions is constraints. You are given n segments on the coordinate axis OX. Segments can intersect, lie inside each other and even coincide...
1267_L. Lexicography_1523
Lucy likes letters. She studied the definition of the lexicographical order at school and plays with it. At first, she tried to construct the lexicographically smallest word out of given letters. It was so easy! Then she tried to build multiple words and minimize one of them. This was much harder! Formally, Lucy want...
# ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode...
{ "input": [ "2 3 1\nabcabc\n", "3 2 2\nabcdef\n", "5 5 5\nuuuuuyyyyybbbbbqqqqqkkkkk\n", "10 9 2\nskpjgpwrqajrdnynybnsmybttmhkboiqlndqtghvqyorholdzwasytnvteawhahjfhjknhfobiifrtfrlbjkqyxgnc\n", "10 1 3\navdtkzddkk\n", "3 2 2\nabcdef\n", "1 10 1\nlduuukmwxx\n", "9 1 9\neeeeeeeee\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Lucy likes letters. She studied the definition of the lexicographical order at school and plays with it. At first, she tried to construct the lexicographically smallest word out of g...
1290_A. Mind Control_1527
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for h...
t = int(input()) for _ in range(t): ans = 0 n, pos, control = map(int, input().split()) control = min(control, pos - 1) not_control = pos - control - 1 num = n - control - not_control a = list(map(int, input().split())) for i in range(control + 1): tmp = 10 ** 10 + 1 for j in...
{ "input": [ "4\n6 4 2\n2 9 2 3 8 5\n4 4 1\n2 13 60 4\n4 1 3\n1 2 2 1\n2 2 0\n1 2\n", "4\n6 4 2\n2 9 2 3 8 5\n4 4 1\n2 13 60 4\n4 1 3\n1 2 1 1\n2 2 0\n1 2\n", "4\n6 4 2\n2 9 2 3 8 5\n4 4 1\n2 13 60 4\n4 2 3\n1 2 2 1\n2 2 0\n1 2\n", "4\n6 3 2\n2 9 2 3 8 5\n4 4 1\n2 13 60 4\n4 2 3\n1 2 2 1\n2 2 0\n1 2\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. E...
1332_G. No Monotone Triples_1532
Given a sequence of integers a of length n, a tuple (i,j,k) is called monotone triples if * 1 ≤ i<j<k≤ n; * a_i ≤ a_j ≤ a_k or a_i ≥ a_j ≥ a_k is satisfied. For example, a=[5,3,4,5], then (2,3,4) is monotone triples for sequence a while (1,3,4) is not. Bob is given a sequence of integers a of length n in a ...
import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from operator import itemgetter import bisect n,q=map(int,input().split()) A=[-1]+list(map(int,input().split())) Q=[list(map(int,input().split()))+[i] for i in range(q)] Q.sort(key=itemgetter(1)) Q_ind=0 ANS1=[-1000,-1000,-1000,-100...
{ "input": [ "6 2\n3 1 4 1 5 9\n1 3\n4 6\n", "10 5\n448242954 927941687 223935474 107020324 288030870 635970780 444360884 697847668 783719272 808030300\n3 10\n4 9\n3 8\n2 5\n5 9\n", "3 1\n1000000000 1000000000 1000000000\n1 3\n", "30 27\n698632656 525581861 577504542 296603772 998990943 682881766 9548...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given a sequence of integers a of length n, a tuple (i,j,k) is called monotone triples if * 1 ≤ i<j<k≤ n; * a_i ≤ a_j ≤ a_k or a_i ≥ a_j ≥ a_k is satisfied. For example, a=...
1353_C. Board Moves_1535
You are given a board of size n × n, where n is odd (not divisible by 2). Initially, each cell of the board contains one figure. In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell (i, j) you can move...
I = input for _ in range(int(I())): n = int(I())+1 s = 0 for i in range(1,n//2): s += 8*i*i print(s)
{ "input": [ "3\n1\n5\n499993\n", "3\n5\n3005\n3005\n", "1\n69791\n", "1\n214541\n", "1\n499999\n", "1\n214145\n", "3\n5\n3005\n1983\n", "1\n203317\n", "3\n7\n3005\n3005\n", "1\n288977\n", "1\n254785\n", "1\n54111\n", "3\n7\n3005\n5103\n", "1\n151107\n", "1\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a board of size n × n, where n is odd (not divisible by 2). Initially, each cell of the board contains one figure. In one move, you can select exactly one figure presen...
1373_B. 01 Game_1539
Alica and Bob are playing a game. Initially they have a binary string s consisting of only characters 0 and 1. Alice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, Alice makes the third one, and so on. During each move, the current player must choose two different adjacent char...
from sys import stdin,stdout t=int(stdin.readline().strip()) for _ in range(t): s=stdin.readline().strip() stdout.write(("NET","DA")[min(s.count('0'),s.count('1')) % 2]+"\n")
{ "input": [ "3\n01\n1111\n0011\n", "1\n00000000000\n", "1\n111111111111111110000000000000000\n", "1\n11111111111\n", "1\n1\n", "1\n0\n", "1\n1111111111\n", "2\n11111111111\n11111111111\n", "1\n00000000010\n", "1\n111111111111101110000000000000000\n", "2\n11111111101\n11111...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alica and Bob are playing a game. Initially they have a binary string s consisting of only characters 0 and 1. Alice and Bob make alternating moves: Alice makes the first move, Bob ...
1396_A. Multiples of Length_1543
You are given an array a of n integers. You want to make all elements of a equal to zero by doing the following operation exactly three times: * Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different). It can...
n=int(input());vals=list(map(int,input().split())) if n>=2: print(1,n) ans=[-i*n for i in vals] print(*ans) print(2,n) a1=[i*(n-1) for i in vals[1:n]] print(*a1) print(1,1) print(vals[0]*(n-1)) else: print(1,1) print(-vals[0]) print(1,1) print(0) print(1,1) print(...
{ "input": [ "4\n1 3 2 4\n", "1\n-2\n", "1\n-1\n", "16\n-15108237 489260742 681810357 -78861365 -416467743 -896443270 904192296 -932642644 173249302 402207268 -329323498 537696045 -899233426 902347982 -595589754 -480337024\n", "2\n-492673762 -496405053\n", "1\n34688642\n", "32\n474784688 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array a of n integers. You want to make all elements of a equal to zero by doing the following operation exactly three times: * Select a segment, for each number ...
141_C. Queue_1547
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch break and t...
def solve(arr,n): arr.sort() names = [x[1] for x in arr] counts = [x[0] for x in arr] segments = [] curr_count = counts[0] curr_len = 0 L = 0 for i in range(n): if counts[i]==curr_count: curr_len += 1 else: segments.append((L,i-1)) ...
{ "input": [ "4\nvasya 0\npetya 1\nmanya 3\ndunay 3\n", "4\na 0\nb 2\nc 0\nd 0\n", "23\nfgtxkw 4\nqgbokh 0\nfrb 0\nnninr 10\nbczzdfvjkh 1\nuxpelsmxxq 2\nntvvx 2\nardaae 0\nctetnl 0\nhgk 0\nvmwo 18\nojaubfibf 1\nnu 7\ngbixtuotft 4\nekmjrgxylj 1\navcygg 1\nmxgdulijf 1\notjmjmvpz 3\nxzubpxqnj 14\nlowoz 3\nmn...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind numbe...
1438_C. Engineer Artem_1551
Artem is building a new robot. He has a matrix a consisting of n rows and m columns. The cell located on the i-th row from the top and the j-th column from the left has a value a_{i,j} written in it. If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells c...
def solve(): input_str = input().split() m, n = int(input_str[0]), int(input_str[1]) nums = [] for i in range(m): nums.append(list(map(int, input().split()))) for i in range(m): for j in range(n): if (i + j) & 1 == 0: if nums[i][j] & 1 == 0: ...
{ "input": [ "3\n3 2\n1 2\n4 5\n7 8\n2 2\n1 1\n3 3\n2 2\n1 3\n2 2\n", "1\n10 10\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n", "3\n3 2\n1 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Artem is building a new robot. He has a matrix a consisting of n rows and m columns. The cell located on the i-th row from the top and the j-th column from the left has a value a_{i,j...
1463_B. Find The Array_1555
You are given an array [a_1, a_2, ..., a_n] such that 1 ≤ a_i ≤ 10^9. Let S be the sum of all elements of the array a. Let's call an array b of n integers beautiful if: * 1 ≤ b_i ≤ 10^9 for each i from 1 to n; * for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or...
for _ in range(int(input())): n = map(int, input().split()) a = list(map(int, input().split())) c = 1 i = 0 while c <= 1000000000: d = 2 * c if c <= a[i] <= d: print(c, end=" ") i += 1 d = 1 if i == len(a): break c = d ...
{ "input": [ "4\n5\n1 2 3 4 5\n2\n4 6\n2\n1 1000000000\n6\n3 4 8 1 2 3\n", "1\n10\n1000000000 500000001 500000001 500000001 500000001 500000001 500000001 500000001 500000001 500000001\n", "2\n7\n818 14 386 159 110 186 49\n7\n694 42 511 953 30 284 57\n", "1\n3\n1 1 1\n", "1\n4\n37 19 19 19\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array [a_1, a_2, ..., a_n] such that 1 ≤ a_i ≤ 10^9. Let S be the sum of all elements of the array a. Let's call an array b of n integers beautiful if: * 1 ≤ b_i ...
148_B. Escape_1559
The princess is going to escape the dragon's cave, and she needs to plan it carefully. The princess runs at vp miles per hour, and the dragon flies at vd miles per hour. The dragon will discover the escape after t hours and will chase the princess immediately. Looks like there's no chance to success, but the princess ...
vp = int(input()) vd = int(input()) t = int(input()) f = int(input()) c = int(input()) distance = t * vp time = t treasure = 0 if vp == vd: if t == 0: print(1) else: print(0) elif vp > vd: print(0) else: while distance < c: timeadd = distance/(vd - vp) distance += vp * ti...
{ "input": [ "1\n2\n1\n1\n10\n", "1\n2\n1\n1\n8\n", "71\n19\n2\n10\n645\n", "6\n24\n9\n8\n628\n", "26\n36\n4\n7\n318\n", "2\n100\n10\n10\n739\n", "47\n43\n10\n9\n43\n", "71\n45\n2\n6\n547\n", "2\n1\n1\n1\n1000\n", "13\n14\n3\n3\n79\n", "77\n1\n6\n8\n831\n", "2\n27\n4\n9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The princess is going to escape the dragon's cave, and she needs to plan it carefully. The princess runs at vp miles per hour, and the dragon flies at vd miles per hour. The dragon w...
1539_D. PriceFixed_1565
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store — "PriceFixed". Here are some rules of that store: * The store has an infinite number of items of every product. * All products have the same price: 2 rubles per item. * ...
n = int(input()) arr = [] for _ in range(n): arr.append(list(map(int, input().split()))) arr.sort(key=lambda x:x[1]) # print(arr) lo, hi = 0, n-1 ans, counter = 0, 0 while lo<=hi: # print(lo, hi, counter) to_buy, req = arr[lo] if counter>=req or to_buy==0: counter += to_buy ans += to_bu...
{ "input": [ "5\n2 7\n2 8\n1 2\n2 4\n1 8\n", "3\n3 4\n1 3\n1 5\n", "8\n1 8\n1 6\n1 5\n1 3\n1 3\n1 8\n1 7\n1 3\n", "4\n4 1\n2 2\n1 6\n1 2\n", "7\n2 11\n3 12\n2 1\n2 6\n1 6\n2 2\n3 14\n", "8\n1 7\n3 16\n1 15\n2 1\n1 15\n4 11\n1 4\n2 15\n", "4\n2 1\n3 2\n4 3\n5 100\n", "15\n1 7\n1 14\n1 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store — "PriceFixed". Here are some rules of tha...
166_A. Rank List_1569
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place. You know the rules of comparing the results of two give...
n, k = map(int, input().split()) table =[] table_dic = dict() for i in range(n): p, t = map(int, input().split()) table_dic[i + 1] = [p,t] table.append([p, 50 - t]) ranking = sorted(table, key=lambda table: (table[0], table[1]), reverse=True) for i in range(n): ranking[i][1] = 50 - ranking[i][1] coun...
{ "input": [ "5 4\n3 1\n3 1\n5 3\n3 1\n3 1\n", "7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10\n", "50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\n50 50\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problem...
233_C. Cycles_1577
John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactly k cycles of length 3. A cycle of length 3 is an unordered group of three distinct graph vertices a, b and c, such that each pair of them is connected by a graph edge. John has been pa...
n, k = 0, int(input()) p = [['0'] * 100 for i in range(100)] while k: for i in range(n): if i > k: break p[n][i] = p[i][n] = '1' k -= i n += 1 print(n) for i in range(n): print(''.join(p[i][:n]))
{ "input": [ "10\n", "1\n", "99016\n", "99022\n", "5\n", "99007\n", "7\n", "99007\n", "98685\n", "100000\n", "99020\n", "99994\n", "97560\n", "4\n", "99021\n", "97560\n", "99994\n", "99011\n", "99004\n", "9\n", "99999\n", "99009\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactly k cycles of length 3. A cycle of length 3 is an ...
259_B. Little Elephant and Magic Square_1581
Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square, the sum of integers in all its rows, columns and diagonals equals 15...
l=[] for i in range(3): l.append(list(map(int,input().split()))) l[1][1]=(l[1][0]+l[1][2])//2 c=sum(l[1]) l[0][0]=c-(l[0][1]+l[0][2]) l[2][2]=c-(l[2][0]+l[2][1]) for i in range(3): for j in range(3): print(l[i][j],end=" ") print()
{ "input": [ "0 1 1\n1 0 1\n1 1 0\n", "0 3 6\n5 0 5\n4 7 0\n", "0 100000 100000\n100000 0 100000\n100000 100000 0\n", "0 99626 99582\n99766 0 99258\n99442 99398 0\n", "0 6361 2304\n1433 0 8103\n7232 3175 0\n", "0 28 10\n12 0 24\n26 8 0\n", "0 97 56\n69 0 71\n84 43 0\n", "0 4 4\n4 0 4\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagon...
282_B. Painting Eggs_1585
The Bitlandians are quite weird people. They have very peculiar customs. As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work. The kids are excited because just as is customary, they're going to be paid for the job! Overall uncle J. h...
import sys input = lambda: sys.stdin.readline() n = int(input()) S = 0 ans = '' for i in range(n): a, g = [int(x) for x in input().split(' ')] if abs(S + a) <= 500: S += a ans += 'A' else: S -= g ans += 'G' print(ans)
{ "input": [ "2\n1 999\n999 1\n", "3\n400 600\n400 600\n400 600\n", "1\n500 500\n", "2\n500 500\n500 500\n", "1\n1 999\n", "2\n499 501\n501 499\n", "10\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n", "1\n0 1000\n", "1\n1000 0\n", "3\n500 500\n1 999\n40...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Bitlandians are quite weird people. They have very peculiar customs. As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked...
305_A. Strange Addition_1589
Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 50, but he cannot sum 1 and 4. Vasya has a set of k distinct non-negative integers d1, d2, ..., dk. Vasya wants to choose some integers ...
n = int(input()) arr = list(map(int,input().split())) t = [] flag = 0 if(0 in arr): t.append(0) if(100 in arr): t.append(100) for i in range(n): if(arr[i]>0 and arr[i]<10): t.append(arr[i]) flag = 1 break for i in range(n): if(arr[i]%10 == 0 and arr[i]>0 and arr[i]<100): ...
{ "input": [ "4\n100 10 1 0\n", "3\n2 70 3\n", "1\n1\n", "39\n16 72 42 70 17 36 32 40 47 94 27 30 100 55 23 77 67 28 49 50 53 83 38 33 60 65 62 64 6 66 69 86 96 75 85 0 89 73 29\n", "3\n99 10 6\n", "2\n10 12\n", "4\n0 100 10 99\n", "1\n100\n", "1\n99\n", "2\n5 6\n", "3\n0 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 5...
331_A2. Oh Sweet Beaverette_1593
— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcomin...
n, t = int(input()), list(map(int, input().split())) a, b = {}, {} for i, j in enumerate(t): if not j in a: a[j] = i else: b[j] = i p = [(a[j], b[j] - 1) for j in b] s = [j if j > 0 else 0 for j in t] u = v = 2 * t[p[0][0]] - 1 for i in range(n - 1): s[i + 1] += s[i] for i, j in p: u = 2 * t[i] + s[j] - s[i...
{ "input": [ "5\n1 2 3 1 2\n", "5\n1 -2 3 1 -2\n", "3\n1000000000 1000000000 1000000000\n", "6\n-1 3 3 5 5 -1\n", "10\n-589330597 -126288833 -126288833 -834860352 -834860352 -834860352 -834860352 -21170405 -834860352 -834860352\n", "20\n-808998072 733614990 579897311 -337992089 579897311 12080...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: — Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday nig...
377_A. Maze_1599
Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side. Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any oth...
from collections import defaultdict from sys import stdin def put(): return map(int, stdin.readline().split()) def find(): for i in range(n): for j in range(m): if mat[i][j]=='.': return i,j return -1,-1 def dfs(i,j): s = [(i,j)] t = 1 vis[i][j]=1 while s: ...
{ "input": [ "3 4 2\n#..#\n..#.\n#...\n", "5 4 5\n#...\n#.#.\n.#..\n...#\n.#.#\n", "2 5 3\n.....\n..#..\n", "3 5 1\n#....\n..#..\n..###\n", "7 7 18\n#.....#\n..#.#..\n.#...#.\n...#...\n.#...#.\n..#.#..\n#.....#\n", "4 4 3\n....\n.#..\n..##\n..##\n", "5 7 4\n.......\n...#...\n...#...\n...#....
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have...
399_C. Cards_1603
User ainta loves to play with cards. He has a cards containing letter "o" and b cards containing letter "x". He arranges the cards in a row, and calculates the score of the deck by the formula below. 1. At first, the score is 0. 2. For each block of contiguous "o"s with length x the score increases by x2. 3. F...
a, b = map(int, input().split()) sx = lambda p: (a - p + 1) ** 2 + p - 1 sy = lambda q: (b % q) * (1 + b // q) ** 2 + (b // q) ** 2 * (q - b % q) n = min(a, b) if a == 0: print( -b ** 2) print( b * "x" ) elif b <= 1: print( a ** 2 - b ** 2 ) print ( a * "o" + b * "x" ) else: res = - (a + b) ** 2 ...
{ "input": [ "0 4\n", "2 3\n", "4 0\n", "7 18030\n", "79979 92032\n", "5 17\n", "1 2\n", "83093 94343\n", "1698 32709\n", "31161 18112\n", "38 5\n", "9966 12376\n", "1 0\n", "21478 14813\n", "206 209\n", "4 9\n", "4 7\n", "99979 12032\n", "19...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: User ainta loves to play with cards. He has a cards containing letter "o" and b cards containing letter "x". He arranges the cards in a row, and calculates the score of the deck by th...
421_D. Bug in Code_1606
Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the n coders on the meeting said: 'I know for sure that either x or y did it!' The head of the company d...
from collections import defaultdict from bisect import bisect_left as lower import sys input = sys.stdin.readline def put(): return map(int, input().split()) try: n,m = put() cnt, mp, ans = [0]*n, defaultdict(), [0]*n for _ in range(n): x,y = put() x,y = x-1,y-1 key = (min(x,y),...
{ "input": [ "8 6\n5 6\n5 7\n5 8\n6 2\n2 1\n7 3\n1 3\n1 4\n", "4 2\n2 3\n1 4\n1 4\n2 1\n", "10 3\n6 3\n6 10\n2 5\n5 7\n6 2\n9 2\n8 1\n10 5\n5 10\n7 6\n", "4 2\n3 4\n4 3\n4 2\n3 1\n", "10 10\n5 6\n1 4\n1 4\n1 2\n1 2\n1 2\n1 3\n1 3\n1 3\n1 4\n", "5 5\n3 2\n3 4\n2 5\n3 2\n4 3\n", "6 6\n5 6\n5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is:...
448_B. Suffix Structures_1610
Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team. At a competition the "Bizons" got the following problem: "You are given two distinct words (strings of English letters), s and t. You need to transform word s into word t". The task looked simple to the guys because they know the suffix...
s = input() t = input() ind = 0 for x in t: ind = s.find(x, ind) + 1 if ind <= 0: break if ind > 0 or len(t) == 0: print('automaton') else: ss = list(s) tt = list(t) bb1 = True if len(ss) >= len(tt): for x in tt: bb = False for y in ss: ...
{ "input": [ "need\ntree\n", "automaton\ntomat\n", "array\narary\n", "both\nhot\n", "hxsvvydmzhxrswvhkvrbjrfqkazbkjabnrdghposgyfeslzumaovfkallszzumztftgpcilwfrzpvhhbgdzdvnmseqywlzmhhoxh\ndbelhtzgkssyfrqgzuurdjhwvmdbhylhmvphjgxpzhxbb\n", "smywwqeolrsytkthfgacnbufzaulgszikbhluzcdbafjclkqueepxbho...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team. At a competition the "Bizons" got the following problem: "You are given two distinct words (strings...
46_C. Hamsters and Tigers_1614
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together...
n=int(input()) s=input() h=0 for i in s: if i=='H':h+=1 r=[] t=0 for i in range(0,n): if s[i]=='H': for b in range((i+1)%n,min(((i+1)%n+h-1),n)): if s[b]=='T': t+=1 if ((i+1)%n+(h-1))>n: for q in range(0,(((i+1)%n+(h-1))%n)): if s[q]=='T':...
{ "input": [ "3\nHTH\n", "9\nHTHTHTHHT\n", "13\nHTTTHHHTTTTHH\n", "35\nTTTTTTHTTHTTTTTHTTTTTTTTTTTHTHTTTTT\n", "178\nTHHHTHTTTHTTHTTHHHHHTTTHTTHHTHTTTHTHTTTTTHHHTHTHHHTHHHTTTTTTTTHHHHTTHHTHHHHTHTTTHHHHHHTHHTHTTHTHTTTTTTTTTHHTTHHTHTTHHTHHHHHTTHHTTHHTTHHHTTHHTTTTHTHHHTHTTHTHTTTHHHHTHHTHHHTHTTTTTT\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces...
492_D. Vanya and Computer Game_1618
Vanya and his friend Vova play a computer game where they need to destroy n monsters to pass a level. Vanya's character performs attack with frequency x hits per second and Vova's character performs attack with frequency y hits per second. Each character spends fixed time to raise a weapon and then he hits (the time to...
n, x, y = map(int, input().split()) for _ in range(n): a = int(input()) c1, c2 = ((a + 1) * x // (x + y)) / x, ((a + 1) * y // (x + y)) / y if c1 == c2: print('Both') elif c1 > c2: print('Vanya') else: print('Vova')
{ "input": [ "2 1 1\n1\n2\n", "4 3 2\n1\n2\n3\n4\n", "10 13 27\n3\n21\n23\n17\n15\n23\n16\n7\n24\n20\n", "5 5 6\n999999999\n999999998\n999999997\n999999996\n999999995\n", "10 10 1\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n", "11 22 33\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65\n", "5 999999 1000000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vanya and his friend Vova play a computer game where they need to destroy n monsters to pass a level. Vanya's character performs attack with frequency x hits per second and Vova's cha...
56_E. Domino Principle_1626
Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has ...
from typing import TypeVar, Generic, Callable, List import sys from array import array # noqa: F401 from bisect import bisect_left, bisect_right def input(): return sys.stdin.buffer.readline().decode('utf-8') T = TypeVar('T') class SegmentTree(Generic[T]): __slots__ = ["size", "tree", "identity", "op", "...
{ "input": [ "4\n16 5\n20 5\n10 10\n18 2\n", "4\n0 10\n1 5\n9 10\n15 10\n", "4\n-6 9\n9 5\n-7 4\n0 9\n", "6\n20 39\n38 13\n-25 48\n-22 38\n13 39\n90 54\n", "5\n-47 19\n-43 14\n83 27\n-67 46\n-58 98\n", "3\n6 7\n2 9\n-6 10\n", "10\n34 9\n-41 26\n-62 32\n1 10\n77 14\n92 7\n-45 35\n75 19\n-23...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from l...
591_C. Median Smoothing_1630
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practi...
def main(): n = int(input()) a = [int(i) for i in input().split()] flag = 0 now_begin = 0 kek = 0 ans = [[-1] * 2 for i in range(n)] for i in range(1, n - 1): if a[i] != a[i - 1] and a[i] != a[i + 1]: kek += 1 else: flag = max((kek + 1) // 2, flag) ...
{ "input": [ "5\n0 1 0 1 0\n", "4\n0 0 1 1\n", "4\n0 1 0 1\n", "3\n1 1 0\n", "3\n1 0 1\n", "3\n0 1 1\n", "3\n0 0 0\n", "168\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filte...
613_A. Peter and Snow Blower_1634
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it...
import math def dot(v1, v2): return v1[0]*v2[0] + v1[1]*v2[1] def cross(v1, v2): return v1[0]*v2[1] - v1[1]*v2[0] def mag(x,y): return math.sqrt(x**2 + y**2) def main(): PI = 3.141592653589793 mags = [] n, x0, y0 = map(int, input().split()) x, y = x0, y0 for i in range(n): ...
{ "input": [ "3 0 0\n0 1\n-1 2\n1 2\n", "4 1 -1\n0 0\n1 2\n2 0\n1 1\n", "10 -99 91\n-99 90\n-98 -12\n-72 -87\n7 -84\n86 -79\n96 -2\n100 36\n99 59\n27 83\n-14 93\n", "4 0 0\n1 -1\n1 3\n3 3\n3 -1\n", "5 -1 -1\n0 0\n8 5\n10 7\n7 5\n2 5\n", "5 -94 52\n-93 52\n-78 -56\n-54 -81\n56 -87\n97 85\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow b...
633_A. Ebony and Ivory_1638
Dante is engaged in a fight with "The Savior". Before he can fight it with his sword, he needs to break its shields. He has two guns, Ebony and Ivory, each of them is able to perform any non-negative number of shots. For every bullet that hits the shield, Ebony deals a units of damage while Ivory deals b units of dama...
a, b, c = map(int, input().split()) h = 0 if c % a == 0: k = c // a else: k = c // a + 1 if c % b == 0: m = c // b else: m = c // b + 1 if c - a*k < 0 and c - b*m < 0 and ((c < 2 * a) and c < 2*b): print('No') else: for i in range(k+1): if (c - a*i) % b == 0 and (h == 0): pri...
{ "input": [ "6 11 6\n", "3 2 7\n", "4 6 15\n", "15 12 26\n", "79 36 2854\n", "11 34 383\n", "16 78 712\n", "76 69 4122\n", "48 65 917\n", "57 52 2634\n", "12 17 15\n", "62 1 501\n", "70 24 1784\n", "21 71 657\n", "63 17 858\n", "1 1 10000\n", "2 17 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dante is engaged in a fight with "The Savior". Before he can fight it with his sword, he needs to break its shields. He has two guns, Ebony and Ivory, each of them is able to perform ...
774_D. Lie or Truth_1648
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an. While Vasya was walking, his little brother Stepan played with Vasya's cubes and chan...
n,l,r = map(int,input().split()) a = list(map(int,input().split())) b = list(map(int,input().split())) if(a[:l - 1] == b[:l - 1] and a[r:] == b[r:]): print("TRUTH") else: print("LIE")
{ "input": [ "4 2 4\n1 1 1 1\n1 1 1 1\n", "5 2 4\n3 4 2 3 1\n3 2 3 4 1\n", "3 1 2\n1 2 3\n3 1 2\n", "5 1 3\n2 2 2 1 2\n2 2 2 1 2\n", "7 2 4\n1 2 3 4 5 7 6\n1 2 3 4 5 6 7\n", "5 1 2\n1 2 3 4 5\n1 2 3 5 4\n", "10 1 10\n6 7 6 1 10 10 9 5 3 9\n7 10 9 6 1 5 9 3 10 6\n", "8 3 6\n5 3 1 1 1 1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order fro...
799_D. Field expansion_1652
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Ar...
def mp(): return map(int,input().split()) def lt(): return list(map(int,input().split())) def pt(x): print(x) def ip(): return input() def it(): return int(input()) def sl(x): return [t for t in x] def spl(x): return x.split() def aj(liste, item): liste.append(item) def bin(x): return "{0:b}".format(x) def listr...
{ "input": [ "5 5 1 2 3\n2 2 3\n", "3 3 3 3 5\n2 3 5 4 2\n", "3 3 2 4 4\n2 5 4 10\n", "3 4 1 1 3\n2 3 2\n", "662 859 2 3 17\n3 2 2 2 3 3 3 2 3 3 2 3 2 2 2 2 2\n", "21865 53623 9 7 116\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the ...
845_B. Luba And The Ticket_1658
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of las...
def main(): s = input() print(solver(s)) def solver(s): L1 = [int(x) for x in s[0:3]] L2 = [int(x) for x in s[3:6]] diff = sum(L2) - sum(L1) if diff == 0: return 0 elif diff < 0: L1, L2 = L2, L1 diff = -diff changes = [9 - L1[i] for i in range(3)] + [L2[i] - 0 for i in range(3)] changes.sort(reverse =...
{ "input": [ "111000\n", "000000\n", "123456\n", "191578\n", "877542\n", "977330\n", "000990\n", "100172\n", "202597\n", "944234\n", "000009\n", "033788\n", "000018\n", "123996\n", "444776\n", "123689\n", "317596\n", "011299\n", "858022\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she need...
865_D. Buy Low Sell High_1662
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you...
from heapq import * N = int(input()) price = [int(i) for i in input().split()] total = 0 inf = (10**6) + 1 h = [inf] #Assume we bought and sold optimally for the first k prices. #We adjust our answer for the (k+1)th price that comes up. for p in price: if p > h[0]: total += (p - heappop(h)) #We p...
{ "input": [ "9\n10 5 4 7 9 12 6 2 10\n", "20\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4\n", "100\n411 642 560 340 276 440 515 519 182 314 35 227 390 136 97 5 502 584 567 79 543 444 413 463 455 316 545 329 437 443 9 435 291 384 328 501 603 234 285 297 453 587 550 72 130 163 282 298 605 349 270 198 24 179 24...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, ...
913_G. Power Substring_1667
You are given n positive integers a1, a2, ..., an. For every ai you need to find a positive integer ki such that the decimal notation of 2ki contains the decimal notation of ai as a substring among its last min(100, length(2ki)) digits. Here length(m) is the length of the decimal notation of m. Note that you don't ha...
import sys #sys.stdout = open('output.txt', 'w') #sys.stdin = open('input.txt', 'r') #for line in sys.stdin: #while 1: # line = sys.stdin.readline() # if line: # print (line,end="") def phi(p,i): if i == 0: return 1 return p**i - p**(i-1) def pwr(x,p,mod): if p == 0: return 1 ...
{ "input": [ "2\n8\n2\n", "2\n3\n4857\n", "50\n415546\n6493493\n5136540\n6965643\n8466744\n3329383\n9711334\n9543203\n4478668\n375580\n1919986\n1906588\n2651062\n6256271\n1372732\n5775823\n7301593\n4894751\n189902\n5406338\n8281625\n3220812\n7228487\n965119\n6142296\n3854700\n685373\n9076805\n5770856\n969...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given n positive integers a1, a2, ..., an. For every ai you need to find a positive integer ki such that the decimal notation of 2ki contains the decimal notation of ai as a ...
936_A. Save Energy!_1671
Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on. During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The s...
from math import ceil k , d , t = [int(x) for x in input().split()] n = ceil(k/d)*d diff = n - k new_k = k+(diff/2) res = (t//new_k)*n if t%new_k <= k : res += t%new_k else : res += k+(t%new_k- k)*2 print(res)
{ "input": [ "4 2 20\n", "3 2 6\n", "8 10 9\n", "312 93 1000000000000000000\n", "42 9 1000000000000000000\n", "1000 1000 1000\n", "963276084 698548036 1000000000\n", "200 10 979220166595737684\n", "3260 4439 6837\n", "87 4 1000\n", "6 3 417701740543616353\n", "9 1000000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on. During cooking, J...
p02603 M-SOLUTIONS Programming Contest 2020 - Road to Millionaire_1689
To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives. He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the n...
n,*a=map(int,open(0).read().split()) m=1000 for i in range(1,n): if a[i]>a[i-1]: m=m//a[i-1]*a[i]+m%a[i-1] print(m)
{ "input": [ "2\n157 193", "7\n100 130 130 130 115 115 150", "6\n200 180 160 140 120 100", "2\n157 145", "7\n100 130 243 130 115 115 150", "6\n200 237 160 140 120 100", "2\n157 218", "7\n100 130 243 130 115 115 271", "6\n200 237 160 233 120 100", "2\n103 218", "7\n110 130 2...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country wher...
p02734 AtCoder Beginner Contest 159 - Knapsack for All Segments_1693
Given are a sequence of N integers A_1, A_2, \ldots, A_N and a positive integer S. For a pair of integers (L, R) such that 1\leq L \leq R \leq N, let us define f(L, R) as follows: * f(L, R) is the number of sequences of integers (x_1, x_2, \ldots , x_k) such that L \leq x_1 < x_2 < \cdots < x_k \leq R and A_{x_1}+A_{...
import sys input = sys.stdin.readline N,S=map(int,input().split()) A=list(map(int,input().split())) mod=998244353 DP=[0]*(S+1) # 多項式の考え方が分かりやすい. # 多項式を使わないなら, # DP[j] := 終端がiになる区間について, S=jのときのF(*,i)の和 # を更新していくと考えれば良いのかな... DP[0]=1 ANS=0 for a in A: for i in range(S-a,-1,-1): DP[i+a]+=DP[i] DP[0]+=1...
{ "input": [ "10 10\n3 1 4 1 5 9 2 6 5 3", "5 8\n9 9 9 9 9", "3 4\n2 2 4", "10 10\n3 1 4 1 5 9 2 6 2 3", "3 4\n2 2 6", "10 10\n3 1 4 1 5 9 1 6 2 3", "3 4\n2 2 2", "6 10\n3 1 4 1 5 9 1 6 2 3", "3 4\n1 2 1", "3 4\n1 3 1", "10 10\n3 1 4 1 5 9 2 6 4 3", "5 8\n14 9 9 9 9", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given are a sequence of N integers A_1, A_2, \ldots, A_N and a positive integer S. For a pair of integers (L, R) such that 1\leq L \leq R \leq N, let us define f(L, R) as follows: *...
p02868 NIKKEI Programming Contest 2019-2 - Shortest Path on a Line_1696
We have N points numbered 1 to N arranged in a line in this order. Takahashi decides to make an undirected graph, using these points as the vertices. In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph. The i-th operation is as follows: * The operation uses integers L_i ...
from heapq import heappush,heappop N,M=map(int,input().split()) LRC=[list(map(int,input().split())) for i in range(M)] data=[[],[]]+[[[i-1,0]] for i in range(2,N+1)] for L,R,C in LRC: data[L].append([R,C]) dist=[[0,1]] inf=float("inf") flag=[inf]*(N+1) while dist: y,x=heappop(dist) if flag[x]!=inf: ...
{ "input": [ "4 3\n1 3 2\n2 4 3\n1 4 6", "10 7\n1 5 18\n3 4 8\n1 3 5\n4 7 10\n5 9 8\n6 10 5\n8 10 3", "4 2\n1 2 1\n3 4 2", "10 7\n1 5 18\n3 4 8\n1 3 5\n4 7 10\n5 9 8\n6 10 5\n8 10 5", "4 2\n1 2 2\n3 4 2", "10 7\n1 5 18\n3 4 8\n1 3 5\n4 7 10\n5 9 8\n6 10 5\n5 10 5", "10 7\n1 5 18\n3 4 8\n1 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have N points numbered 1 to N arranged in a line in this order. Takahashi decides to make an undirected graph, using these points as the vertices. In the beginning, the graph has ...
p03003 AtCoder Beginner Contest 130 - Common Subsequence_1700
You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive). In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content? Here the subsequence of A is a sequence obtained by removing zero or ...
def main(): e=enumerate n,m,*u=map(int,open(0).read().split()) dp=[[1]*(m+1)for _ in range(n+1)] dpi=dp[0] for i,s in e(u[:n]): dpi1=dp[i+1] for j,t in e(u[n:]): dpi1[j+1]=(dpi[j+1]+dpi1[j]-dpi[j]*(s!=t))%(10**9+7) dpi=dpi1 print(dpi[m]) main()
{ "input": [ "10 9\n9 6 5 7 5 9 8 5 6 7\n8 6 8 5 5 7 9 9 7", "20 20\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "4 4\n3 4 5 6\n3 4 5 6", "2 2\n1 1\n1 1", "2 2\n1 3\n3 1", "10 9\n9 6 5 7 5 9 8 5 6 7\n8 5 8 5 5 7 9 9 7", "20 20\n1 1 1 1 1 1 1 0 1 1 1 1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive). In how many pairs of a subsequence of S and a...
p03143 NIKKEI Programming Contest 2019 - Weights on Vertices and Edges_1704
There is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are numbered 1 to M. Also, each of these vertices and edges has a specified weight. Vertex i has a weight of X_i; Edge i has a weight of Y_i and connects Vertex A_i and B_i. We would like to remove zero o...
import sys from collections import deque input = sys.stdin.readline N,M=map(int,input().split()) X=list(map(int,input().split())) EDGE=[list(map(int,input().split())) for i in range(M)] EDGE.sort(key=lambda x:x[2]) EDGELIST=[[] for i in range(N+1)] for i in range(M): x,y,w=EDGE[i] EDGELIST[x].append(i) ED...
{ "input": [ "10 9\n81 16 73 7 2 61 86 38 90 28\n6 8 725\n3 10 12\n1 4 558\n4 9 615\n5 6 942\n8 9 918\n2 7 720\n4 7 292\n7 10 414", "4 4\n2 3 5 7\n1 2 7\n1 3 9\n2 3 12\n3 4 18", "6 10\n4 4 1 1 1 7\n3 5 19\n2 5 20\n4 5 8\n1 6 16\n2 3 9\n3 6 16\n3 4 1\n2 6 20\n2 4 19\n1 2 9", "4 4\n2 3 5 7\n1 2 7\n1 3 9...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are numbered 1 to M. Also, each of these vertices and edges has a sp...
p03287 AtCoder Beginner Contest 105 - Candy Distribution_1708
There are N boxes arranged in a row from left to right. The i-th box from the left contains A_i candies. You will take out the candies from some consecutive boxes and distribute them evenly to M children. Such being the case, find the number of the pairs (l, r) that satisfy the following: * l and r are both integers...
n,m=list(map(int,input().split())) a=list(map(int,input().split())) d={0:1} s=0 for i in a: s=(s+i)%m if s in d: d[s]+=1 else: d[s]=1 def f(x): return int(x*(x-1)/2) s=0 for i in d: s+=f(d[i]) print(s)
{ "input": [ "10 400000000\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "13 17\n29 7 5 7 9 51 7 13 8 55 42 9 81", "3 2\n4 1 5", "10 400000000\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000010 100...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N boxes arranged in a row from left to right. The i-th box from the left contains A_i candies. You will take out the candies from some consecutive boxes and distribute them...
p03603 AtCoder Regular Contest 083 - Bichrome Tree_1713
We have a tree with N vertices. Vertex 1 is the root of the tree, and the parent of Vertex i (2 \leq i \leq N) is Vertex P_i. To each vertex in the tree, Snuke will allocate a color, either black or white, and a non-negative integer weight. Snuke has a favorite integer sequence, X_1, X_2, ..., X_N, so he wants to all...
n = int(input()) parents = list(map(int, input().split())) weights = list(map(int, input().split())) children = [[] for _ in range(n)] for i in range(n-1): children[parents[i]-1].append(i+1) def dfs(cur): if children[cur] == []: return (weights[cur], 0) sum = 0 dp = [[False] * (weights[cur] ...
{ "input": [ "3\n1 1\n4 3 2", "8\n1 1 1 3 4 5 5\n4 1 6 2 2 1 3 3", "1\n\n0", "3\n1 2\n1 2 3", "3\n1 1\n6 3 2", "8\n1 2 2 3 4 5 5\n4 1 6 2 2 1 3 3", "8\n1 1 1 3 4 5 5\n6 1 6 2 2 1 3 3", "1\n\n1", "3\n1 1\n6 6 2", "1\n\n2", "3\n1 1\n3 6 2", "1\n\n3", "3\n1 1\n3 6 4", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a tree with N vertices. Vertex 1 is the root of the tree, and the parent of Vertex i (2 \leq i \leq N) is Vertex P_i. To each vertex in the tree, Snuke will allocate a color,...
p03762 AtCoder Beginner Contest 058 - ###_1717
On a two-dimensional plane, there are m lines drawn parallel to the x axis, and n lines drawn parallel to the y axis. Among the lines parallel to the x axis, the i-th from the bottom is represented by y = y_i. Similarly, among the lines parallel to the y axis, the i-th from the left is represented by x = x_i. For ever...
N,M=map(int,input().split()) X=list(map(int,input().split())) Y=list(map(int,input().split())) mod=10**9+7 x=0 for i in range(N): x+=((i-(N-i-1))*X[i])%mod x%=mod y=0 for j in range(M): y+=((j-(M-j-1))*Y[j])%mod y%=mod print((x*y)%mod)
{ "input": [ "3 3\n1 3 4\n1 3 6", "6 5\n-790013317 -192321079 95834122 418379342 586260100 802780784\n-253230108 193944314 363756450 712662868 735867677", "6 5\n-983443749 -192321079 95834122 418379342 586260100 802780784\n-253230108 193944314 363756450 712662868 735867677", "6 5\n-983443749 -19232107...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: On a two-dimensional plane, there are m lines drawn parallel to the x axis, and n lines drawn parallel to the y axis. Among the lines parallel to the x axis, the i-th from the bottom ...
p03932 square869120Contest #3 - Souvenirs_1721
Input The input is given from standard input in the following format. > $H \ W$ $a_{1, 1} \ a_{1, 2} \ \cdots \ a_{1, W}$ $a_{2, 1} \ a_{2, 2} \ \cdots \ a_{2, W}$ $\vdots \ \ \ \ \ \ \ \ \ \ \vdots \ \ \ \ \ \ \ \ \ \ \vdots$ $a_{H, 1} \ a_{H, 2} \ \cdots \ a_{H, W}$ Output * Print the maximum number of souvenirs...
H,W = map(int,input().split()) src = [list(map(int,input().split())) for i in range(H)] dp = [[[0 for ex in range(W)] for sx in range(W)] for xy in range(H+W-1)] dp[0][0][0] = src[0][0] for xy in range(H+W-2): n = min(xy+1,H,W,H+W-xy-1) sx0 = max(0,xy-H+1) for sx in range(sx0, sx0+n): for ex in ran...
{ "input": [ "6 6\n1 2 3 4 5 6\n8 6 9 1 2 0\n3 1 4 1 5 9\n2 6 5 3 5 8\n1 4 1 4 2 1\n2 7 1 8 2 8", "3 3\n1 0 5\n2 2 3\n4 2 4", "6 6\n1 2 3 4 5 6\n8 6 9 1 2 0\n3 1 4 1 5 9\n2 6 5 3 5 8\n1 4 1 4 2 1\n2 10 1 8 2 8", "3 3\n1 0 5\n1 2 3\n4 2 4", "6 6\n1 2 3 4 5 6\n8 6 9 1 2 0\n1 1 4 1 5 9\n2 6 5 3 5 8\n...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Input The input is given from standard input in the following format. > $H \ W$ $a_{1, 1} \ a_{1, 2} \ \cdots \ a_{1, W}$ $a_{2, 1} \ a_{2, 2} \ \cdots \ a_{2, W}$ $\vdots \ \ \ \ ...
p00025 Hit and Blow_1725
Let's play Hit and Blow game. A imagines four numbers and B guesses the numbers. After B picks out four numbers, A answers: * The number of numbers which have the same place with numbers A imagined (Hit) * The number of numbers included (but different place) in the numbers A imagined (Blow) For example, if A imagin...
import sys e=iter(map(lambda a:a.split(),sys.stdin)) for a,b in zip(e,e): h=sum([1 for i in range(4)if a[i]==b[i]]) w=4-len(set(a)-set(b))-h print(h,w)
{ "input": [ "9 1 8 2\n4 1 5 9\n4 6 8 2\n4 6 3 2", "9 1 8 2\n4 1 5 9\n4 6 8 2\n4 6 3 0", "9 1 8 2\n6 1 5 9\n4 6 8 2\n2 6 3 0", "9 1 8 2\n4 1 5 9\n2 6 8 2\n4 6 3 0", "9 1 8 2\n6 2 5 9\n4 6 8 2\n4 6 3 0", "9 1 8 2\n6 2 5 9\n4 6 8 2\n4 9 3 0", "9 1 8 2\n6 1 2 9\n4 6 13 2\n2 6 3 1", "9 1 8...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's play Hit and Blow game. A imagines four numbers and B guesses the numbers. After B picks out four numbers, A answers: * The number of numbers which have the same place with num...
p00156 Moats around the Castle_1729
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible. Create a program that takes a sketch of the castle as input and ou...
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0156 """ import sys from sys import stdin from collections import deque input = stdin.readline def bfs(field, sx, sy): """ ??????????????°???????????°??????????????§?????????????????? :param field: :param sx: ???????...
{ "input": [ "5 5\n.###.\n#...#\n#.&.#\n#...#\n.###.\n18 15\n..####....####....\n####..####....####\n#...............##\n.#.############.##\n#..#..........#.##\n.#.#.########.#.##\n#..#.#......#.#.##\n.#.#....&...#.#.##\n#..#........#.#.##\n.#.#.########.#.##\n#..#..........#.##\n.#.############.##\n#...............
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up f...
p00313 Secret Investigation_1733
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions: * Thos...
N = int(input()) U = set([i+1 for i in range(N)]) A = set([int(x) for x in input().split()[1:]]) B = set([int(x) for x in input().split()[1:]]) C = set([int(x) for x in input().split()[1:]]) result = ((U-A) & C) | (B & C) print(len(result))
{ "input": [ "5\n3 1 2 3\n2 4 5\n2 3 4", "100\n3 1 100 4\n0\n2 2 3", "5\n3 1 2 3\n2 4 5\n2 2 4", "5\n3 1 2 1\n2 4 5\n2 3 4", "5\n3 1 2 3\n2 4 5\n2 3 2", "5\n3 1 4 3\n2 4 5\n2 3 4", "100\n3 1 100 4\n0\n2 4 3", "5\n3 2 4 3\n2 4 5\n2 3 4", "100\n3 0 100 4\n0\n2 4 3", "100\n3 0 000...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy I...
p00483 Planetary Exploration_1737
After a long journey, the super-space-time immigrant ship carrying you finally discovered a planet that seems to be habitable. The planet, named JOI, is a harsh planet with three types of terrain, "Jungle," "Ocean," and "Ice," as the name implies. A simple survey created a map of the area around the planned residence. ...
from itertools import accumulate from operator import add line, row = [int(x) for x in input().split()] k = int(input()) L=[] J=[[0]*(row+1) for _ in range(line+1)] O=[[0]*(row+1) for _ in range(line+1)] I=[[0]*(row+1) for _ in range(line+1)] for _ in range(line): L.append(input()) for i in range(line): for j...
{ "input": [ "4 7\n4\nJIOJOIJ\nIOJOIJO\nJOIJOOI\nOOJJIJO\n3 5 4 7\n2 2 3 6\n2 2 2 2\n1 1 4 7", "4 7\n4\nJIOJOIJ\nIOJOIJO\nJOIJOOI\nOOJJIJO\n5 5 4 7\n2 2 3 6\n2 2 2 2\n1 1 4 7", "4 7\n4\nJIOJOIJ\nOJIOJOI\nJOIJOOI\nOOJJIJO\n5 5 4 7\n2 2 3 6\n2 2 2 2\n1 1 4 7", "4 7\n4\nJIOJOIJ\nOJIOJOI\nJOIJOOI\nOOJJIJO...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After a long journey, the super-space-time immigrant ship carrying you finally discovered a planet that seems to be habitable. The planet, named JOI, is a harsh planet with three type...
p00669 K Cards_1741
One day, the teacher came up with the following game. The game uses n cards with one number from 1 to 10 and proceeds as follows. 1. The teacher pastes n cards on the blackboard in a horizontal row so that the numbers can be seen, and declares an integer k (k ≥ 1) to the students. For n cards arranged in a horizontal...
from functools import reduce from collections import Counter while True: n, k = map(int, input().split()) if n == 0: break clst = [int(input()) for _ in range(n)] acc = reduce(lambda x, y:x* y, clst[:k]) scores = [acc] for i in range(n - k): acc = acc // clst[i] * clst[i + k] scores.append(acc) ...
{ "input": [ "4 2\n2\n3\n7\n5\n0 0", "4 2\n4\n3\n7\n5\n0 0", "4 2\n1\n6\n4\n5\n0 0", "4 2\n8\n3\n10\n5\n0 0", "4 2\n4\n3\n7\n1\n0 0", "4 2\n4\n1\n14\n3\n0 0", "4 2\n8\n3\n16\n5\n0 0", "4 2\n4\n3\n11\n1\n0 0", "4 2\n9\n5\n16\n5\n0 0", "4 2\n2\n2\n1\n5\n0 0", "4 2\n1\n6\n2\n5...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One day, the teacher came up with the following game. The game uses n cards with one number from 1 to 10 and proceeds as follows. 1. The teacher pastes n cards on the blackboard in ...
p00812 Equals are Equals_1744
Mr. Simpson got up with a slight feeling of tiredness. It was the start of another day of hard work. A bunch of papers were waiting for his inspection on his desk in his office. The papers contained his students' answers to questions in his Math class, but the answers looked as if they were just stains of ink. His hea...
import sys readline = sys.stdin.readline write = sys.stdout.write from string import digits def convert(S): S = S + "$" cur = 0 def expr(): nonlocal cur res = [] op = '+' while 1: r = fact() if op == '-': for e in r: ...
{ "input": [ "a+b+c\n(a+b)+c\na- (b-c)+2\n.\n4ab\n(a - b) (0-b+a) - 1a ^ 2 - b ^ 2\n2 b 2 a\n.\n108 a\n2 2 3 3 3 a\n4 a^1 27\n.\n.", "a+b+c\n(a+b)+c\na- (b-c)+2\n.\n4ab\n(a - b) (0-b+a) - 1a ^ 2 - b ^ 2\n2 b 2 a\n.\n169 a\n2 2 3 3 3 a\n4 a^1 27\n.\n.", "a+b+c\n(a+b)+c\na- (b-c)+2\n.\n4ab\n(a - b) (0-b+a) ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mr. Simpson got up with a slight feeling of tiredness. It was the start of another day of hard work. A bunch of papers were waiting for his inspection on his desk in his office. The p...
p01076 Graph Making_1748
Problem There are n vertices that are not connected to any of the vertices. An undirected side is stretched between each vertex. Find out how many sides can be stretched when the diameter is set to d. The diameter represents the largest of the shortest distances between two vertices. Here, the shortest distance is the...
# AOJ 1591 Graph Making # Python3 2018.7.13 bal4u n, d = map(int, input().split()) if d == 1: print(n*(n-1)//2) else: print((n-1)+(n-d-1)*n-((n-d-1)*(n+d-2)//2))
{ "input": [ "4 2", "5 1", "4 3", "0 1", "4 5", "6 5", "0 2", "4 1", "-1 1", "6 9", "0 8", "8 1", "6 3", "0 9", "1 8", "11 1", "8 3", "0 12", "1 10", "19 1", "8 2", "0 6", "2 8", "9 1", "0 11", "-1 11", "1 11",...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem There are n vertices that are not connected to any of the vertices. An undirected side is stretched between each vertex. Find out how many sides can be stretched when the dia...
p01684 Venn Diagram_1756
Problem Statement Alice is a private teacher. One of her job is to prepare the learning materials for her student. Now, as part of the materials, she is drawing a Venn diagram between two sets $A$ and $B$. Venn diagram is a diagram which illustrates the relationships among one or more sets. For example, a Venn diagra...
import sys readline = sys.stdin.readline write = sys.stdout.write from math import pi, sqrt, acos, sin def solve(): EPS = 1e-9 W, H, A, B, C = map(int, readline().split()) if W == 0: return False a = sqrt(A / pi); b = sqrt(B / pi) mx = max(a, b) if 2*mx > min(W, H) - EPS: write...
{ "input": [ "10 5 1 1 0\n10 5 2 2 1\n10 10 70 70 20\n0 0 0 0 0", "10 5 1 1 0\n10 5 2 2 1\n10 10 71 70 20\n0 0 0 0 0", "10 5 1 1 0\n10 8 2 2 1\n10 10 71 70 20\n0 0 0 0 0", "10 5 1 1 1\n10 8 2 2 1\n10 10 71 70 20\n0 0 0 0 0", "10 5 1 0 0\n10 5 2 2 1\n10 10 71 70 20\n0 0 0 0 0", "17 5 1 1 0\n10 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem Statement Alice is a private teacher. One of her job is to prepare the learning materials for her student. Now, as part of the materials, she is drawing a Venn diagram betwee...
p01828 M and A_1759
Example Input acmicpc tsukuba Output No
def f(s,t): j=0;i=0 while i<len(t) and j<len(s): if t[i]==s[j]:j+=2 i+=1 return j>=len(s) I=input s=I() t=I() print(['No','Yes'][f(s,t) or f(s[1:],t)])
{ "input": [ "acmicpc\ntsukuba", "acmicpc\nttukuba", "acmpcic\nttukuba", "acmpcid\nttukuba", "acmpcid\nttakubu", "acmqcid\nttakubu", "aciqcmd\nttakubu", "aciqcmd\ntt`kubu", "aciqcmd\ntu`ktbu", "dmcqica\ntu`ktbu", "dmcqica\ntu`jtbu", "dmcqica\nut`jtbu", "dmcqica\ntt`...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Example Input acmicpc tsukuba Output No ### Input: acmicpc tsukuba ### Output: No ### Input: acmicpc ttukuba ### Output: No ### Code: def f(s,t): j=0;i=0 while i<len(...
p01963 Separate String_1762
You are given a string $t$ and a set $S$ of $N$ different strings. You need to separate $t$ such that each part is included in $S$. For example, the following 4 separation methods satisfy the condition when $t = abab$ and $S = \\{a, ab, b\\}$. * $a,b,a,b$ * $a,b,ab$ * $ab,a,b$ * $ab,ab$ Your task is to count the n...
from collections import defaultdict import sys def solve(): readline = sys.stdin.readline write = sys.stdout.write mod = 10**9 + 9 base = 37 ca = ord('a') N = int(readline()) SS = [readline().strip() for i in range(N)] SS.sort(key = len) T = readline().strip() L = len(T) L...
{ "input": [ "3\na\nb\nab\nabab", "3\na\nb\nc\nxyz", "10\na\naa\naaa\naaaa\naaaaa\naaaaaa\naaaaaaa\naaaaaaaa\naaaaaaaaa\naaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "7\nabc\nab\nbc\na\nb\nc\naa\naaabcbccababbc", "3\na\nb\nba\nabab", "3\na\nb\nc\nxy{", "10\na\naa\naaa\naaaa\naaaa...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a string $t$ and a set $S$ of $N$ different strings. You need to separate $t$ such that each part is included in $S$. For example, the following 4 separation methods sa...
p02250 Multiple String Matching_1765
Determine whether a text T includes a pattern P. Your program should answer for given queries consisting of P_i. Constraints * 1 ≤ length of T ≤ 1000000 * 1 ≤ length of P_i ≤ 1000 * 1 ≤ Q ≤ 10000 * The input consists of alphabetical characters and digits Input In the first line, a text T is given. In the second lin...
base = 127 mask = (1 << 32) - 1 def calc_hash(f, pl, tl): dl = tl - pl tmp = set() t = 1 for _ in range(pl): t = (t * base) & mask e = 0 for i in range(pl): e = (e * base + f[i]) & mask for i in range(dl): tmp.add(e) e = (e * base - t * f[i] + f[i + pl]) & mas...
{ "input": [ "aabaaa\n4\naa\nba\nbb\nxyz", "aabaaa\n4\nab\nba\nbb\nxyz", "aabaaa\n4\nab\nba\nba\nxyz", "aabaaa\n4\nba\nbb\nac\nzyx", "aabaaa\n4\nca\nab\nba\nzyx", "aacaaa\n4\nba\nb`\nac\nzyx", "aacaaa\n4\nca\nb`\nac\nzyx", "aabaaa\n4\na`\nab\nca\nyzx", "aadaaa\n4\nca\nb`\nca\nzyx",...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Determine whether a text T includes a pattern P. Your program should answer for given queries consisting of P_i. Constraints * 1 ≤ length of T ≤ 1000000 * 1 ≤ length of P_i ≤ 1000 *...
p02398 How Many Divisors?_1769
How Many Divisors? Write a program which reads three integers a, b and c, and prints the number of divisors of c between a and b. Constraints * 1 ≤ a, b, c ≤ 10000 * a ≤ b Input Three integers a, b and c are given in a line separated by a single space. Output Print the number of divisors in a line. Example Inp...
a, b, c = map(int, input().split()) ans = 0 for i in range(a, b + 1): if c % i == 0: ans += 1 print(ans)
{ "input": [ "5 14 80", "7 14 80", "7 4 80", "1 1 80", "9 22 80", "5 20 80", "7 25 80", "1 23 80", "2 23 80", "4 32 80", "4 32 120", "6 18 0", "4 17 0", "3 26 0", "4 30 0", "2 26 0", "7 6 80", "7 1 80", "5 14 149", "13 14 80", "9 4 80...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: How Many Divisors? Write a program which reads three integers a, b and c, and prints the number of divisors of c between a and b. Constraints * 1 ≤ a, b, c ≤ 10000 * a ≤ b Input ...
1016_C. Vasya And The Mushrooms_1779
Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends...
n = int(input()) u1 = list(map(int, input().split())) u2 = list(map(int, input().split())) a1 = u1[:n] a2 = u2[:n] for i in range(1, n): a1[i] += a1[i - 1] a2[i] += a2[i - 1] q1 = [0] * (2 * n) q2 = [0] * (2 * n) for i in range(1, n): q1[i] = u1[i] * (i) + q1[i - 1] q2[i] = u2[i] * (i) + q2[i - 1] for i...
{ "input": [ "3\n1 2 3\n6 5 4\n", "3\n1 1000 10000\n10 100 100000\n", "6\n12 8 12 17 20 5\n17 4 8 8 8 4\n", "6\n4 1 12 9 3 11\n12 20 19 12 19 11\n", "1\n1\n1\n", "2\n1 2\n1 1\n", "1\n7\n5\n", "6\n16 20 18 7 14 2\n17 12 4 10 7 4\n", "6\n20 8 12 17 20 5\n17 4 8 8 8 4\n", "6\n4 1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasy...
103_C. Russian Roulette_1783
After all the events in Orlando we all know, Sasha and Roma decided to find out who is still the team's biggest loser. Thankfully, Masha found somewhere a revolver with a rotating cylinder of n bullet slots able to contain exactly k bullets, now the boys have a chance to resolve the problem once and for all. Sasha se...
#!/usr/bin/env python3 n, k, p = map(int, input().strip().split()) if k == 0: ak = 0 an = n else: ak = k - 1 if n % 2 == 1 else k an = n - (n % 2) ans = '' for i in range(p): v = int(input().rstrip()) if k == 0: print('.', end='') else: if v == n: print('X', en...
{ "input": [ "5 2 5\n1\n2\n3\n4\n5\n", "6 3 6\n1\n2\n3\n4\n5\n6\n", "3 1 3\n1\n2\n3\n", "7 7 7\n1\n2\n3\n4\n5\n6\n7\n", "4 2 8\n1\n3\n4\n2\n3\n4\n1\n2\n", "9 4 9\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", "7 5 7\n1\n2\n3\n4\n5\n6\n7\n", "15 10 15\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After all the events in Orlando we all know, Sasha and Roma decided to find out who is still the team's biggest loser. Thankfully, Masha found somewhere a revolver with a rotating cyl...
1062_D. Fun with Integers_1787
You are given a positive integer n greater or equal to 2. For every pair of integers a and b (2 ≤ |a|, |b| ≤ n), you can transform a into b if and only if there exists an integer x such that 1 < |x| and (a ⋅ x = b or b ⋅ x = a), where |x| denotes the absolute value of x. After such a transformation, your score increas...
i = input() i = int(i) v = 0 g = 2 s = 4 while g <= i: while s <= i: v = v + int(s / g * 4) s = s + g g = g + 1 s = g * 2 print(str(v))
{ "input": [ "2\n", "4\n", "6\n", "28237\n", "13190\n", "39914\n", "81367\n", "8554\n", "61106\n", "67448\n", "39004\n", "22308\n", "16054\n", "71468\n", "60501\n", "100\n", "300\n", "59632\n", "90783\n", "5611\n", "1000\n", "6947...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a positive integer n greater or equal to 2. For every pair of integers a and b (2 ≤ |a|, |b| ≤ n), you can transform a into b if and only if there exists an integer x su...
1084_C. The Fair Nut and String_1791
The Fair Nut found a string s. The string consists of lowercase Latin letters. The Nut is a curious guy, so he wants to find the number of strictly increasing sequences p_1, p_2, …, p_k, such that: 1. For each i (1 ≤ i ≤ k), s_{p_i} = 'a'. 2. For each i (1 ≤ i < k), there is such j that p_i < j < p_{i + 1} and s...
s=input() sss='' for i in s: if i in ['a','b']: sss+=i from itertools import groupby xxx=[''.join(g) for _, g in groupby(sss)] xxx=[len(i)+1 for i in xxx if 'a' in i] ans=1 if len(xxx)==1: print((xxx[0]-1)%1000000007) else: for i in xxx: ans*=i print((ans-1)%1000000007)
{ "input": [ "agaa\n", "baaaa\n", "abbaa\n", "aaaaabb\n", "aabaaaa\n", "babbabaabbbbabwbvbhbbbaaabbabbbbbbbkabbbbabaaabbbbbbajabbaaabazbbbzbalbabbwbbaabbabacabdbabbbfaaabbb\n", "baaahbbbba\n", "ababavvvv\n", "abnxabbaab\n", "krflnzhter\n", "amaabbbbxaabbbabaabaabbaaabaaaaab...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Fair Nut found a string s. The string consists of lowercase Latin letters. The Nut is a curious guy, so he wants to find the number of strictly increasing sequences p_1, p_2, …, p...
1131_E. String Multiplication_1797
Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza's recipe, while Denis invented a string multiplication. According to Denis, the result of multiplication (product) of strings s of length m an...
ALPH = 'abcdefghijklmnopqrstuvwxyz' MAX = 10 ** 9 def cnt(s): c = {ch : 0 for ch in ALPH} i = 0 while i < len(s): j = i + 1 while j < len(s) and s[i] == s[j]: j += 1 c[s[i]] = max(c[s[i]], j - i) i = j return c def nxt(c, t): nc = cnt(t) for ch in AL...
{ "input": [ "2\nbnn\na\n", "3\na\nb\na\n", "9\nlfpgbnlzyn\nc\ns\nw\nr\nm\nq\ny\nyinfblfcdhidphyfvgkxyuwomahiibbhnigdslsguhjkplibnhhrshtekwgefxeugyyyyy\n", "6\nu\np\nm\nz\nv\nvv\n", "4\nqe\nnd\niqryhukieskfvaeettersinksrmazelxtgvartuz\nvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza's rec...
1152_A. Neko Finds Grapes_1801
On a random day, Neko found n treasure chests and m keys. The i-th chest has an integer a_i written on it and the j-th key has an integer b_j on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible. The j-th key can be used to unlock the ...
n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) aeven = 0 aodd = 0 beven = 0 bodd = 0 for i in range(n): if a[i] % 2 == 0: aeven += 1 else: aodd += 1 for i in range(m): if b[i] % 2 == 0: beven += 1 else: bodd += 1 ...
{ "input": [ "5 1\n2 4 6 8 10\n5\n", "1 4\n10\n20 30 40 50\n", "5 4\n9 14 6 2 11\n8 4 7 20\n", "4 1\n3 5 7 8\n2\n", "5 1\n2 2 2 3 3\n3\n", "4 1\n1 1 2 2\n2\n", "10 10\n522312461 931001459 598654597 488228616 544064902 21923894 329635457 980089248 988262691 654502493\n967529230 543358150 83...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: On a random day, Neko found n treasure chests and m keys. The i-th chest has an integer a_i written on it and the j-th key has an integer b_j on it. Neko knows those chests contain th...
1191_C. Tokitsukaze and Discard Items_1807
Recently, Tokitsukaze found an interesting game. Tokitsukaze had n items at the beginning of this game. However, she thought there were too many items, so now she wants to discard m (1 ≤ m ≤ n) special items of them. These n items are marked with indices from 1 to n. In the beginning, the item with index i is placed o...
from collections import deque n,m,k=list(map(int,input().split())) arr=list(map(int,input().split())) d=deque() for i in arr: d.append(i) chances=curr=tot=0 # print("WORKING") while tot<m: # print("S") if (d[0]-curr)%k==0: p=(d[0]-curr)//k else:p=((d[0]-curr)//k)+1 temp=curr while tot<m...
{ "input": [ "13 4 5\n7 8 9 10\n", "10 4 5\n3 5 7 10\n", "10 5 5\n2 3 4 5 6\n", "1 1 1\n1\n", "1000000000000000000 10 68\n9818112234872670 233245257111863071 266483628625876079 375103213040049805 431982380866113633 489732004669478331 501030096787260719 541800997319766183 551892546260532408 6961786...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently, Tokitsukaze found an interesting game. Tokitsukaze had n items at the beginning of this game. However, she thought there were too many items, so now she wants to discard m (...
1230_A. Dawid and Bags of Candies_1813
Dawid has four bags of candies. The i-th of them contains a_i candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total? Note, that you can't keep bags for yourself or thro...
list1 = list(map(int, input().split())) list1.sort() if list1[0] + list1[3] == list1[1] + list1[2]: print("YES") elif list1[0] + list1[2] +list1[1] == list1[3]: print("YES") else: print("NO")
{ "input": [ "1 7 11 5\n", "7 3 2 5\n", "44 58 90 53\n", "20 14 37 71\n", "4 2 4 2\n", "1 2 5 5\n", "1 2 8 9\n", "99 100 3 98\n", "92 69 83 97\n", "3 1 1 1\n", "18 88 18 18\n", "1 1 1 1\n", "69 7 44 30\n", "90 30 30 30\n", "1 2 3 5\n", "1 1 3 1\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dawid has four bags of candies. The i-th of them contains a_i candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute t...
1251_E1. Voting (Easy Version)_1817
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins....
import sys from heapq import * #sys.stdin = open('in', 'r') t = int(input()) for ti in range(t): n = int(input()) a = [] for i in range(n): mi, pi = map(int, input().split()) a.append((mi, -pi)) a.sort() c = 0 h = [] res = 0 for i in reversed(range(n)): heappush(h...
{ "input": [ "3\n3\n1 5\n2 10\n2 8\n7\n0 1\n3 1\n1 1\n6 1\n1 1\n4 1\n4 1\n6\n2 6\n2 3\n2 8\n2 7\n4 4\n5 5\n", "3\n3\n1 5\n2 10\n2 4\n7\n0 1\n3 1\n1 1\n6 1\n1 1\n4 1\n4 1\n6\n2 6\n2 3\n2 8\n2 7\n4 4\n5 5\n", "3\n3\n1 5\n2 10\n2 8\n7\n0 1\n1 1\n1 1\n6 1\n1 1\n4 1\n4 1\n6\n2 6\n2 3\n2 8\n2 7\n4 4\n5 5\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. The...
1271_C. Shawarma Tent_1821
The map of the capital of Berland can be viewed on the infinite coordinate plane. Each point with integer coordinates contains a building, and there are streets connecting every building to four neighbouring buildings. All streets are parallel to the coordinate axes. The main school of the capital is located in (s_x, ...
n,x,y=map(int,input().split()) lu=0 ld=0 ru=0 rd=0 u=0 d=0 l=0 r=0 for i in range(n): a,b=map(int,input().split()) if(a<x and b<y): ld+=1 elif(a<x and b>y): lu+=1 elif(a==x and b!=y): if(b>y): u+=1 else: d+=1 elif(a>x and b<y): rd+=1 ...
{ "input": [ "7 10 12\n5 6\n20 23\n15 4\n16 5\n4 54\n12 1\n4 15\n", "4 3 2\n1 3\n4 2\n5 1\n4 1\n", "3 100 100\n0 0\n0 0\n100 200\n", "1 1000000000 7\n1000000000 9\n", "3 2 2\n2 4\n2 1\n3 6\n", "2 1000000000 100000\n1000000000 1000000\n1000000000 10000\n", "1 100 100\n50 70\n", "1 100 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The map of the capital of Berland can be viewed on the infinite coordinate plane. Each point with integer coordinates contains a building, and there are streets connecting every build...
1294_D. MEX maximizing_1825
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: * for the array [0, 0, 1, 0, 2] MEX equals to 3 because numbers 0, 1 and 2 are presented in the array and 3 is the minimum non-negative integer not presented in the array; * for the array [1, 2, 3, 4] MEX eq...
import os from io import BytesIO, IOBase import sys from collections import defaultdict, deque, Counter from bisect import * from math import sqrt, pi, ceil, log, inf,gcd from itertools import permutations from copy import deepcopy from heapq import * from sys import setrecursionlimit def main(): q, x = map(int, in...
{ "input": [ "4 3\n1\n2\n1\n2\n", "7 3\n0\n1\n2\n2\n0\n0\n10\n", "4 3\n1\n2\n1\n3\n", "3 3\n0\n1\n2\n2\n0\n0\n10\n", "4 2\n1\n2\n1\n0\n", "4 2\n1\n3\n1\n0\n", "4 2\n1\n3\n2\n0\n", "4 2\n0\n3\n2\n0\n", "3 2\n0\n6\n2\n0\n", "2 3\n0\n0\n3\n-1\n", "3 3\n0\n0\n1\n-2\n", "3 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: * for the array [0, 0, 1, 0, 2] MEX equals to 3 because numbers 0, 1 and ...
1315_D. Recommendations_1828
VK news recommendation system daily selects interesting publications of one of n disjoint categories for each user. Each publication belongs to exactly one category. For each category i batch algorithm selects a_i publications. The latest A/B test suggests that users are reading recommended publications more actively ...
# Код программы написал на языке Python 3 import sys from heapq import heappush, heappop def main(): n = int(sys.stdin.readline()) h = sorted(list(zip([int(i) for i in sys.stdin.readline().split()], [int(i) for i in sys.stdin.readline().split()]))) z, w, o, res = [], 0, 0, 0 while o < n: ...
{ "input": [ "5\n3 7 9 7 8\n5 2 5 7 5\n", "5\n1 2 3 4 5\n1 1 1 1 1\n", "5\n1000000000 1000000000 1000000000 1000000000 1000000000\n100000 100000 100000 100000 100000\n", "10\n14 31 50 67 42 44 86 82 35 63\n76 17 26 44 60 54 50 73 71 70\n", "10\n4 4 1 2 2 2 5 4 1 5\n28 81 36 44 2 40 39 43 36 33\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: VK news recommendation system daily selects interesting publications of one of n disjoint categories for each user. Each publication belongs to exactly one category. For each category...
1359_D. Yet Another Yet Another Task_1833
Alice and Bob are playing yet another card game. This time the rules are the following. There are n cards lying in a row in front of them. The i-th card has value a_i. First, Alice chooses a non-empty consecutive segment of cards [l; r] (l ≤ r). After that Bob removes a single card j from that segment (l ≤ j ≤ r). Th...
import math import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) ans = 0 for i in range(31): s = 0 for j in a: if j <= i: s = max(0, s + j) else: s = 0 ans = max(ans, s - i) print(ans)
{ "input": [ "3\n-10 6 -15\n", "8\n5 2 5 3 -30 -30 6 9\n", "5\n5 -2 10 -1 4\n", "10\n-3 4 -4 1 1 -4 -5 3 -4 -2\n", "10\n8 9 -10 -10 -4 -3 5 9 -9 9\n", "6\n5 -4 2 -1 2 -2\n", "11\n3 0 1 -2 5 -5 -1 0 3 2 2\n", "10\n4 -2 -2 -2 2 0 1 0 -4 -1\n", "4\n2 2 -3 4\n", "11\n-11 -7 -2 -24 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice and Bob are playing yet another card game. This time the rules are the following. There are n cards lying in a row in front of them. The i-th card has value a_i. First, Alice ...
139_D. Digits Permutations_1839
Andrey's favourite number is n. Andrey's friends gave him two identical numbers n as a New Year present. He hung them on a wall and watched them adoringly. Then Andrey got bored from looking at the same number and he started to swap digits first in one, then in the other number, then again in the first number and so o...
import itertools def countZeroes(s): ret = 0 for i in s: if i != '0': break ret += 1 return ret def stupid(n): ansMax = 0 bn1 = n bn2 = n for n1 in itertools.permutations(n): for n2 in itertools.permutations(n): val = str(int(''.join(n1)) + i...
{ "input": [ "500\n", "198\n", "545\n", "2603340571199714716025114079373828413509944752618962350632892540710372640383149198328312562980217104434880337288055817064\n", "670042\n", "1099\n", "8679647744\n", "5057017252180797906185\n", "404\n", "4877\n", "7665711\n", "3467...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Andrey's favourite number is n. Andrey's friends gave him two identical numbers n as a New Year present. He hung them on a wall and watched them adoringly. Then Andrey got bored from...
1443_A. Kids Seating_1845
Today the kindergarten has a new group of n kids who need to be seated at the dinner table. The chairs at the table are numbered from 1 to 4n. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers a and b (a ≠ b) will indulge if: 1. gcd(a, b) = 1 or, 2. a divides b or b d...
# cook your dish here for _ in range(int(input())): n=int(input()) s=4*n-2 for i in range(n): print(s,end=" ") s-=2 print()
{ "input": [ "3\n2\n3\n4\n", "3\n2\n3\n4\n", "100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Today the kindergarten has a new group of n kids who need to be seated at the dinner table. The chairs at the table are numbered from 1 to 4n. Two kids can't sit on the same chair. It...
1468_J. Road Reform_1849
There are n cities and m bidirectional roads in Berland. The i-th road connects the cities x_i and y_i, and has the speed limit s_i. The road network allows everyone to get from any city to any other city. The Berland Transport Ministry is planning a road reform. First of all, maintaining all m roads is too costly, ...
from collections import defaultdict from math import ceil, sqrt, floor from heapq import heappush,heappop import sys inf = float("inf") class DisjointSet: def __init__(self, n): self.n = n self.par = [-1] * n self.nc = n def findParent(self, i): if self.par[i] == -1: return i #no parent yet else: v...
{ "input": [ "4\n4 5 7\n4 1 3\n1 2 5\n2 3 8\n2 4 1\n3 4 4\n4 6 5\n1 2 1\n1 3 1\n1 4 2\n2 4 1\n4 3 1\n3 2 1\n3 2 10\n1 2 8\n1 3 10\n5 5 15\n1 2 17\n3 1 15\n2 3 10\n1 4 14\n2 5 8\n", "4\n4 5 7\n4 2 3\n1 2 5\n2 3 8\n2 4 1\n3 4 4\n4 6 5\n1 2 1\n1 3 1\n1 4 2\n2 4 1\n4 3 1\n3 2 1\n3 2 10\n1 2 8\n1 3 10\n5 5 15\n1 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n cities and m bidirectional roads in Berland. The i-th road connects the cities x_i and y_i, and has the speed limit s_i. The road network allows everyone to get from any c...
1493_C. K-beautiful Strings_1853
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is...
#!/usr/bin/python import sys from sys import stdin def solve(N, K, S): if N % K != 0: print(-1) return cnt_dict = {} for s in S: cnt_dict[s] = 0 for s in S: cnt_dict[s] += 1 bea = True for k in cnt_dict: if cnt_dict[k] % K != 0: bea = Fal...
{ "input": [ "4\n4 2\nabcd\n3 1\nabc\n4 3\naaaa\n9 3\nabaabaaaa\n", "12\n12 3\nzazasqzqsasq\n1 1\nz\n1 1\na\n15 4\nabccbaabccbazaa\n16 8\ncadjfdsljfdkljds\n6 2\nbaazaa\n6 3\nbosszb\n6 1\nqwerty\n7 7\ndaaaaaa\n14 2\nacabcbdeffewzd\n18 6\nzzzzzzzzzzzzyyyyyz\n9 4\naabaabbbc\n", "12\n12 3\nzazasqzqsasq\n1 1\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 string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of ...
1543_B. Customising the Track_1858
Highway 201 is the most busy street in Rockport. Traffic cars cause a lot of hindrances to races, especially when there are a lot of them. The track which passes through this highway can be divided into n sub-tracks. You are given an array a where a_i represents the number of traffic cars in the i-th sub-track. You def...
t = int(input()) for T in range(t): n = int(input()) A = [int(i) for i in input().split()] s = sum(A) mean = s//n rem = s%n for i in range(n): if i<rem: A[i] = mean+1 else: A[i] = mean A.sort() cursum = 0 curnums = 0 ans = 0 for i in ra...
{ "input": [ "3\n3\n1 2 3\n4\n0 1 1 0\n10\n8 3 6 11 5 2 1 7 10 4\n", "3\n3\n1 2 3\n4\n0 1 1 0\n10\n8 3 6 11 4 2 1 7 10 4\n", "3\n3\n1 2 3\n4\n1 1 1 0\n10\n8 3 6 11 4 2 1 7 10 4\n", "3\n3\n1 1 3\n4\n1 1 1 0\n10\n8 3 6 11 4 2 1 7 10 4\n", "3\n3\n1 1 3\n4\n1 1 1 0\n10\n8 3 6 11 4 2 1 3 10 4\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Highway 201 is the most busy street in Rockport. Traffic cars cause a lot of hindrances to races, especially when there are a lot of them. The track which passes through this highway ...
171_C. A Piece of Cake_1862
How to make a cake you'll never eat. Ingredients. * 2 carrots * 0 calories * 100 g chocolate spread * 1 pack of flour * 1 egg Method. 1. Put calories into the mixing bowl. 2. Take carrots from refrigerator. 3. Chop carrots. 4. Take chocolate spread from refrigerator. 5. Put chocolate spread ...
a = input().split() s = 0 for i in range(int(a[0])): s += (i + 1) * int(a[i + 1]) print(s)
{ "input": [ "4 1 2 3 4\n", "20 228 779 225 819 142 849 24 494 45 172 95 207 908 510 424 78 100 166 869 456\n", "4 220 380 729 969\n", "23 486 261 249 312 592 411 874 397 18 70 417 512 338 679 517 997 938 328 418 793 522 745 59\n", "17 644 532 255 57 108 413 51 284 364 300 597 646 712 470 42 730 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: How to make a cake you'll never eat. Ingredients. * 2 carrots * 0 calories * 100 g chocolate spread * 1 pack of flour * 1 egg Method. 1. Put calories into the mixi...
191_A. Dynasty Puzzles_1866
The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the...
from sys import stdin, stdout fst = 97 sze = 26 values = [[0 for i in range(sze)] for j in range(sze)] n = int(stdin.readline()) challengers = [] for i in range(n): s = stdin.readline().strip() challengers.append((ord(s[0]) - fst, ord(s[-1]) - fst)) for i in range(sze): if values[i][challeng...
{ "input": [ "4\nvvp\nvvp\ndam\nvvp\n", "3\nab\nc\ndef\n", "3\nabc\nca\ncba\n", "10\nabdcced\nbdacdac\necb\ndc\neaeeebdd\nadcdbadcac\neb\naadecccde\nedbaeacad\naccd\n", "100\nd\na\ne\neb\nc\nd\nd\ne\nc\ne\nac\nd\nc\naa\na\ne\nda\ne\ne\nc\ne\na\nc\nc\nbc\nbd\nd\nda\ncc\nbe\nb\na\nee\nd\nbe\nbe\ne\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconveni...
215_A. Bicycle Chain_1870
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on t...
n = int(input()) a = list(map(int,input().split())) m = int(input()) b = list(map(int,input().split())) count = [] for i in range(n): for j in range(m): if((b[j]/a[i])==int(b[j]/a[i])): count.append(int(b[j]/a[i])) o = max(count) print(count.count(o))
{ "input": [ "4\n1 2 3 4\n5\n10 11 12 13 14\n", "2\n4 5\n3\n12 13 15\n", "1\n1\n2\n1 2\n", "50\n17 20 22 28 36 38 46 47 48 50 52 57 58 62 63 69 70 74 75 78 79 81 82 86 87 90 93 95 103 202 292 442 1756 1769 2208 2311 2799 2957 3483 4280 4324 4932 5109 5204 6225 6354 6561 7136 8754 9670\n40\n68 214 957 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by tra...
239_A. Two Bags of Potatoes_1874
Valera had two bags of potatoes, the first of these bags contains x (x ≥ 1) potatoes, and the second — y (y ≥ 1) potatoes. Valera — very scattered boy, so the first bag of potatoes (it contains x potatoes) Valera lost. Valera remembers that the total amount of potatoes (x + y) in the two bags, firstly, was not gerater ...
""" author - Sayan Bose Brooklyn 99 is love! """ y, k, n = map(int, input().split()) if n-y == 0: print(-1) else: y1 = y res = [] f, f1 = 0, 0 while y <= n: if not y%k: f = 1 if y-y1: f1 = 1 print(y-y1, end = ' ') if not f: temp = y//k + 1 y = k * temp else: y += k if not f1:...
{ "input": [ "10 6 40\n", "10 1 10\n", "78 7 15\n", "10 4 9174\n", "1 999999999 1000000000\n", "17 28 532\n", "1 100000007 1000000000\n", "30351 86 424\n", "100000000 1000000000 1000000000\n", "1000000000 1 1\n", "15946 76 360\n", "46592 52 232\n", "48 6 50\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Valera had two bags of potatoes, the first of these bags contains x (x ≥ 1) potatoes, and the second — y (y ≥ 1) potatoes. Valera — very scattered boy, so the first bag of potatoes (i...
263_D. Cycle in Graph_1878
You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph indexed by integers from 1 to n. We know that each node of graph G is connected by edges with at least k other nodes of this graph. Your task is to find in the given graph a simple cycle of length of at least k + 1. A simpl...
n, m, k = map(int, input().split()) d = {} for _ in range(m): u, v = map(int, input().split()) if u not in d: d[u] = [v] else: d[u].append(v) if v not in d: d[v] = [u] else: d[v].append(u) # print(d) stack = [] not_visited = set(range(1, n+1)) while not_visited: ...
{ "input": [ "4 6 3\n4 3\n1 2\n1 3\n1 4\n2 3\n2 4\n", "3 3 2\n1 2\n2 3\n3 1\n", "30 70 4\n22 1\n1 25\n27 5\n25 10\n10 2\n29 4\n29 26\n28 18\n15 18\n25 11\n11 10\n11 28\n4 11\n4 26\n26 7\n26 18\n18 21\n14 17\n17 30\n30 9\n30 6\n30 27\n27 22\n22 15\n22 8\n8 4\n8 17\n17 16\n16 19\n16 12\n12 23\n23 20\n20 1\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph indexed by integers from 1 to n. We know that each node of graph G is connected by edge...
335_B. Palindrome_1886
Given a string s, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of s and is as long as possible. Input The only line of the input contains one string s of length n (1 ≤ n ≤ 5·104) c...
def p2(a): n = len(a) last = [[0] * 26 for _ in range(n)] last[0][ord(a[0])-97] = 0 for i in range(1, n): for j in range(26): last[i][j] = last[i-1][j] last[i][ord(a[i])-97] = i dp = [''] * n for i in range(n-1, -1, -1): for j in range(n-1, i, -1): k = last[j][ord(a[i])-97] ...
{ "input": [ "rquwmzexectvnbanemsmdufrg\n", "bbbabcbbb\n", "gamjuklsvzwddaocadujdmvlenyyvlflipneqofeyipmtunbdmbdyhkovnpdetueeiunsipowrhxrhtastjniqdhmehcumdcrghewljvpikcraoouhfwtnoaukbnykjapkvyakdnckkypargamvnsqtchesbmuffqqycnjvolmtpjfykvkeexkpdxjexrvdzpcbthhkxuucermkaebrvcxoovidpqnpkgbhiatyjvumihptrigqxse...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given a string s, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that...
358_B. Dima and Text Messages_1890
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other. Dima and Inna are using a secret code in their text messages. ...
def werify_message(words, message): true_message = ''.join(['<3', '<3'.join(words), '<3']) # index in true_message i = 0 for litera in message: if len(true_message) != i: if litera == true_message[i]: i += 1 else: # Дошли до конца исходного сообще...
{ "input": [ "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3\n", "3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3\n", "3\ni\nlove\nyou\n<3i<3love<23you<3ww\n", "1\na\n<3a<3b\n", "4\na\nb\nc\nd\n<3a<3b<3c<3d\n", "3\ni\nlove\ny...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find ...
381_A. Sereja and Dima_1894
Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The ...
n = int(input("")) cards=list(map(int,input().split())) sereja = 0 dima = 0 temp = 0 turn = 1 while cards != []: nMax=max(cards[0],cards[-1]) if turn==1: temp = sereja + nMax sereja+=nMax turn=2 else: temp = dima + nMax dima+=nMax turn=1 cards.remo...
{ "input": [ "7\n1 2 3 4 5 6 7\n", "4\n4 1 2 10\n", "1\n1\n", "30\n334 443 223 424 168 549 189 303 429 559 516 220 459 134 344 346 316 446 209 148 487 526 69 286 102 366 518 280 392 325\n", "95\n122 29 188 265 292 287 183 225 222 187 155 256 64 148 173 278 218 136 290 17 31 130 2 87 57 283 255 280...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players ...
42_A. Guilty — to the kitchen!_1900
It's a very unfortunate day for Volodya today. He got bad mark in algebra and was therefore forced to do some work in the kitchen, namely to cook borscht (traditional Russian soup). This should also improve his algebra skills. According to the borscht recipe it consists of n ingredients that have to be mixed in propor...
n,v=map(int,input().split()) a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] mini=10**9 for i in range(n): mini=min(mini,b[i]/a[i]) sm=0 for i in range(n): sm=sm+mini*a[i] print(min(sm,v))
{ "input": [ "1 100\n1\n40\n", "2 100\n1 1\n60 60\n", "2 100\n1 1\n25 30\n", "5 5005\n5 53 65 52 99\n21 49 9 3 66\n", "14 3615\n81 79 13 94 54 69 92 5 47 98 40 64 44 88\n52 73 7 12 29 40 46 47 60 66 63 68 71 4\n", "7 529\n77 18 67 64 43 51 30\n35 87 17 52 1 97 84\n", "16 1718\n42 68 96 52 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: It's a very unfortunate day for Volodya today. He got bad mark in algebra and was therefore forced to do some work in the kitchen, namely to cook borscht (traditional Russian soup). T...