Winter Cup 8.0 Online Mirror Contest
A. A day in Baladeya
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The local Baladeya has just installed a new automated ticket machine to distribute citizens among $$$N$$$ agents sitting at the counters. At the start of the shift, the Chef Service Fathi configures the ticket machine with two parameters, $$$S$$$ and $$$K$$$. The machine then routes the $$$i$$$-th arriving citizen (starting from $$$i = 0$$$) to agent number $$$(S + i \cdot K) \pmod{N}$$$.

Naturally, the agents are super lazy and want to avoid work at all costs. Due to this routing policy, only a subset of agents will ever actually receive citizens. We call these the unlucky agents (or active agents). Let $$$L$$$ denote the number of unlucky agents.

Each agent $$$k$$$ ($$$0 \le k \lt N$$$) has a "complaining factor" $$$M_k$$$, which represents the amount of headache they cause the manager if they are forced to actually do their work (measured in the number of express coffees they demand, sudden "réseau tayah" excuses and phone calls they make during work).

To survive the day, the Chef Service must maximize the Baladeya's operational efficiency while minimizing his total headache. On one hand, every active agent adds their complaining factor to his daily stress. On the other hand, if too few agents are working, operational efficiency plummets, resulting in massive queues and a severe penalty from angry citizens and higher-ups.

Formally, the Chef Service wants to minimize the total cost (which balances his headache and the inefficiency penalty), defined as: $$$$$$ \text{TotalCost} = \left(\sum_{k \in \text{unlucky}} M_k\right) + \left \lfloor \frac{N}{2} \right \rfloor \cdot (N - L + 1) $$$$$$

Your task is to help the Chef Service find the values of $$$S$$$ and $$$K$$$ that minimize the total cost.

Input

The first line contains a single integer $$$N$$$ ($$$1 \le N \le 2 \cdot 10^5$$$) — the number of agents in the Baladeya.

The second line contains $$$N$$$ integers $$$M_0, M_1, \ldots, M_{N-1}$$$ ($$$1 \le M_i \le 10^9$$$) — the complaining factor of each agent.

Output

Output three integers on a single line: $$$S$$$, $$$K$$$, and the minimum total cost.

The values $$$S$$$ and $$$K$$$ must satisfy $$$0 \le S \lt N$$$ and $$$1 \le K \le N$$$.

If there are multiple solutions with the same minimum cost, output any of them.

Examples
Input
6
5 2 5 1 3 5
Output
3 6 19
Input
16
10 10 12 9 8 4 4 16 4 14 7 16 4 12 15 15
Output
0 4 130

B. Breaking Bad
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a number $$$N$$$ of "legal" labs connected by bidirectional corridors, forming a connected graph. There are $$$M$$$ types of crystals. Every lab makes all types of crystals but specializes in some crystal types, as indicated by a binary string in the input. On day 0, lab $$$i$$$ owns $$$x_{ij}$$$ units of crystal type $$$j$$$.

Every night, all labs update their crystal inventories simultaneously. For a fixed crystal type $$$k$$$, lab $$$i$$$ performs the following process:

A "researcher" from the $$$i$$$ th lab will visit every other lab that can be reached through exactly $$$k$$$ corridors (he can use the same corridor multiple times along his path and it counts). For every possible lab $$$j$$$ that can be reached this way:

- If lab $$$j$$$ specializes in crystal $$$k$$$ and lab $$$i$$$ does not, lab $$$i$$$ gains $$$x_{jk}$$$ (lab $$$j$$$ is not affected).

- If lab $$$i$$$ specializes in crystal $$$k$$$ and lab $$$j$$$ does not, lab $$$i$$$ gains $$$x_{jk}$$$ (lab $$$j$$$ is not affected).

- Otherwise, nothing is gained.

The new amount of crystal $$$k$$$ in lab $$$i$$$ is: the old amount plus all the gained amounts.

All labs compute their new inventories using only the previous day's values.

Your task is to determine, on day $$$D$$$, whether each lab has an odd or even number of each crystal type.

Input

$$$N, M, D$$$ the number of labs, crystal types, and days, where $$$(1 \leq N \leq 200)$$$, $$$(1 \leq M \leq 50)$$$, and $$$(1 \leq D \leq 10^{18})$$$.

Next $$$N$$$ lines: A binary string $$$s_i$$$ of length $$$M$$$, where $$$s_{ik} = 1$$$ if lab $$$i$$$ specializes in crystal $$$k$$$, otherwise $$$0$$$.

Next $$$N$$$ lines: $$$M$$$ integers $$$x_{ik}$$$ per line: the initial crystal quantities for lab $$$i$$$ of crystal $$$k$$$, $$$(x_{ik} \leq 10^{18})$$$.

$$$E$$$ the number of corridors, where $$$0 \leq E \leq \frac{N(N-1)}{2}$$$.

Next $$$E$$$ lines: Two integers $$$u, v$$$ $$$(1 \leq u, v \leq N)$$$ denoting a bidirectional corridor between labs $$$u$$$ and $$$v$$$.

Output

Print $$$N$$$ lines. Each line contains $$$M$$$ values:

- $$$1$$$ if the amount of that crystal is odd on day $$$D$$$.

- $$$0$$$ if it is even.

Examples
Input
3 2 2
10
01
11
1 2
3 4
5 6
2
1 2
2 3
Output
1 0 
1 0 
1 0 
Input
4 3 5
100
010
001
111
10 20 30
15 25 35
12 22 32
18 28 38
4
1 2
2 3
3 4
1 4
Output
0 0 0 
0 1 0 
0 0 0 
0 0 0 

C. Aziza Supermarket Heist
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

you and your friends came up with a reasonable plan: Rob a Aziza supermarket.

However, the store has installed a new security system to prevent such attempts.

To bypass the security, you must generate a key consisting of $$$n$$$ positive integers ( $$$0 \lt a_i \lt 10^9$$$ ) where the sum of the numbers is equal to their product.

Formally, you need to find a sequence $$$a_1, a_2, \dots, a_n$$$ ( where $$$a_i \gt 0$$$ and $$$a_i \lt 10^9$$$ for all i ) such that: $$$$$$ \sum_{i=1}^{n} a_i = \prod_{i=1}^{n} a_i $$$$$$

Your task is simple: for a given $$$n$$$, output any sequence of $$$n$$$ positive integers satisfying this condition.

It is guaranteed that a solution always exists for all given $$$n$$$.

Input

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

The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).

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

Output

For each test case, output $$$n$$$ space-separated integers that satisfy the condition.

If there are multiple solutions, print any of them.

Example
Input
3
1
2
3
Output
1
2 2
1 2 3

D. Sousse
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The city of Sousse is a popular touristic destination, it has n important locations connected by m directed streets.

A popular tourist activity is to take a "walk" through the city: starting at some location and moving along a sequence of directed streets to reach another location. The length of the walk is the number of streets traversed.

For statistical purposes, the city wants to know the total number of possible walks in the city that use at most L streets. This includes walks between any starting point $$$u$$$ and any ending point $$$v$$$ (including walks of length $$$0$$$, where the tourist stays at the same location).

Since the numbers can be huge, they should be reported modulo $$$M=10^9+7=1000000007$$$.

Given the directed graph of $$$n$$$ locations and $$$m$$$ streets, calculate a single integer: the total number of walks of length between $$$0$$$ and $$$L$$$ (inclusive) over all ordered pairs of locations $$$(u, v)$$$, modulo $$$M$$$.

Input
  • The first line contains three integers $$$1 \le n \le 120$$$, $$$1 \le m \le n^2$$$ and $$$0\le L \le 10^{18},$$$ with
    • $$$n$$$: The number of locations
    • $$$m$$$: The number of streets.
    • $$$L$$$: The maximum length of walk.
  • The next $$$m$$$ lines contain each two integer $$$1 \le u,v \le n,$$$ with $$$u\rightarrow v$$$ a one-way street from $$$u$$$ to $$$v$$$
Output

Print a single integer: the total number of walks of length at most $$$L$$$ over all ordered pairs $$$(u, v)$$$, modulo $$$M=1000000007$$$.

Examples
Input
5 7 100
1 2
1 3
2 3
3 4
1 4
4 5
2 5
Output
22
Input
7 6 1
1 2
1 3
2 4
2 5
3 6
3 7
Output
13
Input
5 5 1000
1 2
2 3
3 4
4 5
5 1
Output
5005
Note
  • The Graph may contain loops
  • It is guaranteed that between any two vertices $$$u$$$ and $$$v$$$, there is at most one edge.

E. Game of Divisors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Rami and Khalil are fierce competitors in two-player games. This time, they agreed to play the following turn-based game, designed by Khalil.

The game starts with a list of $$$n$$$ positive integers $$$A = [A_1, A_2, \dots, A_n]$$$.

On each turn, the current player selects one number $$$A_i$$$ from the list and replaces it with either:

  • A proper divisor of $$$A_i$$$ (a positive divisor less than $$$A_i$$$ itself), or
  • Any positive integer whose number of distinct prime factors is strictly less than that of $$$A_i$$$.

The player who cannot make a move on their turn loses. Khalil will be the starting player.

While Khalil was carefully considering his first move, Rami realised something interesting: he can sneakily add some positive integers to the list before the game starts to guarantee his win (as the second player).

Since Rami wants to avoid suspicion, he needs to minimize the number of integers he adds. If multiple minimal solutions exist, any valid set will work.

Help Rami determine the smallest set of positive integers (possibly empty) that he can add to the list so that he will win assuming both players play optimally.

Input
  • The first line contains an integer $$$1\le T\le 10^4$$$, denoting the number of test cases
  • The first line of the $$$i^\text{th}$$$ test case contains an integer $$$0 \le n \le 10^6,$$$ denoting the size of the array $$$A$$$
  • The second line of the $$$i^\text{th}$$$ test case contains $$$n$$$ integers $$$1 \le A_1,\dots,A_n \le 10^6$$$

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^6.$$$ Formally: $$$\sum_{i}n_i \le 10^6$$$

Output

For each test case, output a line containing:

  • First an integer $$$0 \le m\le 10^6,$$$ denoting the number of added integers.
  • Then, if $$$m \neq 0$$$, output $$$m$$$ integers $$$1 \le B_1, \dots, B_m \le 10^6$$$, denoting the numbers that were added.
Example
Input
2
2
9 27
5
2 3 6 5 7
Output
1 3
1 35
Note

After Rami adds these integers, the game proceeds with Khalil making the first move on the modified list $$$A'=\mathrm{concat}(A,B)=[A_1,\dots,A_n, B_1,\dots,B_m]$$$

F. The Carthaginian Cipher
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem

In ancient Carthage, scholars created a secret numerical cipher to protect their most valuable trade secrets. This cipher, believed to be a function $$$f$$$, that maps each natural number to a secret value, was known only to the High Council of Merchants. The cipher was lost when Rome destroyed Carthage in 146 BC.

For centuries, archaeologists have searched for the legendary "Stone of Accounts", the key to unlocking this ancient secret.

Recently, in the ruins of ancient Carthage near modern-day Tunis, you've discovered the fabled Stone of Accounts! It's a mysterious device with two input slots and one output display. Ancient Punic inscriptions explain how it works: "Place two numbered stones in the slots. If their numbers sum to a power of two, the stone reveals the sum of their hidden values. Otherwise, it reveals nothing."

Formally, The Stone accepts two positive integers $$$n$$$ and $$$m$$$:

  • If $$$n+m$$$ is a power of two, it outputs $$$f(n)+f(m)$$$
  • Otherwise, it outputs $$$0$$$
Here, $$$f$$$ is the lost Carthaginian cipher

Centuries ago, archaeologists discovered ancient Carthaginian scrolls containing encrypted trade secrets: $$$S=[S_1,\dots,S_n].$$$ For generations, scholars have tried to decipher them, but without the Stone of Accounts, all attempts failed.

Now, with the Stone finally discovered, you have a chance to succeed where others could not.

Your mission: Recover the true message $$$M=[f(S_1),\dots,f(S_n)].$$$ But be careful! The Stone is ancient and fragile. You can use it at most 10,000 times before it breaks forever.

Input
  1. The first line contains one integer $$$1 \le n \le 100$$$, denoting the length of the encrypted message.
  2. The second line contains $$$n$$$ integers $$$1 \le S_1,\dots,S_n \le 10^{18}$$$, denoting the encrypted message.
Interaction

Your program will interact via standard input/output with the black box.

Starting from $$$i=1$$$, two modes of operations are supported:

  1. Query the Stone, ? n m: You give two positive integers $$$1 \le n,m \lt 2^{64}$$$ to the Stone. The Stone responds with R s where:
    • $$$s=f(n)+f(m)$$$ if $$$n+m$$$ is power of two
    • $$$s=0$$$ otherwise
  2. Declare a Decrypted Value, ! d: This means you've determined that $$$d=f(S_i)$$$. After this command:
    • You receive N.
    • You move to the next encrypted number: $$$i\leftarrow i+1$$$
    • When all numbers are decrypted, your program can exit.
Example
Input
3
1 3 4
R 0
R 2
N
R 4
N
R 8
N


Output
? 1 2
? 1 1
! 1
? 3 1
! 3
? 4 4
! 4
Note
  • It is guaranteed that $$$f$$$ exists and is unique.
  • It is guaranteed that: $$$$$$ \forall n \in \mathbb{N}, \quad f(n) \le 10^{18} $$$$$$

G. Derby
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It is the day of the Grand Derby at the Stade Hammadi Agrebi in Radès. The stadium is packed to capacity, and the atmosphere is electric.

As the lead audio engineer for the broadcast, you are monitoring a row of $$$n$$$ passionate fans in the "Virage" stand. Each fan at position $$$i$$$ has a specific vocal intensity level, represented by an integer $$$a_i$$$.

When a continuous segment of fans chants together, their voices create a "Resonance Wave". The Total Resonance of the segment from index $$$l$$$ to index $$$r$$$ is defined as the product of their individual intensities: $$$$$$ \text{Resonance}(l, r) = a_l \times a_{l+1} \times \dots \times a_r $$$$$$

The stadium's modern "Smart Audio System" filters noise based on power tiers. For a chant to be successfully broadcast clearly on the high-definition channel of Tier $$$k$$$, its Total Resonance must be divisible by $$$10^k$$$.

You must process $$$q$$$ queries from the control room. For each query $$$(l, r, k)$$$, determine if the chant from the fans between $$$l$$$ and $$$r$$$ (inclusive) qualifies for the Tier $$$k$$$ broadcast (i.e., is divisible by $$$10^k$$$).

Input

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 10^5$$$) — the number of fans in the row and the number of queries.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the vocal intensity of each fan.

Each of the next $$$q$$$ lines contains three integers $$$l$$$, $$$r$$$, and $$$k$$$ ($$$1 \le l \le r \le n$$$; $$$1 \le k \le 10^9$$$) — the range of the fans and the required audio tier.

Output

For each query, output a single line:

  • YES — if the Total Resonance of the range is divisible by $$$10^k$$$.
  • NO — otherwise.
Example
Input
5 4
48 250 4 75 128
1 2 4
2 4 3
4 5 1
1 5 6
Output
NO
YES
YES
NO
Note

Note:

Query 1 $$$(l=1, r=2, k=4)$$$: The segment is $$$[48, 250]$$$, The Resonance is $$$48 \times 250 = 12000$$$. We check if $$$12000$$$ is divisible by $$$10^4$$$. Since $$$12,000$$$ is not divisible by $$$10000$$$, the answer is NO.

Query 2 $$$(l=2, r=4, k=3)$$$: The segment is $$$[250, 4, 75]$$$. The Resonance is $$$250 \times 4 \times 75 = 75000$$$. Since $$$75000$$$ is divisible by $$$10^3$$$, the answer is YES.

H. Scratch Expressions
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Touhemi is interested in betting, specifically in Scratch Expressions.

Touhemi buys a card that reveals, when scratched, a hidden string of length $$$n$$$, generated uniformly at random from the alphabet $$$$$$ \Sigma =\{(,),+,-,*,0,1,2,3,4,5,6,7,8,9\}. $$$$$$

The string is interpreted as an arithmetic expression under the standard rules described below.

He only has to scratch the card and hope that this is a winning card. Each generated string belongs to exactly one of the following categories:

  • Winner: the string is a valid expression and evaluates to $$$0$$$ (after applying modulo $$$m$$$).
  • Loser: the string is an invalid expression, or if it evaluates to $$$k\neq 0$$$ (after applying modulo $$$m$$$)

A string is a valid arithmetic expression if and only if:

  • It consists of digits 0-9, binary operators +, -, *, and parentheses().
  • Parentheses are balanced and properly nested.
  • All operators are binary.
  • Each digit represents a single non-negative integer.
  • The entire string forms exactly one expression.

Valid Arithmetic Expressions are defined using the following recursive rules:

  • An expression is either:
    1. A sum, difference or product of two sub-expressions
    2. A sub-expression, grouped by paranthesis
    3. A number. Note that leading zeros are allowed
  • A Number is a sequence of digits
  • A Digit is a character in $$$\{0,1,2,3,4,5,6,7,8,9\}$$$

Any string that does not satisfy these rules is considered invalid.

The valid arithmetic expression is evaluated using standard arithmetic semantics (Detailed in Notes).

Any strings of length $$$n$$$ is generated uniformly at random from the alphabet $$$\Sigma =\{(,),+,-,*,0,1,2,3,4,5,6,7,8,9\}.$$$

Your task is to compute, for each given length $$$n$$$, the probability that Touhemi obtains a Winner card.

Input
  • The first line contains two integers $$$1\le T \le 10^2$$$ and $$$1\le m \le 10^2$$$ with
    • $$$1 \le T \le 100$$$: The number of test cases
    • $$$1\le m \le 100$$$: The modulus shared for all test cases
  • The second line contains $$$T$$$ integers $$$1 \le n_1, \dots,n_T \le 200,$$$ denoting the length of the expressions.
Output

It can be proven that each probability can be expressed as a ratio $$$\frac{p}{q}$$$ where $$$q \not\equiv 0 \pmod M$$$, with $$$M=10^9+7$$$.

You have to express the probability as: $$$$$$ p \times q^{-1} \bmod M, $$$$$$ where $$$q^{-1}$$$ is the modular inverse of $$$q$$$ modulo $$$M$$$.

Examples
Input
5 5
1 2 3 7 10
Output
933333340
288888891
887703710
700873106
13278558
Input
3 67
52 36 7
Output
987476354
514150140
799551473
Note

Valid Arithmetic Expressions

Formally, the set of all valid arithmetic expressions can be described by the grammar: $$$\mathcal{G} = (\{E, N, D\},\ \Sigma,\ \mathcal{R},\ E)$$$

where:

  • $$$E$$$ (expression), $$$N$$$ (number), and $$$D$$$ (digit) are the symbols.
  • $$$\Sigma = \{(, ), +, -, *, 0,1,2,3,4,5,6,7,8,9\}$$$ is the set of characters.
  • $$$E$$$ is the start symbol.
  • $$$\mathcal{R}$$$ contains the production rules, described in the table below:
|p{5.5cm}|p{8cm}|

Rule

DefinitionExplanation
Sum$$$E\rightarrow E+E$$$An Expression can be formed by adding two sub-expressions.
Difference$$$E\rightarrow E-E$$$An Expression can be formed by subtracting one sub-expression from another.
Product$$$E\rightarrow E*E$$$An Expression can be formed by multiplying two sub-expressions.
Parenthesis$$$E\rightarrow (E)$$$An Expression can be surrounded by parentheses to form a new expression
Number is Expression$$$E\rightarrow N$$$An Expression can be a single number
Append Digits$$$N\rightarrow NN$$$A Number can be formed by concatenating two shorter numbers.
Digit is Number$$$N\rightarrow D$$$A Number can be a single Digit
Digit$$$D \rightarrow 0 \mid 1 \mid 2 \mid 3 \mid 4 \mid 5 \mid 6 \mid 7 \mid 8 \mid 9$$$A digit is a character from the set $$$\{0,1,2,3,4,5,6,7,8,9\}$$$
}

Generating Valid Arithmetic Expressions

Starting from $$$E$$$, we repeatedly apply the production rules to replace symbols with other symbols or characters until only characters from $$$\Sigma$$$ remain, forming a valid arithmetic expression.

For example, to generate $$$(02+35)*4$$$: $$$$$$ E \Rightarrow E*E\Rightarrow (E)*N \Rightarrow (N+N)*N \Rightarrow (NN+NN)*D \Rightarrow (DD+DD)*D \Rightarrow (02+35)*4 $$$$$$

It can be proven that any valid arithmetic expression can be generated from the start symbol $$$E$$$ by a finite sequence of production rules.

Standard Arithmetic Semantics

The standard arithmetic semantics (i.e evaluation order) are:

  • Operator precedence: * has higher precedence than + and -. E.g., 2 + 3 * 4 equals 14
  • All operators are left-associative. E.g., 10 - 3 - 2 equals 5
  • Parentheses override precedence. E.g., (2 + 3) * 4 equals 20
  • All computations are performed modulo $$$m$$$.

I. Array Tounsi
time limit per test
1 s
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array $$$A$$$ of size $$$N$$$. You need to process $$$Q$$$ queries of two types:

  • Query: Given an integer $$$x$$$, return the number of elements in the array that are equal to $$$x$$$.

  • Update query: Given an integer $$$x$$$, for every index $$$i$$$ such that $$$A_i \gt x$$$, update the value as: $$$A_i = A_i - x$$$
Input
  • The first line contains two integers $$$N$$$ , $$$Q$$$ $$$(1 \le N \le 10^5)$$$ $$$(1 \le Q \le 10^5)$$$, the size of the array, the number of queries.
  • The second line contains $$$N$$$ integers $$$A_1, A_2, \dots, A_N$$$ $$$(1 \le A_i \le 10^6)$$$.
  • The next $$$Q$$$ lines each contain a query of one of the following types:
    • Type 1: 1 x $$$(1 \le x \le 10^6)$$$ — print the number of elements in the array equal to $$$x$$$.
    • Type 2: 2 x $$$(1 \le x \le 10^6)$$$ — for every index $$$i$$$ such that $$$A_i \gt x$$$, update the value as $$$A_i = A_i - x$$$.
Output

For each query of type 1, output a single integer representing the number of elements equal to $$$x$$$.

Example
Input
2 4
3 1
2 1
2 1
1 1
1 6
Output
2
0

J. The Lake of Ichkeul
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Miss Amina is organizing an excursion for primary school students to The Lake of Ichkeul, well-known locally as Bouhayret Echkel. She's managing the payment queue where students come to settle their trip fees.

The payment system works as follows:

  • Each student in the queue has a payment balance (in dinars).
  • Positive values represent payments made by the student.
  • Negative values represent refunds, scholarships, or discounts applied to the student.
The queue is quite dynamic! Students can join from either end:
  • Front: Priority students (e.g., class representatives, students with urgent matters).
  • Back: Regular arrivals.

Students may also leave the queue in groups from either end (called away for verification, already processed, etc.).

Miss Amina frequently needs to calculate the net payment for specific sections of the queue. This is crucial for:

  • Verifying which bus groups have fully paid their share.
  • Checking if a specific segment has a deficit that needs attention.

Your task is to help Miss Amina by simulating the queue operations and answering her payment queries.

Input

The first line contains a single integer $$$Q (2 \leq Q \leq 10^6)$$$ — the number of operations.

Each of the next $$$Q$$$ lines contains one of the following operations:

  • $$$1 \ x$$$ : Add a student with payment balance $$$x$$$ dinars to the back of the queue $$$(-10^9 \leq x \leq 10^9)$$$.
  • $$$2 \ x$$$ : Add a student with payment balance $$$x$$$ dinars to the front of the queue $$$(-10^9 \leq x \leq 10^9)$$$.
  • $$$3 \ k$$$ : Remove $$$k$$$ students from the $$$back$$$ of the queue $$$(1 \leq k \leq$$$ current queue size$$$)$$$.
  • $$$4 \ k$$$ : Remove $$$k$$$ students from the $$$front$$$ of the queue $$$(1 \leq k \leq$$$ current queue size$$$)$$$.
  • $$$5 \ l \ r$$$ : Calculate the net payment (sum of balances) from position $$$l$$$ to position $$$r$$$ (inclusive, $$$0$$$-indexed from the front) where $$$0 \leq l \leq r \lt $$$ current queue size.

It is guaranteed that:

  • Operations $$$3$$$ and $$$4$$$ will never remove more students than currently in the queue.
  • Operation $$$5$$$ will always query valid positions within the current queue.
  • At least one operation of type $$$5$$$ will be present.
Output

For each operation of type $$$5$$$, output a single integer — the net payment (sum of payment balances) in dinars for the specified range.

Example
Input
7
2 -50
1 100
1 -200
2 150
5 1 2
3 2
5 0 1
Output
50
100
Note

The queue is $$$0$$$-indexed from the front. So if the queue is $$$[a, b, c, d]$$$, then:

  • Position $$$0$$$ is $$$a$$$ (front).
  • Position $$$1$$$ is $$$b$$$.
  • Position $$$2$$$ is $$$c$$$.
  • Position $$$3$$$ is $$$d$$$ (back).

K. The Encrypted Parchment
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Deep inside the ancient Medina of Tunis, among the narrow alleys and whitewashed walls, a respected calligrapher and scholar left behind a mysterious parchment before disappearing during troubled times.

The parchment was discovered in a small shop near the souks. It contains an encrypted message written in lowercase English characters. The message was protected using a Caesar cipher, yet the shift number used to encrypt it is unknown.

Luckily, an old hakim (wise man) from the Medina provides a clue: he is certain that the original message contained at least one word from a given list of important Tunisian words — words such as "carthage", "zitouna", "jasmine", "kasba", or "medina".

A Caesar cipher shifts each letter in the alphabet forward by a fixed number of positions $$$k$$$ ($$$0 \le k \le 25$$$). The shift wraps around at the end of the alphabet. For example, with a shift of $$$1$$$, 'a' becomes 'b', 'b' becomes 'c', and 'z' becomes 'a'.

Your task is to determine the shift number $$$k$$$ that was used to encrypt the message.

In the first example, shifting the original words "treasure" and "madina" forward by $$$k=5$$$ results in the encrypted words "ywjfxzwj" and "rfinsf".

Input

The first line contains a single integer $$$N$$$ ($$$1 \le N \le 1000$$$) — the number of words in the encrypted text.

The second line contains a string $$$S$$$ — the encrypted text. The string contains only lowercase letters and spaces. The length of $$$S$$$ is at most $$$100,000$$$ characters and contains exactly $$$N$$$ words.

The third line contains a single integer $$$M$$$ ($$$1 \le M \le 1000$$$) — the number of words in the known list.

The next $$$M$$$ lines each contain a string — the words that could appear in the decrypted text. Each word contains only lowercase letters and has a length of at most $$$50$$$.

Output

Print a single integer: the shift number($$$0 \le \text{shift} \le 25$$$), or '-1' if it cannot be determined for certain.

Examples
Input
6
ymj ywjfxzwj nx ns ymj rfinsf
2
madina
treasure
Output
5
Input
5
o ziovx u bcxxyh nlyumoly
3
a
gold
treasure
Output
-1

L. Drone Route Planning
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You are in charge of an autonomous delivery drone navigating a futuristic smart city.

The city is represented as a directed weighted graph. Each hub is a node, and each aerial flight path is a one-way connection with a given energy cost.

Some hubs contain delivery packages.

The drone starts at hub $$$U$$$, and its mission is to:

  1. Take off from hub $$$U$$$.
  2. Collect exactly five packages.
  3. Return to hub $$$U$$$.
  4. Minimize the total energy cost.

The drone may revisit hubs and reuse flight paths multiple times.

If the drone visits a hub that contains a package, it is not required to collect it.

There are at most $$$35$$$ hubs in the city that contain packages.

Your task is to determine the minimum total energy cost required to complete the route:

Start at $$$U$$$ $$$\rightarrow$$$ collect 5 packages $$$\rightarrow$$$ return to $$$U$$$.

If no such route exists, output $$$-1$$$.

Input

The first line contains two integers $$$N$$$ and $$$M$$$ ($$$1 \le N \le 10^5$$$, $$$0 \le M \le 10^5$$$) — the number of hubs and the number of directed flight paths .

The second line contains a single integer $$$U$$$ ($$$1 \le U \le N$$$) — the starting hub.

Each of the next $$$M$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \le a, b \le N$$$, $$$1 \le c \le 10^9$$$) — representing a directed flight path from hub $$$a$$$ to hub $$$b$$$ with energy cost $$$c$$$.

The next line contains a single integer $$$K$$$ ($$$0 \le K \le \min(N, 35)$$$) — the number of hubs that contain packages.

The next line contains $$$K$$$ distinct integers $$$v_1, v_2, \dots, v_K$$$ ($$$1 \le v_i \le N$$$) —the indices of the hubs that contain packages.

It is guaranteed that all $$$v$$$ are distinct.

Output

Print a single integer — the minimum total energy cost required for the drone to:

  • start at hub $$$U$$$,
  • collect exactly five packages,
  • and return to hub $$$U$$$.

If it is impossible to complete such a route, print $$$-1$$$.

Example
Input
8 12
1
8 1 2
1 3 2
3 2 1
2 6 12
3 6 8
6 5 2
3 5 5
5 7 4
7 1 6
7 8 10
7 4 3
4 8 9
6
2 3 4 5 6 7
Output
27

M. Hiya Ti7 Wena Ntala3ha
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You have a number $$$x$$$, initially equal to $$$S$$$.

There are $$$N$$$ types of operations available. Each operation $$$i$$$ is described by:

- a range $$$[l_i, r_i]$$$

- an integer $$$d_i$$$

An operation $$$i$$$ can be applied only if the current value of $$$x$$$ satisfies $$$l_i \le x \le r_i$$$.

When applied, the operation reduces $$$x$$$ by exactly $$$d_i$$$: $$$x := x - d_i$$$.

You may apply any operation any number of times, as long as its condition is satisfied at the moment of application. Each application counts as one step.

Your goal is to reduce $$$x$$$ to exactly $$$0$$$ using the minimum number of steps.

If there are multiple sequences of operations that achieve this using the minimum number of steps, output the lexicographically smallest sequence of applied $$$d_i$$$ values.

If there is no solution your answer should be $$$-1$$$.

Input

An integer $$$S$$$ ($$$1 \le S \le 10^6$$$), the initial value of $$$x$$$.

An integer $$$N$$$ ($$$1 \le N \le 10^6$$$), the number of operations.

The next $$$N$$$ lines each contain three integers: $$$l_i, r_i$$$ and $$$d_i$$$ where $$$(1 \le l_i \le r_i \le S)$$$ and $$$(1 \le d_i \le l_i)$$$

- Operation ranges may overlap.

- The sum of all ($$$r_i-l_i+1$$$) does not exceed $$$2 \times 10^7$$$.

Output

Print an integer $$$m$$$: the minimum number of steps required. Then print $$$m$$$ integers — the values $$$d_i$$$ used, in the order they are applied.

If there is no solution, print $$$-1$$$.

Examples
Input
10 2
1 10 1
5 10 5
Output
2
5 5 
Input
15 3
1 15 1
3 12 3
7 15 7
Output
3
1 7 7 
Note

Lexicographical Order:

Given two integer sequences $$$A$$$ and $$$B$$$ of equal length, $$$A$$$ is lexicographically smaller than $$$B$$$ if at the first position where they differ, $$$A$$$ has a smaller value than $$$B$$$. Example: $$$[1, 24, 3]$$$ is lexicographically smaller than $$$[1, 112, 3]$$$ because at the first position where they differ (the second position), $$$24 \le 112$$$.

N. Ons Jabeur and the Perfect Consistency
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ons Jabeur, the "Minister of Happiness" is training in Tunis for her next Grand Slam tournament. She believes that consistency is the key to winning the trophy.

Her coach has lined up N tennis balls, indexed from $$$1$$$ to $$$N$$$. The initial bounce rating of the $$$i-th$$$ ball is $$$A_i$$$ Ons wants to modify the ratings so that all balls eventually have the same value to ensure her practice is perfectly consistent. To modify the bounce ratings, she can perform a special technique. In one operation, she does the following:

  1. She calculates the smallest non-negative integer that does not currently appear in the array of bounce ratings.
  2. She chooses one ball at index $$$i$$$ ($$$1 \le i \le N$$$) and changes its bounce rating to that calculated value.

Help Ons determine if it is possible to make all bounce ratings equal. If it is possible, find the minimum number of operations $$$k$$$ and the sequence of indices $$$i_1, i_2, \dots, i_k$$$ to perform the operations.

If it is impossible to make all elements equal, output $$$-1$$$.

Input

The first line contains a single integer $$$N$$$ ($$$1 \le N \le 10^5$$$) — the number of tennis balls.

The second line contains $$$N$$$ integers $$$A_1, A_2, \dots, A_N$$$ ($$$0 \le A_i \le 10^9$$$) — the initial bounce ratings of the balls.

Output

If it is impossible to make all elements equal, print $$$-1$$$.

Otherwise, print the minimum number of operations $$$k$$$ on the first line, and the indices ($$$1-based$$$) of the balls chosen in each operation on the second line.

Example
Input
1
0
Output
0
Note

non-negative means $$$\ge 0$$$