1337xyz1337xyz/codeforces-editorial-full-2026-07-16 huggingface dataset:
Edge Elimination
Contest: Codeforces Round 783 (Div. 1)
Difficulty rating: 2900
Tags: constructive algorithms, dfs and similar, dp, trees

You are given a tree (connected, undirected, acyclic graph) with $$$n$$$ vertices. Two edges are adjacent if they share exactly one endpoint. In one move you can remove an arbitrary edge, if that edge is adjacent to an even number of remaining edges.

Remove all of the edges, or determine that it is impossible. If there are multiple solutions, print any.

Input format:
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 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$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of vertices in the tree.

Then $$$n-1$$$ lines follow. The $$$i$$$-th of them contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i,v_i \le n$$$) the endpoints of the $$$i$$$-th edge. It is guaranteed that the given graph is a tree.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case print "NO" if it is impossible to remove all the edges.

Otherwise print "YES", and in the next $$$n-1$$$ lines print a possible order of the removed edges. For each edge, print its endpoints in any order.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
Test case $$$1$$$: it is possible to remove the edge, because it is not adjacent to any other edge.

Test case $$$2$$$: both edges are adjacent to exactly one edge, so it is impossible to remove any of them. So the answer is "NO".

Test case $$$3$$$: the edge $$$2-3$$$ is adjacent to two other edges. So it is possible to remove it. After that removal it is possible to remove the remaining edges too.

Example input:
5
2
1 2
3
1 2
2 3
4
1 2
2 3
3 4
5
1 2
2 3
3 4
3 5
7
1 2
1 3
2 4
2 5
3 6
3 7

Example output:
YES
2 1
NO
YES
2 3
3 4
2 1
YES
3 5
2 3
2 1
4 3
NO

Editorial:
When an edge is removed, the two neighbouring vertex have the same parity of edges. We say that an edge is odd, if the parity is odd, and the edge is even otherwise.
One can see, that a vertex with even degree will have the same amount of odd and even edges. For a vertex with odd degree, there will be one more odd edge.
Starting from the leaves, we can decide the parity of each edge (an edge connected to a leaf is odd).
If there is a contradiction somewhere than the answer is no.
Otherwise, there is a construction.
In each vertex decide the removal order of the outgoing edges. Any order is good, when it always changes parity, and ends with an odd edge. Consider the directed graph with these conditions. One can see, that this graph is acyclic, so there is a topological order of that graph which will satisfy all the conditions.
Also, it is possible to solve it recursively.
YES or YES?
Contest: Codeforces Round 806 (Div. 4)
Difficulty rating: 800
Tags: brute force, implementation, strings

There is a string $$$s$$$ of length $$$3$$$, consisting of uppercase and lowercase English letters. Check if it is equal to "YES" (without quotes), where each letter can be in any case. For example, "yES", "Yes", "yes" are all allowable.

Input format:
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of testcases.

The description of each test consists of one line containing one string $$$s$$$ consisting of three characters. Each character of $$$s$$$ is either an uppercase or lowercase English letter.

Output format:
For each test case, output "YES" (without quotes) if $$$s$$$ satisfies the condition, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
The first five test cases contain the strings "YES", "yES", "yes", "Yes", "YeS". All of these are equal to "YES", where each character is either uppercase or lowercase.

Example input:
10
YES
yES
yes
Yes
YeS
Noo
orZ
yEz
Yas
XES

Example output:
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO

Editorial:
You should implement what is written in the statement. Here are three ways to do it:
• Check that the first character is $$$\texttt{Y}$$$ or $$$\texttt{y}$$$, check that the second character is $$$\texttt{E}$$$ or $$$\texttt{e}$$$, and check the third character is $$$\texttt{S}$$$ or $$$\texttt{s}$$$.
• Make an array storing all acceptable strings (there are only $$$8$$$), and loop and see if any of the strings match the input.
• Use some built-in function like tolower() in C++ to make the string all lowercase, and check if $$$s$$$ is equal to $$$\texttt{yes}$$$.
ICPC Balloons
Contest: Codeforces Round 806 (Div. 4)
Difficulty rating: 800
Tags: data structures, implementation

In an ICPC contest, balloons are distributed as follows:

- Whenever a team solves a problem, that team gets a balloon.
- The first team to solve a problem gets an additional balloon.

Determine the total number of balloons that the teams received. Note that some problems may be solved by none of the teams.

Input format:
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of testcases.

The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 50$$$) — the length of the string.

The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of uppercase English letters, denoting the order of solved problems.

Output format:
For each test case, output a single integer — the total number of balloons that the teams received.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, $$$5$$$ balloons are given out:

- Problem $$$\textsf{A}$$$ is solved. That team receives $$$2$$$ balloons: one because they solved the problem, an an additional one because they are the first team to solve problem $$$\textsf{A}$$$.
- Problem $$$\textsf{B}$$$ is solved. That team receives $$$2$$$ balloons: one because they solved the problem, an an additional one because they are the first team to solve problem $$$\textsf{B}$$$.
- Problem $$$\textsf{A}$$$ is solved. That team receives only $$$1$$$ balloon, because they solved the problem. Note that they don't get an additional balloon because they are not the first team to solve problem $$$\textsf{A}$$$.

In the second test case, there is only one problem solved. The team who solved it receives $$$2$$$ balloons: one because they solved the problem, an an additional one because they are the first team to solve problem $$$\textsf{A}$$$.

Example input:
6
3
ABA
1
A
3
ORZ
5
BAAAA
4
BKPT
10
CODEFORCES

Example output:
5
2
6
7
8
17

Editorial:
Let's keep an array $$$a$$$ of booleans, $$$a_i$$$ denoting whether or not some team has solved the $$$i$$$th problem already. Now we can iterate through the string from left to right and keep a running total $$$\mathrm{tot}$$$. If $$$a_i$$$ is true (the $$$i$$$-th problem has already been solved), increase $$$\mathrm{tot}$$$ by $$$1$$$; otherwise, increase $$$\mathrm{tot}$$$ by $$$2$$$ and set $$$a_i$$$ to true.
The time complexity is $$$\mathcal{O}(n)$$$.
Bonus: the answer is always $$$n + \text{number of distinct characters in }s$$$. Can you see why?
Cypher
Contest: Codeforces Round 806 (Div. 4)
Difficulty rating: 800
Tags: brute force, implementation, strings

Luca has a cypher made up of a sequence of $$$n$$$ wheels, each with a digit $$$a_i$$$ written on it. On the $$$i$$$-th wheel, he made $$$b_i$$$ moves. Each move is one of two types:

- up move (denoted by $$$\texttt{U}$$$): it increases the $$$i$$$-th digit by $$$1$$$. After applying the up move on $$$9$$$, it becomes $$$0$$$.
- down move (denoted by $$$\texttt{D}$$$): it decreases the $$$i$$$-th digit by $$$1$$$. After applying the down move on $$$0$$$, it becomes $$$9$$$.

Example for $$$n=4$$$. The current sequence is 0 0 0 0.

Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the number of wheels.

The second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 \leq a_i \leq 9$$$) — the digit shown on the $$$i$$$-th wheel after all moves have been performed.

Then $$$n$$$ lines follow, the $$$i$$$-th of which contains the integer $$$b_i$$$ ($$$1 \leq b_i \leq 10$$$) and $$$b_i$$$ characters that are either $$$\texttt{U}$$$ or $$$\texttt{D}$$$ — the number of moves performed on the $$$i$$$-th wheel, and the moves performed. $$$\texttt{U}$$$ and $$$\texttt{D}$$$ represent an up move and a down move respectively.

Output format:
For each test case, output $$$n$$$ space-separated digits  — the initial sequence of the cypher.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, we can prove that initial sequence was $$$[2,1,1]$$$. In that case, the following moves were performed:

- On the first wheel: $$$2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{D}]{} 9$$$.
- On the second wheel: $$$1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{U}]{} 3$$$.
- On the third wheel: $$$1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{U}]{} 1$$$.

Example input:
3
3
9 3 1
3 DDD
4 UDUU
2 DU
2
0 9
9 DDDDDDDDD
9 UUUUUUUUU
5
0 5 9 8 3
10 UUUUUUUUUU
3 UUD
8 UUDUUDDD
10 UUDUUDUDDU
4 UUUU

Example output:
2 1 1 
9 0 
0 4 9 6 9

Editorial:
We will perform each move in reverse from the final sequence of the cypher.
• down move: it increases the $$$i$$$-th digit by $$$1$$$. After applying the up move on $$$9$$$, it becomes $$$0$$$.
• up move (denoted by $$$\texttt{D}$$$): it decreases the $$$i$$$-th digit by $$$1$$$. After applying the down move on $$$0$$$, it becomes $$$9$$$.
Double Strings
Contest: Codeforces Round 806 (Div. 4)
Difficulty rating: 1100
Tags: brute force, data structures, strings

You are given $$$n$$$ strings $$$s_1, s_2, \dots, s_n$$$ of length at most $$$\mathbf{8}$$$.

For each string $$$s_i$$$, determine if there exist two strings $$$s_j$$$ and $$$s_k$$$ such that $$$s_i = s_j + s_k$$$. That is, $$$s_i$$$ is the concatenation of $$$s_j$$$ and $$$s_k$$$. Note that $$$j$$$ can be equal to $$$k$$$.

Recall that the concatenation of strings $$$s$$$ and $$$t$$$ is $$$s + t = s_1 s_2 \dots s_p t_1 t_2 \dots t_q$$$, where $$$p$$$ and $$$q$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. For example, concatenation of "code" and "forces" is "codeforces".

Input format:
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$$$ ($$$1 \leq n \leq 10^5$$$) — the number of strings.

Then $$$n$$$ lines follow, the $$$i$$$-th of which contains non-empty string $$$s_i$$$ of length at most $$$\mathbf{8}$$$, consisting of lowercase English letters. Among the given $$$n$$$ strings, there may be equal (duplicates).

The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

Output format:
For each test case, output a binary string of length $$$n$$$. The $$$i$$$-th bit should be $$$\texttt{1}$$$ if there exist two strings $$$s_j$$$ and $$$s_k$$$ where $$$s_i = s_j + s_k$$$, and $$$\texttt{0}$$$ otherwise. Note that $$$j$$$ can be equal to $$$k$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case, we have the following:

- $$$s_1 = s_2 + s_2$$$, since $$$\texttt{abab} = \texttt{ab} + \texttt{ab}$$$. Remember that $$$j$$$ can be equal to $$$k$$$.
- $$$s_2$$$ is not the concatenation of any two strings in the list.
- $$$s_3 = s_2 + s_5$$$, since $$$\texttt{abc} = \texttt{ab} + \texttt{c}$$$.
- $$$s_4$$$ is not the concatenation of any two strings in the list.
- $$$s_5$$$ is not the concatenation of any two strings in the list.

Example input:
3
5
abab
ab
abc
abacb
c
3
x
xx
xxx
8
codeforc
es
codes
cod
forc
forces
e
code

Example output:
10100
011
10100101

Editorial:
Use some data structure that allows you to answer queries of the form: "does the string $$$t$$$ appear in the array $$$s_1, \dots, s_n$$$?" For example, in C++ you can use a map<string, bool>, while in Python you can use a dictionary dict.
Afterwards, for each string $$$s$$$, brute force all strings $$$x$$$ and $$$y$$$ such that $$$s = x + y$$$. There are at most $$$7$$$ such strings, because $$$s$$$ has length at most $$$8$$$. Then check if both $$$x$$$ and $$$y$$$ appear in the array using your data structure.
The time complexity is $$$\mathcal{O}(\ell n \log n)$$$ per test case, where $$$\ell$$$ is the maximum length of an input string.
Mirror Grid
Contest: Codeforces Round 806 (Div. 4)
Difficulty rating: 1200
Tags: implementation

You are given a square grid with $$$n$$$ rows and $$$n$$$ columns. Each cell contains either $$$0$$$ or $$$1$$$.

In an operation, you can select a cell of the grid and flip it (from $$$0 \to 1$$$ or $$$1 \to 0$$$). Find the minimum number of operations you need to obtain a square that remains the same when rotated $$$0^{\circ}$$$, $$$90^{\circ}$$$, $$$180^{\circ}$$$ and $$$270^{\circ}$$$.

The picture below shows an example of all rotations of a grid.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the size of the grid.

Then $$$n$$$ lines follow, each with $$$n$$$ characters $$$a_{i,j}$$$ ($$$0 \leq a_{i,j} \leq 1$$$) — the number written in each cell.

Output format:
For each test case output a single integer  — the minimum number of operations needed to make the square look the same rotated $$$0^{\circ}$$$, $$$90^{\circ}$$$, $$$180^{\circ}$$$ and $$$270^{\circ}$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case, we can perform one operations to make the grid $$$\begin{matrix}0 & 1 & 0\\ 1 & 1 & \color{red}{1}\\ 0 & 1 & 0\end{matrix}$$$. Now, all rotations of the square are the same.

In the second test case, all rotations of the square are already the same, so we don't need any flips.

Example input:
5
3
010
110
010
1
0
5
11100
11011
01011
10011
11000
5
01000
10101
01010
00010
01001
5
11001
00000
11111
10110
01111

Example output:
1
0
9
7
6

Editorial:
Let's rotate the grid by $$$0^{\circ}$$$, $$$90^{\circ}$$$, $$$180^{\circ}$$$, and $$$270^{\circ}$$$, and mark all cells that map to each other under these rotations. For example, for $$$4 \times 4$$$ and $$$5 \times 5$$$ grids, mirror grid must have the following patterns, the same letters denoting equal values:
$$$$$$\begin{matrix} a & b & c & a\\ c & d & d & b\\ b & d & d & c\\ a & c & b & a \end{matrix} \qquad \qquad \begin{matrix} a & b & c & d & a\\ d & e & f & e & b\\ c & f & g & f & c\\ b & e & f & e & d\\ a & d & c & b & a \end{matrix}$$$$$$
In general, we can rotate the grid by $$$0^{\circ}$$$, $$$90^{\circ}$$$, $$$180^{\circ}$$$, and $$$270^{\circ}$$$ and see which cells need to have equal values by seeing the positions which each cell maps to.
Now to solve the problem, we consider each equal value (each of the letters $$$a$$$, $$$b$$$, $$$c$$$, ... in the above figures) independently, and consider the minimum number of moves to make them all $$$0$$$ or all $$$1$$$. The answer is the total across all values. See the implementation for better understanding.
The time complexity is $$$\mathcal{O}(n^2)$$$ per testcase.
Yet Another Problem About Pairs Satisfying an Inequality
Contest: Codeforces Round 806 (Div. 4)
Difficulty rating: 1300
Tags: binary search, data structures, dp, greedy, sortings

You are given an array $$$a_1, a_2, \dots a_n$$$. Count the number of pairs of indices $$$1 \leq i, j \leq n$$$ such that $$$a_i < i < a_j < j$$$.

Input format:
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of the array.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10^9$$$) — the elements of the array.

It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, output a single integer — the number of pairs of indices satisfying the condition in the statement.

Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
For the first test cases the pairs are $$$(i, j)$$$ = $$$\{(2, 4), (2, 8), (3, 8)\}$$$.

- The pair $$$(2, 4)$$$ is true because $$$a_2 = 1$$$, $$$a_4 = 3$$$ and $$$1 < 2 < 3 < 4$$$.
- The pair $$$(2, 8)$$$ is true because $$$a_2 = 1$$$, $$$a_8 = 4$$$ and $$$1 < 2 < 4 < 8$$$.
- The pair $$$(3, 8)$$$ is true because $$$a_3 = 2$$$, $$$a_8 = 4$$$ and $$$2 < 3 < 4 < 8$$$.

Example input:
5
8
1 1 2 3 8 2 1 4
2
1 2
10
0 2 1 6 3 4 1 2 8 3
2
1 1000000000
3
0 1000000000 2

Example output:
3
0
10
0
1

Editorial:
Call a pair good if it satisfies the condition. Let's split the inequality into three parts: $$$a_i < i$$$, $$$i < a_j$$$, $$$a_j < j$$$.
Note that if $$$a_i \geq i$$$ for any $$$i$$$, then it can't be an element of a good pair, because it fails the first and third conditions. So we can throw out all elements of the array satisfying $$$a_i \geq i$$$.
For the remaining elements, the first and third inequalities are already satisfied, so we only have to count the number of pairs $$$(i,j)$$$ with $$$i < a_j$$$. Let's iterate $$$j$$$ through the array from the left to the right, and make a list storing all $$$i$$$ that appear before $$$j$$$. Then for each $$$j$$$, count the number of $$$i$$$ less than $$$a_j$$$ by binary searching on the number of elements in the list less than $$$a_j$$$. Afterwards, add $$$j$$$ to the end of the list.
Since we iterate from left to right, the list will always remain sorted (we insert the indices of elements, which are increasing from left to right), so the binary search will always work.
The time complexity is $$$\mathcal{O}(n \log n)$$$.
Gennady and a Card Game
Contest: Hello 2019
Difficulty rating: 800
Tags: brute force, implementation

Gennady owns a small hotel in the countryside where he lives a peaceful life. He loves to take long walks, watch sunsets and play cards with tourists staying in his hotel. His favorite game is called "Mau-Mau".

To play Mau-Mau, you need a pack of $$$52$$$ cards. Each card has a suit (Diamonds — D, Clubs — C, Spades — S, or Hearts — H), and a rank (2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, or A).

At the start of the game, there is one card on the table and you have five cards in your hand. You can play a card from your hand if and only if it has the same rank or the same suit as the card on the table.

In order to check if you'd be a good playing partner, Gennady has prepared a task for you. Given the card on the table and five cards in your hand, check if you can play at least one card.

Input format:
The first line of the input contains one string which describes the card on the table. The second line contains five strings which describe the cards in your hand.

Each string is two characters long. The first character denotes the rank and belongs to the set $$$\{{\tt 2}, {\tt 3}, {\tt 4}, {\tt 5}, {\tt 6}, {\tt 7}, {\tt 8}, {\tt 9}, {\tt T}, {\tt J}, {\tt Q}, {\tt K}, {\tt A}\}$$$. The second character denotes the suit and belongs to the set $$$\{{\tt D}, {\tt C}, {\tt S}, {\tt H}\}$$$.

All the cards in the input are different.

Output format:
If it is possible to play a card from your hand, print one word "YES". Otherwise, print "NO".

You can print each letter in any case (upper or lower).

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, there is an Ace of Spades (AS) on the table. You can play an Ace of Diamonds (AD) because both of them are Aces.

In the second example, you cannot play any card.

In the third example, you can play an Ace of Diamonds (AD) because it has the same suit as a Four of Diamonds (4D), which lies on the table.

Example input:
AS
2H 4C TH JH AD

Example output:
YES

Example input:
2H
3D 4C AC KD AS

Example output:
NO

Editorial:
The task is to check if some card in the second line has a common character with the card in the first line. The best way to do this is to create a single string for the card in the hand and an array of strings for the cards on the table.
After reading the input, just iterate with a loop over the array. If a string in the array has any common character with the single string, then output "YES". If no similarity was found, output "NO".
Petr and a Combination Lock
Contest: Hello 2019
Difficulty rating: 1200
Tags: bitmasks, brute force, dp

Petr has just bought a new car. He's just arrived at the most known Petersburg's petrol station to refuel it when he suddenly discovered that the petrol tank is secured with a combination lock! The lock has a scale of $$$360$$$ degrees and a pointer which initially points at zero:

Petr called his car dealer, who instructed him to rotate the lock's wheel exactly $$$n$$$ times. The $$$i$$$-th rotation should be $$$a_i$$$ degrees, either clockwise or counterclockwise, and after all $$$n$$$ rotations the pointer should again point at zero.

This confused Petr a little bit as he isn't sure which rotations should be done clockwise and which should be done counterclockwise. As there are many possible ways of rotating the lock, help him and find out whether there exists at least one, such that after all $$$n$$$ rotations the pointer will point at zero again.

Input format:
The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 15$$$) — the number of rotations.

Each of the following $$$n$$$ lines contains one integer $$$a_i$$$ ($$$1 \leq a_i \leq 180$$$) — the angle of the $$$i$$$-th rotation in degrees.

Output format:
If it is possible to do all the rotations so that the pointer will point at zero after all of them are performed, print a single word "YES". Otherwise, print "NO". Petr will probably buy a new car in this case.

You can print each letter in any case (upper or lower).

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, we can achieve our goal by applying the first and the second rotation clockwise, and performing the third rotation counterclockwise.

In the second example, it's impossible to perform the rotations in order to make the pointer point at zero in the end.

In the third example, Petr can do all three rotations clockwise. In this case, the whole wheel will be rotated by $$$360$$$ degrees clockwise and the pointer will point at zero again.

Example input:
3
10
20
30

Example output:
YES

Example input:
3
10
10
10

Example output:
NO

Editorial:
The best way to check the condition in the statement is to use bitmasks. Iterate from $$$0$$$ to $$$2^n-1$$$ and for each number consider its binary representation. If the $$$i$$$-th bit of the representation is set to $$$0$$$, then decide to perform the $$$i$$$-th rotation clockwise. In the opposite case, do it counterclockwise. Finally, check if the whole angle is a multiple of $$$360$$$ and if so, output "YES". If no good combination of rotations was found, output "NO".