source stringclasses 4
values | task_type stringclasses 1
value | in_source_id stringlengths 0 138 | problem_statement stringlengths 219 13.2k | gold_standard_solution stringlengths 0 413k | problem_id stringlengths 5 10 | metadata dict | verification_info dict |
|---|---|---|---|---|---|---|---|
apps | verifiable_code | 2158 | Solve the following coding problem using the programming language python:
Consider a sequence [a_1, a_2, ... , a_{n}]. Define its prefix product sequence $[ a_{1} \operatorname{mod} n,(a_{1} a_{2}) \operatorname{mod} n, \cdots,(a_{1} a_{2} \cdots a_{n}) \operatorname{mod} n ]$.
Now given n, find a permutation of [1, ... | ```python
def comp(x):
for i in range(2, x):
if x % i == 0:
return True
return False
N = int(input())
if N == 4:
print('YES', '1', '3', '2', '4', sep = '\n')
elif comp(N):
print('NO')
else:
print('YES', '1', sep = '\n')
if N > 1:
for i in range(2, N):
pr... | vfc_6178 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/487/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\n",
"output": "YES\n1\n2\n5\n6\n3\n4\n7\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n",
"output": "NO\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2159 | Solve the following coding problem using the programming language python:
Polycarp is making a quest for his friends. He has already made n tasks, for each task the boy evaluated how interesting it is as an integer q_{i}, and the time t_{i} in minutes needed to complete the task.
An interesting feature of his quest ... | ```python
from collections import defaultdict
def __starting_point():
n, T = [int(_) for _ in input().split()]
data = defaultdict(list)
for i in range(n):
t, q = [int(_) for _ in input().split()]
data[T - t].append(q)
prev_level = []
for level_id in range(1, T + 1):
level ... | vfc_6182 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/542/F",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 5\n1 1\n1 1\n2 2\n3 3\n4 4\n",
"output": "11\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 5\n4 1\n4 2\n4 3\n4 4\n4 5\n",
"output": "9\n",
"type": "stdin_stdout"
},
... |
apps | verifiable_code | 2160 | Solve the following coding problem using the programming language python:
Polycarp took $n$ videos, the duration of the $i$-th video is $a_i$ seconds. The videos are listed in the chronological order, i.e. the $1$-st video is the earliest, the $2$-nd video is the next, ..., the $n$-th video is the last.
Now Polycarp ... | ```python
import sys
n,k = map(int, sys.stdin.readline().split())
arr = list(map(int, sys.stdin.readline().split()))
tot = sum(arr)
if tot % k != 0:
print('No')
return
tot //= k
idx,cur = 0,0
ans = []
for i in range(n):
cur += arr[i]
idx += 1
if cur == tot:
ans.append(idx)
idx = 0
cur = 0
elif cur > tot:
... | vfc_6186 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1070/K",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6 3\n3 3 1 4 1 6\n",
"output": "Yes\n2 3 1 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 3\n1 1 1\n",
"output": "Yes\n1 1 1 ",
"type": "stdin_stdout"
},
{
"fn_... |
apps | verifiable_code | 2161 | Solve the following coding problem using the programming language python:
Just to remind, girls in Arpa's land are really nice.
Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight w_{i} and some beauty b_{i}. Also each Hos may have some friends. Hoses are divided in some fri... | ```python
f = lambda: map(int, input().split())
n, m, w = f()
wb = [(0, 0)] + list(zip(f(), f()))
t = list(range(n + 1))
def g(x):
if x == t[x]: return x
t[x] = g(t[x])
return t[x]
for i in range(m):
x, y = f()
x, y = g(x), g(y)
if x != y: t[y] = x
p = [[] for j in range(n + 1)]
for i in ra... | vfc_6190 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/741/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 1 5\n3 2 5\n2 4 2\n1 2\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 2 11\n2 4 6 6\n6 4 2 1\n1 2\n2 3\n",
"output": "7\n",
"type": "stdin_stdout"
},
... |
apps | verifiable_code | 2162 | Solve the following coding problem using the programming language python:
Consider a tree $T$ (that is, a connected graph without cycles) with $n$ vertices labelled $1$ through $n$. We start the following process with $T$: while $T$ has more than one vertex, do the following:
choose a random edge of $T$ equiprobab... | ```python
maxn=50+10
g=[None]*maxn
dp=[None]*maxn
c=[None]*maxn
size=[0]*maxn
for i in range(0,maxn):
c[i]=[0]*maxn
c[i][0]=1
for j in range(1,i+1):
c[i][j]=c[i-1][j-1]+c[i-1][j]
n=int(input())
for i in range(1,n+1):
g[i]=[]
for i in range(1,n):
u,v=input().split()
u=int(u)
v=int(v... | vfc_6194 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1060/F",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 2\n1 3\n1 4\n",
"output": "0.1250000000\n0.2916666667\n0.2916666667\n0.2916666667\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"output": "... |
apps | verifiable_code | 2163 | Solve the following coding problem using the programming language python:
Koa the Koala has a binary string $s$ of length $n$. Koa can perform no more than $n-1$ (possibly zero) operations of the following form:
In one operation Koa selects positions $i$ and $i+1$ for some $i$ with $1 \le i < |s|$ and sets $s_i$ to $... | ```python
import sys
readline = sys.stdin.readline
MOD = 10**9+7
S = readline().strip().split('1')
if len(S) == 1:
print(len(S[0]))
else:
S = [len(s)+1 for s in S]
ans = S[0]*S[-1]
S = S[1:-1]
dp = [0]*(max(S)+2)
dp[0] = 1
for ai in S:
res = 0
rz = 0
for i in ra... | vfc_6198 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1383/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "000\n",
"output": "3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "0101\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "000111... |
apps | verifiable_code | 2164 | Solve the following coding problem using the programming language python:
Everybody seems to think that the Martians are green, but it turns out they are metallic pink and fat. Ajs has two bags of distinct nonnegative integers. The bags are disjoint, and the union of the sets of numbers in the bags is $\{0,1,…,M-1\}$,... | ```python
import sys
input = sys.stdin.readline
def main():
n, m = map(int, input().split())
a = list(map(int, input().split())) + [0]*500000
ans_S = 0
a[n] = a[0] + m
s = [0]*600600
for i in range(n):
s[i] = a[i + 1] - a[i]
s[n] = -1
for i in range(n):
s[2*n - i] = s[i]... | vfc_6202 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1045/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 5\n3 4\n",
"output": "1\n2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 1000000000\n5 25 125 625\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name"... |
apps | verifiable_code | 2165 | Solve the following coding problem using the programming language python:
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an h × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white a... | ```python
def init_factorials(N, mod):
f = 1
fac = [1] * N
for i in range(1, N):
f *= i
f %= mod
fac[i] = f
return fac
def init_inv(N, mod, fac):
b = bin(mod-2)[2:][-1::-1]
ret = 1
tmp = fac[N]
if b[0] == '1':
ret = fac[N]
for bi in b[1:]:
tmp... | vfc_6206 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/559/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 4 2\n2 2\n2 3\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "100 100 3\n15 16\n16 15\n99 88\n",
"output": "545732279\n",
"type": "stdin_stdout"
},
{... |
apps | verifiable_code | 2166 | Solve the following coding problem using the programming language python:
Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer.
In this game there are n objects numbered from 1 to n arranged in... | ```python
f = lambda: list(map(int, input().split()))[1:]
n = int(input())
s, p, q = [], [], []
for x in [0, 1]:
r = f()
s.append(r)
t = [len(r)] * n
t[0] = 0
p.append(t)
q.append((x, 0))
while q:
x, i = q.pop()
y = 1 - x
for d in s[y]:
j = (i - d) % n
if p[y][j] < 1:... | vfc_6210 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/786/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n2 3 2\n3 1 2 3\n",
"output": "Lose Win Win Loop\nLoop Win Win Win\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8\n4 6 2 3 4\n2 3 6\n",
"output": "Win Win Win Win Win Win Win\nL... |
apps | verifiable_code | 2167 | Solve the following coding problem using the programming language python:
A function $f : R \rightarrow R$ is called Lipschitz continuous if there is a real constant K such that the inequality |f(x) - f(y)| ≤ K·|x - y| holds for all $x, y \in R$. We'll deal with a more... discrete version of this term.
For an array $... | ```python
def read_data():
n, q = map(int, input().split())
As = list(map(int, input().split()))
LRs = []
for i in range(q):
L, R = list(map(int, input().split()))
LRs.append((L, R))
return n, q, As, LRs
def solve(n, q, As, LRs):
difs = calc_difs(As)
Ls = get_Ls(difs)
Rs... | vfc_6214 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/601/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9\n",
"output": "17\n82\n23\n210\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5\n",
"ou... |
apps | verifiable_code | 2168 | Solve the following coding problem using the programming language python:
Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists of n s... | ```python
import sys
from array import array # noqa: F401
n = int(input())
matrix = [array('i', list(map(int, input().split()))) for _ in range(n)]
aa = tuple([int(x) - 1 for x in input().split()])
ans = [''] * n
for i in range(n-1, -1, -1):
x = aa[i]
for a in range(n):
for b in range(n):
... | vfc_6218 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/295/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1\n0\n1\n",
"output": "0 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n0 5\n4 0\n1 2\n",
"output": "9 0 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2169 | Solve the following coding problem using the programming language python:
You are given a prime number $p$, $n$ integers $a_1, a_2, \ldots, a_n$, and an integer $k$.
Find the number of pairs of indexes $(i, j)$ ($1 \le i < j \le n$) for which $(a_i + a_j)(a_i^2 + a_j^2) \equiv k \bmod p$.
-----Input-----
The firs... | ```python
def check(num1, num2, p, k):
v = num1 + num2
v *= num1 * num1 + num2 * num2
v %= p
v += p
v %= p
return v == k % p
def __starting_point():
n, p, k = (int(x) for x in input().split())
idx___number = [int(x) for x in input().split()]
idx___precount = [((pow(x, 4, p) - k * ... | vfc_6222 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1188/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 3 0\n0 1 2\n",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 7 2\n1 2 3 4 5 6\n",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2170 | Solve the following coding problem using the programming language python:
There are some rabbits in Singapore Zoo. To feed them, Zookeeper bought $n$ carrots with lengths $a_1, a_2, a_3, \ldots, a_n$. However, rabbits are very fertile and multiply very quickly. Zookeeper now has $k$ rabbits and does not have enough ca... | ```python
import heapq
def sum_sqaure(a, k):
q, r = divmod(a, k)
return q**2 * (k-r) + (q+1)**2 * r
def diff(a, k):
return sum_sqaure(a, k+1) - sum_sqaure(a, k)
n, k = map(int, input().split())
nums = list(map(int, input().split()))
curr = sum(sum_sqaure(a, 1) for a in nums)
Q = [(diff(a, 1), a, 1) for ... | vfc_6226 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1428/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 6\n5 3 1\n",
"output": "15\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 4\n19\n",
"output": "91\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"inp... |
apps | verifiable_code | 2171 | Solve the following coding problem using the programming language python:
Iahub is very proud of his recent discovery, propagating trees. Right now, he invented a new tree, called xor-tree. After this new revolutionary discovery, he invented a game for kids which uses xor-trees.
The game is played on a tree having n ... | ```python
import sys
def dfs(tree, root, priv_root, cur_lvl, priv_lvl, diff, pick_list):
if not tree:
return
stack = [(root, priv_root, cur_lvl, priv_lvl)]
while stack:
(root, priv_root, cur_lvl, priv_lvl) = stack.pop()
if cur_lvl ^ diff[root]:
cur_lvl ^= 1
p... | vfc_6230 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/429/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "10\n2 1\n3 1\n4 2\n5 1\n6 2\n7 5\n8 6\n9 8\n10 5\n1 0 1 1 0 1 0 1 0 1\n1 0 1 0 0 1 1 1 0 1\n",
"output": "2\n4\n7\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "15\n2 1\n3 2\n4 3\n5 4\n6 ... |
apps | verifiable_code | 2172 | Solve the following coding problem using the programming language python:
Let's assume that v(n) is the largest prime number, that does not exceed n;
u(n) is the smallest prime number strictly greater than n.
Find $\sum_{i = 2}^{n} \frac{1}{v(i) u(i)}$.
-----Input-----
The first line contains integer t (1 ≤ t... | ```python
def prime(n):
m = int(n ** 0.5) + 1
t = [1] * (n + 1)
for i in range(3, m):
if t[i]: t[i * i :: 2 * i] = [0] * ((n - i * i) // (2 * i) + 1)
return [2] + [i for i in range(3, n + 1, 2) if t[i]]
def gcd(a, b):
c = a % b
return gcd(b, c) if c else b
p = prime(31650)
def g(n):
... | vfc_6234 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/396/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n2\n3\n",
"output": "1/6\n7/30\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1000000000\n",
"output": "999999941999999673/1999999887999999118\n",
"type": "stdin_stdout"
... |
apps | verifiable_code | 2173 | Solve the following coding problem using the programming language python:
Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types: ... | ```python
from itertools import starmap
def main():
n, q = list(map(int, input().split()))
a = list(range(n + 1))
flipped = False
start = 0
end = n
for _ in range(q):
cmd, *args = list(map(int, input().split()))
if cmd == 1:
p = args[0]
if p > end-sta... | vfc_6238 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/461/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7 4\n1 3\n1 2\n2 0 1\n2 1 2\n",
"output": "4\n3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 9\n2 2 9\n1 1\n2 0 1\n1 8\n2 0 8\n1 2\n2 1 3\n1 4\n2 2 4\n",
"output": "7\n2\n10\n4... |
apps | verifiable_code | 2174 | Solve the following coding problem using the programming language python:
Polycarpus has a sequence, consisting of n non-negative integers: a_1, a_2, ..., a_{n}.
Let's define function f(l, r) (l, r are integer, 1 ≤ l ≤ r ≤ n) for sequence a as an operation of bitwise OR of all the sequence elements with indexes from ... | ```python
n, p, q = input(), set(), set()
for i in map(int, input().split()):
q = set(i | j for j in q)
q.add(i)
p.update(q)
print(len(p))
``` | vfc_6242 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/243/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1 2 0\n",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n1 2 3 4 5 6 1 2 9 10\n",
"output": "11",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2175 | Solve the following coding problem using the programming language python:
After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Be... | ```python
buck = [[0, 0] for i in range(2201)]
m = int(input())
for i in range(m):
a = int(input())
ok = True
br = 0
for j in range(2200, -1, -1):
if a & (1 << j):
if(buck[j][0]):
a ^= buck[j][0]
br ^= buck[j][1]
else:
ok =... | vfc_6246 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/504/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\n7\n6\n5\n4\n3\n2\n1\n",
"output": "0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n5\n5\n",
"output": "0\n1 0\n",
"type": "stdin_std... |
apps | verifiable_code | 2176 | Solve the following coding problem using the programming language python:
Let's consider a simplified version of order book of some stock. The order book is a list of orders (offers) from people that want to buy or sell one unit of the stock, each order is described by direction (BUY or SELL) and price.
At every mome... | ```python
import heapq
n = int(input())
buy = [] # negative
sell = []
unknown = []
res = 1
for i in range(n):
cmd, amount = input().strip().split()
amount = int(amount)
if cmd == 'ADD':
if sell and sell[0] < amount:
heapq.heappush(sell, amount)
elif buy and -buy[0] > amount:
... | vfc_6250 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1028/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6\nADD 1\nACCEPT 1\nADD 2\nACCEPT 2\nADD 3\nACCEPT 3\n",
"output": "8\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\nADD 1\nADD 2\nADD 3\nACCEPT 2\n",
"output": "2\n",
"typ... |
apps | verifiable_code | 2177 | Solve the following coding problem using the programming language python:
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.
Vasily decided to sort the cards. To do... | ```python
n = int(input())
s = list(map(int,input().split(' ')))
a = []
for i in range(max(s)):
a.append([])
for i in range(len(s)):
a[s[i]-1].append(i)
a = list([x for x in a if x != []])
if len(a) > 1:
for i in range(1,len(a)):
if len(a[i]) > 1:
s = a[i-1][-1]
if s > a[... | vfc_6254 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/830/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n6 3 1 2\n",
"output": "7\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1000\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input... |
apps | verifiable_code | 2178 | Solve the following coding problem using the programming language python:
The Little Elephant loves permutations of integers from 1 to n very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1, 2, 3, ..., ... | ```python
inp = input().split(' ')
val=[];
totNums = int(inp[0]); totOpt = int(inp[1]);
inp = input().split(' '); #assert(len(inp) == totNums);
for it in inp: val.append(int(it))
dp = [[0.0 for _ in range(0,totNums)] for __ in range(0,totNums)]
for i in range(0,totNums):
for j in range(0,totNums):
if val[... | vfc_6258 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/258/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 1\n1 2\n1 2\n",
"output": "0.500000000\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 3\n1 3 2 4\n1 2\n2 3\n1 4\n",
"output": "3.000000000\n",
"type": "stdin_stdout"
}... |
apps | verifiable_code | 2179 | Solve the following coding problem using the programming language python:
A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes.
Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from ... | ```python
q, b, c = list(map(int, input().split()))
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in a:
if b < i < c:
ans += 1
print(ans)
``` | vfc_6262 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/794/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 3 7\n8\n4 7 5 5 3 6 2 8\n",
"output": "4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 5 7\n5\n1 5 7 92 3\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"... |
apps | verifiable_code | 2180 | Solve the following coding problem using the programming language python:
There is an automatic door at the entrance of a factory. The door works in the following way: when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, when o... | ```python
def solve():
n1, m, a, d = list(map(int, input().split()))
t = list(map(int, input().split()))
from bisect import insort
from math import floor
insort(t, a * n1)
pred = 0
k = 0
kpred = 0
n = 0
step = d // a + 1
sol = 0
fl = 0
for i in t:
if (i > pred... | vfc_6266 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/883/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 1 3 4\n7\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 3 4 2\n7 9 11\n",
"output": "4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2181 | Solve the following coding problem using the programming language python:
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will... | ```python
n = int(input())
a = list(map(int, input().split()))
lp,rp = [0 for i in range(n)],[0 for i in range(n)]
lnr, rnr = [a[i] for i in range(n)],[a[i] for i in range(n)]
mx = a[0]
for i in range(1,n):
if a[i] > mx:
mx = a[i]
lp[i] = lp[i-1]
else:
mx += 1
lp[i] = lp[i-1] +... | vfc_6270 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/847/H",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n1 4 3 2 5\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n1 2 2 2 1\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2182 | Solve the following coding problem using the programming language python:
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided t... | ```python
oleg = input()
igor = input()
oleg = sorted(list(oleg))
igor = sorted(list(igor))
n = len(oleg)
oleg_turns = (n + 1) // 2
igor_turns = n // 2
min_oleg_id = 0
min_igor_id = n - igor_turns
ans = ['?'] * n
max_oleg_id = oleg_turns - 1
max_igor_id = n - 1
curr_turn = 'o'
next_turn = {'o' : 'i', 'i' : 'o'}
l_ans ... | vfc_6274 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/794/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "tinkoff\nzscoder\n",
"output": "fzfsirk\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "xxxxxx\nxxxxxx\n",
"output": "xxxxxx\n",
"type": "stdin_stdout"
},
{
"fn_n... |
apps | verifiable_code | 2183 | Solve the following coding problem using the programming language python:
We start with a string $s$ consisting only of the digits $1$, $2$, or $3$. The length of $s$ is denoted by $|s|$. For each $i$ from $1$ to $|s|$, the $i$-th character of $s$ is denoted by $s_i$.
There is one cursor. The cursor's location $\ell... | ```python
import sys
mod = 10**9 + 7
for _ in range(int(input())):
x = int(input())
s = list(map(int, input()))
ans = len(s)
for i in range(1, x+1):
ans = (i + (ans-i) * s[i-1]) % mod
r = len(s)
for _ in range(s[i-1]-1):
if len(s) < x:
s += s[i:r]
... | vfc_6278 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1280/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n5\n231\n7\n2323\n6\n333\n24\n133321333\n",
"output": "25\n1438\n1101\n686531475\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "9\n1500\n1212\n1500\n1221\n1500\n122\n1500\n12121\n1500\n... |
apps | verifiable_code | 2184 | Solve the following coding problem using the programming language python:
This is an easier version of the next problem. In this version, $q = 0$.
A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must ... | ```python
n, _q = map(int, input().split())
mni = [-1] * 200001
mxi = [-1] * 200001
cnt = [0] * 200001
nd = 0
a = list(map(int, input().split()))
for i, v in enumerate(a):
if mni[v] == -1: mni[v] = i; nd += 1
mxi[v] = i
cnt[v] += 1
r = 0
z = 0
currmax = 0
for i, v in enumerate(a):
if i == mni[v]: z += 1... | vfc_6282 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1209/G1",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 0\n3 7 3 7 3\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 0\n1 2 1 2 3 1 1 1 50 1\n",
"output": "4\n",
"type": "stdin_stdout"
},
{
"fn_na... |
apps | verifiable_code | 2185 | Solve the following coding problem using the programming language python:
Vova and Marina love offering puzzles to each other. Today Marina offered Vova to cope with the following task.
Vova has a non-directed graph consisting of n vertices and m edges without loops and multiple edges. Let's define the operation of c... | ```python
n, m = list(map(int, input().split()))
g = [[] for _ in range(n)]
for i in range(m):
p, q = list(map(int, input().split()))
g[p - 1].append(q - 1)
g[q - 1].append(p - 1)
comp = [-1] * n
def shortest(root):
dist = [-1] * n
q = [0] * n
left, right = 0, 1
q[left] = root
dist[root... | vfc_6286 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/542/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 4\n1 2\n2 3\n3 4\n3 5\n",
"output": "3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 6\n1 2\n2 3\n1 3\n3 4\n2 4\n1 4\n",
"output": "-1\n",
"type": "stdin_stdout"
},
... |
apps | verifiable_code | 2186 | Solve the following coding problem using the programming language python:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree,... | ```python
MOD = 1000000007
n = int(input())
p = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
children = [[] for x in range(n)]
for i in range(1,n):
children[p[i-1]].append(i)
#print(children)
count = [(0,0) for i in range(n)]
for i in reversed(list(range(n))):
prod = 1
for ch... | vfc_6290 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/461/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n0 0\n0 1 1\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n0 1 1 0 4\n1 1 0 0 1 0\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name... |
apps | verifiable_code | 2187 | Solve the following coding problem using the programming language python:
Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store.
Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the gre... | ```python
"""
Codeforces Round 257 Div 1 Problem C
Author : chaotic_iak
Language: Python 3.3.4
"""
def read(mode=2):
# 0: String
# 1: List of strings
# 2: List of integers
inputs = input().strip()
if mode == 0:
return inputs
if mode == 1:
return inputs.split()
if mode == 2... | vfc_6294 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/449/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6\n",
"output": "2\n6 3\n2 4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "9\n",
"output": "3\n9 3\n2 4\n6 8\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2188 | Solve the following coding problem using the programming language python:
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: + a_{i} — add non-negative integer a_{i}... | ```python
from sys import stdin
def main():
cnt = [0] * 2 ** 18
t = str.maketrans("0123456789", "0101010101")
_, *l = stdin.read().splitlines()
for sign, s in map(str.split, l):
if sign == '?':
print(cnt[int(s, 2)])
else:
cnt[int(s.translate(t), 2)] += 1 if sign... | vfc_6298 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/713/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "12\n+ 1\n+ 241\n? 1\n+ 361\n- 241\n? 0101\n+ 101\n? 101\n- 101\n? 101\n+ 4000\n? 0\n",
"output": "2\n1\n2\n1\n1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n+ 200\n+ 200\n- 200\n? 0\... |
apps | verifiable_code | 2189 | Solve the following coding problem using the programming language python:
T is playing a game with his friend, HL.
There are $n$ piles of stones, the $i$-th pile initially has $a_i$ stones.
T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a s... | ```python
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int,input().split()))
sumA = sum(a)
TWins = False
for elem in a:
if elem > sumA // 2:
TWins = True
break
if TWins or sumA % 2 != 0:
print("T")
else:
print("HL")
``` | vfc_6302 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1396/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1\n2\n2\n1 1\n",
"output": "T\nHL\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n4\n2 3 1 2\n",
"output": "HL\n",
"type": "stdin_stdout"
},
{
"fn_name": nu... |
apps | verifiable_code | 2190 | Solve the following coding problem using the programming language python:
There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At each step every psycho who has an id greater than the psycho to his right (if exists) kills his right neighbor in the line. Note that a psycho might... | ```python
n = int(input())
ans = 0
stk = []
for v in map(int, input().split()):
last = 0
while len(stk) and stk[-1][0] < v and stk[-1][1]:
last = max(last, stk[-1][1])
del stk[-1]
if not len(stk) or stk[-1][0] < v:
stk.append((v, 0))
else:
stk.append((v, last + 1)); ans ... | vfc_6306 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/319/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "10\n10 9 7 8 6 5 3 4 2 1\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n1 2 3 4 5 6\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name... |
apps | verifiable_code | 2191 | Solve the following coding problem using the programming language python:
You are given a sequence a consisting of n integers. Find the maximum possible value of $a_{i} \operatorname{mod} a_{j}$ (integer remainder of a_{i} divided by a_{j}), where 1 ≤ i, j ≤ n and a_{i} ≥ a_{j}.
-----Input-----
The first line conta... | ```python
def main():
input()
aa = sorted(map(int, input().split()))
maxa = max(aa)
m = [False] * (maxa + 1)
x = []
b = 0
for a in aa:
if b != a:
m[a] = True
for i in range(b, a):
x.append(b)
b = a
x.append(b)
ans = 0
fo... | vfc_6310 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/484/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n3 4 5\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 2 4\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input"... |
apps | verifiable_code | 2192 | Solve the following coding problem using the programming language python:
There are n beacons located at distinct positions on a number line. The i-th beacon has position a_{i} and power level b_{i}. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within dis... | ```python
n = int(input())
pos_blast = [list(map(int, input().split())) for _ in range(n)]
MAX_N = max(pos_blast, key=lambda x: x[0])[0] + 2
power = [0 for _ in range(MAX_N)]
tower = [False for _ in range(MAX_N)]
can_destroy = [0 for _ in range(MAX_N)]
for pos, blast in pos_blast:
pos += 1
tower[pos] = True
... | vfc_6314 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/607/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 9\n3 1\n6 1\n7 4\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n",
"output": "3\n",
"type": "stdin_stdout"
},
... |
apps | verifiable_code | 2193 | Solve the following coding problem using the programming language python:
John Smith knows that his son, Thomas Smith, is among the best students in his class and even in his school. After the students of the school took the exams in English, German, Math, and History, a table of results was formed.
There are $n$ stu... | ```python
def main():
n = int(input())
scores = []
for i in range(n):
a = list(map(int, input().split()))
tot = sum(a)
scores.append((-tot, i))
scores.sort()
for i in range(n):
if scores[i][1] == 0:
print(i + 1)
main()
``` | vfc_6318 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1017/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n100 98 100 100\n100 100 100 100\n100 100 99 99\n90 99 90 100\n100 98 60 99\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n100 80 90 99\n60 60 60 60\n90 60 100 60\n6... |
apps | verifiable_code | 2194 | Solve the following coding problem using the programming language python:
Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number... | ```python
n = int(input())
pos,tree,ans,sz = list(map(int,input().split())) if n > 1 else [],[],[],[]
for i in range(n):
tree.append([])
ans.append(0.0)
sz.append(0)
for i in range(n-1):
tree[pos[i]-1].append(i+1)
for i in range(n)[::-1]:
sz[i] = 1
for to in tree[i]:
sz[i] += sz[to]
for i in range(n)... | vfc_6322 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/696/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\n1 2 1 1 4 4\n",
"output": "1.0 4.0 5.0 3.5 4.5 5.0 5.0 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "12\n1 1 2 2 4 4 3 3 1 10 8\n",
"output": "1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7... |
apps | verifiable_code | 2195 | Solve the following coding problem using the programming language python:
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such in... | ```python
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
res = 0
cur = 0
for i in sorted(c.keys()):
d = min(c[i], cur)
cur -= d
res += d
cur += c[i]
print(res)
``` | vfc_6326 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1007/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\n10 1 1 1 5 5 3\n",
"output": "4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n1 1 1 1 1\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2196 | Solve the following coding problem using the programming language python:
You are given a string S of length n with each character being one of the first m lowercase English letters.
Calculate how many different strings T of length n composed from the first m lowercase English letters exist such that the length of L... | ```python
n, m = list(map(int, input().split()))
s = input()
p = c = 0
for i in range(1, n):
if s[i] == s[i - 1]:
c += n * (m - 1)
p = i
elif s[i] != s[i - 2]:
p = i - 1
c += i - p
ans = n * n * (m - 1) - c
print(ans)
``` | vfc_6330 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/578/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 3\naaa\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 3\naab\n",
"output": "11\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input... |
apps | verifiable_code | 2197 | Solve the following coding problem using the programming language python:
You are given a string A. Find a string B, where B is a palindrome and A is a subsequence of B.
A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the ord... | ```python
a = input()
b = a[::-1]
print(a + b)
``` | vfc_6334 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/932/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "aba\n",
"output": "abaaba",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "ab\n",
"output": "abba",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "abca... |
apps | verifiable_code | 2198 | Solve the following coding problem using the programming language python:
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet.
For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "... | ```python
n = int(input())
s = set()
for a in range(n):
name = input()
name = name.replace('u', 'oo')
while (name.count('kh') > 0):
name = name.replace('kh', 'h')
s.add(name)
print(len(s))
``` | vfc_6338 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/883/F",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon\n",
"output": "4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "9\nhariton\nhkariton\nbuoi\nkkkharito... |
apps | verifiable_code | 2199 | Solve the following coding problem using the programming language python:
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
[Image]
T... | ```python
import sys
input = sys.stdin.readline
n, k = list(map(int, input().split()))
begin = [-1] * n
end = [-1] * n
hurt = [-1] * n
adj = [[] for i in range(n)]
for _ in range(n-1):
u ,v = list(map(int, input().split()))
adj[u-1].append(v-1)
adj[v-1].append(u-1)
hurt[0] = 1
begin[0] = 0
stack = [0]
c... | vfc_6342 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1336/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7 4\n1 2\n1 3\n1 4\n3 5\n3 6\n4 7\n",
"output": "7",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 1\n1 2\n1 3\n2 4\n",
"output": "2",
"type": "stdin_stdout"
},
{
... |
apps | verifiable_code | 2200 | Solve the following coding problem using the programming language python:
Catherine received an array of integers as a gift for March 8. Eventually she grew bored with it, and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with ... | ```python
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = a
ans = 0
for k in range(29):
a0 = []
a1 = []
a0a = a0.append
a1a = a1.append
b0 = []
b1 = []
b0a = b0.append
b1a = b1.append
for i in a:
if i&(1<<k): a1a(i)
... | vfc_6346 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1322/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1 2\n",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 2 3\n",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n... |
apps | verifiable_code | 2201 | Solve the following coding problem using the programming language python:
You are given a sequence of n integers a_1, a_2, ..., a_{n}.
Determine a real number x such that the weakness of the sequence a_1 - x, a_2 - x, ..., a_{n} - x is as small as possible.
The weakness of a sequence is defined as the maximum value... | ```python
import sys
n = int(sys.stdin.readline())
a = [int(x) for x in sys.stdin.readline().split()]
eps = 1e-12
def f(x):
mx = a[0] - x
tsmx = 0.0
mn = a[0] - x
tsmn = 0.0
for ai in a:
tsmx = max(tsmx + ai - x, ai - x)
mx = max(tsmx, mx)
tsmn = min(tsmn + ai - x, ai - x)
... | vfc_6350 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/578/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1 2 3\n",
"output": "1.000000000000000\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n1 2 3 4\n",
"output": "2.000000000000000\n",
"type": "stdin_stdout"
},
{
... |
apps | verifiable_code | 2202 | Solve the following coding problem using the programming language python:
An array of integers $p_{1},p_{2}, \ldots,p_{n}$ is called a permutation if it contains each number from $1$ to $n$ exactly once. For example, the following arrays are permutations: $[3,1,2], [1], [1,2,3,4,5]$ and $[4,3,1,2]$. The following arra... | ```python
import sys
input = sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
BIT=[0]*(n+1)
def update(v,w):
while v<=n:
BIT[v]+=w
v+=(v&(-v))
def getvalue(v):
ANS=0
while v!=0:
ANS+=BIT[v]
v-=(v&(-v))
return ANS
for i in range(1,n+1):
update(i,... | vfc_6354 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1208/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n0 0 0\n",
"output": "3 2 1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n0 1\n",
"output": "1 2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"in... |
apps | verifiable_code | 2203 | Solve the following coding problem using the programming language python:
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as p_{i}. For all pairs of distinct integers i, j between 1 and n, he wrote the number a_{i}, j = min(p_{i}, p_{j}). He writes a_{... | ```python
def main():
n = int(input())
a = [[int(i) for i in input().split()] for j in range(n)]
result = [-1] * n
for i in range(n - 1):
for j in range(n):
d = set(a[j][k] for k in range(n) if result[k] == -1 and j != k)
if len(d) == 1:
result[j] = d... | vfc_6358 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/618/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n0 1\n1 0\n",
"output": "2 1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0\n",
"output": "2 5 4 1 3\n",
"type": "st... |
apps | verifiable_code | 2204 | Solve the following coding problem using the programming language python:
You are given a directed graph of $n$ vertices and $m$ edges. Vertices are numbered from $1$ to $n$. There is a token in vertex $1$.
The following actions are allowed: Token movement. To move the token from vertex $u$ to vertex $v$ if there i... | ```python
import sys
input = sys.stdin.readline
import heapq
mod=998244353
n,m=list(map(int,input().split()))
E=[[] for i in range(n+1)]
E2=[[] for i in range(n+1)]
for i in range(m):
x,y=list(map(int,input().split()))
E[x].append(y)
E2[y].append(x)
TIME=[1<<29]*(n+1)
TIME[1]=0
def shuku(x,y):
ret... | vfc_6362 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1442/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 4\n1 2\n2 3\n3 4\n4 1\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 3\n2 1\n2 3\n4 3\n",
"output": "10\n",
"type": "stdin_stdout"
},
{
"fn_... |
apps | verifiable_code | 2205 | Solve the following coding problem using the programming language python:
Please notice the unusual memory limit of this problem.
Orac likes games. Recently he came up with the new game, "Game of Life".
You should play this game on a black and white grid with $n$ rows and $m$ columns. Each cell is either black or wh... | ```python
def main():
import sys
from array import array
from collections import deque
input = sys.stdin.readline
H, W, Q = list(map(int, input().split()))
grid = array('b', [0] * (H*W))
#flg_0 = 0
#flg_1 = 0
for h in range(H):
line = input().rstrip('\n')
"""
... | vfc_6366 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1349/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 3 3\n000\n111\n000\n1 1 1\n2 2 2\n3 3 3\n",
"output": "1\n1\n1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 2 2\n01\n10\n01\n10\n01\n1 1 4\n5 1 4\n",
"output": "0\n0\n",
... |
apps | verifiable_code | 2206 | Solve the following coding problem using the programming language python:
Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins together. Their favorite occupation is to sort collections of coins. Sasha likes having things in order, that is why he wants his coins to be arranged ... | ```python
n = int(input())
a = list(map(int, input().split()))
p = [0] * (n + 1)
ans = [1] * (n + 1)
ind = n
for i in range(n):
p[a[i] - 1] = 1
while ind > 0 and p[ind - 1] == 1:
ind -= 1
ans[i + 1] = 1 + (i + 1) - (n - ind)
print(' '.join(map(str, ans)))
``` | vfc_6370 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/875/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 3 4 2\n",
"output": "1 2 3 2 1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8\n6 8 3 4 7 2 1 5\n",
"output": "1 2 2 3 4 3 4 5 1\n",
"type": "stdin_stdout"
},
{
... |
apps | verifiable_code | 2207 | Solve the following coding problem using the programming language python:
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to ... | ```python
n=int(input())
ta,tb,da,db=[0]*4
for i in range (n):
t,x,y=list(map(int,input().split()))
if t==1:
ta+=(x+y)
da+=y
if (t==2):
tb+=(x+y)
db+=y
if (ta-da>=0.5*ta):
print ('LIVE')
else :
print ('DEAD')
if (tb-db>=0.5*tb):
print ('LIVE')
else :
pr... | vfc_6374 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/245/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1 5 5\n2 6 4\n",
"output": "LIVE\nLIVE\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 0 10\n2 0 10\n1 10 0\n",
"output": "LIVE\nDEAD\n",
"type": "stdin_stdout"
},
... |
apps | verifiable_code | 2208 | Solve the following coding problem using the programming language python:
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are $n$ snacks flavors, numbered wit... | ```python
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def... | vfc_6378 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1209/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 4\n1 2\n4 3\n1 4\n3 4\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 5\n2 3\n2 1\n3 4\n6 5\n4 5\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
... |
apps | verifiable_code | 2209 | Solve the following coding problem using the programming language python:
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold:
For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on th... | ```python
n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
u,v = [int(i)-1 for i in input().split()]
g[u].append(v)
g[v].append(u)
leaf = [len(i)==1 for i in g]
root = -1
mx = n-1
for i in range(n):
if leaf[i]:
root = i
leafs = 0
for j in g[i]:
if leaf[j]:
... | vfc_6382 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1338/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6\n1 3\n2 3\n3 4\n4 5\n5 6\n",
"output": "1 4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n1 3\n2 3\n3 4\n4 5\n4 6\n",
"output": "3 3\n",
"type": "stdin_stdout"
},
... |
apps | verifiable_code | 2210 | Solve the following coding problem using the programming language python:
You are given a sequence a = \{a_1, ..., a_N\} with all zeros, and a sequence b = \{b_1, ..., b_N\} consisting of 0 and 1. The length of both is N.
You can perform Q kinds of operations. The i-th operation is as follows:
- Replace each of a_{l_... | ```python
import sys
input=sys.stdin.readline
n=int(input())
b=list(map(int,input().split()))
ope=[[] for i in range(n)]
Q=int(input())
for i in range(Q):
l,r=list(map(int,input().split()))
ope[r-1].append(l-1)
res=b.count(0)
Data=[(-1)**((b[i]==1)+1) for i in range(n)]
for i in range(1,n):
Data[i]+=Dat... | vfc_6386 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://atcoder.jp/contests/arc085/tasks/arc085_d",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1 0 1\n1\n1 3\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 0 1\n2\n1 1\n3 3\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name"... |
apps | verifiable_code | 2211 | Solve the following coding problem using the programming language python:
Bessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play!
More specifically, he wants to get from $(0,0)$ to $(x,0)$ by making multiple hops. He is only willing to hop... | ```python
import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:list(map(int,input().split()))
for _ in range(int(input())):
n,x=mii()
has=0
a=0
for i in mii():
if x==i: has=1
a=max(a,i)
if has:
print(1)
else:
print(max(2,(x-1)//a+1))
``` | vfc_6390 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1307/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n2 4\n1 3\n3 12\n3 4 5\n1 5\n5\n2 10\n15 4\n",
"output": "2\n3\n1\n2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n10 999999733\n25 68 91 55 36 29 96 4 63 3\n",
"output": "104... |
apps | verifiable_code | 2212 | Solve the following coding problem using the programming language python:
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he h... | ```python
import sys
[n, q] = map(int, sys.stdin.readline().strip().split())
qis = [int(sys.stdin.readline().strip()) for _ in range(q)]
def query(n, q):
d = 2 * n - q
while d % 2 == 0:
d //= 2
return (n - d // 2)
for qi in qis:
print (query(n, qi))
``` | vfc_6394 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/949/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 3\n2\n3\n4\n",
"output": "3\n2\n4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "13 4\n10\n5\n4\n8\n",
"output": "13\n3\n8\n9\n",
"type": "stdin_stdout"
},
{
"... |
apps | verifiable_code | 2213 | Solve the following coding problem using the programming language python:
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $f(0... | ```python
T = int(input())
for t in range(T):
a, b, n = [int(i) for i in input().split()]
if n%3 == 2:
print(a^b)
elif n%3 == 1:
print(b)
else:
print(a)
``` | vfc_6398 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1208/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n3 4 2\n4 5 0\n325 265 1231232\n",
"output": "7\n4\n76\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n0 0 1000000000\n1002 2003 36523\n233 5656 898989\n0 2352 0\n21132 23256 2323256... |
apps | verifiable_code | 2214 | Solve the following coding problem using the programming language python:
The country has n cities and n - 1 bidirectional roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from 1 to n inclusive.
All the roads are initially bad, but t... | ```python
class Graph:
def __init__(self, n_vertices, edges, directed=True, weighted=False):
self.n_vertices = n_vertices
self.edges = edges
self.directed = directed
self.weighted = weighted
@property
def adj(self):
try:
return self._adj
except ... | vfc_6402 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/543/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1 1\n",
"output": "4 3 3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n1 2 3 4\n",
"output": "5 8 9 8 5",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2215 | Solve the following coding problem using the programming language python:
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city u_{i} to v_{i} (and vise versa) using the i-th road, the ... | ```python
#===============================================================================================
#importing some useful libraries.
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
# from itertools import *
from heapq import *
from math i... | vfc_6406 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/449/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3\n",
"output": "2\n",
... |
apps | verifiable_code | 2216 | Solve the following coding problem using the programming language python:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q_1q_2... q_{k}. The algorithm consists of two steps:
Find any c... | ```python
import sys
s=sys.stdin.readline().split()[0]
m=int(sys.stdin.readline())
Numx=[]
Numy=[]
Numz=[]
x=0
y=0
z=0
for i in range(len(s)):
if(s[i]=='x'):
x+=1
if(s[i]=='y'):
y+=1
if(s[i]=='z'):
z+=1
Numx.append(x)
Numy.append(y)
Numz.append(z)
Ans=""
for M in ... | vfc_6410 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/367/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 6\n",
"output": "YES\nYES\nNO\nYES\nNO\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "yxzyzxzzxyyzzxxxzyyzzyzxxzxyzyyzxyzxyxxyzxyxzyzxyzxyyxzzzyzxyy... |
apps | verifiable_code | 2217 | Solve the following coding problem using the programming language python:
Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer d_{i}, which can be equal to 0, 1 or - 1. To... | ```python
import sys
n, m = list(map(int, sys.stdin.readline().split()))
d = list(map(int, sys.stdin.readline().split()))
gph = [[] for _ in range(n)]
for _ in range(m):
u, v = list(map(int, sys.stdin.readline().split()))
u -= 1
v -= 1
gph[u].append((v, _))
gph[v].append((u, _))
t = -1
if d.c... | vfc_6414 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/840/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 0\n1\n",
"output": "-1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 5\n0 0 0 -1\n1 2\n2 3\n3 4\n1 4\n2 4\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"... |
apps | verifiable_code | 2218 | Solve the following coding problem using the programming language python:
There is a country with $n$ citizens. The $i$-th of them initially has $a_{i}$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social se... | ```python
n=int(input())
a=list(map(int,input().split()))
q=int(input())
changes=[0]*q
for i in range(q):
changes[-i-1]=tuple(map(int,input().split()))
final=[-1]*n
curr=0
for guy in changes:
if guy[0]==1:
if final[guy[1]-1]==-1:
final[guy[1]-1]=max(guy[2],curr)
else:
curr=max(cu... | vfc_6418 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1198/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 2 3 4\n3\n2 3\n1 2 2\n2 1\n",
"output": "3 2 3 4 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n3 50 2 1 10\n3\n1 2 0\n2 8\n1 3 20\n",
"output": "8 8 20 8 10 \n",
"ty... |
apps | verifiable_code | 2219 | Solve the following coding problem using the programming language python:
During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now wonder... | ```python
n, q = map(int, input().split())
s = '!' + input()
nxt = [[n + 1] * (n + 2) for _ in range(26)]
for i in range(n - 1, -1, -1):
c = ord(s[i + 1]) - 97
for j in range(26):
nxt[j][i] = nxt[j][i + 1]
nxt[c][i] = i + 1
w = [[-1], [-1], [-1]]
idx = lambda i, j, k: i * 65536 + j * 256 + k
dp = ... | vfc_6422 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1149/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6 8\nabdabc\n+ 1 a\n+ 1 d\n+ 2 b\n+ 2 c\n+ 3 a\n+ 3 b\n+ 1 c\n- 2\n",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nNO\nYES\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 8\nabbaab\n+ 1 a\n+... |
apps | verifiable_code | 2220 | Solve the following coding problem using the programming language python:
Serge came to the school dining room and discovered that there is a big queue here. There are $m$ pupils in the queue. He's not sure now if he wants to wait until the queue will clear, so he wants to know which dish he will receive if he does. A... | ```python
import sys
from itertools import accumulate
class Lazysegtree:
#RAQ
def __init__(self, A, intv, initialize = True, segf = min):
#区間は 1-indexed で管理
self.N = len(A)
self.N0 = 2**(self.N-1).bit_length()
self.intv = intv
self.segf = segf
self.lazy = [0]*(2*... | vfc_6426 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1179/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 1\n1\n1\n1\n1 1 100\n",
"output": "100\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1\n1\n1\n1\n2 1 100\n",
"output": "-1\n",
"type": "stdin_stdout"
},
{
"... |
apps | verifiable_code | 2221 | Solve the following coding problem using the programming language python:
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm.
Sereja takes a blank piece of paper. Then he starts writing out the sequence in m stages. Each time he either adds a new num... | ```python
n=int(input())
a=[]
for i in range(n):
a.append(list(map(int,input().split())))
m=int(input())
b=list(map(lambda x:int(x)-1,input().split()))
c=[]
now=0
k=0
ans=[]
for i in range(n):
t=a[i]
last=now
if t[0]==1:
now+=1
if len(c)<100000: c.append(t[1])
if k<m and b[k]==no... | vfc_6430 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/380/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6\n1 1\n1 2\n2 2 1\n1 3\n2 5 2\n1 4\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n",
"output": "1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1 33085\n1... |
apps | verifiable_code | 2222 | Solve the following coding problem using the programming language python:
The mobile application store has a new game called "Subway Roller".
The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n c... | ```python
def dfs(x, y):
vis.append((x, y))
y += 1
nonlocal flag
if flag or str.isalpha(grid[x][y]):
return
if y >= n - 1:
flag = True
return
# stay idle
if not str.isalpha(grid[x][y + 1]) and not str.isalpha(grid[x][y + 2]) and (x, y + 2) not in vis:
dfs... | vfc_6434 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/585/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n16 4\n...AAAAA........\ns.BBB......CCCCC\n........DDDDD...\n16 4\n...AAAAA........\ns.BBB....CCCCC..\n.......DDDDD....\n",
"output": "YES\nNO\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"inpu... |
apps | verifiable_code | 2223 | Solve the following coding problem using the programming language python:
You've got a list of program warning logs. Each record of a log stream is a string in this format: "2012-MM-DD HH:MM:SS:MESSAGE" (without the quotes).
String "MESSAGE" consists of spaces, uppercase and lowercase English letters and characters... | ```python
# import atexit
# import io
# import sys
#
# _INPUT_LINES = sys.stdin.read().splitlines()
# input = iter(_INPUT_LINES).__next__
# _OUTPUT_BUFFER = io.StringIO()
# sys.stdout = _OUTPUT_BUFFER
#
#
# @atexit.register
# def write():
# sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
import bisect
from datetim... | vfc_6438 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/245/F",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "60 3\n2012-03-16 16:15:25: Disk size is\n2012-03-16 16:15:25: Network failute\n2012-03-16 16:16:29: Cant write varlog\n2012-03-16 16:16:42: Unable to start process\n2012-03-16 16:16:43: Disk size is too small\n2012-03-16 16:16:53: ... |
apps | verifiable_code | 2224 | Solve the following coding problem using the programming language python:
Rudolf is on his way to the castle. Before getting into the castle, the security staff asked him a question:
Given two binary numbers $a$ and $b$ of length $n$. How many different ways of swapping two digits in $a$ (only in $a$, not $b$) so tha... | ```python
n = int(input())
a = [int(x) for x in input().strip()]
b = [int(x) for x in input().strip()]
p, q, r, s = 0, 0, 0, 0
for i in range(n):
if a[i] * 2 + b[i] == 0:
p += 1
if a[i] * 2 + b[i] == 1:
q += 1
if a[i] * 2 + b[i] == 2:
r += 1
if a[i] * 2 + b[i] == 3:
s += ... | vfc_6442 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1017/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n01011\n11001\n",
"output": "4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n011000\n010011\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
"fn_name": nul... |
apps | verifiable_code | 2225 | Solve the following coding problem using the programming language python:
For strings s and t, we will say that s and t are prefix-free when neither is a prefix of the other.
Let L be a positive integer. A set of strings S is a good string set when the following conditions hold true:
- Each string in S has a length b... | ```python
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
from collections import deque
class Node:
def __init__(self,depth):
self.depth=depth
self.left=None
self.right=None
def insert(node,s):
n=node
for i in range(len(s)):
t=s[i]
if t=='0':
... | vfc_6446 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://atcoder.jp/contests/arc087/tasks/arc087_c",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 2\n00\n01\n",
"output": "Alice\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 2\n00\n11\n",
"output": "Bob\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2226 | Solve the following coding problem using the programming language python:
Zookeeper is buying a carton of fruit to feed his pet wabbit. The fruits are a sequence of apples and oranges, which is represented by a binary string $s_1s_2\ldots s_n$ of length $n$. $1$ represents an apple and $0$ represents an orange.
Since... | ```python
class SegmentTree:
def __init__(self, data, default=0, func=max):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 *... | vfc_6450 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1428/F",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n0110\n",
"output": "12\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n1101001\n",
"output": "30\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"inp... |
apps | verifiable_code | 2227 | Solve the following coding problem using the programming language python:
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 characters "1" and "+" between the original charact... | ```python
n, k = map(int, input().split())
a = [0] * n
b = ['0'] * n
c = []
s = input()
for i in range(n):
if k != 0:
if s[i] == '(':
c.append(i)
else:
d = c.pop()
a[i] = 1
a[d] = 1
k -= 2
for i in range(n):
if a[i] == 1:
print(... | vfc_6454 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1023/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6 4\n()(())\n",
"output": "()()\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8 8\n(()(()))\n",
"output": "(()(()))\n",
"type": "stdin_stdout"
},
{
"fn_name": n... |
apps | verifiable_code | 2228 | Solve the following coding problem using the programming language python:
There are $n$ persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of $m$ days. On each trip, you have to select a group of people that ... | ```python
from collections import deque
def solve(adj, m, k, uv):
n = len(adj)
nn = [len(a) for a in adj]
q = deque()
for i in range(n):
if nn[i] < k:
q.append(i)
while q:
v = q.popleft()
for u in adj[v]:
nn[u] -= 1
if nn[u] == k-1:
... | vfc_6458 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1037/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"output": "0\n0\n3\n3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"output": "0\n0\n0\n3\n3\n4\n4\n... |
apps | verifiable_code | 2229 | Solve the following coding problem using the programming language python:
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t ... | ```python
def sub(a, s):
pa = 0
ps = 0
while pa < len(a) and ps < len(s):
if a[pa] == s[ps]:
ps += 1
pa += 1
else:
pa += 1
return ps == len(s)
def subword(t, ord_ar, n):
t_copy = []
for i in range(len(ord_ar)):
if ord_ar[i] >= n:
... | vfc_6462 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/778/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "ababcba\nabb\n5 3 4 1 7 6 2\n",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "bbbabb\nbb\n1 6 3 4 2 5\n",
"output": "4",
"type": "stdin_stdout"
},
{
... |
apps | verifiable_code | 2230 | Solve the following coding problem using the programming language python:
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers p_{i}, a_{i} and b_{i}, where p_{i} is the price of the i-th t-shirt, a_{i} is front color of the i-th t-shirt and b_{i} is back color of the i-th t... | ```python
n = int(input())
p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
s = []
for i in range(n):
s.append([p[i], a[i], b[i]])
s = sorted(s)
m = int(input())
c = [int(i) for i in input().split()]
idx = [0]*4
ans = []
for i in range(m):
... | vfc_6466 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/799/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n300 200 400 500 911\n1 2 1 2 3\n2 1 3 2 1\n6\n2 3 1 2 1 1\n",
"output": "200 400 300 500 911 -1 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1000000000 1\n1 1\n1 2\n2\n2 1\n",
... |
apps | verifiable_code | 2231 | Solve the following coding problem using the programming language python:
You are an all-powerful being and you have created a rectangular world. In fact, your world is so bland that it could be represented by a $r \times c$ grid. Each cell on the grid represents a country. Each country has a dominant religion. There ... | ```python
import sys
input = sys.stdin.readline
MOD = 10**9 + 7
t = int(input())
for _ in range(t):
r, c = list(map(int, input().split()))
s = [list(input()) for i in range(r)]
cnt_a = 0
flag_kado = False
flag_hen = False
flag_hen2 = False
if s[0][0] == "A" or s[0][c-1] == "A" or s[r-1][0... | vfc_6470 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1280/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n7 8\nAAPAAAAA\nPPPPAAAA\nPPPPAAAA\nAPAAPPPP\nAPAPPAPP\nAAAAPPAP\nAAAAPPAA\n6 5\nAAAAA\nAAAAA\nAAPAA\nAAPAP\nAAAPP\nAAAPP\n4 4\nPPPP\nPPPP\nPPPP\nPPPP\n3 4\nPPPP\nPAAP\nPPPP\n",
"output": "2\n1\nMORTAL\n4\n",
"type": ... |
apps | verifiable_code | 2232 | Solve the following coding problem using the programming language python:
ZS the Coder is playing a game. There is a number displayed on the screen and there are two buttons, ' + ' (plus) and '$\sqrt{}$' (square root). Initially, the number 2 is displayed on the screen. There are n + 1 levels in the game and ZS the Co... | ```python
"""
Codeforces Round 372 Div 1 Problem A
Author : chaotic_iak
Language: Python 3.5.2
"""
################################################### SOLUTION
def main():
n, = read()
curr = 2
for lv in range(1, n+1):
tgt = (lv*(lv+1))**2
print((tgt - curr) // lv)
curr = lv*(lv+1... | vfc_6474 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/715/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n",
"output": "2\n17\n46\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n",
"output": "2\n17\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": ... |
apps | verifiable_code | 2233 | Solve the following coding problem using the programming language python:
The Bubble Cup hypothesis stood unsolved for $130$ years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem:
Give... | ```python
import os
import sys
from io import BytesIO, IOBase
def main():
pass
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mod... | vfc_6478 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1423/J",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n2 4\n",
"output": "2\n4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n9\n",
"output": "9\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "... |
apps | verifiable_code | 2234 | Solve the following coding problem using the programming language python:
Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's ... | ```python
import sys
from collections import deque
def solve():
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
writelines = sys.stdout.writelines
N = int(readline())
G = [[] for i in range(N)]
for i in range(N-1):
u, v = map(int, readline().split())
G[u-1].append(v-1)... | vfc_6482 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/613/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 3\n2 3\n4 3\n4\n2 1 2\n3 2 3 4\n3 1 2 4\n4 1 2 3 4\n",
"output": "1\n-1\n1\n-1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n1 2\n2 3\n3 4\n1 5\n5 6\n5 7\n1\n4 2 4 6 7\n",
... |
apps | verifiable_code | 2235 | Solve the following coding problem using the programming language python:
Sergey Semyonovich is a mayor of a county city N and he used to spend his days and nights in thoughts of further improvements of Nkers' lives. Unfortunately for him, anything and everything has been done already, and there are no more possible i... | ```python
def main():
def countchildren(graph,vert,memo,pard=None):
dumi=0
for child in graph[vert]:
if child!=pard:
if len(graph[child])==1:
memo[child]=0
else:
memo[child]=countchildren(graph,child,memo,vert)[0]
... | vfc_6486 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1060/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 2\n1 3\n1 4\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n1 2\n2 3\n3 4\n",
"output": "7\n",
"type": "stdin_stdout"
},
{
"fn_name": nul... |
apps | verifiable_code | 2236 | Solve the following coding problem using the programming language python:
There are $n$ lamps on a line, numbered from $1$ to $n$. Each one has an initial state off ($0$) or on ($1$).
You're given $k$ subsets $A_1, \ldots, A_k$ of $\{1, 2, \dots, n\}$, such that the intersection of any three subsets is empty. In othe... | ```python
import sys
readline = sys.stdin.readline
class UF():
def __init__(self, num):
self.par = [-1]*num
self.weight = [0]*num
def find(self, x):
if self.par[x] < 0:
return x
else:
stack = []
while self.par[x] >= 0:
stack.app... | vfc_6490 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1290/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n",
"output": "1\n2\n3\n3\n3\n3\n3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1... |
apps | verifiable_code | 2237 | Solve the following coding problem using the programming language python:
You are given a permutation $p_1, p_2, \ldots, p_n$.
In one move you can swap two adjacent values.
You want to perform a minimum number of moves, such that in the end there will exist a subsegment $1,2,\ldots, k$, in other words in the end the... | ```python
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
class Binary_Indexed_Tree():
def __init__(self, n):
self.n = n
self.data = [0]*(n+1)
def add(self, i, x):
while i <= self.n:
self.data[i] += x
i += i & -i
def get(self, i)... | vfc_6494 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1268/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n5 4 3 2 1\n",
"output": "0 1 3 6 10 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 2 3\n",
"output": "0 0 0 \n",
"type": "stdin_stdout"
},
{
"fn_name": ... |
apps | verifiable_code | 2238 | Solve the following coding problem using the programming language python:
Let's denote as $\text{popcount}(x)$ the number of bits set ('1' bits) in the binary representation of the non-negative integer x.
You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤... | ```python
def popcount(n):
res = 0
while n > 0:
res += n & 1
n >>= 2
def A(l, r):
r += 1
t = 1 << 64
while t & (l ^ r) == 0:
t >>= 1
res = l | (t - 1)
#print(t, res)
return res
def __starting_point():
"""assert(A(1, 2) == 1)
assert(A(2, 4) == 3)
assert(A(1, 10) == 7)
assert(A(13, 13) == 13)
assert(... | vfc_6498 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/484/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1 2\n2 4\n1 10\n",
"output": "1\n3\n7\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "55\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n2 2\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10... |
apps | verifiable_code | 2239 | Solve the following coding problem using the programming language python:
As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) wit... | ```python
def mat(shape, inital_val=None):
if len(shape) > 1:
return [mat(shape[1:], inital_val) for _ in range(shape[0])]
else:
return [inital_val] * shape[0]
def main():
n, m = [int(x) for x in input().split()]
graph = [{} for _ in range(n)]
for _ in range(m):
... | vfc_6502 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/917/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 4\n1 2 b\n1 3 a\n2 4 c\n3 4 b\n",
"output": "BAAA\nABAA\nBBBA\nBBBB\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 8\n5 3 h\n1 2 c\n3 1 c\n3 2 r\n5 1 r\n4 3 z\n5 4 r\n5 2 h\n",
... |
apps | verifiable_code | 2240 | Solve the following coding problem using the programming language python:
A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices.
A subtree of a tree T is a tree with both v... | ```python
n = int(input())
r = [[] for i in range(n + 1)]
r[1] = [0]
for i in range(n - 1):
a, b = map(int, input().split())
r[a].append(b)
r[b].append(a)
t = list(map(int, input().split()))
u, v = [0] * (n + 1), [0] * (n + 1)
for i, j in enumerate(t, 1):
if j < 0: u[i] = - j
else: v[i] = j
t, p = [... | vfc_6506 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/274/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1 2\n1 3\n1 -1 1\n",
"output": "3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n2 3\n4 5\n2 5\n1 3\n0 2 1 4 3\n",
"output": "8\n",
"type": "stdin_stdout"
},
{
... |
apps | verifiable_code | 2241 | Solve the following coding problem using the programming language python:
12:17 (UTC): The sample input 1 and 2 were swapped. The error is now fixed. We are very sorry for your inconvenience.
There are N children in AtCoder Kindergarten, conveniently numbered 1 through N. Mr. Evi will distribute C indistinguishable ca... | ```python
MOD=10**9+7
N,C=map(int, input().split())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
P=[[1] for _ in range(401)]
for _ in range(1,401):
for i in range(1,401):
P[i].append(P[i][-1]*i%MOD)
R=[[] for _ in range(N)]
for i,AB in enumerate(zip(A, B)):
AA,BB=AB
for a in r... | vfc_6510 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://atcoder.jp/contests/arc059/tasks/arc059_c",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 3\n1 1\n1 1\n",
"output": "4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 2\n1\n3\n",
"output": "14\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2242 | Solve the following coding problem using the programming language python:
There are 2N balls in the xy-plane. The coordinates of the i-th of them is (x_i, y_i).
Here, x_i and y_i are integers between 1 and N (inclusive) for all i, and no two balls occupy the same coordinates.
In order to collect these balls, Snuke pre... | ```python
import sys
input = sys.stdin.readline
MOD = 10**9 + 7
N = int(input())
ball = (tuple(int(x) for x in row.split()) for row in sys.stdin.readlines())
# x座標を1,2,...,N
# y座標をN+1,N+2,...,N+N
graph = [set() for _ in range(N+N+1)]
for x,y in ball:
graph[x].add(y+N)
graph[y+N].add(x)
visited = [False] * ... | vfc_6514 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://atcoder.jp/contests/arc083/tasks/arc083_d",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1 1\n1 2\n2 1\n2 2\n",
"output": "8\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n3 2\n1 2\n4 1\n4 2\n2 2\n4 4\n2 1\n1 3\n",
"output": "7392\n",
"type": "stdin_stdout"
... |
apps | verifiable_code | 2243 | Solve the following coding problem using the programming language python:
There are N robots and M exits on a number line.
The N + M coordinates of these are all integers and all distinct.
For each i (1 \leq i \leq N), the coordinate of the i-th robot from the left is x_i.
Also, for each j (1 \leq j \leq M), the coord... | ```python
from bisect import bisect
from collections import defaultdict
class Bit:
def __init__(self, n, MOD):
self.size = n
self.tree = [0] * (n + 1)
self.depth = n.bit_length()
self.mod = MOD
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
... | vfc_6518 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://atcoder.jp/contests/arc101/tasks/arc101_d",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 2\n2 3\n1 4\n",
"output": "3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 4\n2 5 10\n1 3 7 13\n",
"output": "8\n",
"type": "stdin_stdout"
},
{
"fn_name": n... |
apps | verifiable_code | 2244 | Solve the following coding problem using the programming language python:
AtCoDeer the deer found N rectangle lying on the table, each with height 1.
If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i]... | ```python
# seishin.py
N = int(input())
P = [list(map(int, input().split())) for i in range(N)]
from heapq import heappush, heappop
l0, r0 = P[0]
L = [-l0+1]
R = [l0-1]
s = t = 0
res = 0
for i in range(N-1):
l0, r0 = P[i]
l1, r1 = P[i+1]
s += (r1 - l1); t += (r0 - l0)
if -s-L[0] <= l1-1 <= t+R[0]:
... | vfc_6522 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://atcoder.jp/contests/arc070/tasks/arc070_c",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1 3\n5 7\n1 3\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n2 5\n4 6\n1 4\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name": nul... |
apps | verifiable_code | 2245 | Solve the following coding problem using the programming language python:
Greg has an array a = a_1, a_2, ..., a_{n} and m operations. Each operation looks as: l_{i}, r_{i}, d_{i}, (1 ≤ l_{i} ≤ r_{i} ≤ n). To apply operation i to the array means to increase all array elements with numbers l_{i}, l_{i} + 1, ..., r_{i} ... | ```python
from sys import stdin, stdout
rd = lambda: list(map(int, stdin.readline().split()))
n, m, k = rd()
a = rd()
b = [rd() for _ in range(m)]
x = [0]*(m+1)
y = [0]*(n+1)
for _ in range(k):
l, r = rd()
x[l-1] += 1
x[r ] -= 1
s = 0
for i in range(m):
l, r, d = b[i]
s += x[i]
y[l-1] += s*d
y[r ] -... | vfc_6526 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/295/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 3 3\n1 2 3\n1 2 1\n1 3 2\n2 3 4\n1 2\n1 3\n2 3\n",
"output": "9 18 17\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1 1\n1\n1 1 1\n1 1\n",
"output": "2\n",
"type": "stdin... |
apps | verifiable_code | 2246 | Solve the following coding problem using the programming language python:
Alan decided to get in shape for the summer, so he created a precise workout plan to follow. His plan is to go to a different gym every day during the next N days and lift $X[i]$ grams on day $i$. In order to improve his workout performance at t... | ```python
from sys import stdin
from heapq import heappop,heappush
def main():
n,k = map(int,stdin.readline().split())
X = list(map(int,stdin.readline().split()))
A = int(stdin.readline().strip())
C = list(map(int,stdin.readline().split()))
l = list()
i = 0;g = k;ans = 0;flag = True
while i < n and flag:
heap... | vfc_6530 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1218/F",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 10000\n10000 30000 30000 40000 20000\n20000\n5 2 8 3 6\n",
"output": "5\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 10000\n10000 40000 30000 30000 20000\n10000\n5 2 8 3 6\n",
... |
apps | verifiable_code | 2247 | Solve the following coding problem using the programming language python:
Momiji has got a rooted tree, consisting of n nodes. The tree nodes are numbered by integers from 1 to n. The root has number 1. Momiji decided to play a game on this tree.
The game consists of several steps. On each step, Momiji chooses one of... | ```python
# https://codeforces.com/problemset/problem/280/C
from collections import defaultdict, deque
import sys
nodes = int(sys.stdin.readline())
edges = defaultdict(list)
for line in sys.stdin:
a, b = line.split()
a = int(a)
b = int(b)
edges[a].append(b)
edges[b].append(a)
bfs = deque([(1, 1)])
... | vfc_6534 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/280/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1 2\n",
"output": "1.50000000000000000000\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 2\n1 3\n",
"output": "2.00000000000000000000\n",
"type": "stdin_stdout"
},... |
apps | verifiable_code | 2248 | Solve the following coding problem using the programming language python:
Oleg's favorite subjects are History and Math, and his favorite branch of mathematics is division.
To improve his division skills, Oleg came up with $t$ pairs of integers $p_i$ and $q_i$ and for each pair decided to find the greatest integer $x... | ```python
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
def PrimeDecomposition(N):
ret = {}
n = int(N ** 0.5)
for d in range(2, n + 1):
while N % d == 0:
if d not in ret:
ret[d] = 1
e... | vfc_6538 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1444/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n10 4\n12 6\n179 822\n",
"output": "10\n4\n179\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n246857872446986130 713202678\n857754240051582063 933416507\n873935277189052612 53079552... |
apps | verifiable_code | 2249 | Solve the following coding problem using the programming language python:
Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point $(x_1,y_1)$ to the point $(x_2,y_2)$.
He has a rope, which he can use to pull the box. He can only pull the box if he stands exactly $... | ```python
for __ in range(int(input())):
a, b, c, d = list(map(int, input().split()))
if a == c or b == d:
print(abs(a - c) + abs(b - d))
else:
print(abs(a - c) + abs(b - d) + 2)
``` | vfc_6542 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1428/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1 2 2 2\n1 1 2 2\n",
"output": "1\n4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n69 69 69 69\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name":... |
apps | verifiable_code | 2250 | Solve the following coding problem using the programming language python:
There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is located at point (x_{i}, y_{i}). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at... | ```python
n, m, k = list(map(int,input().split()))
dm, dp = {}, {}
vis = {}
sensors = []
border = set()
for el in [(0, m), (n, 0), (0, 0), (n, m)]:
border.add(el)
for _ in range(k):
x, y = list(map(int, input().split()))
if not (x - y) in dm:
dm[x - y] = []
dm[x - y].append((x, y))
if not (... | vfc_6546 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/724/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 3 4\n1 1\n1 2\n2 1\n2 2\n",
"output": "1\n-1\n-1\n2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 4 6\n1 1\n2 1\n1 2\n2 2\n1 3\n2 3\n",
"output": "1\n-1\n-1\n2\n5\n-1\n",
... |
apps | verifiable_code | 2251 | Solve the following coding problem using the programming language python:
Konrad is a Human Relations consultant working for VoltModder, a large electrical equipment producer. Today, he has been tasked with evaluating the level of happiness in the company.
There are $n$ people working for VoltModder, numbered from $1... | ```python
import sys
n, m = list(map(int, sys.stdin.readline().strip().split()))
L = [0 for i in range (0, n)]
H = [[] for i in range (0, n)]
for i in range (0, m):
x, y = list(map(int, sys.stdin.readline().strip().split()))
x = x - 1
y = y - 1
if x > y:
x, y = y, x
L[y] = L[y] + 1
H[x]... | vfc_6550 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1229/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 5\n1 2\n2 4\n1 3\n3 4\n2 3\n2\n2\n3\n",
"output": "4\n3\n2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 3\n1 2\n2 3\n1 3\n5\n1\n2\n2\n1\n3\n",
"output": "1\n1\n1\n1\n1\n1\n",
... |
apps | verifiable_code | 2252 | Solve the following coding problem using the programming language python:
After learning a lot about space exploration, a little girl named Ana wants to change the subject.
Ana is a girl who loves palindromes (string that can be read the same backwards as forward). She has learned how to check for a given string whet... | ```python
#!/usr/bin/env python3
"""
Created on Wed Feb 28 11:47:12 2018
@author: mikolajbinkowski
"""
import sys
N = int(input())
string_count = {}
for _ in range(N):
s = str(input())
char_count = {}
for c in s:
char_count[c] = char_count.get(c, 0) + 1
s0 = []
for a in 'abcdefghijklmnopq... | vfc_6554 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1045/I",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\naa\nbb\ncd\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\naab\nabcac\ndffe\ned\naa\naade\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
... |
apps | verifiable_code | 2253 | Solve the following coding problem using the programming language python:
ZS the Coder loves mazes. Your job is to create one so that he can play with it. A maze consists of n × m rooms, and the rooms are arranged in n rows (numbered from the top to the bottom starting from 1) and m columns (numbered from the left to ... | ```python
corr = lambda x, y: 1 <= x <= n and 1 <= y <= m
T = int(input())
a = []
while T:
a.append(T % 6)
T //= 6
L = len(a)
n = m = L * 2 + 2
ans = [(1, 2, 2, 2), (2, 1, 2, 2)]
f = [[1] * 9 for i in range(7)]
f[1][2] = f[2][2] = f[2][6] = f[3][5] = 0
f[4][5] = f[4][6] = f[5][2] = f[5][5] = f[5][6] = 0
p = [0... | vfc_6558 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/715/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n",
"output": "4 4\n5\n1 2 2 2\n1 3 2 3\n1 4 2 4\n2 1 2 2\n4 1 4 2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n",
"output": "4 4\n4\n1 2 2 2\n1 3 2 3\n2 1 2 2\n4 1 4 2\n",
... |
apps | verifiable_code | 2254 | Solve the following coding problem using the programming language python:
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those ... | ```python
#!/usr/bin/env python
#-*-coding:utf-8 -*-
import sys,collections
n,q=list(map(int,input().split()))
M=collections.defaultdict(collections.deque)
Q=collections.deque()
L=[]
s=n=m=0
for _ in range(q):
y,x=list(map(int,input().split()))
if 2>y:
s+=1
Q.append(x)
M[x].append(n)
n+=1
elif 3>y:
y=M.get... | vfc_6562 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/704/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 4\n1 3\n1 1\n1 2\n2 3\n",
"output": "1\n2\n3\n2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 6\n1 2\n1 4\n1 2\n3 3\n1 3\n1 3\n",
"output": "1\n2\n3\n0\n1\n2\n",
"type": ... |
apps | verifiable_code | 2255 | Solve the following coding problem using the programming language python:
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve uns... | ```python
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
from collections import Counter as C
n = ii()
a = li()
oe = [C(), C()]
oe[1][0] = 1
x = 0
ans = 0
for i in range(n):
x ^= a[i]
ans += oe[i % 2][x]
oe[i % 2][x] += 1
print(ans)
``` | vfc_6566 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1109/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n1 2 3 4 5\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n3 2 2 3 7 6\n",
"output": "3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
... |
apps | verifiable_code | 2256 | Solve the following coding problem using the programming language python:
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a proble... | ```python
import sys
input = sys.stdin.readline
n,m=list(map(int,input().split()))
ANS=[]
for i in range(1,n//2+1):
for j in range(1,m+1):
sys.stdout.write("".join((str(i)," ",str(j),"\n")))
sys.stdout.write("".join((str(n-i+1)," ",str(m-j+1),"\n")))
if n%2==1:
for j in range(1,m//2+1):
... | vfc_6570 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1179/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 3\n",
"output": "1 1\n1 3\n1 2\n2 2\n2 3\n2 1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1\n",
"output": "1 1\n",
"type": "stdin_stdout"
},
{
"fn_name": nu... |
apps | verifiable_code | 2257 | Solve the following coding problem using the programming language python:
Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter $y$ Koa selects must be strictly greater alphabetically than $x$ (read statement for better understandi... | ```python
import sys
input = lambda: sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
N = int(input())
A = [ord(a) - 97 for a in input()]
B = [ord(a) - 97 for a in input()]
X = [[0] * 20 for _ in range(20)]
for a, b in zip(A, B):
X[a][b] = 1
if a > b:
pri... | vfc_6574 | {
"difficulty": "competition",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1383/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n3\naab\nbcc\n4\ncabc\nabcb\n3\nabc\ntsr\n4\naabd\ncccd\n5\nabcbd\nbcdda\n",
"output": "2\n-1\n3\n2\n-1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n1\na\nb\n1\nb\na\n3\nabc\ndef\... |
Subsets and Splits
Random Hard Python Problems
Retrieves a random sample of 10 hard difficulty questions along with their answers and problem IDs, providing basic filtering but limited analytical value.