There are $$$n$$$ pieces of cake on a line. The $$$i$$$-th piece of cake has weight $$$a_i$$$ ($$$1 \leq i \leq n$$$).
The tastiness of the cake is the maximum total weight of two adjacent pieces of cake (i. e., $$$\max(a_1+a_2,\, a_2+a_3,\, \ldots,\, a_{n-1} + a_{n})$$$).
You want to maximize the tastiness of the cake. You are allowed to do the following operation at most once (doing more operations would ruin the cake):
The subsegment $$$a[l, r]$$$ of the array $$$a$$$ is the sequence $$$a_l, a_{l+1}, \dots, a_r$$$.
If you reverse it, the array will become $$$a_1, a_2, \dots, a_{l-2}, a_{l-1}, \underline{a_r}, \underline{a_{r-1}}, \underline{\dots}, \underline{a_{l+1}}, \underline{a_l}, a_{r+1}, a_{r+2}, \dots, a_{n-1}, a_n$$$.
For example, if the weights are initially $$$[5, 2, 1, 4, 7, 3]$$$, you can reverse the subsegment $$$a[2, 5]$$$, getting $$$[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$$$. The tastiness of the cake is now $$$5 + 7 = 12$$$ (while before the operation the tastiness was $$$4+7=11$$$).
Find the maximum tastiness of the cake after doing the operation at most once.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 50$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 1000$$$) — the number of pieces of cake.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the weight of the $$$i$$$-th piece of cake.
For each test case, print a single integer: the maximum tastiness of the cake after doing the operation at most once.
565 2 1 4 7 3332 78 78369 54 918999021 999021 999021 999021 999652 999021 999021 99902121000000000 1000000000
12 156 160 1998673 2000000000
In the first test case, after reversing the subsegment $$$a[2, 5]$$$, you get a cake with weights $$$[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$$$. The tastiness of the cake is now $$$\max(5+7, 7+4, 4+1, 1+2, 2+3) = 12$$$. This is the maximum possible tastiness of the cake one can obtain by performing the operation at most once.
In the second test case, it's optimal not to do any operation. The tastiness is $$$78+78 = 156$$$.
In the third test case, after reversing the subsegment $$$a[1, 2]$$$, you get a cake with weights $$$[\underline{54}, \underline{69}, 91]$$$. The tastiness of the cake is now $$$\max(54+69, 69+91) = 160$$$. There is no way to make the tastiness of the cake greater than $$$160$$$ by performing at most one operation.
You are given a string $$$s$$$ consisting of lowercase letters of the English alphabet. You must perform the following algorithm on $$$s$$$:
A prefix is a string consisting of several first letters of a given string, without any reorders. An empty prefix is also a valid prefix. For example, the string "abcd" has 5 prefixes: empty string, "a", "ab", "abc" and "abcd".
For instance, if we perform the algorithm on $$$s =$$$ "abcabdc",
Find the final state of the string after performing the algorithm.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
This is followed by $$$t$$$ lines, each containing a description of one test case. Each line contains a string $$$s$$$. The given strings consist only of lowercase letters of the English alphabet and have lengths between $$$1$$$ and $$$2 \cdot 10^5$$$ inclusive.
It is guaranteed that the sum of the lengths of $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single line containing the string $$$s$$$ after executing the algorithm. It can be shown that such string is non-empty.
6abcabdcabbbbbbbbbbcodeforcescffcfccffccfcffcfccfcffccffcfccfzyzyzwxxyyxxyyzzyzzxxwzxwywxwzxxyzzw
abdc a b deforces cf xyzzw
The first test case is explained in the statement.
In the second test case, no operations can be performed on $$$s$$$.
In the third test case,
In the fourth test case,
Alice has a cake, and she is going to cut it. She will perform the following operation $$$n-1$$$ times: choose a piece of the cake (initially, the cake is all one piece) with weight $$$w\ge 2$$$ and cut it into two smaller pieces of weight $$$\lfloor\frac{w}{2}\rfloor$$$ and $$$\lceil\frac{w}{2}\rceil$$$ ($$$\lfloor x \rfloor$$$ and $$$\lceil x \rceil$$$ denote floor and ceiling functions, respectively).
After cutting the cake in $$$n$$$ pieces, she will line up these $$$n$$$ pieces on a table in an arbitrary order. Let $$$a_i$$$ be the weight of the $$$i$$$-th piece in the line.
You are given the array $$$a$$$. Determine whether there exists an initial weight and sequence of operations which results in $$$a$$$.
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 2 \cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).
It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single line: print YES if the array $$$a$$$ could have resulted from Alice's operations, otherwise print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
1413272869 5412985214736 98521473732 3 132 3 361 1 1 1 1 16100 100 100 100 100 1008100 100 100 100 100 100 100 10082 16 1 8 64 1 4 32101 2 4 7 1 1 1 1 7 2107 1 1 1 3 1 3 3 2 3101 4 4 1 1 1 3 3 3 1102 3 2 2 1 2 2 2 2 24999999999 999999999 999999999 999999999
YES NO YES YES NO YES NO YES YES YES YES NO NO YES
In the first test case, it's possible to get the array $$$a$$$ by performing $$$0$$$ operations on a cake with weight $$$327$$$.
In the second test case, it's not possible to get the array $$$a$$$.
In the third test case, it's possible to get the array $$$a$$$ by performing $$$1$$$ operation on a cake with weight $$$1\,970\,429\,473$$$:
In the fourth test case, it's possible to get the array $$$a$$$ by performing $$$2$$$ operations on a cake with weight $$$6$$$:
Alice's potion making professor gave the following assignment to his students: brew a potion using $$$n$$$ ingredients, such that the proportion of ingredient $$$i$$$ in the final potion is $$$r_i \gt 0$$$ (and $$$r_1 + r_2 + \cdots + r_n = 1$$$).
He forgot the recipe, and now all he remembers is a set of $$$n-1$$$ facts of the form, "ingredients $$$i$$$ and $$$j$$$ should have a ratio of $$$x$$$ to $$$y$$$" (i.e., if $$$a_i$$$ and $$$a_j$$$ are the amounts of ingredient $$$i$$$ and $$$j$$$ in the potion respectively, then it must hold $$$a_i/a_j = x/y$$$), where $$$x$$$ and $$$y$$$ are positive integers. However, it is guaranteed that the set of facts he remembers is sufficient to uniquely determine the original values $$$r_i$$$.
He decided that he will allow the students to pass the class as long as they submit a potion which satisfies all of the $$$n-1$$$ requirements (there may be many such satisfactory potions), and contains a positive integer amount of each ingredient.
Find the minimum total amount of ingredients needed to make a potion which passes the class. As the result can be very large, you should print the answer modulo $$$998\,244\,353$$$.
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$$$ ($$$2 \le n \le 2 \cdot 10^5$$$).
Each of the next $$$n-1$$$ lines contains four integers $$$i, j, x, y$$$ ($$$1 \le i, j \le n$$$, $$$i\not=j$$$, $$$1\le x, y \le n$$$) — ingredients $$$i$$$ and $$$j$$$ should have a ratio of $$$x$$$ to $$$y$$$. It is guaranteed that the set of facts is sufficient to uniquely determine the original values $$$r_i$$$.
It is also guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print the minimum total amount of ingredients needed to make a potion which passes the class, modulo $$$998\,244\,353$$$.
343 2 3 41 2 4 31 4 2 485 4 2 36 4 5 41 3 5 26 8 2 13 5 3 43 2 2 56 7 4 3178 7 4 169 17 4 55 14 13 1211 1 17 146 13 8 92 11 3 114 17 7 217 16 8 615 5 1 1416 7 1 1012 17 13 1011 16 7 210 11 6 413 17 14 63 11 15 815 6 12 8
69 359 573672453
In the first test case, the minimum total amount of ingredients is $$$69$$$. In fact, the amounts of ingredients $$$1, 2, 3, 4$$$ of a valid potion are $$$16, 12, 9, 32$$$, respectively. The potion is valid because
In the second test case, the amounts of ingredients $$$1, 2, 3, 4, 5, 6, 7, 8$$$ in the potion that minimizes the total amount of ingredients are $$$60, 60, 24, 48, 32, 60, 45, 30$$$.
You are given an array of integers $$$a_1, a_2, \ldots, a_n$$$.
You can do the following operation any number of times (possibly zero):
What is the minimum number of operations needed to turn $$$a$$$ into an arithmetic progression? The array $$$a$$$ is an arithmetic progression if $$$a_{i+1}-a_i=a_i-a_{i-1}$$$ for any $$$2 \leq i \leq n-1$$$.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$).
Print a single integer: the minimum number of operations needed to turn $$$a$$$ into an arithmetic progression.
9 3 2 7 8 6 9 5 4 1
6
14 19 2 15 8 9 14 17 13 4 14 4 11 15 7
10
10 100000 1 60000 2 20000 4 8 16 32 64
7
4 10000 20000 10000 1
2
In the first test, you can get the array $$$a = [11, 10, 9, 8, 7, 6, 5, 4, 3]$$$ by performing $$$6$$$ operations:
$$$a$$$ is an arithmetic progression: in fact, $$$a_{i+1}-a_i=a_i-a_{i-1}=-1$$$ for any $$$2 \leq i \leq n-1$$$.
There is no sequence of less than $$$6$$$ operations that makes $$$a$$$ an arithmetic progression.
In the second test, you can get the array $$$a = [-1, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38]$$$ by performing $$$10$$$ operations.
In the third test, you can get the array $$$a = [100000, 80000, 60000, 40000, 20000, 0, -20000, -40000, -60000, -80000]$$$ by performing $$$7$$$ operations.
You are given an integer $$$n$$$ and a string $$$s$$$ consisting of $$$2^n$$$ lowercase letters of the English alphabet. The characters of the string $$$s$$$ are $$$s_0s_1s_2\cdots s_{2^n-1}$$$.
A string $$$t$$$ of length $$$2^n$$$ (whose characters are denoted by $$$t_0t_1t_2\cdots t_{2^n-1}$$$) is a xoration of $$$s$$$ if there exists an integer $$$j$$$ ($$$0\le j \leq 2^n-1$$$) such that, for each $$$0 \leq i \leq 2^n-1$$$, $$$t_i = s_{i \oplus j}$$$ (where $$$\oplus$$$ denotes the operation bitwise XOR).
Find the lexicographically minimal xoration of $$$s$$$.
A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds:
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 18$$$).
The second line contains a string $$$s$$$ consisting of $$$2^n$$$ lowercase letters of the English alphabet.
Print a single line containing the lexicographically minimal xoration of $$$s$$$.
2 acba
abca
3 bcbaaabb
aabbbcba
4 bdbcbccdbdbaaccd
abdbdccacbdbdccb
5 ccfcffccccccffcfcfccfffffcccccff
cccccffffcccccffccfcffcccccfffff
1 zz
zz
In the first test, the lexicographically minimal xoration $$$t$$$ of $$$s =$$$"acba" is "abca". It's a xoration because, for $$$j = 3$$$,
In the second test, the minimal string xoration corresponds to choosing $$$j = 4$$$ in the definition of xoration.
In the third test, the minimal string xoration corresponds to choosing $$$j = 11$$$ in the definition of xoration.
In the fourth test, the minimal string xoration corresponds to choosing $$$j = 10$$$ in the definition of xoration.
In the fifth test, the minimal string xoration corresponds to choosing either $$$j = 0$$$ or $$$j = 1$$$ in the definition of xoration.
There are $$$n$$$ locations on a snowy mountain range (numbered from $$$1$$$ to $$$n$$$), connected by $$$n-1$$$ trails in the shape of a tree. Each trail has length $$$1$$$. Some of the locations are base lodges. The height $$$h_i$$$ of each location is equal to the distance to the nearest base lodge (a base lodge has height $$$0$$$).
There is a skier at each location, each skier has initial kinetic energy $$$0$$$. Each skier wants to ski along as many trails as possible. Suppose that the skier is skiing along a trail from location $$$i$$$ to $$$j$$$. Skiers are not allowed to ski uphill (i.e., if $$$h_i \lt h_j$$$). It costs one unit of kinetic energy to ski along flat ground (i.e., if $$$h_i = h_j$$$), and a skier gains one unit of kinetic energy by skiing downhill (i.e., if $$$h_i \gt h_j$$$). For each location, compute the length of the longest sequence of trails that the skier starting at that location can ski along without their kinetic energy ever becoming negative. Skiers are allowed to visit the same location or trail multiple times.
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$).
The second line contains $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \le l_i \le 1$$$). If $$$l_i = 1$$$, location $$$i$$$ is a base lodge; if $$$l_i = 0$$$, location $$$i$$$ is not a base lodge. It is guaranteed that there is at least $$$1$$$ base lodge.
Each of the next $$$n-1$$$ lines contains two integers $$$u, v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), meaning that there is a trail that connects the locations $$$u$$$ and $$$v$$$. It is guaranteed that the given trails form a tree.
Print $$$n$$$ integers: the $$$i$$$-th integer is equal to the length of the longest sequence of trails that the skier starting at location $$$i$$$ can ski along without their kinetic energy ever becoming negative.
6 1 1 0 0 0 0 1 3 2 4 3 4 4 5 5 6
0 0 1 1 3 5
9 0 0 0 0 0 0 1 1 1 1 3 2 3 2 5 3 6 4 5 4 7 5 8 6 9
5 3 2 1 1 1 0 0 0
14 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 5 3 4 4 5 3 6 4 8 5 9 7 8 6 11 7 12 8 13 9 14 10 11
8 5 4 3 2 2 1 1 1 0 0 0 0 0
20 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 1 17 3 11 12 6 10 18 19 8 14 16 20 5 3 2 11 7 10 2 15 8 3 3 15 9 16 7 13 16 1 19 2 2 16 6 1 4 17
2 2 1 5 3 4 8 1 2 6 4 6 10 0 0 0 3 0 1 0
In the first test, $$$h = [0, 0, 1, 1, 2, 3]$$$. The skier starting from $$$6$$$ can ski along at most $$$5$$$ trails, in the path $$$6 \rightarrow 5 \rightarrow 4 \rightarrow 3 \rightarrow 4 \rightarrow 2$$$ (notice that a skier can ski multiple times along the same trail and can visit more than once the same location):
There isn't any sequence of trails of length greater than $$$5$$$ such that the kinetic energy is always non-negative.
Moreover,
In the second test, $$$h = [3, 2, 2, 1, 1, 1, 0, 0, 0]$$$. The skier starting from $$$1$$$ can ski along at most $$$5$$$ trails, in the path $$$1 \rightarrow 3 \rightarrow 2 \rightarrow 5 \rightarrow 4 \rightarrow 7$$$.
There isn't any sequence of trails of length greater than $$$5$$$ such that the kinetic energy is always non-negative.
In the third test, for the skier starting from vertex $$$1$$$, the optimal path is $$$1 \rightarrow 2 \rightarrow 5 \rightarrow 4 \rightarrow 3 \rightarrow 6 \rightarrow 11 \rightarrow 10 \rightarrow 11$$$.
Here are pictures of the first, second, and third test, with the base lodges shown in red:
Given a list of distinct values, we denote with first minimum, second minimum, and third minimum the three smallest values (in increasing order).
A permutation $$$p_1, p_2, \dots, p_n$$$ is good if the following statement holds for all pairs $$$(l,r)$$$ with $$$1\le l \lt l+2 \le r\le n$$$.
You are given an integer $$$n$$$ and a string $$$s$$$ of length $$$m$$$ consisting of characters "<" and ">".
Count the number of good permutations $$$p_1, p_2,\dots, p_n$$$ such that, for all $$$1\le i\le m$$$,
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \leq m \leq \min(100, n-1)$$$).
The second line contains a string $$$s$$$ of length $$$m$$$, consisting of characters "<" and ">".
Print a single integer: the number of good permutations satisfying the constraints described in the statement, modulo $$$998\,244\,353$$$.
5 3 >>>
5
5 1 <
56
6 5 <<><>
23
10 5 ><<><
83154
1008 20 <><<>>><<<<<>>>>>>>>
284142857
In the first test, there are $$$5$$$ good permutations satisfying the constraints given by the string $$$s$$$: $$$[4, 3, 2, 1, 5]$$$, $$$[5, 3, 2, 1, 4]$$$, $$$[5, 4, 2, 1, 3]$$$, $$$[5, 4, 3, 1, 2]$$$, $$$[5, 4, 3, 2, 1]$$$. Each of them
In the second test, there are $$$60$$$ permutations such that $$$p_1 \lt p_2$$$. Only $$$56$$$ of them are good: the permutations $$$[1, 4, 3, 5, 2]$$$, $$$[1, 5, 3, 4, 2]$$$, $$$[2, 4, 3, 5, 1]$$$, $$$[2, 5, 3, 4, 1]$$$ are not good because the required condition doesn't hold for $$$(l, r)$$$ = $$$(1, 5)$$$. For example, for the permutation $$$[2, 4, 3, 5, 1]$$$,
In the third test, there are $$$23$$$ good permutations satisfying the constraints given by the string $$$s$$$: $$$[1, 2, 4, 3, 6, 5]$$$, $$$[1, 2, 5, 3, 6, 4]$$$, $$$[1, 2, 6, 3, 5, 4]$$$, $$$[1, 3, 4, 2, 6, 5]$$$, $$$[1, 3, 5, 2, 6, 4]$$$, $$$[1, 3, 6, 2, 5, 4]$$$, $$$[1, 4, 5, 2, 6, 3]$$$, $$$[1, 4, 6, 2, 5, 3]$$$, $$$[1, 5, 6, 2, 4, 3]$$$, $$$[2, 3, 4, 1, 6, 5]$$$, $$$[2, 3, 5, 1, 6, 4]$$$, $$$[2, 3, 6, 1, 5, 4]$$$, $$$[2, 4, 5, 1, 6, 3]$$$, $$$[2, 4, 6, 1, 5, 3]$$$, $$$[2, 5, 6, 1, 4, 3]$$$, $$$[3, 4, 5, 1, 6, 2]$$$, $$$[3, 4, 5, 2, 6, 1]$$$, $$$[3, 4, 6, 1, 5, 2]$$$, $$$[3, 4, 6, 2, 5, 1]$$$, $$$[3, 5, 6, 1, 4, 2]$$$, $$$[3, 5, 6, 2, 4, 1]$$$, $$$[4, 5, 6, 1, 3, 2]$$$, $$$[4, 5, 6, 2, 3, 1]$$$.