apaz/codeforces-genesys huggingface dataset:
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
Omkar is building a house. He wants to decide how to make the floor plan for the last floor.

Omkar's floor starts out as $$$n$$$ rows of $$$m$$$ zeros ($$$1 \le n,m \le 100$$$). Every row is divided into intervals such that every $$$0$$$ in the row is in exactly $$$1$$$ interval. For every interval for every row, Omkar can change exactly one of the $$$0$$$s contained in that interval to a $$$1$$$. Omkar defines the quality of a floor as the sum of the squares of the sums of the values in each column, i. e. if the sum of the values in the $$$i$$$-th column is $$$q_i$$$, then the quality of the floor is $$$\sum_{i = 1}^m q_i^2$$$.

Help Omkar find the maximum quality that the floor can have.

## Input Format
The first line contains two integers, $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 100$$$), which are the number of rows and number of columns, respectively.

You will then receive a description of the intervals in each row. For every row $$$i$$$ from $$$1$$$ to $$$n$$$: The first row contains a single integer $$$k_i$$$ ($$$1 \le k_i \le m$$$), which is the number of intervals on row $$$i$$$. The $$$j$$$-th of the next $$$k_i$$$ lines contains two integers $$$l_{i,j}$$$ and $$$r_{i,j}$$$, which are the left and right bound (both inclusive), respectively, of the $$$j$$$-th interval of the $$$i$$$-th row. It is guaranteed that all intervals other than the first interval will be directly after the interval before it. Formally, $$$l_{i,1} = 1$$$, $$$l_{i,j} \leq r_{i,j}$$$ for all $$$1 \le j \le k_i$$$, $$$r_{i,j-1} + 1 = l_{i,j}$$$ for all $$$2 \le j \le k_i$$$, and $$$r_{i,k_i} = m$$$.

## Output Format
Output one integer, which is the maximum possible quality of an eligible floor plan.

## Examples
```input
4 5
2
1 2
3 5
2
1 3
4 5
3
1 1
2 4
5 5
3
1 1
2 2
3 5
```
```output
36
```

## Note
The given test case corresponds to the following diagram. Cells in the same row and have the same number are a part of the same interval.

The most optimal assignment is:

The sum of the $$$1$$$st column is $$$4$$$, the sum of the $$$2$$$nd column is $$$2$$$, the sum of the $$$3$$$rd and $$$4$$$th columns are $$$0$$$, and the sum of the $$$5$$$th column is $$$4$$$.

The quality of this floor plan is $$$4^2 + 2^2 + 0^2 + 0^2 + 4^2 = 36$$$. You can show that there is no floor plan with a higher quality.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
Koa the Koala has a binary string $$$s$$$ of length $$$n$$$. Koa can perform no more than $$$n-1$$$ (possibly zero) operations of the following form:

In one operation Koa selects positions $$$i$$$ and $$$i+1$$$ for some $$$i$$$ with $$$1 \le i < |s|$$$ and sets $$$s_i$$$ to $$$max(s_i, s_{i+1})$$$. Then Koa deletes position $$$i+1$$$ from $$$s$$$ (after the removal, the remaining parts are concatenated).

Note that after every operation the length of $$$s$$$ decreases by $$$1$$$.

How many different binary strings can Koa obtain by doing no more than $$$n-1$$$ (possibly zero) operations modulo $$$10^9+7$$$ ($$$1000000007$$$)?

## Input Format
The only line of input contains binary string $$$s$$$ ($$$1 \le |s| \le 10^6$$$). For all $$$i$$$ ($$$1 \le i \le |s|$$$) $$$s_i = 0$$$ or $$$s_i = 1$$$.

## Output Format
On a single line print the answer to the problem modulo $$$10^9+7$$$ ($$$1000000007$$$).

## Examples
```input
000
```
```output
3
```
-----
```input
0101
```
```output
6
```
-----
```input
0001111
```
```output
16
```
-----
```input
00101100011100
```
```output
477
```

## Note
In the first sample Koa can obtain binary strings: $$$0$$$, $$$00$$$ and $$$000$$$.

In the second sample Koa can obtain binary strings: $$$1$$$, $$$01$$$, $$$11$$$, $$$011$$$, $$$101$$$ and $$$0101$$$. For example:

- to obtain $$$01$$$ from $$$0101$$$ Koa can operate as follows: $$$0101 \rightarrow 0(10)1 \rightarrow 011 \rightarrow 0(11) \rightarrow 01$$$.
- to obtain $$$11$$$ from $$$0101$$$ Koa can operate as follows: $$$0101 \rightarrow (01)01 \rightarrow 101 \rightarrow 1(01) \rightarrow 11$$$.

Parentheses denote the two positions Koa selected in each operation.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
You are given an undirected graph where each edge has one of two colors: black or red.

Your task is to assign a real number to each node so that:

- for each black edge the sum of values at its endpoints is $$$1$$$;
- for each red edge the sum of values at its endpoints is $$$2$$$;
- the sum of the absolute values of all assigned numbers is the smallest possible.

Otherwise, if it is not possible, report that there is no feasible assignment of the numbers.

## Input Format
The first line contains two integers $$$N$$$ ($$$1 \leq N \leq 100\,000$$$) and $$$M$$$ ($$$0 \leq M \leq 200\,000$$$): the number of nodes and the number of edges, respectively. The nodes are numbered by consecutive integers: $$$1, 2, \ldots, N$$$.

The next $$$M$$$ lines describe the edges. Each line contains three integers $$$a$$$, $$$b$$$ and $$$c$$$ denoting that there is an edge between nodes $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq N$$$) with color $$$c$$$ ($$$1$$$ denotes black, $$$2$$$ denotes red).

## Output Format
If there is a solution, the first line should contain the word "YES" and the second line should contain $$$N$$$ space-separated numbers. For each $$$i$$$ ($$$1 \le i \le N$$$), the $$$i$$$-th number should be the number assigned to the node $$$i$$$.

Output should be such that:

- the sum of the numbers at the endpoints of each edge differs from the precise value by less than $$$10^{-6}$$$;
- the sum of the absolute values of all assigned numbers differs from the smallest possible by less than $$$10^{-6}$$$.

If there are several valid solutions, output any of them.

If there is no solution, the only line should contain the word "NO".

## Examples
```input
4 4
1 2 1
2 3 2
1 3 2
3 4 1
```
```output
YES
0.5 0.5 1.5 -0.5
```
-----
```input
2 1
1 2 1
```
```output
YES
0.3 0.7
```
-----
```input
3 2
1 2 2
2 3 2
```
```output
YES
0 2 0
```
-----
```input
3 4
1 2 2
2 2 1
2 1 1
1 2 2
```
```output
NO
```

## Note
Scoring:
Subtasks:

1. (5 points) $$$N \leq 5$$$, $$$M \leq 14$$$
2. (12 points) $$$N \leq 100$$$
3. (17 points) $$$N \leq 1000$$$
4. (24 points) $$$N \leq 10\,000$$$
5. (42 points) No further constraints

Note that in the second example the solution is not unique.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 0.75 seconds
Memory limit: 256.0 MB

# Problem
This problem is split into two tasks. In this task, you are required to find the minimum possible answer. In the task Village (Maximum) you are required to find the maximum possible answer. Each task is worth $$$50$$$ points.

There are $$$N$$$ houses in a certain village. A single villager lives in each of the houses. The houses are connected by roads. Each road connects two houses and is exactly $$$1$$$ kilometer long. From each house it is possible to reach any other using one or several consecutive roads. In total there are $$$N-1$$$ roads in the village.

One day all villagers decided to move to different houses — that is, after moving each house should again have a single villager living in it, but no villager should be living in the same house as before. We would like to know the smallest possible total length in kilometers of the shortest paths between the old and the new houses for all villagers.

Example village with seven houses

For example, if there are seven houses connected by roads as shown on the figure, the smallest total length is $$$8$$$ km (this can be achieved by moving $$$1 \to 6$$$, $$$2 \to 4$$$, $$$3 \to 1$$$, $$$4 \to 2$$$, $$$5 \to 7$$$, $$$6 \to 3$$$, $$$7 \to 5$$$).

Write a program that finds the smallest total length of the shortest paths in kilometers and an example assignment of the new houses to the villagers.

## Input Format
The first line contains an integer $$$N$$$ ($$$1 < N \le 10^5$$$). Houses are numbered by consecutive integers $$$1, 2, \ldots, N$$$.

Then $$$N-1$$$ lines follow that describe the roads. Each line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le N$$$, $$$a \neq b$$$) denoting that there is a road connecting houses $$$a$$$ and $$$b$$$.

## Output Format
In the first line output the smallest total length of the shortest paths in kilometers.

In the second line describe one valid assignment of the new houses with the smallest total length: $$$N$$$ space-separated distinct integers $$$v_1, v_2, \ldots, v_N$$$. For each $$$i$$$, $$$v_i$$$ is the house number where the villager from the house $$$i$$$ should move ($$$v_i \neq i$$$). If there are several valid assignments, output any of those.

## Examples
```input
4
1 2
2 3
3 4
```
```output
4
2 1 4 3
```
-----
```input
7
4 2
5 7
3 4
6 3
1 3
4 5
```
```output
8
3 4 6 2 7 1 5
```

## Note
Scoring:
Subtasks:

1. (6 points) $$$N \leq 10$$$
2. (19 points) $$$N \leq 1\,000$$$
3. (25 points) No further constraints

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 0.75 seconds
Memory limit: 256.0 MB

# Problem
This problem is split into two tasks. In this task, you are required to find the maximum possible answer. In the task Village (Minimum) you are required to find the minimum possible answer. Each task is worth $$$50$$$ points.

There are $$$N$$$ houses in a certain village. A single villager lives in each of the houses. The houses are connected by roads. Each road connects two houses and is exactly $$$1$$$ kilometer long. From each house it is possible to reach any other using one or several consecutive roads. In total there are $$$N-1$$$ roads in the village.

One day all villagers decided to move to different houses — that is, after moving each house should again have a single villager living in it, but no villager should be living in the same house as before. We would like to know the largest possible total length in kilometers of the shortest paths between the old and the new houses for all villagers.

Example village with seven houses

For example, if there are seven houses connected by roads as shown on the figure, the largest total length is $$$18$$$ km (this can be achieved by moving $$$1 \to 7$$$, $$$2 \to 3$$$, $$$3 \to 4$$$, $$$4 \to 1$$$, $$$5 \to 2$$$, $$$6 \to 5$$$, $$$7 \to 6$$$).

Write a program that finds the largest total length of the shortest paths in kilometers and an example assignment of the new houses to the villagers.

## Input Format
The first line contains an integer $$$N$$$ ($$$1 < N \le 10^5$$$). Houses are numbered by consecutive integers $$$1, 2, \ldots, N$$$.

Then $$$N-1$$$ lines follow that describe the roads. Each line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le N$$$, $$$a \neq b$$$) denoting that there is a road connecting houses $$$a$$$ and $$$b$$$.

## Output Format
In the first line output the largest total length of the shortest paths in kilometers.

In the second line describe one valid assignment of the new houses with the largest total length: $$$N$$$ space-separated distinct integers $$$v_1, v_2, \ldots, v_N$$$. For each $$$i$$$, $$$v_i$$$ is the house number where villager from the house $$$i$$$ should move ($$$v_i \neq i$$$). If there are several valid assignments, output any of those.

## Examples
```input
4
1 2
2 3
3 4
```
```output
8
4 3 2 1
```
-----
```input
7
4 2
5 7
3 4
6 3
1 3
4 5
```
```output
18
2 7 4 1 3 5 6
```

## Note
Scoring:
Subtasks:

1. (6 points) $$$N \leq 10$$$
2. (19 points) $$$N \leq 1\,000$$$
3. (25 points) No further constraints

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
One Sunday Petr went to a bookshop and bought a new book on sports programming. The book had exactly n pages.

Petr decided to start reading it starting from the next day, that is, from Monday. Petr's got a very tight schedule and for each day of the week he knows how many pages he will be able to read on that day. Some days are so busy that Petr will have no time to read whatsoever. However, we know that he will be able to read at least one page a week.

Assuming that Petr will not skip days and will read as much as he can every day, determine on which day of the week he will read the last page of the book.

## Input Format
The first input line contains the single integer n (1 ≤ n ≤ 1000) — the number of pages in the book.

The second line contains seven non-negative space-separated integers that do not exceed 1000 — those integers represent how many pages Petr can read on Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday correspondingly. It is guaranteed that at least one of those numbers is larger than zero.

## Output Format
Print a single number — the number of the day of the week, when Petr will finish reading the book. The days of the week are numbered starting with one in the natural order: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.

## Examples
```input
100
15 20 20 15 10 30 45
```
```output
6
```
-----
```input
2
1 0 0 0 0 0 0
```
```output
1
```

## Note
Note to the first sample:

By the end of Monday and therefore, by the beginning of Tuesday Petr has 85 pages left. He has 65 pages left by Wednesday, 45 by Thursday, 30 by Friday, 20 by Saturday and on Saturday Petr finishes reading the book (and he also has time to read 10 pages of something else).

Note to the second sample:

On Monday of the first week Petr will read the first page. On Monday of the second week Petr will read the second page and will finish reading the book.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
BThero is a powerful magician. He has got $$$n$$$ piles of candies, the $$$i$$$-th pile initially contains $$$a_i$$$ candies. BThero can cast a copy-paste spell as follows:

1. He chooses two piles $$$(i, j)$$$ such that $$$1 \le i, j \le n$$$ and $$$i \ne j$$$.
2. All candies from pile $$$i$$$ are copied into pile $$$j$$$. Formally, the operation $$$a_j := a_j + a_i$$$ is performed.

BThero can cast this spell any number of times he wants to — but unfortunately, if some pile contains strictly more than $$$k$$$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power?

## Input Format
The first line contains one integer $$$T$$$ ($$$1 \le T \le 500$$$) — the number of test cases.

Each test case consists of two lines:

- the first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 1000$$$, $$$2 \le k \le 10^4$$$);
- the second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le k$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$10^4$$$.

## Output Format
For each test case, print one integer — the maximum number of times BThero can cast the spell without losing his magic power.

## Examples
```input
3
2 2
1 1
3 5
1 2 3
3 7
3 2 2
```
```output
1
5
4
```

## Note
In the first test case we get either $$$a = [1, 2]$$$ or $$$a = [2, 1]$$$ after casting the spell for the first time, and it is impossible to cast it again.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.

Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.

For example:

- if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$;
- if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$.

Help RedDreamer to paint the array optimally!

## Input Format
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow.

The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively.

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

The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

## Output Format
For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$.

If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them.

## Examples
```input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
```
```output
1 0 0 1 1 0 
1 0 0
```

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
Yura is tasked to build a closed fence in shape of an arbitrary non-degenerate simple quadrilateral. He's already got three straight fence segments with known lengths $$$a$$$, $$$b$$$, and $$$c$$$. Now he needs to find out some possible integer length $$$d$$$ of the fourth straight fence segment so that he can build the fence using these four segments. In other words, the fence should have a quadrilateral shape with side lengths equal to $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$. Help Yura, find any possible length of the fourth side.

A non-degenerate simple quadrilateral is such a quadrilateral that no three of its corners lie on the same line, and it does not cross itself.

## Input Format
The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 1000$$$). The next $$$t$$$ lines describe the test cases.

Each line contains three integers $$$a$$$, $$$b$$$, and $$$c$$$ — the lengths of the three fence segments ($$$1 \le a, b, c \le 10^9$$$).

## Output Format
For each test case print a single integer $$$d$$$ — the length of the fourth fence segment that is suitable for building the fence. If there are multiple answers, print any. We can show that an answer always exists.

## Examples
```input
2
1 2 3
12 34 56
```
```output
4
42
```

## Note
We can build a quadrilateral with sides $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$.

We can build a quadrilateral with sides $$$12$$$, $$$34$$$, $$$56$$$, $$$42$$$.

Now solve the problem and return the code.