src_uid
stringlengths
32
32
prob_desc_description
stringlengths
63
2.99k
tags
stringlengths
6
159
source_code
stringlengths
29
58.4k
lang_cluster
stringclasses
1 value
categories
listlengths
1
5
desc_length
int64
63
3.13k
code_length
int64
29
58.4k
games
int64
0
1
geometry
int64
0
1
graphs
int64
0
1
math
int64
0
1
number theory
int64
0
1
probabilities
int64
0
1
strings
int64
0
1
trees
int64
0
1
labels_dict
dict
__index_level_0__
int64
0
4.98k
d4b6bea78b80b0a94646cbaf048b473f
You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1').In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than k moves? It is possible that you do no...
['greedy']
n=int(input()) for i in range(n): n,k=map(int,input().split(' ')) stri=input() result=['1']*n t=0 for j in range(n): if(stri[j]=='0'): s=min(k,j-t) k-=s result[j-s]='0' t+=1 print("".join(result))
Python
[ "other" ]
599
276
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,239
be12bb8148708f9ad3dc33b83b55eb1e
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.Vasya has an account in each bank. Its balance may be negative, meaning ...
['data structures', 'constructive algorithms', 'sortings', 'greedy']
n = int(input()) b = list(map(int, input().split())) from collections import Counter from itertools import accumulate cum = list(accumulate(b)) cnt = Counter(cum) print (n - cnt.most_common(1)[0][1])
Python
[ "other" ]
887
201
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,685
9fd09b05d0e49236dca45615e684ad85
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be...
['binary search']
n, k = map(int, input().split()) A = list(map(int, input().split())) def f(x): over = 0 need = 0 for a in A: if a > x: over += (a - x) else: need += (x - a) return need <= (over * (1 - k/100)) left = 0 right = 1000 while right - left > 1e-12: m = (left + rig...
Python
[ "other" ]
754
409
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,075
ed5bf2596ec33a1a044ffd7952cdb897
Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a piece of squared paper and wrote all distinct non-empty non-decreasing subsequences of sequence a. Then for each sequence written on the squared paper, Sereja wrote on a piece of lines paper all sequences that do not exceed...
['data structures', 'dp']
M=10**9+7 N=2**20 v,w=[0]*N,[0]*N def S(y): s=0 while y>0: s+=w[y] y-=y&-y return s input() for x in map(int,raw_input().split()): s=1+S(x) d=((s*x-v[x])%M+M)%M v[x]+=d y=x while y<N: w[y]=(w[y]+d)%M y+=y&-y print S(N-1)%M
Python
[ "other" ]
659
255
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,342
c249103153c5006e9b37bf15a51fe261
You are given a following process. There is a platform with n columns. 1 \times 1 squares are appearing one after another in some columns on this platform. If there are no squares in the column, a square will occupy the bottom row. Otherwise a square will appear at the top of the highest square of this column. When all...
['implementation']
#JMD #Nagendra Jha-4096 #a=list(map(int,sys.stdin.readline().split(' '))) #n,k,s= map(int, sys.stdin.readline().split(' ')) import sys import math #import fractions #import numpy ###Defines...### mod=1000000007 ###FUF's...### def nospace(l): ans=''.join(str(i) for i in l) return ans ##### Main #### n,m=...
Python
[ "other" ]
576
484
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,775
c4929ec631caae439ccb9bc6882ed816
You have an array of integers a of size n. Initially, all elements of the array are equal to 1. You can perform the following operation: choose two integers i (1 \le i \le n) and x (x &gt; 0), and then increase the value of a_i by \left\lfloor\frac{a_i}{x}\right\rfloor (i.e. make a_i = a_i + \left\lfloor\frac{a_i}{x}\r...
['dp', 'greedy']
def quotient(n): li = set() for i in range(1, n + 1): li.add(n // i) return list(li) def main(): seen = {} for i in range(1, 1001): seen[i] = False tree = [[] for i in range(13)] tree[0] = [1] seen[1] = True for i in range(1, 13): cand = set() for el in tree[i - 1]: for q in quo...
Python
[ "other" ]
622
1,155
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,670
09faf19627d2ff00c3821d4bc2644b63
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, \dots, a_n, where a_i is the price of berPhone on the day i.Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if ...
['data structures', 'implementation']
t = int(input()) for _ in range(t): n = int(input()) lis = list(map(int, input().split())) stack = [] bad = 0 for i in range(len(lis)): if len(stack) == 0 or stack[-1] <= lis[i]: stack.append(lis[i]) else: while(len(stack) > 0 and stack[-1] > lis[i]): ...
Python
[ "other" ]
623
418
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,186
99d2b0386d101da802ac508ef8f7325b
There is a game called "Unique Bid Auction". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).Let's simplify this game a bit. Formally, there are n participants, the i-th participant chose the number a_i. The winner of the game is s...
['implementation']
import math for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) d,dd={},{} for ii in range(0,n): dd[a[ii]]=ii+1 for i in a: if i in d: d[i]+=1 else:d[i]=1 f,m=1,100000000 for j in d: # print(j,m) if d[j]==1: ...
Python
[ "other" ]
768
472
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,459
9a6ee18e144a38935d7c06e73f2e6384
The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY Cup contestants for help.A message is a sequence of n integers a1, a2, ..., an. Encryption uses a key which is a sequence of m integers b...
['data structures']
n,m,c = map(int,input().split()) a = list(input().split()) b = list(input().split()) sum = 0 for i in range(n): if i<m: sum = sum + int(b[i]) sum = sum%c if i >= n - m + 1: sum = c - int(b[i-n+m-1]) + sum sum = sum%c print((int(a[i])+sum)%c,end = ' ')
Python
[ "other" ]
1,048
300
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,387
516ed4dbe4da9883c88888b134d6621f
Sonya was unable to think of a story for this problem, so here comes the formal description.You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operation...
['dp', 'sortings']
from heapq import * class Maxheap: def __init__(_): _.h = [] def add(_, v): heappush(_.h, -v) def top(_): return -_.h[0] def pop(_): return -heappop(_.h) class Graph: def __init__(_): _.change = Maxheap() # increment slope at ... _.change.add(-10**18) _.a = _.y = 0 # last li...
Python
[ "other" ]
409
1,225
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,168
bf6e2310e4f808ca4c8ff28ba8a51a68
You are given the following concurrent program. There are N processes and the i-th process has the following pseudocode: repeat ni times yi := y y := yi + 1end repeatHere y is a shared variable. Everything else is local for the process. All actions on a given row are atomic, i.e. when the process starts executing a row...
['constructive algorithms']
import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') n, w = map(int, input().split()) a = [0] + list(map(int, input().split())) total = sum(a) def ng(): print('No') exit() def ok(a): print('Yes') print(*a) exit() if w < 1 or t...
Python
[ "other" ]
730
1,137
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
477
92b6ab47c306a15631f045d624a1bf37
You are given an array a consisting of n integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0.You may choose at most one consecutive subarray of ...
['dp', 'greedy', 'divide and conquer', 'data structures', 'brute force']
n, x = map(int, raw_input().split()) A = map(int, raw_input().split()) dpm = [0] * (n+1) dp = [0] * (n+1) dpn = [0] * (n+1) ans = 0 for i in xrange(1, n+1): dpm[i] = A[i-1]*x + max(dpm[i-1], dpn[i-1], 0) dp[i] = A[i-1] + max(dpm[i-1], dp[i-1], dpn[i-1], 0) dpn[i] = A[i-1] + max(dpn[i-1], 0) ans = max(ans, dpm[i], ...
Python
[ "other" ]
486
344
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
128
6bb26991c9ea70e51a6bda5653de8131
Roma (a popular Russian name that means 'Roman') loves the Little Lvov Elephant's lucky numbers.Let us remind you that lucky numbers are positive integers whose decimal representation only contains lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Roma's got n positive integers. He ...
['implementation']
n, k = map(int, raw_input().split()) t = 0 for a in raw_input().split(): if a.count('4') + a.count('7') <= k: t += 1 print t
Python
[ "other" ]
443
127
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
923
0ea79b2a7ddf3d4da9c7a348e61933a7
Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products.There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki peo...
['implementation']
import fileinput f = fileinput.input() n = int(f.readline()) ks = [] line = f.readline() ks = map(int, line.split()) ms = [] for i in range(n): ms.append(map(int, f.readline().split())) def time(n): return sum(ms[n]) * 5 + ks[n] * 15 print min(map(time, range(n)))
Python
[ "other" ]
823
276
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,567
1985566215ea5a7f22ef729bac7205ed
A and B are preparing themselves for programming contests.B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some ...
['data structures', 'implementation', 'sortings']
n=int(input()) s1=input().split() s1=sorted(s1) s2=input().split() s2=sorted(s2) s2.append(0) s3=input().split() s3=sorted(s3) s3.append(0) for i in range(len(s1)): if int(s1[i])-int(s2[i])!=0: print(s1[i]) break for i in range(len(s2)): if int(s2[i])-int(s3[i])!=0: ...
Python
[ "other" ]
852
353
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
787
6c165390c7f9fee059ef197ef40ae64f
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.The exam consists of n problems that can be solved in T minutes. Thus, the exam begins at time 0 and ends at time T. Petya can leave the exam at any integer time from 0 t...
['two pointers', 'sortings', 'greedy']
import sys t = int(input()) ans = [0]*t for pi in range(t): n, T, a, b = map(int, sys.stdin.readline().split()) a1 = list(map(int, sys.stdin.readline().split())) a2 = list(map(int, sys.stdin.readline().split())) easy_count = a1.count(0) hard_count = n - easy_count tasks = sorted((t, x) for x, t...
Python
[ "other" ]
2,543
862
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,898
30c4f155336cf762699a1bbc55a60d27
School holidays come in Berland. The holidays are going to continue for n days. The students of school №N are having the time of their lives and the IT teacher Marina Sergeyevna, who has spent all the summer busy checking the BSE (Berland State Examination) results, has finally taken a vacation break! Some people are i...
['implementation']
n,m = map(int,raw_input().split()) a = [0]*n for _ in xrange(m): b,c = map(int,raw_input().split()) for i in xrange(b,c+1,1): a[i-1]+=1 for i in xrange(n): if a[i]<>1: print i+1,a[i] exit(0) print 'OK'
Python
[ "other" ]
766
210
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
189
072fe39ccec85497af67cf46551a5920
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery.Soon the expenses started to overcome the income, so Slastyona decided to study the sweets market. She learned it's profitable to pack cake...
['dp', 'two pointers', 'divide and conquer', 'data structures', 'binary search']
import sys from collections import defaultdict as di range = range input = raw_input class LazySegmentTree: def __init__(self, data, default=0, func=max): """initialize the lazy segment tree with data""" self._default = default self._func = func self._len = len(data) self._...
Python
[ "other" ]
1,039
3,296
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
852
88e6651e1b0481d711e89c8071be1edf
Today the «Z» city residents enjoy a shell game competition. The residents are gathered on the main square to watch the breath-taking performance. The performer puts 3 non-transparent cups upside down in a row. Then he openly puts a small ball under one of the cups and starts to shuffle the cups around very quickly so ...
['implementation']
fil = open("input.txt","r") start = int(fil.readline().strip()) for i in range(3): a,b = list(map(int, fil.readline().strip().split(" "))) if a == start: start = b elif b == start: start = a fil2 = open("output.txt","w") fil2.write(str(start))
Python
[ "other" ]
549
247
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,373
5312a505bd59b55a6c5487f67a33d81a
At the store, the salespeople want to make all prices round. In this problem, a number that is a power of 10 is called a round number. For example, the numbers 10^0 = 1, 10^1 = 10, 10^2 = 100 are round numbers, but 20, 110 and 256 are not round numbers. So, if an item is worth m bourles (the value of the item is not gr...
['constructive algorithms']
import math for _ in [0] * int(input()): number = int(input()) k = 1 if number == 1 or math.log(number, 10) == int(math.log(number, 10)): print(0) elif number < 10: print(number - 1) else: while k <= (number // 10): k *= 10 print(num...
Python
[ "other" ]
853
330
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,297
7372994c95acc3b999dd9657abd35a24
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya.n athletes participate in the tournament, numbered from 1 to n. Burenka determined the strength of the i-th athlete as an integer a_i, where 1 \leq a_i \leq n. All the strength values are diff...
['binary search', 'data structures', 'implementation', 'two pointers']
import sys, threading import math from os import path from collections import defaultdict, Counter, deque from bisect import * from string import ascii_lowercase from functools import cmp_to_key import heapq def readInts(): x = list(map(int, (sys.stdin.readline().rstrip().split()))) return x[0] ...
Python
[ "other" ]
1,168
3,136
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,566
a440099306ef411a4deca7fe3305c08b
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order...
['implementation']
def main(): num = int(input()) data = [int(x) for x in input().split()] left = 0 right = 0 sign = True i = 0 j = 0 if len(data)<2: sign = False #find left for i in range(len(data)): if data[i] != i+1: break left = i+1 #check left...
Python
[ "other" ]
917
855
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,503
1392daad6638b7a93e84dd474cdf916a
You are a paparazzi working in Manhattan.Manhattan has r south-to-north streets, denoted by numbers 1, 2,\ldots, r in order from west to east, and r west-to-east streets, denoted by numbers 1,2,\ldots,r in order from south to north. Each of the r south-to-north streets intersects each of the r west-to-east streets; the...
['dp']
import sys range = xrange input = raw_input inp = [int(x) for x in sys.stdin.read().split()]; ii = 0 r = inp[ii]; ii += 1 n = inp[ii]; ii += 1 T = inp[ii + 0: ii + 3 * n: 3] X = inp[ii + 1: ii + 3 * n: 3] Y = inp[ii + 2: ii + 3 * n: 3] big = 501 DP = [0] * n DPmax = [0] * n for i in range(n): x = X[i] y = ...
Python
[ "other" ]
1,403
754
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,623
1cd295e204724335c63e685fcc0708b8
The Little Elephant loves sortings.He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ ...
['greedy', 'brute force']
n = int(input()) l = list(map(int,input().split())) l.append(max(l)) m,ans = l[0],0 for i in range(1,n) : if l[i-1]>l[i] : ans += l[i-1]-l[i] print(ans)
Python
[ "other" ]
585
164
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
730
d197a682b956f000422aeafd874816ce
Vasya has a sequence a consisting of n integers a_1, a_2, \dots, a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (\dots 00000000110_2) into 3 (\dots 00000000011_2), 12 (\dots 00000000110...
['dp', 'bitmasks']
#!/usr/bin/env python3 from __future__ import absolute_import import sys from itertools import imap def rint(): return imap(int, sys.stdin.readline().split()) #lines = stdin.readlines() def get_num1(i): cnt = 0 while i: if i%2: cnt +=1 i //=2 return cnt n = int(raw_input()...
Python
[ "other" ]
896
1,040
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,851
0d586ba7d304902caaeb7cd9e6917cd6
One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won.Alex has grown up since then, so he easily wins the most difficult...
['implementation']
n, m = map(int, input().split()) maps = [] for i in range(n): s = [ i for i in input()] maps.append(s) import sys for i in range(n): for j in range(m): ans = 0 if i-1 >=0 : if maps[i-1][j] == '*': ans += 1 if j-1 >= 0 and maps[i-1][j-1] == '*': ...
Python
[ "other" ]
953
1,013
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,610
e30085b163c820cff68fb24b94088ec1
Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself. To make everything right, Petya is going to move at most one bracket from its...
['data structures', 'greedy']
n = int(input()) start = 0 tmp = 0 wrong = 0 for b in input(): if b == '(': start += 1 else: if start > 0: start -= 1 else: wrong += 1 if wrong == 1: if start == 1: print('Yes') else: print('No') elif wrong == 0: if start == 0: ...
Python
[ "other" ]
951
390
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,994
a1ea9eb8db25289958a6f730c555362f
Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m ...
['constructive algorithms', 'sortings']
def cmp(x): return x[1] n,m=map(int,input().split()) q=[[] for i in range(m)] for i in range(n): name,r,b=input().split() q[int(r)-1].append([name,int(b)]) for i in q: i=sorted(i,key=cmp) if len(i)==2: print(i[-1][0],i[-2][0]) elif i[-1][1]==i[-2][1]==i[-3][1] or i[-2][1]==i[-3][1]: print('?') e...
Python
[ "other" ]
1,379
351
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
42
0135818c10fae9fc9efcc724fa43181f
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is a_i.You have to form k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So...
['dp', 'two pointers', 'sortings']
n, k = map(int, raw_input().split()) a = sorted(map(int, raw_input().split())) dp = [[0] * k for i in range(n)] dp[0][0] = 1 for i in range(1, n): l = i while l >= 0 and (a[i] - a[l]) <= 5: l -= 1 # print a[i], i - l, l for j in range(min(k, i + 1)): if j == 0: dp[i][j] = max(i - l...
Python
[ "other" ]
1,097
540
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,065
fbde86a29f416b3d83bcc63fb3776776
In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substrin...
['constructive algorithms']
s = ['a', 'a', 'b', 'b'] n = int(input()) q = [] for i in range(n): q.append(s[i % 4]) print(*q, sep="")
Python
[ "other" ]
488
108
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,717
16c54cf7d8b484b5e22a7d391fdc5cd3
Arkady's morning seemed to be straight of his nightmare. He overslept through the whole morning and, still half-asleep, got into the tram that arrived the first. Some time after, leaving the tram, he realized that he was not sure about the line number of the tram he was in.During his ride, Arkady woke up several times ...
['implementation']
from collections import Counter as c x=int(input()) l=[] for i in range(x): l=l+list(map(int,input().split()))[1:] l=dict(c(l)) for a,b in l.items(): if b==x: print(a,end=' ')
Python
[ "other" ]
532
192
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,148
4585419ab2b7200770cfe1e607161e9f
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative intege...
['greedy']
def read_input(): n = int(input()) line = input() line = line.strip().split() b = [int(num) for num in line] return n, b def mishka_and_the_last_exam(n, b): l = [0] * n half = n // 2 for i, sum_ in enumerate(b[::-1]): if i == 0: l[half - 1 - i] = sum_ // 2 ...
Python
[ "other" ]
1,299
958
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,089
6e7c2c0d7d4ce952c53285eb827da21d
Sherlock Holmes and Dr. Watson played some game on a checkered board n × n in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to count the number...
['brute force']
n = int(raw_input()) lis = [] for i in range(n): l = [] parts = [int(x) for x in raw_input().split()] for p in parts: l.append(p) lis.append(l) rows = [] columns = [] for i in range(n): sum = 0 for j in range(n): sum += lis[i][j] rows.append(sum) for i in range(n): sum = 0 for j in ...
Python
[ "other" ]
975
501
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,711
2a6c457012f7ceb589b3dea6b889f7cb
George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living i...
['implementation']
import sys n = int(sys.stdin.readline().split()[0]) rooms = [[0 for x in range(2)] for y in range(n)] row = 0 for line in sys.stdin: column = 0 for word in line.split(): rooms[row][column] = int(word) column += 1 row += 1 rooms_acceptable = 0 for room in rooms: if room[0] + 2 <= ro...
Python
[ "other" ]
461
382
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,713
bac276604d67fa573b61075c1024865a
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhap...
['dp', 'sortings']
from sys import stdin from itertools import repeat, groupby from operator import itemgetter def main(): n, m = map(int, stdin.readline().split()) d = [[] for _ in xrange(100001)] IN = stdin.read().split() IN = map(int, IN, repeat(10, len(IN))) for i in xrange(0, 3*m, 3): d[IN[i+2]].append((I...
Python
[ "other" ]
593
553
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,720
994bfacedc61a4a67c0997011cadb333
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...
['data structures', 'greedy']
from heapq import heappush as hpush, heappop as hpop INF = float('inf') n = input() p = map(int, raw_input().strip().split()) free = [] sold = [] hpush(free, p[0]) ans = 0 for i in xrange(1, n): try: p1 = hpop(free) except: p1 = INF try: p2 = hpop(sold) except: p2 ...
Python
[ "other" ]
445
789
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,144
7bb5df614d1fc8323eba51d04ee7bf2a
One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (1 ...
['dp']
n = int(input()) P = list(map(int, input().split())) dp = [0] * (n + 2) dp[1] = 0 M = 10 ** 9 + 7 for i in range(2, n + 2): dp[i] = 2 * dp[i - 1] + 2 - dp[P[i - 2]] dp[i] %= M print(dp[-1])
Python
[ "other" ]
1,017
197
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,779
848ead2b878f9fd8547e1d442e2f85ff
Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, b...
['implementation', 'greedy']
def main(): n, v = map(int, input().split()) l = [0] * 3002 for _ in range(n): a, b = map(int, input().split()) l[a] += b a = res = 0 for b in l: x = a + b if x < v: res += x a = 0 else: res += v if a < v: ...
Python
[ "other" ]
658
434
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,763
585bb4a040144da39ed240366193e705
The robot is located on a checkered rectangular board of size n \times m (n rows, m columns). The rows in the board are numbered from 1 to n from top to bottom, and the columns — from 1 to m from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.The sequence of comm...
['implementation']
t = int(input()) for case in range(t): n, m = map(int, input().split()) s = input() x, y = 1, 1 x_max_r, x_max_l, y_max_u, y_max_d = 0, 0, 0, 0 x_last, y_last = 1, 1 x_mov, y_mov = 0, 0 # First find longest path for i in s: if i == "R": x_mov += 1 ...
Python
[ "other" ]
1,724
1,284
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
680
d5fc2bc618dd9d453a5e7ae30ccb73f3
Polycarp is playing a new computer game. This game has n stones in a row. The stone on the position i has integer power a_i. The powers of all stones are distinct.Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone)....
['brute force', 'dp', 'greedy']
t = int(input()) for p in range(t): n = int(input()) a = list(map(int, input().rstrip().split())) x = max(a) j = a.index(x) y = min(a) i = a.index(y) mid = n // 2 a1 = 0 a2 = 0 a3 = 0 for k in range(n): if a[k] == y or a[k] == x: a1 = k + 1 ...
Python
[ "other" ]
1,349
1,075
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,950
5fa2af185c4e3c8a1ce3df0983824bad
Tenten runs a weapon shop for ninjas. Today she is willing to sell n shurikens which cost 1, 2, ..., n ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available...
['data structures', 'implementation', 'greedy']
def read_seq(v): seq = [] for i in range(2 * v): s = input().split() if len(s) == 2: s[1] = int(s[1]) seq.append(s) return seq def solve(): n = int(input()) pos = [-1] * (2 * n) stack = [] seq = read_seq(n) for i, act in enumerate(seq): if ac...
Python
[ "other" ]
946
1,001
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,940
4d743a00e11510c824080ad7f1804021
Vasya has found a piece of paper with a coordinate system written on it. There are n distinct squares drawn in this coordinate system. Let's number the squares with integers from 1 to n. It turned out that points with coordinates (0, 0) and (ai, ai) are the opposite corners of the i-th square.Vasya wants to find such i...
['implementation', 'sortings', 'greedy']
n, k = map(int, raw_input().split()) squares = map(int, raw_input().split()) squares.sort(reverse=True) if k > n: print -1 else: print squares[k - 1], print 0
Python
[ "other" ]
590
170
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,292
cc928c02b337f2b5ad7aed37bb83d139
Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then ...
['data structures', 'dp']
##[1,7,2,3] ## ##[8,1,1,3] ## ##[1,8,10,13] ## ##[8,9,10,13] ## ##[-7,-1,0,0] n=int(input()) A=list(map(int,input().split())) B=list(map(int,input().split())) Sa=[0]*n Sb=[0]*n Sa[0]=A[0] Sb[0]=B[0] for i in range(1,n): Sa[i]=Sa[i-1]+A[i] Sb[i]=Sb[i-1]+B[i] D=[0]*n for i in range(n): D[i]=Sa[i]-Sb[i] m...
Python
[ "other" ]
1,934
800
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,407
4d24aaf5ebf70265b027a6af86e09250
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characte...
['bitmasks', 'brute force', 'implementation']
f=int(input()) for i in range(f): li=input() if li[0] == li[-1]: print("NO") continue x=0 y=0 z=0 a1=li[0] a2=li[-1] open1=0 close1=0 summ=0 for i in range(len(li)): if li[i]==a1: open1+=1 elif li[i]==a2: ...
Python
[ "other" ]
1,055
910
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,361
e702594a8e993e135ea8ca78eb3ecd66
A coordinate line has n segments, the i-th segment starts at the position li and ends at the position ri. We will denote such a segment as [li, ri].You have suggested that one of the defined segments covers all others. In other words, there is such segment in the given set, which contains all other ones. Now you want t...
['implementation', 'sortings']
def main(): number = int(raw_input()) min0 = -1 max0 = -1 covn = -1 for k in range(0, number): line = raw_input() words = line.split(' ', 1) l0 = int(words[0]) r0 = int(words[1]) if min0 == -1 or l0 < min0: min0 = l0 covn = -1 i...
Python
[ "other" ]
585
475
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
616
c1e51cc003cbf60a617fc11de9c73bcf
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...n friends live in a city which can be represented as a number line. The i-th friend lives in a house with an integer coordinate x_i. The i-th friend can come celebrate the New Year to the house with coordinate x_...
['dp', 'greedy']
# cook your dish here n = int(input()) f = list(map(int, input().split())) f.sort() f1 = [] for i in range(n): f1.append(f[i]) if (f1[0] > 0): f1[0] -= 1 for i in range(1, n): if (f1[i]-f1[i-1] > 1): f1[i] -= 1 elif (f1[i] == f1[i-1]): if (i == n-1 or f1[i+1] > f1[i]): ...
Python
[ "other" ]
1,068
609
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,502
88d56c1e3a7ffa94354ce0c70d8e958f
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken cl...
['implementation', 'brute force']
n=input() a,b=map(str,raw_input().split(':')) if(int(a)>[n-1,n][n==12]):a='0'+a[1] if(int(b)>59):b='0'+b[1] print ''.join([[a,'10'][a=='00' and n==12],':',b])
Python
[ "other" ]
763
158
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
34
fdd60d5cde436c2f274fae89f4abf556
You are given a circular array with n elements. The elements are numbered from some element with values from 1 to n in clockwise order. The i-th cell contains the value ai. The robot Simba is in cell s.Each moment of time the robot is in some of the n cells (at the begin he is in s). In one turn the robot can write out...
['dp']
from heapq import heappush, heappop from collections import defaultdict def solve(start, lt): n = len(lt) ii = list(sorted(set(lt))) maxlt = max(ii) right = {} left = {} leftn = {} rightn = {} last = {} for i in range(2 * n): l = lt[i % n] if l in last: ...
Python
[ "other" ]
732
4,896
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,480
0a157c97599c2240ca916317524c67e5
Even if you just leave them be, they will fall to pieces all by themselves. So, someone has to protect them, right?You find yourself playing with Teucer again in the city of Liyue. As you take the eccentric little kid around, you notice something interesting about the structure of the city.Liyue can be represented as a...
['bitmasks', 'constructive algorithms', 'divide and conquer']
# LUOGU_RID: 93245551 n,k=map(int,input().split()) cnt=0 sum=1 while sum<n: sum=sum*k cnt=cnt+1 print(cnt) for i in range(0,n): for j in range(i+1,n): u=i v=j ans=0 while u!=v: u//=k v//=k ans=ans+1 print(ans,end=' '...
Python
[ "other" ]
1,319
321
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,902
3efc451a0fd5b67d58812eff774b3c6a
Student Dima from Kremland has a matrix a of size n \times m filled with non-negative integers.He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him!Formally, he wants to choose an integers sequence c_1, c_2, ...
['dp', 'constructive algorithms', 'bitmasks', 'brute force']
n, m = map(int, raw_input().split()) a = [] for i in range(n): a.append(map(int, raw_input().split())) z = 'NIE' l = 0 r = 0 for i in range(n): for j in range(m): x = 0 for k in range(n): if k != i: x = x ^ a[k][0] else: x = x ^ a[k][j] if x > 0: z = ...
Python
[ "other" ]
661
556
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,814
d24c7d022efd1425876e6b45150362be
John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N.John first has to select from which city he will start his journey....
['dp']
n = int(input()) cnt = [[] for _ in range(n)] for i in range (n - 1): fr, to = map(int, input().split()) cnt[fr - 1].append(to - 1); cnt[to - 1].append(fr - 1); l = 0 for i in range(n): if (len(cnt[i]) == 1): l += 1 ans = (n - l) * pow(2, n - l, 10 ** 9 + 7) ans += l * pow(2, n - l + 1, 10 ** 9 ...
Python
[ "other" ]
1,329
353
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
204
b0446162c528fbd9c13f3e9d5c70d076
As you know, the most intelligent beings on the Earth are, of course, cows. This conclusion was reached long ago by the Martian aliens, as well as a number of other intelligent civilizations from outer space. Sometimes cows gather into cowavans. This seems to be seasonal. But at this time the cows become passive and re...
['data structures', 'sortings', 'brute force']
import sys range = xrange input = raw_input inp = [int(x) for x in sys.stdin.read().split()] ii = 0 n = inp[ii] ii += 1 A = [float(x) for x in inp[ii:ii + n]] ii += n queries = [[] for _ in range(n)] q = inp[ii] ii += 1 m = 500 ans = [0]*q B = [] for _ in range(q): a = inp[ii] - 1 ii += 1 b = inp[ii]...
Python
[ "other" ]
1,377
1,015
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,777
cc1b29b49711131d4790d91d0fae4a5a
Recently Monocarp got a job. His working day lasts exactly m minutes. During work, Monocarp wants to drink coffee at certain moments: there are n minutes a_1, a_2, \dots, a_n, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). ...
['data structures', 'two pointers', 'binary search', 'greedy']
from collections import Counter, defaultdict, deque read = lambda: list(map(int,input().split())) def solve(a,d,df): cnt = 1 res = [0] * n Q = deque([(a[0], cnt)]) res[df[a[0]]] = 1 for i in range(1,n): if a[i] > Q[0][0] + d: val, day = Q.popleft() res[df[a[i]]] = da...
Python
[ "other" ]
1,156
591
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,975
4cc5a6975d33cee60e53e8f648ec30de
You are given two strings s and t, both consisting only of lowercase Latin letters.The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, \dots, s_r without changing the order.Each of the occurrences of string a in a string b is a position i (1 \le i \le |b| - |a| + 1) such that b[i....
['implementation', 'brute force']
a1 = list(map(int, input().split(' ')[:3])) bigString = input() littleString = input() all = 0 left = [0]*len(bigString) right = [0]*len(bigString) leftCount = 0 rightCount = 0 for i in range(len(bigString)): left[i] = leftCount if bigString.find(littleString,i,i + len(littleString)) != -1: leftCount +=...
Python
[ "other" ]
599
915
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,450
ed0a8a10e03de931856e287f9e650e1a
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≤ i ≤ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. ...
['binary search', 'greedy']
N, M = map(int, input().split()) books_list = list(map(int, input().split())) while books_list[-1] == 0: books_list.pop() books_list.insert(0, 0) def check(Time): piles = books_list[:] last_pile_no = len(piles) - 1 for i in range(M): #student i_time = Time - last_pile_no while True: ...
Python
[ "other" ]
1,150
783
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
265
6c35a8bcee2e29142108c9a0da7a74eb
Masha and Grisha like studying sets of positive integers.One day Grisha has written a set A containing n different integers ai on a blackboard. Now he asks Masha to create a set B containing n different integers bj such that all n2 integers that can be obtained by summing up ai and bj for all possible pairs of i and j ...
['constructive algorithms', 'brute force']
from random import randint def solve(): n, a = int(input()), list(map(int, input().split())) bb, ab = set(), set() while True: b = randint(1, 1000000) for i in a: if i + b in ab: break else: bb.add(b) if len(bb) == n: break for i in a: ab.add(b + i) print('YES') print(' '.join(map(str...
Python
[ "other" ]
538
372
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,465
97bbbf864fafcf95794db671deb6924b
Bob came to a cash &amp; carry store, put n items into his trolley, and went to the checkout counter to pay. Each item is described by its price ci and time ti in seconds that a checkout assistant spends on this item. While the checkout assistant is occupied with some item, Bob can steal some other items from his troll...
['dp']
n=input() t=[0] a=[0] for i in range(n): v1,v2=map(int,raw_input().split(" ")) v1+=1 t.append(v1) a.append(v2) ans=[10**15 for j in range(n+1)] #print ans ans[0]=0 for i in range(1,n+1): for j in range(n,0,-1): ans[j]=min(ans[j],ans[max(0,j-t[i])]+a[i]) print ans[n]
Python
[ "other" ]
554
313
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,169
b9dacff0cab78595296d697d22dce5d9
Let us remind you part of the rules of Codeforces. The given rules slightly simplified, use the problem statement as a formal document.In the beginning of the round the contestants are divided into rooms. Each room contains exactly n participants. During the contest the participants are suggested to solve five problems...
['implementation']
n = input() man = '' mas = -3000 for i in xrange(n): ss = raw_input().split() curm = sum(map(int,ss[3:]),0) curm += int(ss[1])*100 - int(ss[2])*50 if curm>mas: man = ss[0] mas = curm print man
Python
[ "other" ]
902
203
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,977
35dccda260cfdaea651d1950df452e7a
The only difference between the two versions is that in this version n \leq 2 \cdot 10^5 and the sum of n over all test cases does not exceed 2 \cdot 10^5.A terminal is a row of n equal segments numbered 1 to n in order. There are two terminals, one above the other. You are given an array a of length n. For all i = 1, ...
['data structures', 'divide and conquer', 'sortings']
from array import array for _ in range(int(input())): n = int(input()) a = array('i', map(int, input().split())) ans = 0 # Codeforces 1676H2 Maximum Crossings # https://codeforces.com/contest/1676/problem/H2 def mergeSort(arr): global ans if len(arr) < 2: ...
Python
[ "other" ]
878
754
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,149
7c1f7740bdac042147e969a98897f594
Jeff's friends know full well that the boy likes to get sequences and arrays for his birthday. Thus, Jeff got sequence p1, p2, ..., pn for his birthday.Jeff hates inversions in sequences. An inversion in sequence a1, a2, ..., an is a pair of indexes i, j (1 ≤ i &lt; j ≤ n), such that an inequality ai &gt; aj holds.Jeff...
['greedy']
n = int(input()) inp = input() seq = inp.split(' ') seq = [ abs(int(x)) for x in seq ] Max = max(seq) nxt = [0] * n cnt = [0] * n pos = [n] * (Max+1) for i in range(n-1, -1, -1): nxt[i] = pos[seq[i]] pos[seq[i]] = i for i in range(0, Max+1): j = pos[i] while(j<n): front = sum(cnt[0:j]) b...
Python
[ "other" ]
516
691
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,216
7b806b9a402a83a5b8943e7f3436cc9a
Berland annual chess tournament is coming!Organizers have gathered 2·n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.Thus, organizers should...
['implementation', 'sortings']
n = int(input()) l = sorted(map(int, input().split())) l2 = l[n:] for a in l[:n]: if a in l2: print('NO') exit() print('YES')
Python
[ "other" ]
1,063
133
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,617
b06d5b48525cd386a0141bdf44579a5c
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are n players arranged in a circle, so that for all j such that 2 \leq j \leq n, player j - 1 is to the left of the player j, and player j is to the right of player j - 1. Additionally, player n is to the left of player 1, and player 1 is ...
['dp', 'greedy']
for _ in range(int(input())): n=int(input()) a=list(str(input())) ans=0 flag=0 c=1 for i in range(1,n): if a[i]!=a[i-1]: flag=1 break if i==n-1 and flag==0: if a[0]=='R': a[0]='L' else: a[0]='R' ans=1 else: ...
Python
[ "other" ]
1,769
490
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
307
5ccef7fbfd5e85d7fc7ef92f9ebc4088
You are given two arrays a and b of n elements, each element is either 0 or 1.You can make operations of 2 kinds. Pick an index i and change a_i to 1-a_i. Rearrange the array a however you want. Find the minimum number of operations required to make a equal to b.
['brute force', 'greedy', 'sortings']
n = int(input()) for i in range(n): index = int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) ans = abs(sum(a)-sum(b)) cnt = ans for x in range(index): if cnt <= 0: break if a[x] != b[x]: a[x] = 1- a[...
Python
[ "other" ]
338
427
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,443
14959b7266ceebe96c33bfa1791e26b4
You have a garden consisting entirely of grass and weeds. Your garden is described by an n × m grid, with rows numbered 1 to n from top to bottom, and columns 1 to m from left to right. Each cell is identified by a pair (r, c) which means that the cell is located at row r and column c. Each cell may contain either gras...
['sortings', 'greedy']
#------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() ...
Python
[ "other" ]
1,426
2,390
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,970
b17eaccfd447b11c4f9297e3276a3ca9
Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab.There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing pro...
['implementation']
n, k = map(int, input().split()) a = list(map(int, input().split())) p = [[0, 0, 0] for i in range(k)] j = 0 m = 0 ans = [0] * n flag = True while flag: flag = False t = 0 for i in range(k): p[i][0] += 1 if p[i][0] == int(m / n * 100 + 0.5): ans[p[i][2]] = 1 if p[i][0] >=...
Python
[ "other" ]
1,783
641
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
712
4a48b828e35efa065668703edc22bf9b
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.There are n displays placed along a road, and the i-th of them can display a text with font size s_i only. Maria Stepanovna wants to rent such thr...
['dp', 'implementation', 'brute force']
from operator import __gt__, __lt__ def helper(op): xx = list(map(float, input().split())) le, ri, a = [], [], xx[0] for b in xx: if a > b: a = b le.append(a) for a in reversed(xx): if op(b, a): b = a ri.append(b) ri.reverse() return xx, l...
Python
[ "other" ]
654
871
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,865
11fabde93815c1805369bbbc5a40e2d8
Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m.Public transport is not free. There are 4 types of tickets: A ticket for one ride on so...
['implementation', 'greedy']
c1, c2, c3, c4 = map(int, input().split()) n, m = map(int, input().split()) lst1 = list(map(int, input().split())) lst2 = list(map(int, input().split())) buss = 0 for i in lst1: buss += min(c1 * i, c2) buss = min(buss, c3) tr = 0 for i in lst2: tr += min(c1 * i, c2) tr = min(tr, c3) print(min(c4, buss + tr))
Python
[ "other" ]
831
320
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,988
c091ca39dd5708f391b52de63faac6b9
This is an interactive problem.Homer likes arrays a lot and he wants to play a game with you. Homer has hidden from you a permutation a_1, a_2, \dots, a_n of integers 1 to n. You are asked to find any index k (1 \leq k \leq n) which is a local minimum. For an array a_1, a_2, \dots, a_n, an index i (1 \leq i \leq n) is ...
['binary search', 'interactive', 'ternary search']
#import io, os #input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline import math n=int(input()) hi=n lo=1 if n==1: print("!",n) else: print("?",1,flush=True) a=int(input()) print("?",2,flush=True) b=int(input()) print("?",n,flush=True) c=int(input()) print("?",n-1,flush=True...
Python
[ "other" ]
981
790
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
512
79d8943cb9a9f63b008cb38ee51ae41e
You like numbers, don't you? Nastia has a lot of numbers and she wants to share them with you! Isn't it amazing?Let a_i be how many numbers i (1 \le i \le k) you have.An n \times n matrix is called beautiful if it contains all the numbers you have, and for each 2 \times 2 submatrix of the original matrix is satisfied: ...
['binary search', 'constructive algorithms', 'dp', 'greedy']
import sys I=lambda:[*map(int,sys.stdin.readline().split())] def totalcells(n): return n*n-(n//2)*(n//2) def maxfreq(n): return n*((n+1)//2) t, = I() for _ in range(t): m, k = I() a = I() mf = max(a) n = 0 while totalcells(n) < m or maxfreq(n) < mf: n += 1 print(n) ans = [[...
Python
[ "other" ]
489
1,468
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,508
ad242f98f1c8eb8d30789ec672fc95a0
Tokitsukaze has a sequence a of length n. For each operation, she selects two numbers a_i and a_j (i \ne j; 1 \leq i,j \leq n). If a_i = a_j, change one of them to 0. Otherwise change both of them to \min(a_i, a_j). Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to 0. I...
['implementation']
for _ in range(int(input())): a=int(input()) b=list(map(int,input().split())) if 0 in b: c=b.count(0) print(a-c) else: r=list(set(b)) if len(r)==len(b): print(a+1) elif len(r)<a: print(a)
Python
[ "other" ]
429
300
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
580
d5be4e2cd91df0cd8866d46a979be4b1
You have been invited as a production process optimization specialist to some very large company. The company has n machines at its factory, standing one behind another in the production chain. Each machine can be described in one of the following two ways: (+,~a_i) or (*,~a_i).If a workpiece with the value x is suppli...
['binary search', 'brute force', 'greedy']
from bisect import bisect import sys input = sys.stdin.readline n, b, p, m = map(int, input().split()) adds = [] curr = [] mults = [] i = 0 for _ in range(n): t, v = input().split() v = int(v) if t == '*': if v == 1: continue curr.sort() a...
Python
[ "other" ]
1,688
2,692
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,382
97e149fe5933bf1c9dbe8d958c1b2e05
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of n rows and n columns of square cells. The rows are numbered from 1 to n, from top to bottom, and the columns are numbered from 1 to n, from left to right. The position of a cell at row r and column c is r...
['dp', 'two pointers', 'implementation', 'data structures', 'brute force']
import fileinput def D(a):print(a) def S(s,I):return int(s.split(" ")[I]) def sm(I,B,E): if(B==0):return S[I][E] return S[I][E]-S[I][B-1] def main(): global S z=0 A=0 N=0 K=0 C=0 for l in fileinput.input(): z+=1 if(z<2): N=S(l,0) K=S(l,1) ...
Python
[ "other" ]
1,236
1,837
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,643
7e678f141f411d3872f25559e2c1f17c
You are given an array a of n non-negative integers. In one operation you can change any number in the array to any other non-negative integer.Let's define the cost of the array as \operatorname{DIFF}(a) - \operatorname{MEX}(a), where \operatorname{MEX} of a set of non-negative integers is the smallest non-negative int...
['binary search', 'brute force', 'constructive algorithms', 'data structures', 'greedy', 'two pointers']
import os, sys from io import BytesIO, IOBase from array import array class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.wri...
Python
[ "other" ]
663
4,361
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,023
b7aef95015e326307521fd59d4fccb64
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the requir...
['implementation']
n = int(input()) l = list(map(int,input().split())) d1 = {l[i]:i+1 for i in range(n)} m = int(input()) queries = list(map(int,input().split())) count1 = 0 count2 = 0 k1 = set({}) k2 = set({}) for i in queries: count1 = count1 + d1[i] count2 = count2 + n+1-d1[i] print(count1,count2)
Python
[ "other" ]
1,781
284
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,278
8a6953a226abef41a44963c9b4998a25
There are n programmers that you want to split into several non-empty teams. The skill of the i-th programmer is a_i. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team ...
['dp', 'greedy', 'implementation', 'sortings', 'brute force']
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s); sys.stdout.write('\n') def wi(n): sys.stdout.write(str(n)); sys.stdout.write('\n') def wia(a, sep=' '): sys.stdout.write(sep...
Python
[ "other" ]
518
1,045
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,094
ec1a29826209a0820e8183cccb2d2f01
You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Def...
['greedy']
n, k = map(int, input().split()) temp = [int(x) for x in input().split()] if k > 2: print(max(temp)) elif k == 1: print(min(temp)) else: print(max(temp[0], temp[-1]))
Python
[ "other" ]
382
179
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,572
d497431eb37fafdf211309da8740ece6
Vova is again playing some computer game, now an RPG. In the game Vova's character received a quest: to slay the fearsome monster called Modcrab.After two hours of playing the game Vova has tracked the monster and analyzed its tactics. The Modcrab has h2 health points and an attack power of a2. Knowing that, Vova has d...
['implementation', 'greedy']
[h1, a1, c1] = map(int, raw_input().strip().split(' ')) [h2, a2] = map(int, raw_input().strip().split(' ')) total = 0 s = "" while h2 > 0: total += 1 if h2 - a1 <= 0: s += "STRIKE\n" break if h1 - a2 <= 0: s += "HEAL\n" h1 += c1 h1 -= a2 else: s += "STRIKE\n" h1 -= a2 h2 -= a1 print total print s
Python
[ "other" ]
1,465
320
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
519
d8a93129cb5e7f05a5d6bbeedbd9ef1a
Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is that each of them uses at most two distinct letters. Andrew decided to send an article to the magazine, but as he hasn't written any article, he just decided to take a random one from magazine 26Char. However, before sendi...
['implementation', 'brute force']
from itertools import combinations n = int(input()) a = [input() for _ in range(n)] ans = 0 for (c, d) in combinations('abcdefghijklmnopqrstuvwxyz', 2): t = 0 for s in a: if len(s) == s.count(c) + s.count(d): t += len(s) ans = max(ans, t) print(ans)
Python
[ "other" ]
699
295
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,405
bc1587d3affd867804e664fdb38ed765
The hero is addicted to glory, and is fighting against a monster. The hero has n skills. The i-th skill is of type a_i (either fire or frost) and has initial damage b_i. The hero can perform all of the n skills in any order (with each skill performed exactly once). When performing each skill, the hero can play a magic ...
['greedy', 'implementation', 'sortings']
nn = int(input()) for _ in range(nn): n = int(input()) fire_or_frost=input()#[0,0,0,1,1,1]#[randint(0,1) for x in range(n_skills)] fire_or_frost = fire_or_frost.split(' ') skills = [int(x) for x in fire_or_frost] scores = input()#[3,4,5,6,7,8]#[randint(1,10) for x in range(n_skills)] scores = scores.split(' ') d...
Python
[ "other" ]
1,128
1,012
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
346
66daa3864875e48d32a7acd23bd7a829
There are n traps numbered from 1 to n. You will go through them one by one in order. The i-th trap deals a_i base damage to you.Instead of going through a trap, you can jump it over. You can jump over no more than k traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if...
['constructive algorithms', 'greedy', 'sortings']
def get_minimum_damage(trap_count: int, base_damage: list, jumps: int): # check if all the traps can be jumped across if trap_count <= jumps: return 0 # store idx, val, profit/loss if jumped across = [(idx, value, profit)] damage_loss_pairs = [(idx, val, val - (trap_count - idx) + 1) for idx, va...
Python
[ "other" ]
856
1,208
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,782
74095fe82bd22257eeb97e1caf586499
Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.This time mr. Boosch plans to sign 2k law...
['dp', 'implementation', 'data structures']
n, k = map(int, raw_input().split()) x = map(int, raw_input().split()) sum = [0] for i in range(1, n + 1): sum.append(sum[i - 1] + x[i - 1]) ksum = [0] for i in range(1, n - k + 2): ksum.append(sum[i + k - 1] - sum[i - 1]) right = [0 for i in ksum] pos = [0 for i in ksum] right[len(ksum) - 1] = ksum[len(ksum) ...
Python
[ "other" ]
1,109
730
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,683
1378b4c9ba8029d310f07a1027a8c7a6
A team quiz game called "What? Where? When?" is very popular in Berland. The game is centered on two teams competing. They are the team of six Experts versus the team of the Audience. A person from the audience asks a question and the experts are allowed a minute on brainstorming and finding the right answer to the que...
['implementation']
f = open('input.txt', 'r') n, k = map(int, f.readline().split()) a, k = list(map(int, f.readline().split())), k - 1 while not a[k]: k = (k + 1) % n print(k + 1, file=open('output.txt', 'w'))
Python
[ "other" ]
1,153
195
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,930
7e6a2329633ee283e3327413114901d1
You are given a grid, consisting of 2 rows and n columns. Each cell of this grid should be colored either black or white.Two cells are considered neighbours if they have a common border and share the same color. Two cells A and B belong to the same component if they are neighbours, or if there is a neighbour of A that ...
['dp', 'bitmasks']
n,k = map(int,input().split()) mod = 998244353 dp = [[[0,0]for j in range(2*n+1)] for i in range(n)] dp[0][0][0] = dp[0][1][1] = 1 for i in range(1,n): for j in range(2*n-1): dp[i][j][0] += (dp[i-1][j][0] + dp[i-1][j][1] + dp[i-1][j][1]) %mod dp[i][j+1][0] += dp[i-1][j][0] % mod dp[i][j+1][1] += (dp[i-1]...
Python
[ "other" ]
583
456
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,417
9f8660190134606194cdd8db891a3d46
You are given a non-empty string s=s_1s_2\dots s_n, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string s if there is an integer ...
['dp', 'greedy']
from heapq import heappush,heappop,heapify from collections import deque,defaultdict,Counter import itertools from itertools import permutations,combinations import sys import bisect import string import math import time #import random def I(): return int(input()) def MI(): return map(int,input().split()) def L...
Python
[ "other" ]
1,134
2,624
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
655
1d55d31320368ddb1439ee086d40b57c
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas). V...
['dp', 'bitmasks', 'brute force']
# -*- coding: utf-8 -*- import sys def make_string(k, k2string, s1, s2): if k in k2string: return k2string[k] if k == 1: k2string[k] = s1 elif k == 2: k2string[k] = s2 else: k2string[k] = make_string(k-2, k2string, s1, s2) + make_string(k-1, k2string, s1, s2) return k2string[k] def count_comb(k, k2comb):...
Python
[ "other" ]
1,217
2,075
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,298
56535017d012fdfcc13695dfd5b33084
A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larg...
['implementation', 'brute force']
L=lambda x,y:2*y>=x>=y D=lambda x,y:2*y<x>=y a,b,c,d=map(int,raw_input().split()) for i in range(199): for j in range(i): for k in range(j): if L(i,a) and L(j,b) and L(k,c) and D(i,d) and D(j,d) and L(k,d): print i,'\n',j,'\n',k exit(0) print -1
Python
[ "other" ]
772
301
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,215
b9bafdc49709b4fc4b5fe786d5aa99a3
This is the easy version of this problem. The only difference is the constraint on k — the number of gifts in the offer. In this version: k=2.Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "k of goods for the price of one" is held in store. ...
['dp', 'sortings', 'greedy']
t = int(input()) answer = [] for i in range(t): n, p, k = [int(j) for j in input().split()] a = [int(j) for j in input().split()] a.sort() j = 2 if p - a[0] < 0: v = 0 else: v = 1 p -= a[0] while v and j < n and p - a[j] >= 0: p -= a[j] v += 2 ...
Python
[ "other" ]
1,969
571
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,449
cd6bc23ea61c43b38c537f9e04ad11a6
Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.You are to determine the minimum possible nu...
['implementation']
from sys import stdin n, m = map(int, stdin.readline().strip().split()) u = float('inf') d = -float('inf') l = float('inf') r = -float('inf') num_blacks = 0 for i in xrange(n): row = stdin.readline().strip() for j in xrange(m): if row[j] == 'B': if i <= u: u = i ...
Python
[ "other" ]
560
619
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
786
c5c048a25ca6b63f00b20f23f0adcda6
Let's look at the following process: initially you have an empty stack and an array s of the length l. You are trying to push array elements to the stack in the order s_1, s_2, s_3, \dots s_{l}. Moreover, if the stack is empty or the element at the top of this stack is not equal to the current element, then you just pu...
['data structures', 'dp']
# encoding: utf-8 from sys import stdin def solve(a): # root node of tries denotes empty stack stack = [None] node_stack = [[1, {}]] trie = node_stack[-1] counter = 0 for i in range(len(a)): el = a[i] if len(stack) == 0 or stack[-1] != el: current_node = node_sta...
Python
[ "other" ]
1,672
1,027
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,197
6a260fc32ae8153a2741137becc6cfb4
This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For exa...
['constructive algorithms', 'implementation', 'brute force']
ans=[] for i in range(10): print(str(i)*4) A=input() if A!='0 0':ans.append(i) import itertools A=list(itertools.permutations(ans)) for i in A: print(''.join(map(str,list(i)))) a=input() if a=='4 0':break
Python
[ "other" ]
2,553
230
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,121
f13f27a131b9315ebbb8688e2f43ddde
The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi me...
['binary search']
n = int(input()) xs = list(map(int, input().split())) vs = list(map(int, input().split())) l = 0 r = max(xs) - min(xs) + 1 for i in range(50): m = (r + l) / 2 lev = 0 prav = 1000000000 for j in range(n): prav = min(prav, xs[j] + vs[j] * m) lev = max(lev, xs[j] - vs[j] * m) if pr...
Python
[ "other" ]
568
423
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
3,188
41bdb08253cf5706573f5d469ab0a7b3
Vasya, or Mr. Vasily Petrov is a dean of a department in a local university. After the winter exams he got his hands on a group's gradebook.Overall the group has n students. They received marks for m subjects. Each student got a mark from 1 to 9 (inclusive) for each subject.Let's consider a student the best at some sub...
['implementation']
def Golden_Boys_Girls(students,subjects,matrix): The_Best = []; results = [] for i in range(subjects): Current_Max_Score = 0 for j in range(students): if matrix[j][i] > Current_Max_Score: if j in results: The_Best = [] elif j not in...
Python
[ "other" ]
538
967
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,203
b856eafe350fccaabd39b97fb18d9c2f
There are n pieces of cake on a line. The i-th piece of cake has weight a_i (1 \leq i \leq n).The tastiness of the cake is the maximum total weight of two adjacent pieces of cake (i. e., \max(a_1+a_2,\, a_2+a_3,\, \ldots,\, a_{n-1} + a_{n})).You want to maximize the tastiness of the cake. You are allowed to do the foll...
['greedy', 'implementation', 'sortings']
t = int(input()) for arrow in range(t): n = int(input()) a = list(map(int, input().split())) mx1, mx2 = 0, 0 for i in a: if mx1 <= i: mx2 = mx1 mx1 = i if i < mx1: mx2 = max(i, mx2) print(mx1 + mx2)
Python
[ "other" ]
1,233
285
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,377
0870110338a84f76d4870d06dc9da2df
Vasya learns to type. He has an unusual keyboard at his disposal: it is rectangular and it has n rows of keys containing m keys in each row. Besides, the keys are of two types. Some of the keys have lowercase Latin letters on them and some of the keys work like the "Shift" key on standard keyboards, that is, they make ...
['implementation']
from collections import Counter as cnt def dis(ls, shifts, z): x, y = ls[0], ls[1] ans = [] for shift in shifts: x1, y1 = shift[0], shift[1] a = (x - x1) ** 2 b = (y - y1) ** 2 d = (a + b) ** 0.5 if d > z: ans.append(True) else: an...
Python
[ "other" ]
1,121
1,307
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
323
3d6151b549bd52f95ab7fdb972e6fb98
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis!The computers bought for the room were different. Some of them had only USB po...
['two pointers', 'implementation', 'sortings', 'greedy']
a, b, c = map(int, raw_input().split()) m = int(raw_input()) u = [] p = [] for _ in xrange(m) : s = list(raw_input().split()) if s[1] == "USB" : u.append(int(s[0])) else : p.append(int(s[0])) u.sort() p.sort() a = min(a, len(u)) b = min(b, len(p)) um = sorted(u[a:] + p[b:]) c = min(c, len(...
Python
[ "other" ]
920
394
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,005
fa7a44fd24fa0a8910cb7cc0aa4f2155
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and th...
['greedy']
# FILE: caseOfZerosAndOnes.py # CodeForces 556A def main(): length = int(input()) string = input() countZero = 0 countOne = 0 for i in range(length): if(string[i] == '0'): countZero += 1 else: countOne += 1 min = countZero if(countOne < countZer...
Python
[ "other" ]
638
386
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
2,514
dc93c41e70c7eb82af407359b194d28a
In the evening Polycarp decided to analyze his today's travel expenses on public transport.The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop to ...
['implementation', 'sortings', 'greedy']
def main(): trips, reg, cheap, cards, card_cost = map(int, input().split()) costs = [] indexes = {} total = 0 last = "" for i in range(trips): a, b = input().split() pair = (min(a, b), max(a, b)) if pair in indexes: index = indexes[pair] else: costs.append(0) indexes[pai...
Python
[ "other" ]
1,668
661
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
911
337b6d2a11a25ef917809e6409f8edef
Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the ...
['dp', 'implementation', 'greedy', 'brute force']
from io import StringIO import sys data = sys.stdin def score_line(row): score = 0 max_score = 0 combo = False for b in row: if b == 1: if combo: score += 1 else: score = 1 combo = True else: score = 0 ...
Python
[ "other" ]
916
789
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
1,739
9b277feec7952947357b133a152fd599
After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. ...
['greedy']
n = int(input()) scores = [] for _ in range(3): letters = {} word = input() if n >= len(word) - 1 and n > 1: print("Draw") exit(0) for c in word: if c in letters: letters[c] += 1 else: letters[c] = 1 scores.append(max(letters.values()) + n) ...
Python
[ "other" ]
1,530
663
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
183
99e5c907b623c310d6f1599f485ca21d
n heroes fight against each other in the Arena. Initially, the i-th hero has level a_i.Each minute, a fight between two different heroes occurs. These heroes can be chosen arbitrarily (it's even possible that it is the same two heroes that were fighting during the last minute).When two heroes of equal levels fight, nob...
['implementation', 'sortings']
m=int(input()) for i in range(m): n=int(input()) ar=[int(x) for x in input().split()] c=0 for i in range(0,len(ar)): for j in range(0,len(ar)): if(ar[i]>ar[j]): c+=1 break print(c)
Python
[ "other" ]
874
230
0
0
0
0
0
0
0
0
{ "games": 0, "geometry": 0, "graphs": 0, "math": 0, "number theory": 0, "probabilities": 0, "strings": 0, "trees": 0 }
4,009