problem_id stringlengths 5 6 | url stringlengths 48 49 | title stringlengths 2 46 | rating int64 800 3.5k | tags sequencelengths 1 11 | div stringclasses 16
values | time_limit_ms int64 1k 13k | memory_limit_mb int64 32 1.02k | description stringlengths 67 2.52k | input stringlengths 99 1.93k โ | output stringlengths 47 1.35k โ | examples sequencelengths 1 5 | note stringlengths 0 1.68k | prompt stringlengths 505 6.96k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2006A | https://codeforces.com/problemset/problem/2006/A | Iris and Game on the Tree | 1,700 | [
"constructive algorithms",
"dfs and similar",
"games",
"graphs",
"greedy",
"trees"
] | Div. 1 | 2,000 | 256 | Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$.
Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the we... | Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 5\cdot 10^4$) โ the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$) โ the number of vertices in the tree.
The f... | For each test case, output a single integer โ the final score of the tree. | [
[
"6\n4\n1 2\n1 3\n4 1\n0101\n4\n1 2\n3 2\n2 4\n???0\n5\n1 2\n1 3\n2 4\n2 5\n?1?01\n6\n1 2\n2 3\n3 4\n5 3\n3 6\n?0????\n5\n1 2\n1 3\n1 4\n1 5\n11?1?\n2\n2 1\n??",
"2\n1\n1\n2\n1\n0"
]
] | In the first test case, all the values of the vertices have been determined. There are three different paths from the root to a leaf:
* From vertex $1$ to vertex $2$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$. * From vertex $1$ to vertex $3$. The string formed by the pa... | Title: Iris and Game on the Tree
time_limit_ms: 2000
memory_limit_mb: 256
Description: Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$.
Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the value... |
2006B | https://codeforces.com/problemset/problem/2006/B | Iris and the Tree | 1,800 | [
"brute force",
"data structures",
"dfs and similar",
"dsu",
"math",
"trees"
] | Div. 1 | 3,000 | 256 | Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$.
Iris does not know the values of $t_i$, but she knows that $\displaystyle\sum_{i=2}^n t_i = w$ and each of the $t_i$ is ... | Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) โ the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $w$ ($2 \le n \le 2 \cdot 10^5$, $0 \leq w \leq 10^{12}$) โ the number of ... | For each test case, output one line containing $n-1$ integers, each representing the answer after each event. | [
[
"4\n2 1000000000000\n1\n2 1000000000000\n4 9\n1 1 1\n2 2\n4 4\n3 3\n6 100\n1 2 3 2 1\n6 17\n3 32\n2 4\n4 26\n5 21\n10 511\n1 2 2 4 2 1 1 8 8\n3 2\n6 16\n10 256\n9 128\n2 1\n5 8\n8 64\n4 4\n7 32",
"2000000000000\n25 18 18\n449 302 247 200 200\n4585 4473 2681 1567 1454 1322 1094 1022 1022"
]
] | In the first test case, $\operatorname{dist}(1, 2) = \operatorname{dist}(2, 1) = t_2 = w = 10^{12}$, so $\operatorname{dist}(1, 2) + \operatorname{dist}(2, 1) = 2 \cdot 10^{12}$.
In the second test case, the tree after Iris found out all $t_x$ is shown below:

$\oper... | Title: Iris and the Tree
time_limit_ms: 3000
memory_limit_mb: 256
Description: Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$.
Iris does not know the values of $t_i$, b... |
2006C | https://codeforces.com/problemset/problem/2006/C | Eri and Expanded Sets | 2,300 | [
"data structures",
"divide and conquer",
"math",
"number theory",
"two pointers"
] | Div. 1 | 3,000 | 512 | Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $x$ and $y$ rem... | Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) โ the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) โ length of the array $a$.
The second line... | For each test case, output a single integer โ the number of brilliant subarrays. | [
[
"6\n2\n2 2\n6\n1 3 6 10 15 21\n5\n6 30 18 36 9\n1\n1000000000\n6\n1 1 4 5 1 4\n12\n70 130 90 90 90 108 612 500 451 171 193 193",
"3\n18\n5\n1\n18\n53"
]
] | In the first test case, the array $a = [2, 2]$ has $3$ subarrays: $[2]$, $[2]$, and $[2, 2]$. For all of them, the set only contains a single integer $2$, therefore it's always consecutive. All these subarrays are brilliant, so the answer is $3$.
In the second test case, let's consider the subarray $[3, 6, 10]$. We ca... | Title: Eri and Expanded Sets
time_limit_ms: 3000
memory_limit_mb: 512
Description: Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer ... |
2018A | https://codeforces.com/problemset/problem/2018/A | Cards Partition | 1,600 | [
"2-sat",
"brute force",
"greedy",
"implementation",
"math"
] | Div. 1 | 2,000 | 256 | [DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram)
โ
You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.
There is also ... | Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$, $k$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^{16}$) โ the number of distinct types of cards a... | For each test case, output a single integer: the maximum possible size of a deck if you operate optimally. | [
[
"9\n3 1\n3 2 2\n5 4\n2 6 1 2 4\n2 100\n1410065408 10000000000\n10 8\n7 4 6 6 9 3 10 2 8 7\n2 12\n2 2\n2 70\n0 1\n1 0\n1\n3 0\n2 1 2\n3 1\n0 3 3",
"2\n3\n1\n7\n2\n2\n1\n1\n2"
]
] | In the first test case, you can buy one card with the number $1$, and your cards become $[1, 1, 1, 1, 2, 2, 3, 3]$. You can partition them into the decks $[1, 2], [1, 2], [1, 3], [1, 3]$: they all have size $2$, and they all contain distinct values. You can show that you cannot get a partition with decks of size greate... | Title: Cards Partition
time_limit_ms: 2000
memory_limit_mb: 256
Description: [DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram)
โ
You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, yo... |
2018B | https://codeforces.com/problemset/problem/2018/B | Speedbreaker | 1,900 | [
"binary search",
"data structures",
"dp",
"greedy",
"implementation",
"two pointers"
] | Div. 1 | 2,000 | 256 | [Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker)
โ
There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.
* At time $1$, you conquer exactly one city, called the starting city. * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conqu... | Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ the number of cities.
The second line of each test case contains $... | For each test case, output a single integer: the number of starting cities that allow you to win. | [
[
"3\n6\n6 3 3 3 5 5\n6\n5 6 4 1 4 5\n9\n8 6 4 2 1 3 5 7 9",
"3\n0\n1"
]
] | In the first test case, cities $2$, $3$, and $4$ are good starting cities.
In the second test case, there are no good starting cities.
In the third test case, the only good starting city is city $5$. | Title: Speedbreaker
time_limit_ms: 2000
memory_limit_mb: 256
Description: [Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker)
โ
There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.
* At time $1$, you conquer exactly one city, called the starting city. * A... |
2018C | https://codeforces.com/problemset/problem/2018/C | Tree Pruning | 1,700 | [
"brute force",
"dfs and similar",
"greedy",
"sortings",
"trees"
] | Div. 1 | 3,000 | 256 | [t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little)
โ
You are given a tree with $n$ nodes, rooted at node $1$. In this problem, a leaf is a non-root node with degree $1$.
In one operation, you can remove a leaf and the edge adjacent to it (possibly, new lea... | Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($3 \leq n \leq 5 \cdot 10^5$) โ the number of nodes.
Each of the next $n-1$ lines contains two i... | For each test case, output a single integer: the minimum number of operations needed to achieve your goal. | [
[
"3\n7\n1 2\n1 3\n2 4\n2 5\n4 6\n4 7\n7\n1 2\n1 3\n1 4\n2 5\n3 6\n5 7\n15\n12 9\n1 6\n6 14\n9 11\n8 7\n3 5\n13 5\n6 10\n13 15\n13 6\n14 12\n7 2\n8 1\n1 4",
"2\n2\n5"
]
] | In the first two examples, the tree is as follows:

In the first example, by removing edges $(1, 3)$ and $(2, 5)$, the resulting tree has all leaves (nodes $6$ and $7$) at the same distance from the root (node $1$), which is $3$. The answer is $2$, as it is the minimu... | Title: Tree Pruning
time_limit_ms: 3000
memory_limit_mb: 256
Description: [t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little)
โ
You are given a tree with $n$ nodes, rooted at node $1$. In this problem, a leaf is a non-root node with degree $1$.
In one opera... |
2018D | https://codeforces.com/problemset/problem/2018/D | Max Plus Min Plus Size | 2,200 | [
"data structures",
"dp",
"dsu",
"greedy",
"implementation",
"matrices",
"sortings"
] | Div. 1 | 2,000 | 256 | [EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern)
โ
You are given an array $a_1, a_2, \ldots, a_n$ of positive integers.
You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $1 \leq i \leq n-1$, at least one of $a_i$ an... | Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ the length of the array.
The second line of each test case contain... | For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement. | [
[
"4\n3\n5 4 5\n3\n4 5 4\n10\n3 3 3 3 4 1 2 3 5 4\n10\n17 89 92 42 29 41 92 14 70 45",
"12\n11\n12\n186"
]
] | In the first test case, you can color the array as follows: $[\color{red}{5}, 4, \color{red}{5}]$. Your score is $\max([5, 5]) + \min([5, 5]) + \text{size}([5, 5]) = 5+5+2 = 12$. This is the maximum score you can get.
In the second test case, you can color the array as follows: $[4, \color{red}{5}, 4]$. Your score is ... | Title: Max Plus Min Plus Size
time_limit_ms: 2000
memory_limit_mb: 256
Description: [EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern)
โ
You are given an array $a_1, a_2, \ldots, a_n$ of positive integers.
You can color some elements of the array red, but there cannot b... |
2023B | https://codeforces.com/problemset/problem/2023/B | Skipping | 1,700 | [
"binary search",
"dp",
"graphs",
"shortest paths"
] | Div. 1 | 2,000 | 256 | It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a certain parameter $b_i$ ($1 \le b_i \le n$).
Initially, the testing sy... | Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) โ the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) โ the number of problems in the olympiad.
... | For each test case, output a single integer โ the maximum number of points that Prokhor can achieve. | [
[
"4\n2\n15 16\n2 1\n5\n10 10 100 100 1000\n3 4 1 1 1\n3\n100 49 50\n3 2 2\n4\n100 200 300 1000\n2 3 4 1",
"16\n200\n100\n1000"
]
] | In the first test case, Prokhor can skip the first problem; then he will receive the problem with index $b_1 = 2$. Prokhor can submit it and receive $a_2 = 16$ points. After that, the competition will end because Prokhor has already received all problems. Note that if Prokhor submits the first problem, he will receive ... | Title: Skipping
time_limit_ms: 2000
memory_limit_mb: 256
Description: It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a c... |
2023C | https://codeforces.com/problemset/problem/2023/C | C+K+S | 2,400 | [
"constructive algorithms",
"dfs and similar",
"graphs",
"greedy",
"hashing",
"implementation",
"strings"
] | Div. 1 | 3,000 | 256 | You are given two strongly connected$^{\dagger}$ directed graphs, each with exactly $n$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature โ the length of any cycle in these graphs is divisible by $k$.
Each of the $2n$ vertices belongs to exactly one of two type... | Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) โ the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$) โ the number of vertices in each grap... | For each test case, output "YES" (without quotes) if it is possible to draw $n$ new edges such that all conditions are met, and "NO" (without quotes) otherwise.
You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). | [
[
"3\n4 2\n1 0 0 1\n4\n1 2\n2 3\n3 4\n4 1\n1 0 0 1\n4\n1 3\n3 2\n2 4\n4 1\n3 3\n0 0 0\n3\n1 2\n2 3\n3 1\n1 1 0\n3\n1 2\n2 3\n3 1\n4 2\n1 1 1 1\n4\n1 2\n2 3\n3 4\n4 1\n0 0 0 0\n6\n1 2\n2 1\n1 3\n3 1\n1 4\n4 1",
"YES\nNO\nYES"
]
] | In the first test case, it is possible to draw edges from the first graph to the second graph as $(1, 3)$ and $(4, 2)$ (the first number in the pair is the vertex number in the first graph, and the second number in the pair is the vertex number in the second graph), and from the second graph to the first graph as $(1, ... | Title: C+K+S
time_limit_ms: 3000
memory_limit_mb: 256
Description: You are given two strongly connected$^{\dagger}$ directed graphs, each with exactly $n$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature โ the length of any cycle in these graphs is divisible by... |
1993A | https://codeforces.com/problemset/problem/1993/A | Question Marks | 800 | [
"greedy",
"implementation"
] | Div. 2 | 1,000 | 256 | Tim is doing a test consisting of $4n$ questions; each question has $4$ options: 'A', 'B', 'C', and 'D'. For each option, there are exactly $n$ correct answers corresponding to that option โ meaning there are $n$ questions with the answer 'A', $n$ questions with the answer 'B', $n$ questions with the answer 'C', and $n... | The first line contains a single integer $t$ ($1 \le t \le 1000$) โ the number of test cases.
The first line of each test case contains an integer $n$ ($1 \le n \le 100$).
The second line of each test case contains a string $s$ of $4n$ characters ($s_i \in \\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}, \texttt{?}... | For each test case, print a single integer โ the maximum score that Tim can achieve. | [
[
"6\n1\nABCD\n2\nAAAAAAAA\n2\nAAAABBBB\n2\n????????\n3\nABCABCABCABC\n5\nACADC??ACAC?DCAABC?C",
"4\n2\n4\n0\n9\n13"
]
] | In the first test case, there is exactly one question with each answer 'A', 'B', 'C', and 'D'; so it's possible that Tim gets all his answers correct.
In the second test case, there are only two correct answers 'A' which makes him get exactly $2$ points in any case.
In the third test case, Tim can get at most $2$ cor... | Title: Question Marks
time_limit_ms: 1000
memory_limit_mb: 256
Description: Tim is doing a test consisting of $4n$ questions; each question has $4$ options: 'A', 'B', 'C', and 'D'. For each option, there are exactly $n$ correct answers corresponding to that option โ meaning there are $n$ questions with the answer 'A', ... |
1984C1 | https://codeforces.com/problemset/problem/1984/C1 | Magnitude (Easy Version) | 1,300 | [
"dp",
"greedy",
"math"
] | Div. 1 + 2 | 2,000 | 256 | The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.
You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:
* Option $1$: set $c$ to $c ... | The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) โ the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$).
The second line of each case contains $n$ integers $a_1$, $a_2$, $a_3$, $\ldots$, $a_n$ ($-10^9 \leq a_i \leq 10^9$).
The sum ... | For each test case, output a single integer โ the value of $k$. | [
[
"5\n4\n10 -9 -3 4\n8\n1 4 3 4 1 4 3 4\n3\n-1 -2 -3\n4\n-1000000000 1000000000 1000000000 1000000000\n4\n1 9 8 4",
"6\n24\n6\n4000000000\n22"
]
] | In the first test case, if we set $c$ to its absolute value every time we add to it, we end up with $6$. It can be shown that this is the maximum result.
In the second test case, taking the absolute value will never change anything, so we can just sum the array without doing anything to get $24$.
In the third test ca... | Title: Magnitude (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.
You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ ... |
1973B | https://codeforces.com/problemset/problem/1973/B | Cat, Fox and the Lonely Array | 1,300 | [
"binary search",
"bitmasks",
"data structures",
"greedy",
"math",
"two pointers"
] | Div. 2 | 2,000 | 256 | Today, Cat and Fox found an array $a$ consisting of $n$ non-negative integers.
Define the loneliness of $a$ as the smallest positive integer $k$ ($1 \le k \le n$) such that for any two positive integers $i$ and $j$ ($1 \leq i, j \leq n - k +1$), the following holds: $$a_i | a_{i+1} | \ldots | a_{i+k-1} = a_j | a_{j+1}... | Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4 $) โ the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 10^5$) โ the length of the array $a$.
The second line of each... | For each test case, print one integer โ the loneliness of the given array. | [
[
"7\n1\n0\n3\n2 2 2\n3\n1 0 2\n5\n3 0 1 4 2\n5\n2 0 4 0 2\n7\n0 0 0 0 1 2 4\n8\n0 1 3 2 2 1 0 3",
"1\n1\n3\n4\n4\n7\n3"
]
] | In the first example, the loneliness of an array with a single element is always $1$, so the answer is $1$.
In the second example, the OR of each subarray of length $k = 1$ is $2$, so the loneliness of the whole array is $1$.
In the seventh example, it's true that $(0 | 1 | 3) = (1 | 3 | 2) = (3 | 2 | 2) = (2 | 2 | 1... | Title: Cat, Fox and the Lonely Array
time_limit_ms: 2000
memory_limit_mb: 256
Description: Today, Cat and Fox found an array $a$ consisting of $n$ non-negative integers.
Define the loneliness of $a$ as the smallest positive integer $k$ ($1 \le k \le n$) such that for any two positive integers $i$ and $j$ ($1 \leq i, j... |
2027D2 | https://codeforces.com/problemset/problem/2027/D2 | The Endspeaker (Hard Version) | 2,200 | [
"binary search",
"data structures",
"dp",
"greedy",
"implementation",
"two pointers"
] | Div. 2 | 2,000 | 256 | This is the hard version of this problem. The only difference is that you need to also output the number of optimal sequences in this version. You must solve both versions to be able to hack.
You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i > b_{i+1}$ for all $1 \le i < m$). Initially, the... | Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}$).
The second ... | For each test case, if it's possible to make $a$ empty, then output two integers. The first should be the minimum total cost of the operations, and the second should be the number of sequences of operations which achieve this minimum cost, modulo $10^9 + 7$.
If there is no possible sequence of operations which makes $... | [
[
"5\n4 2\n9 3 4 3\n11 7\n1 2\n20\n19 18\n10 2\n2 5 2 1 10 3 2 9 9 6\n17 9\n10 11\n2 2 2 2 2 2 2 2 2 2\n20 18 16 14 12 10 8 6 4 2 1\n1 6\n10\n32 16 8 4 2 1",
"1 3\n-1\n2 11\n10 42\n4 1"
]
] | In the first test case, there are $3$ optimal sequences of operations which yield a total cost of $1$:
* All $3$ sequences begin with a type $2$ operation, removing the prefix $[9]$ to make $a = [3, 4, 3]$, incurring a cost of $1$. Then, we perform a type $1$ operation to increase the value of $k$ by $1$. All subseq... | Title: The Endspeaker (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the hard version of this problem. The only difference is that you need to also output the number of optimal sequences in this version. You must solve both versions to be able to hack.
You're given an array $a$ of length ... |
1993F1 | https://codeforces.com/problemset/problem/1993/F1 | Dyn-scripted Robot (Easy Version) | 2,400 | [
"brute force",
"chinese remainder theorem",
"constructive algorithms",
"math",
"number theory"
] | Div. 2 | 3,000 | 256 | This is the easy version of the problem. The only difference is that in this version $k \le n$. You can make hacks only if both versions of the problem are solved. Given a $w \times h$ rectangle on the $Oxy$ plane, with points $(0, 0)$ at the bottom-left and $(w, h)$ at the top-right of the rectangle.
You also ha... | The first line contains a single integer $t$ ($1 \le t \le 10^4$) โ the number of test cases.
The first line of each test case contains four integers $n$, $k$, $w$, and $h$ ($1 \le n, w, h \le 10^6$; $1 \le k \le n$).
The second line contains a single string $s$ of size $n$ ($s_i \in \\{\texttt{L}, \texttt{R}, \textt... | For each test case, print a single integer โ the number of times the robot reaches $(0, 0)$ when executing script $s$ for $k$ times continuously. | [
[
"5\n2 2 2 2\nUR\n4 2 1 1\nLLDD\n6 3 3 1\nRLRRRL\n5 5 3 3\nRUURD\n7 5 3 4\nRRDLUUU",
"0\n4\n3\n0\n1"
]
] | In the first test case, the robot only moves up and right. In the end, it occupies the position $(2, 2)$ but never visits $(0, 0)$. So the answer is $0$.
In the second test case, each time executing the script the robot visits the origin twice. And since $k=2$, it visits the origin $2 \cdot 2 = 4$ times overall.

time_limit_ms: 3000
memory_limit_mb: 256
Description: This is the easy version of the problem. The only difference is that in this version $k \le n$. You can make hacks only if both versions of the problem are solved. Given a $w \times h$ rectangle on the $Oxy$ plane, with ... |
1990C | https://codeforces.com/problemset/problem/1990/C | Mad MAD Sum | 1,500 | [
"brute force",
"greedy",
"math"
] | Div. 2 | 2,000 | 256 | We define the $\operatorname{MAD}$ (Maximum Appearing Duplicate) in an array as the largest number that appears at least twice in the array. Specifically, if there is no number that appears at least twice, the $\operatorname{MAD}$ value is $0$.
For example, $\operatorname{MAD}([1, 2, 1]) = 1$, $\operatorname{MAD}([2, ... | The first line contains an integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) โ the number of test cases.
For each test case:
* The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) โ the size of the array $a$; * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) โ the el... | For each test case, output the value of $sum$ in a new line. | [
[
"4\n1\n1\n3\n2 2 3\n4\n2 1 1 2\n4\n4 4 4 4",
"1\n13\n9\n40"
]
] | In the first test case, $a=[1]$ initially.
In the first loop:
1. Set $sum := sum + a_1 = 0+1=1$; 2. Set $b_1 :=\ \operatorname{MAD}([a_1])=\ \operatorname{MAD}([1])=0$, and then set $a_1 := b_1$.
After the first loop, $a=[0]$ and the process ends. The value of $sum$ after the process is $1$.
In the second tes... | Title: Mad MAD Sum
time_limit_ms: 2000
memory_limit_mb: 256
Description: We define the $\operatorname{MAD}$ (Maximum Appearing Duplicate) in an array as the largest number that appears at least twice in the array. Specifically, if there is no number that appears at least twice, the $\operatorname{MAD}$ value is $0$.
F... |
1971A | https://codeforces.com/problemset/problem/1971/A | My First Sorting Problem | 800 | [
"implementation",
"sortings"
] | Div. 4 | 1,000 | 256 | You are given two integers $x$ and $y$.
Output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$. | The first line contains a single integer $t$ ($1 \leq t \leq 100$) โ the number of test cases.
The only line of each test case contains two space-separated integers $x$ and $y$ ($0 \leq x, y \leq 9$). | For each test case, output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$. | [
[
"10\n1 9\n8 4\n1 4\n3 4\n2 0\n2 4\n6 9\n3 3\n0 0\n9 9",
"1 9\n4 8\n1 4\n3 4\n0 2\n2 4\n6 9\n3 3\n0 0\n9 9"
]
] | Title: My First Sorting Problem
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given two integers $x$ and $y$.
Output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.
Input: The first line contains a single integer $t$ ($1 \leq t \leq 100$) โ the number of test cases.
... | |
1994C | https://codeforces.com/problemset/problem/1994/C | Hungry Games | 1,600 | [
"binary search",
"dp",
"two pointers"
] | Div. 1 + 2 | 2,000 | 256 | Yaroslav is playing a computer game, and at one of the levels, he encountered $n$ mushrooms arranged in a row. Each mushroom has its own level of toxicity; the $i$-th mushroom from the beginning has a toxicity level of $a_i$. Yaroslav can choose two integers $1 \le l \le r \le n$, and then his character will take turns... | Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) โ the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers $n$, $x$ ($1 \leq n \leq 2 \cdot 10^5, 1 \le x \le 10^9$) โ the number of mushrooms... | For each test case, output a single number โ the number of subsegments such that the final value of $g$ will not be zero. | [
[
"5\n4 2\n1 1 1 1\n3 2\n1 2 3\n1 6\n10\n6 3\n1 2 1 4 3 8\n5 999999999\n999999999 999999998 1000000000 1000000000 500000000",
"8\n2\n0\n10\n7"
]
] | In the first test case, the subsegments $(1, 1)$, $(1, 2)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(3, 3)$, $(3, 4)$ and $(4, 4)$ are suitable.
In the second test case, non-zero $g$ will remain only on the subsegments $(1, 1)$ and $(2, 2)$.
In the third test case, on the only possible subsegment, $g$ will be zero. | Title: Hungry Games
time_limit_ms: 2000
memory_limit_mb: 256
Description: Yaroslav is playing a computer game, and at one of the levels, he encountered $n$ mushrooms arranged in a row. Each mushroom has its own level of toxicity; the $i$-th mushroom from the beginning has a toxicity level of $a_i$. Yaroslav can choose ... |
2023E | https://codeforces.com/problemset/problem/2023/E | Tree of Life | 3,300 | [
"dp",
"greedy",
"trees"
] | Div. 1 | 2,000 | 512 | In the heart of an ancient kingdom grows the legendary Tree of Life โ the only one of its kind and the source of magical power for the entire world. The tree consists of $n$ nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are $n-1$ cha... | Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 4 \cdot 10^4$) โ the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 5 \cdot 10^5$) โ the number of nodes in the Tree o... | For each test case, output a single integer โ the minimum number of paths that the sages need to select to prevent a catastrophe. | [
[
"5\n4\n1 2\n2 3\n3 4\n2\n1 2\n4\n1 2\n1 3\n1 4\n8\n3 7\n2 4\n1 2\n2 5\n3 6\n1 3\n3 8\n6\n2 3\n1 2\n3 6\n1 5\n1 4",
"1\n0\n3\n7\n3"
]
] | In the first test case, there are two pairs of channels emanating from a single node: $(1, 2)$ and $(2, 3)$, $(2, 3)$ and $(3, 4)$. It is sufficient to perform the ritual along the path $1-2-3-4$. Thus, the answer is $1$.
In the second test case, there are no pairs of channels emanating from a single node, so the answ... | Title: Tree of Life
time_limit_ms: 2000
memory_limit_mb: 512
Description: In the heart of an ancient kingdom grows the legendary Tree of Life โ the only one of its kind and the source of magical power for the entire world. The tree consists of $n$ nodes. Each node of this tree is a magical source, connected to other su... |
1971D | https://codeforces.com/problemset/problem/1971/D | Binary Cut | 1,100 | [
"dp",
"greedy",
"implementation",
"sortings",
"strings"
] | Div. 4 | 2,000 | 256 | You are given a binary string$^{\dagger}$. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string.

Note that:
* each character must lie in exactly one of the pieces; * the pie... | The first line contains a single integer $t$ ($1 \leq t \leq 500$) โ the number of test cases.
The only line of each test case contains a single string $s$ ($1 \leq |s| \leq 500$) consisting of characters $\texttt{0}$ and $\texttt{1}$, where $|s|$ denotes the length of the string $s$. | For each test case, output a single integer โ the minimum number of pieces needed to be able to rearrange the string into a sorted binary string. | [
[
"6\n11010\n00000000\n1\n10\n0001111\n0110",
"3\n1\n1\n2\n1\n2"
]
] | The first test case is pictured in the statement. It can be proven that you can't use fewer than $3$ pieces.
In the second and third test cases, the binary string is already sorted, so only $1$ piece is needed.
In the fourth test case, you need to make a single cut between the two characters and rearrange them to mak... | Title: Binary Cut
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given a binary string$^{\dagger}$. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string.

Note that:... |
1972C | https://codeforces.com/problemset/problem/1972/C | Permutation Counting | 1,400 | [
"binary search",
"constructive algorithms",
"greedy",
"implementation",
"math",
"sortings"
] | Div. 2 | 2,000 | 256 | You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.
There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy $k$ new cards in total, and the c... | Each test contains multiple test cases. The first line contains the number of test cases $t\ (1\le t\le 100)$. The description of the test cases follows.
The first line of each test case contains two integers $n$, $k$ ($1\le n \le 2 \cdot 10^5$, $0\le k \le 10^{12}$) โ the number of distinct types of cards and the num... | For each test case, output a single line containing an integer: the maximum score you can get. | [
[
"8\n1 10\n1\n2 4\n8 4\n3 4\n6 1 8\n3 9\n7 6 2\n5 3\n6 6 7 4 6\n9 7\n7 6 1 7 6 2 4 3 3\n10 10\n1 3 1 2 1 9 3 5 7 5\n9 8\n5 8 7 5 1 3 2 9 8",
"11\n15\n15\n22\n28\n32\n28\n36"
]
] | In the first test case, the final (and only) array we can get is $[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]$ (including $11$ single $1$s), which contains $11$ subarrays consisting of a permutation of $[1]$.
In the second test case, we can buy $0$ cards of type $1$ and $4$ cards of type $2$, and then we rearrange the cards as ... | Title: Permutation Counting
time_limit_ms: 2000
memory_limit_mb: 256
Description: You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.
There is also a shop which contains unlimited cards ... |
1978B | https://codeforces.com/problemset/problem/1978/B | New Bakery | 800 | [
"binary search",
"greedy",
"math",
"ternary search"
] | Div. 2 | 1,000 | 256 | Bob decided to open a bakery. On the opening day, he baked $n$ buns that he can sell. The usual price of a bun is $a$ coins, but to attract customers, Bob organized the following promotion:
* Bob chooses some integer $k$ ($0 \le k \le \min(n, b)$). * Bob sells the first $k$ buns at a modified price. In this case, ... | Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) โ the number of test cases. The description of the test cases follows.
The only line of each test case contains three integers $n$, $a$, and $b$ ($1 \le n, a, b \le 10^9$) โ the number of buns, the usual price ... | For each test case, output a single integer โ the maximum profit that Bob can obtain. | [
[
"7\n4 4 5\n5 5 9\n10 10 5\n5 5 11\n1000000000 1000000000 1000000000\n1000000000 1000000000 1\n1000 1 1000",
"17\n35\n100\n45\n1000000000000000000\n1000000000000000000\n500500"
]
] | In the first test case, it is optimal for Bob to choose $k = 1$. Then he will sell one bun for $5$ coins, and three buns at the usual price for $4$ coins each. Then the profit will be $5 + 4 + 4 + 4 = 17$ coins.
In the second test case, it is optimal for Bob to choose $k = 5$. Then he will sell all the buns at the mod... | Title: New Bakery
time_limit_ms: 1000
memory_limit_mb: 256
Description: Bob decided to open a bakery. On the opening day, he baked $n$ buns that he can sell. The usual price of a bun is $a$ coins, but to attract customers, Bob organized the following promotion:
* Bob chooses some integer $k$ ($0 \le k \le \min(n, b)... |
1985D | https://codeforces.com/problemset/problem/1985/D | Manhattan Circle | 900 | [
"implementation",
"math"
] | Div. 4 | 2,000 | 256 | Given a $n$ by $m$ grid consisting of '.' and '#' characters, there exists a whole manhattan circle on the grid. The top left corner of the grid has coordinates $(1,1)$, and the bottom right corner has coordinates $(n, m)$.
Point ($a, b$) belongs to the manhattan circle centered at ($h, k$) if $|h - a| + |k - b| < r$,... | The first line contains $t$ ($1 \leq t \leq 1000$) โ the number of test cases.
The first line of each test case contains $n$ and $m$ ($1 \leq n \cdot m \leq 2 \cdot 10^5$) โ the height and width of the grid, respectively.
The next $n$ lines contains $m$ characters '.' or '#'. If the character is '#', then the point ... | For each test case, output the two integers, the coordinates of the center of the circle. | [
[
"6\n5 5\n.....\n.....\n..#..\n.....\n.....\n5 5\n..#..\n.###.\n#####\n.###.\n..#..\n5 6\n......\n......\n.#....\n###...\n.#....\n1 1\n#\n5 6\n...#..\n..###.\n.#####\n..###.\n...#..\n2 10\n..........\n...#......",
"3 3\n3 3\n4 2\n1 1\n3 4\n2 4"
]
] | Title: Manhattan Circle
time_limit_ms: 2000
memory_limit_mb: 256
Description: Given a $n$ by $m$ grid consisting of '.' and '#' characters, there exists a whole manhattan circle on the grid. The top left corner of the grid has coordinates $(1,1)$, and the bottom right corner has coordinates $(n, m)$.
Point ($a, b$) be... | |
1987D | https://codeforces.com/problemset/problem/1987/D | World is Mine | 1,800 | [
"dp",
"games"
] | Div. 1 + 2 | 2,000 | 256 | Alice and Bob are playing a game. Initially, there are $n$ cakes, with the $i$-th cake having a tastiness value of $a_i$.
Alice and Bob take turns eating them, with Alice starting first:
* In her turn, Alice chooses and eats any remaining cake whose tastiness is strictly greater than the maximum tastiness of any of... | Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 500$) โ the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$) โ the number of cakes.
The second line of each te... | For each test case, output a single integer โ the number of cakes Alice will eat if both players play optimally. | [
[
"9\n4\n1 4 2 3\n3\n1 1 1\n5\n1 4 2 3 4\n4\n3 4 1 4\n1\n1\n8\n4 3 2 5 6 8 3 4\n7\n6 1 1 3 5 3 1\n11\n6 11 6 8 7 5 3 11 2 3 5\n17\n2 6 5 3 9 1 6 2 5 6 3 2 3 9 6 1 6",
"2\n1\n3\n2\n1\n3\n2\n4\n4"
]
] | In the first test case, one possible sequence of turns is:
1. Alice eats a cake with a tastiness value of $1$. The remaining cakes are $[4, 2, 3]$. 2. Bob eats a cake with a tastiness value of $2$. The remaining cakes are $[4, 3]$. 3. Alice eats a cake with a tastiness of $3$. The remaining cakes are $[4]$. ... | Title: World is Mine
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice and Bob are playing a game. Initially, there are $n$ cakes, with the $i$-th cake having a tastiness value of $a_i$.
Alice and Bob take turns eating them, with Alice starting first:
* In her turn, Alice chooses and eats any remaining ca... |
1980A | https://codeforces.com/problemset/problem/1980/A | Problem Generator | 800 | [
"math"
] | Div. 3 | 1,000 | 256 | Vlad is planning to hold $m$ rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'.
Vlad already has a bank of $n$ problems, where the $i$-th problem has a difficulty level of $a_i$. There may not be enough of these problems, so he may have to come up with ... | The first line contains a single integer $t$ ($1 \le t \le 1000$) โ the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 50$, $1 \le m \le 5$) โ the number of problems in the bank and the number of upcoming rounds, respectively.
The second line of each test case c... | For each test case, output a single integer โ the minimum number of problems that need to come up with to hold $m$ rounds. | [
[
"3\n10 1\nBGECDCBDED\n10 2\nBGECDCBDED\n9 1\nBBCDEFFGG",
"2\n5\n1"
]
] | Title: Problem Generator
time_limit_ms: 1000
memory_limit_mb: 256
Description: Vlad is planning to hold $m$ rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'.
Vlad already has a bank of $n$ problems, where the $i$-th problem has a difficulty level of $a... | |
1984C2 | https://codeforces.com/problemset/problem/1984/C2 | Magnitude (Hard Version) | 1,700 | [
"combinatorics",
"dp",
"greedy",
"math"
] | Div. 1 + 2 | 2,000 | 256 | The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.
You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:
* Option $1$: set $c$ to $c ... | The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) โ the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$).
The sum of $n$ o... | For each test case, output a single integer โ the number of unique procedures that result in $c = k$, modulo $998\,244\,353$. | [
[
"5\n4\n2 -5 3 -3\n8\n1 4 3 4 1 4 3 4\n3\n-1 -2 -3\n4\n-1000000000 1000000000 1000000000 1000000000\n4\n1 9 8 4",
"12\n256\n1\n8\n16"
]
] | In the first test case, it can be shown that our maximal final value of $c$ is $3$. There are $12$ ways to achieve this because in order to get $3$, we have to take absolute value at indices $2$ or $4$, or both, resulting in $3$ ways. For the other two indices, it doesn't change the value whether we take absolute value... | Title: Magnitude (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.
You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ ... |
1976D | https://codeforces.com/problemset/problem/1976/D | Invertible Bracket Sequences | 2,000 | [
"binary search",
"combinatorics",
"data structures",
"divide and conquer",
"implementation",
"two pointers"
] | Div. 2 | 2,000 | 256 | 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 characters of the sequence. For example:
* bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");... | The first line contains a single integer $t$ ($1 \le t \le 10^4$) โ the number of test cases.
The only line of each test case contains a non-empty regular bracket sequence; it consists only of characters '(' and/or ')'.
Additional constraint on the input: the total length of the regular bracket sequences over all tes... | For each test case, print a single integer โ the number of pairs $(l,r)$ meeting the conditions from the statement. | [
[
"4\n(())\n()\n()()()\n(()())(())",
"1\n0\n3\n13"
]
] | In the first example, there is only one pair:
* $(2, 3)$: (()) $\rightarrow$ ()().
In the second example, there are no pairs.
In the third example, there are three pairs:
* $(2, 3)$: ()()() $\rightarrow$ (())(); * $(4, 5)$: ()()() $\rightarrow$ ()(()); * $(2, 5)$: ()()() $\rightarrow$ (()()); | Title: Invertible Bracket Sequences
time_limit_ms: 2000
memory_limit_mb: 256
Description: 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 characters of the sequence. For example:
* bracket sequences... |
2002F1 | https://codeforces.com/problemset/problem/2002/F1 | Court Blue (Easy Version) | 2,600 | [
"brute force",
"dfs and similar",
"dp",
"math",
"number theory"
] | Div. 1 + 2 | 3,000 | 512 | This is the easy version of the problem. In this version, $n=m$ and the time limit is lower. You can make hacks only if both versions of the problem are solved.
In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins... | The first line contains an integer $t$ ($1\leq t \leq 10^3$) โ the number of test cases.
The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$, $\bf{n=m}$): $n$, $m$ gives the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ d... | For each test case, output a single integer โ the maximum total score of a successful performance. | [
[
"8\n3 3 2 5\n4 4 1 4\n6 6 2 2\n7 7 2 3\n9 9 9 1\n2 2 1 4\n5 5 1 4\n8 8 6 7",
"19\n17\n18\n33\n86\n9\n24\n86"
],
[
"1\n20000000 20000000 1341 331",
"33439999007"
],
[
"2\n1984 1984 19 84\n9982 9982 44 35",
"204143\n788403"
]
] | In the first test case, a possible performance is as follows:
* Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Lelle wins, $\gcd(2,3)=1$. * Lelle and Flamm agree to stop the match.
The final score is $2\cdot2+3\cdot5=19$.
In the t... | Title: Court Blue (Easy Version)
time_limit_ms: 3000
memory_limit_mb: 512
Description: This is the easy version of the problem. In this version, $n=m$ and the time limit is lower. You can make hacks only if both versions of the problem are solved.
In the court of the Blue King, Lelle and Flamm are having a performance... |
2027B | https://codeforces.com/problemset/problem/2027/B | Stalin Sort | 1,100 | [
"brute force",
"greedy"
] | Div. 2 | 1,000 | 256 | Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an $\mathcal{O}(n)$ time complexity.
It goes as follows: starting from the second element in the array, if it is strictly smaller than the previous element (ig... | Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) โ the number of test cases. This is followed by descriptions of the test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$) โ the size of the array.
The second line of eac... | For each test case, output a single integer โ the minimum number of integers which must be removed from the array to make it vulnerable. | [
[
"6\n7\n3 6 4 9 2 5 2\n5\n5 4 4 2 2\n8\n2 2 4 4 6 6 10 10\n1\n1000\n9\n6 8 9 10 12 9 7 5 4\n7\n300000000 600000000 400000000 900000000 200000000 400000000 200000000",
"2\n0\n6\n0\n4\n2"
]
] | In the first test case, the optimal answer is to remove the numbers $3$ and $9$. Then we are left with $a = [6, 4, 2, 5, 2]$. To show this array is vulnerable, we can first apply a Stalin Sort on the subarray $[4, 2, 5]$ to get $a = [6, 4, 5, 2]$ and then apply a Stalin Sort on the subarray $[6, 4, 5]$ to get $a = [6, ... | Title: Stalin Sort
time_limit_ms: 1000
memory_limit_mb: 256
Description: Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an $\mathcal{O}(n)$ time complexity.
It goes as follows: starting from the second elem... |
1982F | https://codeforces.com/problemset/problem/1982/F | Sorting Problem Again | 2,600 | [
"binary search",
"data structures",
"sortings"
] | Div. 2 | 2,500 | 256 | You have an array $a$ of $n$ elements. There are also $q$ modifications of the array. Before the first modification and after each modification, you would like to know the following:
What is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array $a$ to be completely sorted i... | Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10$) โ the number of test cases. Then follows the description of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 5 \cdot 10^{5}$).
The second line of each test case contains $n$ inte... | For each test case, output $q + 1$ lines. Each line should contain $2$ integers $l, r$ โ the boundaries of the minimum subarray, such that sorting it will make the array $a$ completely sorted. If $a$ is already sorted, then output $l = -1$, $r = -1$. | [
[
"2\n5\n2 2 3 4 5\n3\n2 1\n4 1\n1 1\n5\n1 2 3 4 5\n9\n1 4\n2 3\n5 2\n3 1\n1 1\n5 1\n4 1\n3 1\n2 1",
"-1 -1\n1 2\n1 4\n3 4\n-1 -1\n1 3\n1 3\n1 5\n1 5\n2 5\n2 5\n2 5\n2 5\n-1 -1"
]
] | Let's consider the first test case:
* Initially, the array is sorted in non-decreasing order: $[2, 2, 3, 4, 5]$ * After the first query, the array looks like this: $[\color{red}{2}, \color{red}{1}, 3, 4, 5]$. * After the second query, the array looks like this: $[\color{red}{2}, \color{red}{1}, \color{red}{3},... | Title: Sorting Problem Again
time_limit_ms: 2500
memory_limit_mb: 256
Description: You have an array $a$ of $n$ elements. There are also $q$ modifications of the array. Before the first modification and after each modification, you would like to know the following:
What is the minimum length subarray that needs to be ... |
1992B | https://codeforces.com/problemset/problem/1992/B | Angry Monk | 800 | [
"greedy",
"math",
"sortings"
] | Div. 3 | 2,000 | 256 | To celebrate his recovery, k1o0n has baked an enormous $n$ metres long potato casserole.
Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into $k$ pieces, of lengths $a_1, a_2, \dots, a_k$ meters.
k1o0n wasn't keen on that. Luckily, everything can be fixed. In order... | Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$).
Description of each test case consists of two lines. The first line contains two integers $n$ and $k$ ($2 \le n \le 10^9$, $2 \le k \le 10^5$) โ length of casserole and the number of pieces.
The second l... | For each test case, output the minimum number of operations K1o0n needs to restore his pie after the terror of Noobish_Monk. | [
[
"4\n5 3\n3 1 1\n5 2\n3 2\n11 4\n2 3 1 5\n16 6\n1 6 1 1 1 6",
"2\n3\n9\n15"
]
] | Title: Angry Monk
time_limit_ms: 2000
memory_limit_mb: 256
Description: To celebrate his recovery, k1o0n has baked an enormous $n$ metres long potato casserole.
Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into $k$ pieces, of lengths $a_1, a_2, \dots, a_k$ meters... | |
1974C | https://codeforces.com/problemset/problem/1974/C | Beautiful Triple Pairs | 1,400 | [
"combinatorics",
"data structures"
] | Div. 3 | 4,000 | 256 | Polycarp was given an array $a$ of $n$ integers. He really likes triples of numbers, so for each $j$ ($1 \le j \le n - 2$) he wrote down a triple of elements $[a_j, a_{j + 1}, a_{j + 2}]$.
Polycarp considers a pair of triples $b$ and $c$ beautiful if they differ in exactly one position, that is, one of the following c... | The first line contains a single integer $t$ ($1 \le t \le 10^4$) โ the number of test cases.
The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) โ the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$)... | For each test case, output a single integer โ the number of beautiful pairs of triples among the pairs of the form $[a_j, a_{j + 1}, a_{j + 2}]$.
Note that the answer may not fit into 32-bit data types. | [
[
"8\n5\n3 2 2 2 3\n5\n1 2 1 2 1\n8\n1 2 3 2 2 3 4 2\n4\n2 1 1 1\n8\n2 1 1 2 1 1 1 1\n7\n2 1 1 1 1 1 1\n6\n2 1 1 1 1 1\n5\n2 1 1 1 1",
"2\n0\n3\n1\n8\n4\n3\n2"
]
] | In the first example, $a = [3, 2, 2, 2, 3]$, Polycarp will write the following triples:
1. $[3, 2, 2]$; 2. $[2, 2, 2]$; 3. $[2, 2, 3]$.
The beautiful pairs are triple $1$ with triple $2$ and triple $2$ with triple $3$.
In the third example, $a = [1, 2, 3, 2, 2, 3, 4, 2]$, Polycarp will write the following t... | Title: Beautiful Triple Pairs
time_limit_ms: 4000
memory_limit_mb: 256
Description: Polycarp was given an array $a$ of $n$ integers. He really likes triples of numbers, so for each $j$ ($1 \le j \le n - 2$) he wrote down a triple of elements $[a_j, a_{j + 1}, a_{j + 2}]$.
Polycarp considers a pair of triples $b$ and $... |
2021B | https://codeforces.com/problemset/problem/2021/B | Maximize Mex | 1,200 | [
"brute force",
"greedy",
"math",
"number theory"
] | Div. 2 | 1,000 | 256 | You are given an array $a$ of $n$ positive integers and an integer $x$. You can do the following two-step operation any (possibly zero) number of times:
1. Choose an index $i$ ($1 \leq i \leq n$). 2. Increase $a_i$ by $x$, in other words $a_i := a_i + x$.
Find the maximum value of the $\operatorname{MEX}$ of $a... | Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 2 \cdot 10^5$; $1 \le x \le 10^9$) โ the length of the array and the integer to ... | For each test case, output a single integer: the maximum $\operatorname{MEX}$ of $a$ if you perform the operations optimally. | [
[
"3\n6 3\n0 3 2 1 5 2\n6 2\n1 3 4 1 0 2\n4 5\n2 5 10 3",
"4\n6\n0"
]
] | In the first test case, the $\operatorname{MEX}$ of $a$ is $4$ without performing any operations, which is the maximum.
In the second test case, the $\operatorname{MEX}$ of $a$ is $5$ without performing any operations. If we perform two operations both with $i=1$, we will have the array $a=[5,3,4,1,0,2]$. Then, the $\... | Title: Maximize Mex
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given an array $a$ of $n$ positive integers and an integer $x$. You can do the following two-step operation any (possibly zero) number of times:
1. Choose an index $i$ ($1 \leq i \leq n$). 2. Increase $a_i$ by $x$, in other words $a... |
1973F | https://codeforces.com/problemset/problem/1973/F | Maximum GCD Sum Queries | 3,100 | [
"bitmasks",
"brute force",
"dp",
"implementation",
"number theory"
] | Div. 2 | 5,000 | 512 | For $k$ positive integers $x_1, x_2, \ldots, x_k$, the value $\gcd(x_1, x_2, \ldots, x_k)$ is the greatest common divisor of the integers $x_1, x_2, \ldots, x_k$ โ the largest integer $z$ such that all the integers $x_1, x_2, \ldots, x_k$ are divisible by $z$.
You are given three arrays $a_1, a_2, \ldots, a_n$, $b_1, ... | There are two integers on the first line โ the numbers $n$ and $q$ ($1 \leq n \leq 5 \cdot 10^5$, $1 \leq q \leq 5 \cdot 10^5$).
On the second line, there are $n$ integers โ the numbers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^8$).
On the third line, there are $n$ integers โ the numbers $b_1, b_2, \ldots, b_n$ ($... | Print $q$ integers โ the maximum value you can get for each of the $q$ possible values $d$. | [
[
"3 4\n1 2 3\n4 5 6\n1 1 1\n0 1 2 3",
"2 3 3 3"
],
[
"5 5\n3 4 6 8 4\n8 3 4 9 3\n10 20 30 40 50\n5 55 13 1000 113",
"2 7 3 7 7"
],
[
"1 1\n3\n4\n5\n0",
"7"
]
] | In the first query of the first example, we are not allowed to do any swaps at all, so the answer is $\gcd(1, 2, 3) + \gcd(4, 5, 6) = 2$. In the second query, one of the ways to achieve the optimal value is to swap $a_2$ and $b_2$, then the answer is $\gcd(1, 5, 3) + \gcd(4, 2, 6) = 3$.
In the second query of the seco... | Title: Maximum GCD Sum Queries
time_limit_ms: 5000
memory_limit_mb: 512
Description: For $k$ positive integers $x_1, x_2, \ldots, x_k$, the value $\gcd(x_1, x_2, \ldots, x_k)$ is the greatest common divisor of the integers $x_1, x_2, \ldots, x_k$ โ the largest integer $z$ such that all the integers $x_1, x_2, \ldots, x... |
1985F | https://codeforces.com/problemset/problem/1985/F | Final Boss | 1,500 | [
"binary search",
"data structures"
] | Div. 4 | 2,000 | 256 | You are facing the final boss in your favorite video game. The boss enemy has $h$ health. Your character has $n$ attacks. The $i$'th attack deals $a_i$ damage to the boss but has a cooldown of $c_i$ turns, meaning the next time you can use this attack is turn $x + c_i$ if your current turn is $x$. Each turn, you can us... | The first line contains $t$ ($1 \leq t \leq 10^4$) โ the number of test cases.
The first line of each test case contains two integers $h$ and $n$ ($1 \leq h, n \leq 2 \cdot 10^5$) โ the health of the boss and the number of attacks you have.
The following line of each test case contains $n$ integers $a_1, a_2, ..., a... | For each test case, output an integer, the minimum number of turns required to beat the boss. | [
[
"8\n3 2\n2 1\n2 1\n5 2\n2 1\n2 1\n50 3\n5 6 7\n5 6 7\n50 3\n2 2 2\n3 3 3\n90000 2\n200000 200000\n1 1\n100000 1\n1\n200000\n6 7\n3 2 3 2 3 1 2\n6 5 9 5 10 7 7\n21 6\n1 1 1 1 1 1\n5 5 8 10 7 6",
"1\n3\n15\n25\n1\n19999800001\n1\n21"
]
] | For the first test case, you can use attacks $1$ and $2$ on the first turn, dealing $3$ damage in total, and slaying the boss.
For the second case, you can beat the boss in $3$ turns by using the following attacks:
Turn $1$: Use attacks $1$ and $2$, dealing $3$ damage to the boss. The boss now has $2$ health left.
T... | Title: Final Boss
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are facing the final boss in your favorite video game. The boss enemy has $h$ health. Your character has $n$ attacks. The $i$'th attack deals $a_i$ damage to the boss but has a cooldown of $c_i$ turns, meaning the next time you can use this att... |
2028F | https://codeforces.com/problemset/problem/2028/F | Alice's Adventures in Addition | 2,700 | [
"bitmasks",
"brute force",
"dp",
"implementation"
] | Div. 2 | 3,000 | 32 | Note that the memory limit is unusual.
The Cheshire Cat has a riddle for Alice: given $n$ integers $a_1, a_2, \ldots, a_n$ and a target $m$, is there a way to insert $+$ and $\times$ into the circles of the expression $$a_1 \circ a_2 \circ \cdots \circ a_n = m$$ to make it true? We follow the usual order of operations... | Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) โ the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n, m$ ($1\le n\le 2\cdot 10^5$; $1\le m\le 10^4$) โ the number of integers an... | For each test case, output "YES" without quotes if it is possible to get the target by inserting $+$ or $\times$ and "NO" otherwise.
You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). | [
[
"6\n5 4\n2 1 1 1 2\n5 5\n2 1 1 1 2\n5 6\n2 1 1 1 2\n5 7\n2 1 1 1 2\n5 8\n2 1 1 1 2\n5 6\n2 0 2 2 3",
"YES\nYES\nYES\nYES\nNO\nYES"
]
] | Possible solutions for the first four test cases are shown below. $$\begin{align*} 2 \times 1 + 1 \times 1 \times 2 &= 4 \\\ 2 \times 1 + 1 + 1 \times 2 &= 5 \\\ 2 \times 1 + 1 + 1 \times 2 &= 6 \\\ 2 + 1 + 1 + 1 + 2 &= 7 \\\ \end{align*}$$ It is impossible to get a result of $8$ in the fifth test case. | Title: Alice's Adventures in Addition
time_limit_ms: 3000
memory_limit_mb: 32
Description: Note that the memory limit is unusual.
The Cheshire Cat has a riddle for Alice: given $n$ integers $a_1, a_2, \ldots, a_n$ and a target $m$, is there a way to insert $+$ and $\times$ into the circles of the expression $$a_1 \cir... |
1986C | https://codeforces.com/problemset/problem/1986/C | Update Queries | 1,100 | [
"data structures",
"greedy",
"sortings"
] | Div. 3 | 2,000 | 256 | Let's consider the following simple problem. You are given a string $s$ of length $n$, consisting of lowercase Latin letters, as well as an array of indices $ind$ of length $m$ ($1 \leq ind_i \leq n$) and a string $c$ of length $m$, consisting of lowercase Latin letters. Then, in order, you perform the update operation... | Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) โ the number of sets of input data. Then follows their description.
The first line of each set of input data contains two integers $n$ and $m$ ($1 \leq n, m \leq 10^5$) โ the length of the string $s$ a... | For each set of input data, output the lexicographically smallest string $s$ that can be obtained by rearranging the indices in the array $ind$ and the letters in the string $c$ as you like. | [
[
"4\n1 2\na\n1 1\ncb\n4 4\nmeow\n1 2 1 4\nzcwz\n7 4\nabacaba\n1 3 5 7\ndamn\n7 10\ntraktor\n7 6 5 4 3 2 1 6 4 2\ncodeforces",
"b\ncwoz\nabdcmbn\nccdeefo"
]
] | In the first set of input data, you can leave the array $ind$ and the string $c$ unchanged and simply perform all operations in that order.
In the second set of input data, you can set the array $ind = [1, 1, 4, 2]$ and $c =$ "zczw". Then the string $s$ will change as follows: $meow \rightarrow zeow \rightarrow ceow \... | Title: Update Queries
time_limit_ms: 2000
memory_limit_mb: 256
Description: Let's consider the following simple problem. You are given a string $s$ of length $n$, consisting of lowercase Latin letters, as well as an array of indices $ind$ of length $m$ ($1 \leq ind_i \leq n$) and a string $c$ of length $m$, consisting ... |
2025E | https://codeforces.com/problemset/problem/2025/E | Card Game | 2,200 | [
"combinatorics",
"dp",
"fft",
"greedy",
"math"
] | Div. 2 | 2,000 | 512 | In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the deck for each combination of suit and rank.
A card with suit $a$ and rank... | The only line contains two integers $n$ and $m$ ($1 \le n, m \le 500$).
Additional constraint on the input: $m$ is even. | Print a single integer โ the number of ways to distribute the cards so that the first player wins, taken modulo $998244353$. | [
[
"1 4",
"2"
],
[
"2 2",
"2"
],
[
"3 6",
"1690"
],
[
"5 4",
"568"
],
[
"500 500",
"84693741"
]
] | Title: Card Game
time_limit_ms: 2000
memory_limit_mb: 512
Description: In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the dec... | |
1989B | https://codeforces.com/problemset/problem/1989/B | Substring and Subsequence | 1,200 | [
"brute force",
"greedy",
"strings"
] | Div. 2 | 2,000 | 256 | You are given two strings $a$ and $b$, both consisting of lowercase Latin letters.
A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a string is a contiguous subsequence of that string.
For example, consider the string a... | The first line contains a single integer $t$ ($1 \le t \le 10^3$) โ the number of test cases.
The first line of each test case contains a string $a$ ($1 \le |a| \le 100$), consisting of lowercase Latin letters.
The second line of each test case contains a string $b$ ($1 \le |b| \le 100$), consisting of lowercase Lati... | For each test case, print a single integer โ the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence. | [
[
"5\naba\ncb\ner\ncf\nmmm\nmmm\ncontest\ntest\ncde\nabcefg",
"4\n4\n3\n7\n7"
]
] | In the examples below, the characters that correspond to the subsequence equal to $b$ are bolded.
In the first example, one of the possible answers is caba.
In the second example, one of the possible answers is ercf.
In the third example, one of the possible answers is mmm.
In the fourth example, one of the possibl... | Title: Substring and Subsequence
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given two strings $a$ and $b$, both consisting of lowercase Latin letters.
A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a... |
1986F | https://codeforces.com/problemset/problem/1986/F | Non-academic Problem | 1,900 | [
"dfs and similar",
"graphs",
"trees"
] | Div. 3 | 2,000 | 256 | You are given a connected undirected graph, the vertices of which are numbered with integers from $1$ to $n$. Your task is to minimize the number of pairs of vertices $1 \leq u < v \leq n$ between which there exists a path in this graph. To achieve this, you can remove exactly one edge from the graph.
Find the smalles... | Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) โ the number of sets of input data. Then follows their description.
The first line of each set of input data contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $n - 1 \leq m \leq \min(10^5, \frac... | For each set of input data, output the smallest number of pairs of reachable vertices, if exactly one edge can be removed. | [
[
"6\n2 1\n1 2\n3 3\n1 2\n2 3\n1 3\n5 5\n1 2\n1 3\n3 4\n4 5\n5 3\n6 7\n1 2\n1 3\n2 3\n3 4\n4 5\n4 6\n5 6\n5 5\n1 2\n1 3\n2 3\n2 4\n3 5\n10 12\n1 2\n1 3\n2 3\n2 4\n4 5\n5 6\n6 7\n7 4\n3 8\n8 9\n9 10\n10 8",
"0\n3\n4\n6\n6\n21"
]
] | In the first set of input data, we will remove the single edge $(1, 2)$ and the only pair of vertices $(1, 2)$ will become unreachable from each other.
In the second set of input data, no matter which edge we remove, all vertices will be reachable from each other.
In the fourth set of input data, the graph looks like... | Title: Non-academic Problem
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given a connected undirected graph, the vertices of which are numbered with integers from $1$ to $n$. Your task is to minimize the number of pairs of vertices $1 \leq u < v \leq n$ between which there exists a path in this graph. ... |
End of preview. Expand in Data Studio
README.md exists but content is empty.
- Downloads last month
- 12