Professional Documents
Culture Documents
2)
A. Suits input
1 second, 256 megabytes 17
14
5
A new delivery of clothing has arrived today to the clothing store. This
21
delivery consists of 𝑎 ties, 𝑏 scarves, 𝑐 vests and 𝑑 jackets. 15
17
The store does not sell single clothing items — instead, it sells suits of
two types: output
325
a suit of the first type consists of one tie and one jacket;
a suit of the second type consists of one scarf, one vest and one It is possible to compose three suits of the second type in the first
jacket. example, and their total cost will be 6. Since all jackets will be used, it's
impossible to add anything to this set.
Each suit of the first type costs 𝑒 coins, and each suit of the second type
costs 𝑓 coins. The best course of action in the second example is to compose nine suits
of the first type and eleven suits of the second type. The total cost is
Calculate the maximum possible cost of a set of suits that can be 9 ⋅ 4 + 11 ⋅ 6 = 102 .
composed from the delivered clothing items. Note that one item cannot
be used in more than one suit (though some items may be left unused).
B. Blocks
Input
The first line contains one integer 𝑎 (1 ≤ 𝑎 ≤ 100 000) — the number of 2 seconds, 256 megabytes
ties.
There are 𝑛 blocks arranged in a row and numbered from left to right,
The second line contains one integer 𝑏 (1 ≤ 𝑏 ≤ 100 000) — the starting from one. Each block is either black or white.
number of scarves.
You may perform the following operation zero or more times: choose two
The third line contains one integer 𝑐 (1 ≤ 𝑐 ≤ 100 000) — the number of adjacent blocks and invert their colors (white block becomes black, and
vests. vice versa).
The fourth line contains one integer 𝑑 (1 ≤ 𝑑 ≤ 100 000) — the number You want to find a sequence of operations, such that they make all the
of jackets. blocks having the same color. You don't have to minimize the number of
operations, but it should not exceed 3 ⋅ 𝑛 . If it is impossible to find such a
The fifth line contains one integer 𝑒 (1 ≤ 𝑒 ≤ 1 000) — the cost of one
sequence of operations, you need to report it.
suit of the first type.
The sixth line contains one integer 𝑓 (1 ≤ 𝑓 ≤ 1 000) — the cost of one
Input
The first line contains one integer 𝑛 (2 ≤ 𝑛 ≤ 200 ) — the number of
suit of the second type.
blocks.
Output The second line contains one string 𝑠 consisting of 𝑛 characters, each
Print one integer — the maximum total cost of some set of suits that can
character is either "W" or "B". If the 𝑖-th character is "W", then the 𝑖-th
be composed from the delivered items.
block is white. If the 𝑖-th character is "B", then the 𝑖-th block is black.
input Output
4 If it is impossible to make all the blocks having the same color, print −1.
5
6 Otherwise, print an integer 𝑘 (0 ≤ 𝑘 ≤ 3 ⋅ 𝑛) — the number of
3 operations. Then print 𝑘 integers 𝑝1 , 𝑝2 , … , 𝑝𝑘 (1 ≤ 𝑝𝑗 ≤ 𝑛 − 1) , where
1 𝑝𝑗 is the position of the left block in the pair of blocks that should be
2
affected by the 𝑗 -th operation.
output
If there are multiple answers, print any of them.
6
input
input
8
12 BWWWWWWB
11
13 output
20
3
4
6 2 4
6
output input
102
4
BWBB
output
-1
/
input The second line should contain two integers 𝑝𝑥 and 𝑝𝑦 — the
coordinates where the tent should be located. If there are multiple
5
WWWWW answers, print any of them. Note that each of 𝑝𝑥 and 𝑝𝑦 should be not
less than 0 and not greater than 10 .
9
output
0 input
4 3 2
input 1 3
4 2
3 5 1
BWB 4 1
output output
2
3
2 1
4 2
After classes end, each student walks from the school to his house along In the second example, it is possible to build the shawarma tent in (1, 1),
one of the shortest paths. So the distance the 𝑖-th student goes from the then both students living in (0, 0) will visit it.
school to his house is |𝑠𝑥 − 𝑥𝑖 | + |𝑠𝑦 − 𝑦𝑖 | .
You want to find the maximum possible number of students buying need at least 𝑎𝑖 warriors (you are so good at this game that you don't
shawarma and the optimal location for the tent itself. lose any warriors while taking over a castle, so your army stays the same
after the fight). After you take control over a castle, you recruit new
Input
warriors into your army — formally, after you capture the 𝑖-th castle, 𝑏𝑖
The first line contains three integers 𝑛, 𝑠𝑥 , 𝑠𝑦 (1 ≤ 𝑛 ≤ 200 000,
warriors join your army. Furthermore, after capturing a castle (or later) you
0 ≤ 𝑠𝑥 , 𝑠𝑦 ≤ 10 ) — the number of students and the coordinates of the
9
can defend it: if you leave at least one warrior in a castle, this castle is
school, respectively. considered defended. Each castle has an importance parameter 𝑐𝑖 , and
Then 𝑛 lines follow. The 𝑖-th of them contains two integers 𝑥𝑖 , 𝑦𝑖 ( your total score is the sum of importance values over all defended
0 ≤ 𝑥𝑖 , 𝑦𝑖 ≤ 10 ) — the location of the house where the 𝑖-th student
9 castles. There are two ways to defend a castle:
lives. Some locations of houses may coincide, but no student lives in the
if you are currently in the castle 𝑖, you may leave one warrior to
same location where the school is situated.
defend castle 𝑖;
Output there are 𝑚 one-way portals connecting the castles. Each portal is
The output should consist of two lines. The first of them should contain characterised by two numbers of castles 𝑢 and 𝑣 (for each portal
one integer 𝑐 — the maximum number of students that will buy holds 𝑢 > 𝑣). A portal can be used as follows: if you are currently in
shawarmas at the tent.
the castle 𝑢, you may send one warrior to defend castle 𝑣 .
/
Obviously, when you order your warrior to defend some castle, he leaves input
your army.
4 3 7
You capture the castles in fixed order: you have to capture the first one, 7 4 17
3 0 8
then the second one, and so on. After you capture the castle 𝑖 (but only 11 2 0
before capturing castle 𝑖 + 1) you may recruit new warriors from castle 𝑖, 14 3 5
leave a warrior to defend castle 𝑖, and use any number of portals leading 3 1
from castle 𝑖 to other castles having smaller numbers. As soon as you 2 1
4 3
capture the next castle, these actions for castle 𝑖 won't be available to
you. output
If, during some moment in the game, you don't have enough warriors to -1
capture the next castle, you lose. Your goal is to maximize the sum of
importance values over all defended castles (note that you may hire new The best course of action in the first example is as follows:
warriors in the last castle, defend it and use portals leading from it even
1. capture the first castle;
after you capture it — your score will be calculated afterwards).
2. hire warriors from the first castle, your army has 11 warriors now;
Can you determine an optimal strategy of capturing and defending the 3. capture the second castle;
castles? 4. capture the third castle;
Input 5. hire warriors from the third castle, your army has 13 warriors now;
The first line contains three integers 𝑛, 𝑚 and 𝑘 (1 ≤ 𝑛 ≤ 5000 , 6. capture the fourth castle;
0 ≤ 𝑚 ≤ min(
𝑛(𝑛−1) 5
, 3 ⋅ 10 ) ,0 ≤ 𝑘 ≤ 5000 ) — the number of 7. leave one warrior to protect the fourth castle, your army has 12
2
warriors now.
castles, the number of portals and initial size of your army, respectively.
Then 𝑛 lines follow. The 𝑖-th line describes the 𝑖-th castle with three This course of action (and several other ones) gives 5 as your total score.
integers 𝑎𝑖 , 𝑏𝑖 and 𝑐𝑖 (0 ≤ 𝑎𝑖 , 𝑏𝑖 , 𝑐𝑖 ≤ 5000 ) — the number of warriors
The best course of action in the second example is as follows:
required to capture the 𝑖-th castle, the number of warriors available for
hire in this castle and its importance value. 1. capture the first castle;
Then 𝑚 lines follow. The 𝑖-th line describes the 𝑖-th portal with two 2. hire warriors from the first castle, your army has 11 warriors now;
integers 𝑢𝑖 and 𝑣 𝑖 (1 ≤ 𝑣 𝑖 < 𝑢𝑖 ≤ 𝑛 ), meaning that the portal leads from 3. capture the second castle;
the castle 𝑢𝑖 to the castle 𝑣 𝑖 . There are no two same portals listed. 4. capture the third castle;
5. hire warriors from the third castle, your army has 13 warriors now;
It is guaranteed that the size of your army won't exceed 5000 under any
𝑛 6. capture the fourth castle;
circumstances (i. e. 𝑘 + ∑ 𝑏𝑖 ≤ 5000 ). 7. leave one warrior to protect the fourth castle, your army has 12
𝑖=1
warriors now;
Output 8. send one warrior to protect the first castle through the third portal,
If it's impossible to capture all the castles, print one integer −1. your army has 11 warriors now.
Otherwise, print one integer equal to the maximum sum of importance This course of action (and several other ones) gives 22 as your total
values of defended castles.
score.
input In the third example it's impossible to capture the last castle: you need
14 warriors to do so, but you can accumulate no more than 13 without
4 3 7
7 4 17 capturing it.
3 0 8
11 2 0
13 3 5 E. Common Number
3 1
2 1 2 seconds, 256 megabytes
4 3
At first, let's define function 𝑓 (𝑥) as follows:
output
5 𝑥
if 𝑥 is even
2
𝑓 (𝑥) =
{
𝑥 − 1 otherwise
input
4 3 7 We can see that if we choose some value 𝑣 and will apply function 𝑓 to
7 4 17 it, then apply 𝑓 to 𝑓 (𝑣), and so on, we'll eventually get 1. Let's write
3 0 8 down all values we get in this process in a list and denote this list as
11 2 0
𝑝𝑎𝑡ℎ(𝑣). For example, 𝑝𝑎𝑡ℎ(1) = [1] , 𝑝𝑎𝑡ℎ(15) = [15, 14, 7, 6, 3, 2, 1] ,
13 3 5
3 1 𝑝𝑎𝑡ℎ(32) = [32, 16, 8, 4, 2, 1] .
2 1
4 1 Let's write all lists 𝑝𝑎𝑡ℎ(𝑥) for every 𝑥 from 1 to 𝑛. The question is next:
what is the maximum value 𝑦 such that 𝑦 is contained in at least 𝑘
output different lists 𝑝𝑎𝑡ℎ(𝑥) ?
22
Formally speaking, you need to find maximum 𝑦 such that
|{𝑥 | 1 ≤ 𝑥 ≤ 𝑛, 𝑦 ∈ 𝑝𝑎𝑡ℎ(𝑥)}| ≥ 𝑘 .
Input
The first line contains two integers 𝑛 and 𝑘 (1 ).
18
≤ 𝑘 ≤ 𝑛 ≤ 10
/
Output As usual, some students skip some classes. Each student considers
Print the only integer — the maximum value that is contained in at least 𝑘 some number of subjects (from 0 to 3) to be useless — that means, he
paths. skips all classes on these subjects (and attends all other classes). This
data is given to you as follows — the 𝑖-th group consists of:
input
1. 𝑑𝑖,1 students which attend all classes;
11 3
2. 𝑑𝑖,2 students which attend all classes, except for P. E.;
output 3. 𝑑𝑖,3 students which attend all classes, except for programming;
5 4. 𝑑𝑖,4 students which attend only maths classes;
5. 𝑑𝑖,5 students which attend all classes, except for maths;
input 6. 𝑑𝑖,6 students which attend only programming classes;
11 6 7. 𝑑𝑖,7 students which attend only P. E.
output There is one more type of students — those who don't attend any
4 classes at all (but they, obviously, don't need any place in auditoriums,
labs or gyms, so the number of those students is insignificant in this
input problem).
20 20 Your task is to divide each group into two subgroups so that every
auditorium (or lab, or gym) assigned to each subgroup has enough place
output
for all students from this subgroup attending the corresponding classes (if
1 it is possible). Each student of the 𝑖-th group should belong to exactly
one subgroup of the 𝑖-th group; it is forbidden to move students between
input groups.
14 5
Input
output The first line contains one integer 𝑡 (1 ≤ 𝑡 ≤ 300 ) — the number of
6 groups.
Then the descriptions of groups follow. The description of the 𝑖-th group
input consists of three lines:
1000000 100
the first line contains three integers 𝑎𝑖,1 , 𝑏𝑖,1 and 𝑐𝑖,1 (
output 1 ≤ 𝑎𝑖,1 , 𝑏𝑖,1 , 𝑐𝑖,1 ≤ 3000 ) — the capacity of the auditorium, lab and
31248 gym assigned to the first subgroup of the 𝑖-th group, respectively;
the second line contains three integers 𝑎𝑖,2 , 𝑏𝑖,2 and 𝑐𝑖,2 (
In the first example, the answer is 5, since 5 occurs in 𝑝𝑎𝑡ℎ(5), 𝑝𝑎𝑡ℎ(10)
1 ≤ 𝑎𝑖,2 , 𝑏𝑖,2 , 𝑐𝑖,2 ≤ 3000 ) — the capacity of the auditorium, lab and
and 𝑝𝑎𝑡ℎ(11) .
gym assigned to the second subgroup of the 𝑖-th group, respectively;
In the second example, the answer is 4, since 4 occurs in 𝑝𝑎𝑡ℎ(4), the third line contains integers 𝑑𝑖,1 , 𝑑𝑖,2 , ..., 𝑑𝑖,7 (0 ≤ 𝑑𝑖,𝑗 ≤ 3000 ) —
𝑝𝑎𝑡ℎ(5), 𝑝𝑎𝑡ℎ(8), 𝑝𝑎𝑡ℎ(9), 𝑝𝑎𝑡ℎ(10) and 𝑝𝑎𝑡ℎ(11) . the number of students belonging to each of the seven
In the third example 𝑛 = 𝑘, so the answer is 1, since 1 is the only number aforementioned types in the 𝑖-th group. It is not guaranteed that the
occuring in all paths for integers from 1 to 20 . sum of these values is positive — a group can consist entirely of
students that don't attend classes at all.
F. Divide The Students It is guaranteed that the total number of students in all groups is not
8 seconds, 512 megabytes greater than 3000 .
/
output
1 1 3 4 2 0 7
-1
0 0 0 0 0 0 0