Innopolis Open 2025-2026. Final round
A. Forgetful Shustrik and the Remote Control
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The hamster Shustrik received an unusual calculator as a gift from the hedgehog Shurshik. Initially, the number $$$A$$$ is displayed on the calculator screen.

The instructions for the calculator specify a fixed number $$$M$$$. All arithmetic operations are performed with this number. Additionally, Shurshik has thought of numbers $$$N$$$ and $$$B$$$ and informed Shustrik about them.

The calculator can perform only four operations, and in one step, one of the following operations can be applied:

  • Operation "+": replace the current value $$$A$$$ with $$$A + M$$$.
  • Operation "-": replace the current value $$$A$$$ with $$$A - M$$$.
  • Operation "*": replace the current value $$$A$$$ with $$$A \cdot M$$$.
  • Operation "%": replace the current value $$$A$$$ with the remainder of the division $$$A$$$ by $$$M$$$. After this operation, the condition $$$0 \le A \lt M$$$ holds.

After performing some (possibly empty) sequence of operations, the number $$$A'$$$ will be obtained.

The hedgehog Shurshik became curious whether Shustrik can obtain such a number $$$A'$$$ that

$$$$$$ A' \equiv B \pmod N. $$$$$$

In other words, the numbers $$$A'$$$ and $$$B$$$ must have the same remainders when divided by $$$N$$$.

Your task is to help the hamster find any sequence of operations of the smallest length, after which the specified condition will be satisfied, or determine that it is impossible to obtain such a number.

Input

In a single line, four integers are given separated by spaces: $$$A$$$, $$$M$$$, $$$B$$$, and $$$N$$$ ($$$|A| \leq 10^9$$$, $$$1 \leq M \leq 10^9$$$, $$$0 \leq B \lt N \leq 2 \cdot 10^6$$$).

Output

If the desired sequence of operations does not exist, output "-1" in a single line.

Otherwise, in the first line, output the length $$$x$$$ of the minimal suitable sequence. In the second line, output a string of length $$$x$$$ consisting of the characters "+", "-", "*", and "%" — the sequence of operations that transforms the initial $$$A$$$ into such $$$A'$$$ that $$$A' \equiv B \pmod N$$$.

If there are multiple such sequences, output any of them.

It can be proven that the length of the answer does not exceed $$$N$$$.

Scoring

Points for each subtask are awarded only if all tests for that subtask and necessary subtasks are successfully passed.

GroupPointsAdditional constraints Required groups
17$$$|A|, M, N \le 8$$$
26$$$M = 1$$$
38$$$M = N$$$
412$$$M \equiv 0 \pmod N$$$3
515$$$N \equiv 0 \pmod M$$$2–3
616$$$N \cdot M \le 2 \cdot 10^{6}$$$1–2
718$$$A = 0$$$
8181–7
Examples
Input
1 3 2 5
Output
2
+*
Input
6 7 2 4
Output
0

Input
-1 3 2 4
Output
1
+
Input
5 6 1 6
Output
-1
Input
0 6 3 7
Output
3
---
Input
5 9 1 8
Output
3
*%+
Note

Consider the answer to the first example of the input data:

  • Initially, $$$A = 1$$$.
  • Operation "+": replace $$$A$$$ with $$$A + 3 = 1 + 3 = 4$$$.
  • Operation "*": replace $$$A$$$ with $$$A \cdot 3 = 4 \cdot 3 = 12$$$.
  • We get $$$A' = 12$$$. At the same time, $$$A' \equiv B \pmod N$$$ holds true, since $$$12 \equiv 2 \pmod 5$$$.
It can be proven that in this example there is no sequence of zero or one operation that results in a suitable $$$A'$$$. A sequence of two operations "++" is also suitable: $$$1+3+3 = 7 \equiv 2 \pmod 5$$$.

B. Grand Renaming
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

On the monorail line, there are $$$n$$$ stations, numbered in order from $$$1$$$ to $$$n$$$.

Each station $$$i$$$ has a name $$$s_i$$$ — a string of no more than 20 Latin letters. Each letter is written on a separate nameplate.

As part of the urban infrastructure upgrade project, it was decided to rename all the stations to some new names. The order for renaming has already been signed and will take effect tomorrow. However, there is a problem: no one has had time to replace the names on the nameplates at the stations. Now it is your task.

Unfortunately, there is no time to make new nameplates. Therefore, the only possible option is to move the nameplates from one station to another.

Formally, in one operation, you can choose two indices $$$1 \le i, j \le n$$$, as well as any letter from the string $$$s_i$$$, and move that letter to any position in the string $$$s_j$$$. This operation takes $$$1 + |i - j|$$$ precious units of time.

For example, if $$$s_i = \texttt{ab}$$$ and $$$s_j = \texttt{cd}$$$, then in one operation of moving a letter from $$$i$$$ to $$$j$$$, you can do:

  • $$$s_i = \texttt{a}$$$; $$$s_j = \texttt{bcd}$$$ or $$$s_j = \texttt{cbd}$$$ or $$$s_j = \texttt{cdb}$$$.
  • $$$s_i = \texttt{b}$$$; $$$s_j = \texttt{acd}$$$ or $$$s_j = \texttt{cad}$$$ or $$$s_j = \texttt{cda}$$$.

Note that it is also allowed to choose $$$i = j$$$ in the operation.

You need to replace the nameplates with names at each station as quickly as possible so that they correspond to the new names. Determine whether this is possible, and if so, the minimum necessary amount of time.

Input

The first line of the test contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of stations on the line.

The second line of the test contains $$$n$$$ strings: $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le |s_i| \le 20$$$) — the original names of the stations.

The third line of the test contains $$$n$$$ strings: $$$t_1, t_2, \ldots, t_n$$$ ($$$1 \le |t_i| \le 20$$$) — the desired new names of the stations.

It is guaranteed that all strings in the test consist of lowercase Latin letters, characters from 'a' to 'z'.

Output

If there is no way to rename all the stations, output $$$-1$$$ in a single line. Otherwise, output a single number: the minimum possible total time required for the renaming operations.

Scoring
GroupPointsAdditional constraints Required groups
114$$$n = 1$$$
217$$$\operatorname{sorted}(s_i) = \operatorname{sorted}(t_i) $$$
325$$$s_i$$$ and $$$t_i$$$ have no common letters
413$$$n \leq 1000; |s_i|, |t_i| \leq 10$$$
511$$$|s_i| = 1$$$, $$$|t_i| = 1$$$
611All strings consist of the letter a
791–6

Here, $$$\operatorname{sorted}(s_i)$$$ denotes the string $$$s_i$$$ sorted in alphabetical order. For example, $$$\operatorname{sorted}(\texttt{abacaba}) = \texttt{aaaabbc}$$$

Examples
Input
3
no iislop nepon
inno polis open
Output
10
Input
3
eleven plus two
twelve plus one
Output
12
Input
1
evil
live
Output
3
Input
2
a b
ba ab
Output
-1
Input
2
indian cat
init canada
Output
-1
Input
4
aaaa aaa aa a
a aa aaa aaaa
Output
14
Input
7
s e a s i d e
d i s e a s e
Output
22

C. Olympiad Schedule
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Persik — a shark in the world of programming — decided to create a schedule for all tracks of the Innopolis Open olympiad.

There are $$$D$$$ days in the academic year, numbered from $$$1$$$ to $$$D$$$. In total, $$$n$$$ olympiads are held.

The olympiads are numbered from $$$1$$$ to $$$n$$$ in non-decreasing order of their initially scheduled dates. Each olympiad takes place on exactly one day. For each olympiad, its initially scheduled date and its benefit for the younger generation are known.

You may change the date of any olympiad, but only to a later day within the same academic year (that is, the new date cannot be earlier than the original one and cannot exceed $$$D$$$).

After all changes, the olympiads must remain in non-decreasing order of their scheduled dates.

The total benefit of the olympiad season is defined as follows. For each day, consider all olympiads scheduled on that day. If at least one olympiad is scheduled on a given day, the benefit of that day is the maximum benefit among those olympiads. If no olympiads are scheduled on that day, the benefit of that day is $$$0$$$.

The benefit of the season is the sum of the benefits over all $$$D$$$ days.

For the benefit of the students, determine the maximum possible total benefit of the season.

Input

The first line contains three integers $$$n$$$, $$$D$$$, and $$$t$$$ — the number of olympiads and the number of days in the academic year, as well as the type of answer ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le D \le 2 \cdot 10^9$$$; $$$1 \le t \le 2$$$).

In the $$$i$$$-th of the following $$$n$$$ lines, two integers $$$d_i$$$ and $$$b_i$$$ are given — the scheduled date (day number) of the $$$i$$$-th olympiad and its benefit ($$$1 \le d_i \le D$$$; $$$1 \le b_i \le 2 \cdot 10^9$$$).

It is guaranteed that $$$d_i \le d_j$$$ for all $$$i$$$ and $$$j$$$ such that $$$i \lt j$$$.

Output

If $$$t = 1$$$, output a single integer — the maximum possible benefit of the olympiad season.

If $$$t = 2$$$, then in the first line output a single integer — the maximum possible benefit of the olympiad season; and in the second line output the schedule of the olympiads — $$$n$$$ integers, where the $$$i$$$-th is the final date of the $$$i$$$-th olympiad. The specified final dates must achieve the maximum possible benefit of the olympiad season.

Scoring

The tests for this problem consist of thirteen groups. Points for each group are awarded only if all tests of the group and all tests necessary for it are passed.

GroupPointsAdditional constraints Necessary groups
110$$$n, D \le 20$$$; $$$b_i \le 20$$$ for all $$$i$$$; $$$t = 1$$$
27$$$n, D \le 20$$$; $$$b_i \le 20$$$ for all $$$i$$$1
311$$$n, D \le 100$$$; $$$b_i \le 100$$$ for all $$$i$$$1, 2
49$$$n, D \le 800$$$; $$$b_i \le 800$$$ for all $$$i$$$1, 2, 3
55$$$n, D \le 2500$$$; $$$b_i \le 2500$$$ for all $$$i$$$; $$$t = 1$$$1
64$$$n, D \le 2500$$$; $$$b_i \le 2500$$$ for all $$$i$$$1–5
75$$$b_i = b_j$$$ for all $$$i$$$, $$$j$$$
85$$$d_i \neq d_j$$$ for all $$$i$$$ and $$$j$$$, such that $$$i \neq j$$$
95$$$d_i = 1$$$ for all $$$i$$$; $$$t = 1$$$
105$$$d_i = d_j$$$ for all $$$i$$$, $$$j$$$9
118$$$D \le 2 \cdot 10^6$$$1–6
1210$$$t = 1$$$1, 5, 9
13161–12
Examples
Input
3 10 1
1 3
1 5
1 2
Output
10
Input
3 10 2
1 3
1 5
1 2
Output
10
1 2 3
Input
5 6 2
1 7
2 6
5 8
5 9
6 4
Output
30
1 3 5 6 6
Note

Note that the answer for the second test will not be suitable as an answer for the first, as for $$$t = 1$$$ only one number needs to be output.

In the second example, the benefit of the first day is $$$3$$$, the benefit of the second is $$$5$$$, the third is $$$2$$$, and the benefits of days from the $$$4$$$th to the $$$10$$$th are $$$0$$$. Thus, the benefit of the olympiad season is $$$3+5+2+0+\ldots+0=10$$$. Note that the schedule of olympiads $$$[2, 1, 3]$$$ is not possible, as the olympiads must remain in non-decreasing order of their scheduled dates.

In the third example, a schedule of olympiads $$$[1, 2, 5, 6, 6]$$$ is also suitable, but $$$[1, 2, 3, 5, 6]$$$ is not suitable, as the third olympiad cannot take place before the $$$5$$$th day

D. Tower of Boxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem.

Cyber-shark Zubastik$$$^{\dagger}$$$ works in a warehouse and stacks boxes into a single vertical tower. The height of the tower is unlimited, but the shark can only remove and place the top box on the tower. There is also a buffer nearby — a free area where boxes can be temporarily held (any number of boxes can be in the buffer).

Boxes are numbered in the order they arrive, starting from $$$1$$$. For each box, the moment in time $$$t_j$$$ when it needs to be extracted from the warehouse is known.

During the work, there are two types of events:

  • + t — a new box has arrived, which needs to be extracted at time $$$t$$$ $$$(1 \le t \le T)$$$. All values of $$$t$$$ are distinct.
  • - j — the moment $$$t_j$$$ has arrived, and box number $$$j$$$ needs to be extracted.

Actions. During the processing of an event, the shark can perform the following operations:

  • 1 j — place box $$$j$$$ on top of the tower (allowed only if box $$$j$$$ is in the buffer).
  • 2 j — place box $$$j$$$ in the buffer (allowed only if box $$$j$$$ is on top of the tower).

Rules for processing events.

  • At the event + t, the new box is immediately placed in the buffer. By the end of processing this event, the buffer must be empty.
  • At the event - j, by the end of processing this event, there must be exactly one box left in the buffer — box $$$j$$$. After this, box $$$j$$$ is considered extracted and removed from the system.

Your task is to output a set of operations for each event that correctly processes that event.

$$$^{\dagger}$$$ see fig.

Cyber-shark Zubastik
Input

The first line contains $$$T$$$ — the number of moments in time to consider.

In the $$$i$$$-th of the subsequent $$$T$$$ lines, the description of the next event occurring at time $$$i$$$ is given in the following format:

  • + t — a new box has arrived, which needs to be removed at time $$$t$$$ $$$(1 \le t \le T)$$$. (Boxes are numbered in the order of arrival). After processing this event, the buffer must be empty.
  • - j — it is time to extract box $$$j$$$. After processing this event, box $$$j$$$ must remain in the buffer.
Output

For each event, you need to output a sequence of operations that will process it.

First, an integer $$$K$$$ — the number of operations.

Then a list of $$$K$$$ operations — for each, two numbers:

  • The type of operation (1 or 2).
  • Then the number of the box with which the operation is performed.

The total number of operations output across all events must not exceed $$$10^6$$$. If this limit is violated, the solution will receive a verdict of Wrong Answer.

Interaction

First, your program must read an integer $$$T$$$ — the number of events.

Then for each $$$i = 1,2,\dots,T$$$, the interactor outputs a line with the description of the event occurring at time $$$i$$$:

  • + t — a new box has arrived, which needs to be extracted at time $$$t$$$ $$$(1 \le t \le T)$$$. The box receives the next sequential number. By the end of processing the event, the buffer must be empty.
  • - j — box $$$j$$$ needs to be extracted. By the end of processing this event, exactly one box $$$j$$$ must remain in the buffer.

After receiving the next event, your program must output the response for it in the following format:

  • In the first line, an integer $$$K$$$ — the number of operations to process the current event;
  • Then $$$K$$$ lines, each with two integers: the type of operation ($$$1$$$ or $$$2$$$) and the number of box $$$j$$$.

To receive the next event, you must output a correct response for the current event and end the line with a newline character.

After outputting the response, be sure to flush the output buffer:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python.

Note that for faster output, it is advisable to call flush (or endl) after outputting all $$$K$$$ operations, rather than after each operation.

The interactor in this problem is non-adaptive: within a single test, the sequence of events is fixed and does not change based on your responses.

Scoring

In this problem, there are two types of tests.

The first type of test — an array [1, 1, 2, 2, ..., N, N] is generated and shuffled randomly, resulting in an array $$$[a_1, a_2, \dots, a_T]$$$ of size $$$T$$$, where $$$T = 2N$$$. A box is added at time $$$t_1$$$ and taken out at time $$$t_2$$$, if $$$t_1 \lt t_2$$$ and $$$a[t_1] = a[t_2]$$$.

The second type of test — it is guaranteed that the boxes are taken out in the same order they are added.

For each value of $$$N$$$ (the number of boxes), one test of each type is generated.

GroupTestsValues of $$$N$$$PointsCumulative SumDependencies
11-2Tests from the statement0 per test0-
23-10100, 200, 330, 4201 per test8-
311-18480, 720, 820, 10801 per test161
419-261240, 1420, 1630, 18701 per test241-2
527-302150, 24701 per test281-3
531-342840, 32602 per test361-3
635-423740, 4300, 4940, 56802 per test521-4
743-506530, 7500, 8620, 99102 per test681-5
851-5811390, 13090, 15050, 173002 per test841-6
959-6617500, 18000, 18500, 190002 per test1001-7
Examples
Input
4
+ 3
+ 4
- 1
- 2
Output
1
1 1
3
2 1
1 2
1 1
1
2 1
1
2 2
Input
8
+ 8
+ 3
- 2
+ 7
+ 6
- 4
- 3
- 1
Output
1
1 1
1
1 2
1
2 2
1
1 3
1
1 4
1
2 4
1
2 3
1
2 1

E. Cheng Shi and the Collection of Bamboo Inscriptions
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Elephant Filimon decided to visit his panda-friend Cheng Shi from Chengdu, China. Cheng Shi loves bamboo and showed her collection of bamboo inscriptions. Each inscription is a string of lowercase Latin letters.

Cheng Shi's collection is a dynamic multiset of strings. Initially, it is empty.

The panda can perform operations of two types:

  • + s — add the string $$$s$$$ to the multiset.
  • - s — remove one copy of the string $$$s$$$ from the multiset.

Elephant Filimon became curious about the minimum number of actions required after each operation to make all strings in the current multiset identical.

Allowed actions on one string from the collection:

  1. Remove the last character of the string — Cheng Shi carefully bites off a piece of bamboo.
  2. Add any character to the end of the string — the panda plants bamboo, and it grows, adding one character to the end.
An unlimited number of actions can be performed on each string (bamboo).

The string to which all others are brought can be any (including empty) and does not have to be present in the multiset.

Note: The strings in the multiset are not actually changed. After each query, it is only necessary to compute the minimum number of actions required to make all strings identical. For subsequent operations, the multiset remains unchanged.

Help Cheng Shi quickly respond to Filimon's question after each change.

Input

The first line contains a single integer $$$q$$$ ($$$1 \le q \le 5 \cdot 10^5$$$) — the number of operations.

Each of the following $$$q$$$ lines describes one operation and has the form:

+ s or - s

where $$$s$$$ is a non-empty string consisting of lowercase Latin letters.

  • The operation + s means adding the string $$$s$$$ to the multiset.
  • The operation - s means removing one copy of the string $$$s$$$ from the multiset. It is guaranteed that the string $$$s$$$ is present in the multiset at the time of removal.

In all queries, $$$1 \le |s| \le 5 \cdot 10^5$$$. The total length of all strings in all operations does not exceed $$$5 \cdot 10^5$$$.

Output

After each operation, output a single integer — the minimum number of actions required to make all strings in the current multiset identical.

If the multiset is empty after the operation, output $$$0$$$.

Scoring
Additional constraints
SubgroupPoints$$$q$$$$$$\sum |s_i|$$$Type of operations Required subgroups
110$$$q \le 100$$$$$$\sum |s_i| \le 1000$$$+ and -
210$$$q \le 1000$$$$$$\sum |s_i| \le 10^4$$$+ and -1
315$$$q \le 50\,000$$$$$$\sum |s_i| \le 10^5$$$+ and -1,2
410$$$q \le 5 \cdot 10^5$$$$$$\sum |s_i| \le 5 \cdot 10^5$$$, $$$|s_i| \le 10$$$+
510$$$q \le 5 \cdot 10^5$$$$$$\sum |s_i| \le 5 \cdot 10^5$$$, $$$|s_i| \le 10$$$+ and -4
620$$$q \le 5 \cdot 10^5$$$$$$\sum |s_i| \le 5 \cdot 10^5$$$+4
725+ and -1–6
Example
Input
5
+ abc
+ ab
+ a
- ab
+ b
Output
0
1
2
2
4

F. Nimesweeper!
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alexander and Babin live on an abandoned island. Their only entertainment consists of trees, stones, and bombs (don't ask where they came from). They are very bored and want to play a game. Alexander suggested playing "Minesweeper", while Babin suggested "Nim". As a compromise, they decided to play Nimesweeper!

With no other choice, the guys chose a tree as the game board. Recall that a tree is a connected undirected graph without cycles.

In the course of the overall game, Alexander starts playing the subgame "Minesweeper". Initially, none of the vertices of the tree contain bombs. On his turn, Alexander chooses any vertex without a bomb and places a bomb in it. After that, in each vertex that does not contain a bomb, a number of stones equal to the number of bombs in its neighboring vertices is placed.

Babin can interrupt Alexander after any of his moves. Once this happens, they start playing "Nim" with the resulting piles of stones, with Babin going first.

If Babin does not interrupt Alexander, Alexander makes the next move (places another bomb), after which the piles of stones are updated, and so on, until Babin starts the game of "Nim".

The player who cannot make a move in "Nim" loses.

Babin really wants to win, so he will interrupt Alexander at the earliest possible moment when he can guarantee his victory.

Alexander, well aware that he cannot win this game, decided instead to maximize his enjoyment, so he wants to place as many bombs as possible before Babin starts the game of "Nim". Please help Alexander find the maximum number of bombs he can place!

Recall that in the game of "Nim", the first player wins if and only if the bitwise XOR of the sizes of the piles is non-zero.

Recall that XOR or Exclusive OR is a binary operation that is applied to each pair of bits in the same position in the binary representations of numbers such that the result at that position is 0 if the bits are equal, and 1 otherwise. For example, 3 XOR 5 = 6, since $$$3_{10}$$$ = $$$011_2$$$, $$$5_{10}$$$ = $$$101_2$$$, and therefore after applying the operation, the second and third bits become equal to 1, while the first bit becomes 0, resulting in $$$110_2$$$ = $$$6_{10}$$$.

Input

The first line of the input contains a single integer $$$n$$$ ($$$2 \leq n \leq 369$$$) — the number of vertices in the tree.

The following $$$n - 1$$$ lines contain pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$) — the edges of the tree. It is guaranteed that these edges indeed form a tree.

Output

In a single line, output a single number — the maximum number of bombs that can be placed.

Scoring
GroupPointsAdditional Constraints Required Groups
18$$$n \le 8$$$
211$$$n \le 18$$$1
320$$$n \le 45$$$1–2
419$$$n \leq 128$$$1–3
518$$$n \leq 243$$$1–4
620Degrees of all vertices $$$\le 3$$$
741–6
Examples
Input
3
1 2
1 3
Output
2
Input
9
1 2
2 3
3 4
4 5
5 6
6 7
5 8
8 9
Output
5
Input
13
7 6
7 5
7 4
7 3
7 2
7 1
1 12
2 11
3 10
4 9
5 8
6 13
Output
8