Othman is a lazy student who relies entirely on leaked exam statistics to pass his high school national exams with a high score.
This morning, Othman is taking his final Biology exam, which consists of $$$100$$$ multiple-choice questions. Each question has four possible choices: a, b, c, and d.
By pure luck (or so he claims), Othman found out that among the $$$100$$$ questions, exactly $$$A$$$ questions have the correct answer a, $$$B$$$ questions have the correct answer b, $$$C$$$ questions have the correct answer c, and $$$D$$$ questions have the correct answer d. However, he has no idea which specific answer belongs to which question.
To make use of this information, Othman decides to mark exactly $$$A$$$ questions as a, $$$B$$$ questions as b, $$$C$$$ questions as c, and $$$D$$$ questions as d on his answer sheet.
Help Othman determine the maximum number of questions he is guaranteed to answer correctly, regardless of how the actual correct answers are arranged.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.
Each test case consists of a single line containing four non-negative integers $$$A$$$, $$$B$$$, $$$C$$$, and $$$D$$$ ($$$0 \le A, B, C, D \le 100$$$, $$$A + B + C + D = 100$$$) — the counts of options a, b, c, and d, respectively.
For each test case, output a single integer — the maximum number of correct answers Othman is guaranteed to get in the worst-case scenario.
325 25 25 2550 0 50 060 20 20 0
0020
Omar left his home for 14 years because of the war in Syria.
He has a favorite array $$$a$$$ of size $$$n$$$.
Before he left, he used to do the following process:
After returning home, he found that his array was lost due to the destruction. However, he managed to find the paper where he had written all the medians.
But the paper is damaged — some numbers may be incorrect. Omar is not sure if the information on the paper is valid or not.
Given the size $$$n$$$ and a multiset of $$$\binom{n}{3}$$$ numbers (the numbers written on the paper), your task is to determine whether there exists any array $$$a$$$ of size $$$n$$$ such that the multiset of medians of all triples of indices matches exactly the given multiset.
Here $$$\binom{n}{3}$$$ denotes the binomial coefficient, which is the number of triples of distinct indices from $$$n$$$ indices:
$$$$$$ \binom{n}{3} = \frac{n(n-1)(n-2)}{6} $$$$$$
For example, $$$\binom{4}{3} = 4$$$, $$$\binom{5}{3} = 10$$$, and $$$\binom{6}{3} = 20$$$.
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 100$$$).
The second line contains $$$\binom{n}{3}$$$ integers $$$b_1, b_2, \ldots, b_{\binom{n}{3}}$$$ ($$$1 \le b_i \le 10^9$$$) — the numbers written on the paper.
print "YES" if there exists an array $$$a$$$ of length $$$n$$$ that produces exactly this multiset of medians, otherwise print "NO".
47 3 3 7
YES
51 2 3 4 5 6 7 8 9 10
NO
Tomeh always boasts "Strings are my playground"
yet he never manages to solve the problems without resorting to overkill.
To prove that simplicity is the true mark of mastery: Given a string $$$s$$$, write a program that outputs the frequency count of the most frequent substring$$$^\dagger$$$ within $$$s$$$, without using tomeh's overkill power.
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$), the number of test cases.
Each test case consists of two lines: The first line of each test case contains a single integer $$$N$$$ ($$$1 \le N \le 10^5$$$), representing the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$N$$$, consisting of lowercase English letters.
It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$10^5$$$.
Output a single integer representing the frequency count of the most frequent substring within $$$s$$$.
34abab6cabbca2dd
2 2 2
$$$^\dagger$$$A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
This is an interactive problem.
After the war in T-Land ended, the people were divided into three groups:
There are $$$n$$$ people in the waiting room. Your task is to determine the type of every person.
Fortunately, you can ask Abu Dujana for help. He is able to identify people's apparent identities by simply looking at them.
You may perform the following operation any number of times:
Formally, let the chosen group contain $$$T$$$ Thowwar, $$$F$$$ Floul, and $$$M$$$ Mkaw3. Abu Dujana returns a single integer:
Determine the type of every person while using at most 812 operations (in honor of 8/12, T-Land Liberation Day).
It is guaranteed that among the $$$n$$$ people there is at least one Thowwar, at least one Floul and at least one Mkaw3.
The first line contains a single integer $$$n$$$ ($$$3 \le n \le 400$$$) — the number of people.
The first line of the input contains a single integer $$$n$$$ ($$$3 \le n \le 400$$$) — the number of people.
To ask a query, print a line in the following format:
? k i1 i2 ... ik
where $$$2 \le k \le n$$$, all indices are distinct, and $$$1 \le i_j \le n$$$.
After printing a query, flush the output and read a single integer $$$x$$$.
Suppose the chosen group contains:
The interactor responds as follows:
You may ask at most 812 queries.
When you have determined the type of every person, print
! s
where $$$s$$$ is a string of length $$$n$$$, and the $$$i$$$-th character is:
After printing the answer, terminate your program immediately.
Your program will receive the verdict Wrong Answer if it asks more than $$$812$$$ queries or prints an invalid query.
Remember to flush the output after every query. For example, use:
5 FMTMF 3 0 0 1
? 5 1 2 3 4 5 ? 3 1 2 5 ? 2 1 5 ? 2 3 5 ! FMTMF
In the sample, there are $$$n = 5$$$ people. The hidden identities are FMTMF (Persons $$$1$$$ and $$$5$$$ are Floul, Persons $$$2$$$ and $$$4$$$ are Mkaw3, and Person $$$3$$$ is Thowwar).
The interaction proceeds as follows:
Khaled and Saleem spent months building a complex logistics network, but everything crashed right on launch day. Watching the servers collapse on the screen, Saleem slammed his hand on the desk and shouted: "Mesh mante2 ya zalame!"
To salvage what they could, Khaled realized they had to track and identify the good vertices in their directed network to correct the system's routing and avoid bankruptcy.
Their network is represented as a directed graph containing $$$N$$$ vertices and $$$M$$$ edges and dosen't have self loops or multiple edges, and they have a constant factor $$$K$$$ that controls the routing cycles.
Due to continuous failures, the system faces $$$Q$$$ queries to test the network. In each query, you are given a subset $$$S$$$ of vertices of size $$$c$$$, along with two integers $$$a$$$ and $$$b$$$.
For each query and for each vertex $$$v$$$ in the graph, we define two values:
For each query, help Khaled and Saleem find the number of good vertices to bring the system back online and prove that things can return to making logical sense.
The first line contains three integers $$$N$$$, $$$M$$$, and $$$K$$$ ($$$1 \le N \le 6000$$$, $$$1 \le M \le 50000$$$, $$$1 \le K \le 20$$$).
Each of the following $$$M$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le N$$$), representing a directed edge from vertex $$$u$$$ to vertex $$$v$$$.
The next line contains a single integer $$$Q$$$ ($$$1 \le Q \le 100000$$$) representing the number of queries.
Each of the following $$$Q$$$ lines describes a query. The line begins with three integers $$$c$$$, $$$a$$$, and $$$b$$$ ($$$1 \le c \le N$$$, $$$0 \le a, b \lt K$$$), followed by $$$c$$$ distinct integers $$$s_1, s_2, \dots, s_c$$$ ($$$1 \le s_i \le N$$$) representing the vertices in the subset $$$S$$$.
It is guaranteed that the sum of $$$c$$$ over all queries does not exceed $$$200000$$$.
For each query, print a single line containing the number of good vertices.
6 5 31 42 55 43 66 423 1 2 1 2 32 2 1 2 3
1 0
$$$^\dagger$$$ walk is a sequence of vertices and edges where both vertices and edges can be visited multiple times. The length of a walk is defined as the total number of edges traversed in this sequence (counting repetitions), not the number of distinct edges.
You are given an array $$$A$$$ of size $$$N$$$.
You have to answer $$$Q$$$ queries. Each query consists of two integers $$$l$$$ and $$$r$$$.
For each query, count the number of contiguous subarrays that satisfy both of the following conditions:
In other words, the elements of the subarray form a permutation of the integers $$$l, l+1, \ldots, r$$$.
The first line contains two integers $$$N$$$ and $$$Q$$$ ($$$1 \le N, Q \le 2 \cdot 10^5$$$).
The second line contains $$$N$$$ integers $$$A_1, A_2, \ldots, A_N$$$ ($$$1 \le A_i \le 2 \cdot 10^5$$$).
Each of the next $$$Q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 2 \cdot 10^5$$$).
For each query, print a single integer — the number of contiguous subarrays whose length is exactly $$$r-l+1$$$ and whose elements form a permutation of all integers from $$$l$$$ to $$$r$$$.
5 3 1 2 4 3 5 1 3 1 4 2 5
0 1 1
11 3 1 3 2 4 2 1 3 4 3 2 1 1 4 1 3 3 4
4 3 2
While most of the world spends their time in disputes about ($$$7as$$$ or $$$3rr$$$), (i64 or ll), (VS Code or Geany), a baby lives a peaceful life, saying only one word: "AGUAGU".
Now, the troublemakers want to discover the baby's bias.
They ask him a single question: is he $$$7as$$$ or $$$3rr$$$? The baby responds with the only word he knows: "AGUAGU".
The input contains a single string: "7as or 3rr ?".
Print a single line containing the exact word the baby says: "AGUAGU".
7as or 3rr ?
AGUAGU
After finally replacing his old MacBook with a gaming laptop, Tomeh has become addicted to video games, just as everyone expected.
He is currently playing The Great Hall. The game is represented by a string $$$t$$$ and consists of $$$k$$$ consecutive stages.
At the end of the $$$i$$$-th stage ($$$1 \le i \le k$$$), Tomeh encounters the boss of that stage. To let him pass, the boss gives him exactly one of the following choices:
As a true "Shami", Tomeh will not spend a single coin unless he is absolutely forced to.
You are given a string $$$s$$$, an integer $$$k$$$, and $$$q$$$ queries.
Each query consists of two integers $$$l$$$ and $$$r$$$. Let $$$t = s[l \dots r]$$$ denote the corresponding substring of $$$s$$$.
For each query, determine the minimum total number of gold coins Tomeh must pay to clear all $$$k$$$ stages of the game played on the string $$$t$$$.
The first line contains an integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of test cases.
For each test case:
The first line contains two integers $$$N$$$ and $$$K$$$ ($$$1 \le K \le N \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$ and the number of stages.
The second line contains a string $$$s$$$ of length $$$N$$$, consisting of lowercase English letters.
The third line contains an integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries.
Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le N$$$), describing the substring $$$t = s[l \dots r]$$$.
It is guaranteed that:
For each test case, print $$$q$$$ lines. For each query, output a single integer — the minimum total number of gold coins Tomeh must pay to clear all $$$K$$$ stages when the game is played on the string $$$t$$$.
29 5abcbaffff21 91 512 8fbcaaaaacbef23 104 7
0 6 14 26
Khaled and his brother are having an intense Mortal Kombat match. Determined to secure a "Flawless Victory" and leave his brother no room to breathe, Khaled decides to unleash a relentless chain of special moves.
In this game, the combo system is represented as a rooted tree consisting of $$$n$$$ nodes, where node $$$1$$$ is the root. Each node $$$i$$$ has a specific button assigned to it, represented by a lowercase English letter $$$c_i$$$.
To execute the special move associated with node $$$x$$$, a player must input the exact sequence of buttons along the simple path from the root to node $$$x$$$. Let this string of button presses be $$$S_x$$$.
For his ultimate attack, Khaled wants to execute two special moves, $$$u$$$ and $$$v$$$, in a single rapid string of inputs. A sequence of button presses is considered valid if both $$$S_u$$$ and $$$S_v$$$ are contiguous substrings$$$^\dagger$$$ of this sequence(they can overlap).
Since Khaled wants to execute the attack as quickly as possible, he needs your help to determine the minimum number of button presses required to perform both moves. You are given $$$q$$$ queries, each consisting of two nodes $$$u$$$ and $$$v$$$.For each query, output the minimum length of a valid button sequence.
————————————————————————
$$$^\dagger$$$A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 5 * 10 ^ 5$$$) — the number of nodes in the combo tree and the number of queries, respectively.
The second line contains a string $$$C$$$ of length $$$n$$$ consisting of lowercase English letters, where the $$$i$$$ - th character represents $$$c_i$$$, the button assigned to node $$$i$$$.
Each of the next $$$n - 1$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le n$$$), denoting an edge between node $$$x$$$ and node $$$y$$$ in the tree.
Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u \neq v \le n$$$) — the nodes representing the two special moves Khaled wants to execute.
For each query, print a single integer on a new line— the minimum number of button presses required to execute both special moves $$$u$$$ and $$$v$$$.
5 2abaab1 22 31 44 53 52 4
4 3
The string of node 3 is $$$S_3 = $$$ "aba" (path $$$1 \to 2 \to 3$$$).
The string of node 5 is $$$S_5 = $$$ "aab" (path $$$1 \to 4 \to 5$$$).
The string of node 2 is $$$S_2 = $$$ "ab" (path $$$1 \to 2$$$).
The string of node 4 is $$$S_4 = $$$ "aa" (path $$$1 \to 4$$$).
For the first query($$$u = 3$$$, $$$v = 5$$$) : Khaled needs the strings "aba" and "aab". The shortest sequence that contains both is "aaba" (length 4).
For the second query($$$u = 2$$$, $$$v = 4$$$) : Khaled needs the strings "ab" and "aa". The shortest sequence that contains both is "aab"(length 3).
One day, Hosen found a massive, incredibly heavy tree with $$$n$$$ nodes. The tree was special: each node had a value $$$a_i$$$ written on it, forming a permutation of integers from $$$0$$$ to $$$n-1$$$.
Without warning, Hosen threw the giant tree right into Rasoulo's arms!
"Hold the tree! Don't let it fall!" Hosen shouted as he started running away. "I am going to get us some shawarma. I will calculate its paths when I get back!"
Now, Rasoulo is stuck holding the tree. His arms are shaking, his back hurts, and he is sweating. The tree is so heavy! He desperately wants to put it down. To finish the task before his arms completely give out, Rasoulo decides to calculate the answer himself.
For any simple path between node $$$u$$$ and node $$$v$$$ ($$$u \lt v$$$), we define:
Rasoulo wants to calculate the total sum of $$$F(u, v)$$$ for all valid pairs of nodes $$$(u, v)$$$ such that $$$u \lt v$$$, where: $$$$$$F(u, v) = \text{length}(u, v) \times \text{MEX}(u, v)$$$$$$
Since the answer can be very large, print it modulo $$$10^9 + 7$$$. Can you help Rasoulo calculate the answer so he can finally drop the tree?
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of nodes in the tree.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \lt n$$$) — the values written on the nodes. It is guaranteed that all $$$a_i$$$ are distinct (the array $$$a$$$ is a permutation).
Each of the next $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$), denoting an edge between node $$$u$$$ and node $$$v$$$. It is guaranteed that the given edges form a valid tree.
Print a single integer — the sum of $$$F(u, v)$$$ for all pairs of nodes $$$u \lt v$$$, modulo $$$10^9 + 7$$$.
40 2 1 31 22 32 4
9
In the given sample, the tree has $$$4$$$ nodes. The values on the nodes are: $$$a_1 = 0$$$, $$$a_2 = 2$$$, $$$a_3 = 1$$$, and $$$a_4 = 3$$$. Let's analyze all valid pairs $$$(u, v)$$$ where $$$u \lt v$$$:
The total sum is $$$1 + 6 + 2 + 0 + 0 + 0 = 9$$$.
Homsi had a strange dream involving two positive integers $$$k$$$ and $$$b$$$.
In his dream, he saw a strictly increasing infinite array $$$a$$$ such that for every non-negative integer $$$x$$$, there exist exactly one ordered tuple of indices $$$i_1, i_2, \ldots, i_k$$$ (where the indices are not necessarily distinct and are not required to be in non-decreasing order) satisfying
$$$$$$x = a_{i_1} + b \cdot a_{i_2} + b^2 \cdot a_{i_3} + \cdots + b^{k-1} \cdot a_{i_k}$$$$$$
In other words, every non-negative integer has a unique representation of the above form.
After waking up, Homsi told his friends about the dream. Hussain doubted that such an array could even exist, so he challenged Homsi with the following task.
For each test case, given the integers $$$b$$$, $$$k$$$, $$$l$$$, and $$$r$$$, determine the value of $$$a_l + a_{l+1} + \cdots + a_r$$$ modulo $$$10^9+7$$$.
If no array satisfying the required property exists, output -1.
The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10^5$$$).
Each of the next $$$t$$$ lines contains three integers $$$b$$$, $$$k$$$, $$$l$$$, and $$$r$$$ ($$$2 \le b, k \le 10^{18}, 1 \le l \le r \le 10^{18}$$$).
For each test case, print the sum $$$a_l + a_{l+1} + \cdots + a_r \bmod (10^9+7)$$$ if such an array exists, -1 otherwise.
32 2 1 32 3 1 13 4 5 6
5 0 165
Due to severe destruction in Syria, many residents have fled and left their homes empty. The thief Hamoosh takes advantage of this to steal iron from the abandoned buildings, but the policeman Homsi spots him and initiates a pursuit.
There are $$$N$$$ buildings arranged in a circle, numbered $$$1$$$ to $$$N$$$ in clockwise order. For $$$1\le i \lt N$$$, the building following building $$$i$$$ is $$$i+1$$$, and the building following building $$$N$$$ is $$$1$$$. The $$$i$$$-th building contains $$$W_i$$$ units of iron.
You must choose the initial starting building for Hamoosh and the initial starting building for Homsi. At time $$$0$$$, both are placed on their respective starting buildings. In each turn, they move simultaneously in the clockwise direction: Hamoosh jumps forward by exactly $$$A$$$ buildings, and Homsi jumps forward by exactly $$$B$$$ buildings.
Hamoosh steals all $$$W_i$$$ units of iron from any building $$$i$$$ he lands on for the first time. If Hamoosh and Homsi land on the same building at the exact same moment, Hamoosh is immediately caught and the game ends. When caught, Hamoosh is arrested before he can steal the iron from that specific building.
You must assign the starting positions such that Homsi is guaranteed to eventually catch Hamoosh. Under this restriction, what is the maximum amount of iron Hamoosh can collect before being caught?
The first line contains three integers $$$N$$$, $$$A$$$, and $$$B$$$ ($$$1\le N\le 5000$$$, $$$1\le A,B\le N$$$) — the number of buildings, the number of buildings Hamoosh jumps per turn, and the number of buildings Homsi jumps per turn.
The second line contains $$$N$$$ integers $$$W_1,W_2,\dots,W_N$$$ ($$$0\le W_i\le 10^9$$$) — the amount of iron in each building.
Print a single integer — the maximum amount of iron Hamoosh can collect before being caught.
5 2 310 20 30 40 50
140
You are given a permutation $$$P = (P_1, P_2, \dots, P_N)$$$ of the integers from $$$1$$$ to $$$N$$$.
For a subarray $$$P[l \dots r] = (P_l, P_{l+1}, \dots, P_r)$$$ (where $$$1 \le l \le r \le N$$$), let us define its Value as follows: $$$$$$\text{Value}(l, r) = \text{Sum}(l, r) - \text{MEX}(l, r)$$$$$$
Where:
We call a range 3afwey if its value is as large as possible. El 3afweya is calling ..., and you have to respond. Determine the value of the 3afwey range.
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases.
Each testcase is structured as follows:
The first line contains an integers $$$N$$$ ($$$2 \le N \le 5 \cdot 10^5$$$) — the size of the permutation.
The second line contains $$$N$$$ space-separated integers $$$P_1, P_2, \dots, P_N$$$ ($$$1 \le P_i \le N$$$) — the initial permutation.
It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$
Print T integers, The $$$i$$$-th line should contain a single integer representing the maximum Value of a subarray of the $$$i$$$-th test case.
251 3 2 4 553 1 4 2 5
13 10
Khaled is the CEO of a rapidly growing industrial empire. His company recently acquired $$$N$$$ new factories, and he has hired $$$N$$$ workers to operate them. Both the workers' homes and the factories are located along a long, straight highway, which can be represented as the $$$X$$$-axis.
The $$$i$$$-th worker lives at coordinate $$$x_i$$$, and the $$$j$$$-th factory is located at coordinate $$$y_j$$$. Khaled needs to assign exactly one worker to each factory so that no two workers are assigned to the same factory.
Khaled requires a "stable" assignment. An assignment is invalid if swapping two workers' factories strictly increases the sum of their squared travel distances. Formally, for any worker $$$i$$$ assigned to factory $$$a$$$ and worker $$$j$$$ assigned to factory $$$b$$$, the following condition must NOT hold:
$$$$$$(x_i - y_a)^2 + (x_j - y_b)^2 \lt (x_i - y_b)^2 + (x_j - y_a)^2$$$$$$
Given the locations, help Khaled find any valid assignment satisfying his strict stability condition.
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$$$ ($$$1 \le N \le 3000$$$) — the number of workers and the number of factories.
The second line of each test case contains $$$N$$$ distinct integers $$$x_1, x_2, \dots, x_N$$$ ($$$-10^9 \le x_i \le 10^9$$$) — the coordinates of the workers.
The third line of each test case contains $$$N$$$ distinct integers $$$y_1, y_2, \dots, y_N$$$ ($$$-10^9 \le y_i \le 10^9$$$) — the coordinates of the factories.
It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$3000$$$.
For each test case, output a single line containing $$$N$$$ space-separated integers $$$p_1, p_2, \dots, p_N$$$, where $$$p_i$$$ is the 1-based index of the factory assigned to the $$$i$$$-th worker.
If there are multiple valid assignments that satisfy Khaled's condition, you may output any of them.
147 -5 -9 52 -8 7 -11
4 1 3 2