0% found this document useful (0 votes)
9 views19 pages

TCArg2024 Inicial Contest1

Initial contest competitive programming first edition

Uploaded by

valentin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

TCArg2024 Inicial Contest1

Initial contest competitive programming first edition

Uploaded by

valentin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Contest 1 - Inicial

PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST

Range Sorting
B1. Range Sorting (Easy Version)
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

The only difference between this problem and the hard version is the constraints on 𝑡 and →
𝑛. Vir
as
You are given an array 𝑎, consisting of 𝑛 distinct integers 𝑎1 , 𝑎2 , … , 𝑎𝑛. su
yo
for
Define the beauty of an array 𝑝1 , 𝑝2 , … 𝑝𝑘 as the minimum amount of time needed to sort this jus
vir
array using an arbitrary number of range-sort operations. In each range-sort operation, you will do the
the
the following: du

Choose two integers 𝑙 and 𝑟 (1 ≤ 𝑙 < 𝑟 ≤ 𝑘).


Sort the subarray 𝑝𝑙 , 𝑝𝑙+1 , … , 𝑝𝑟 in 𝑟 − 𝑙 seconds.

Please calculate the sum of beauty over all subarrays of array 𝑎. →


A subarray of an array is defined as a sequence of consecutive elements of the array. Yo

Input
Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (
1 ≤ 𝑡 ≤ 5 ⋅ 103 ). The description of the test cases follows.

The first line of each test case contains a single integer 𝑛 ( 1 ≤ 𝑛 ≤ 5 ⋅ 103) — the length of the
array 𝑎 . La

The second line of each test case consists of 𝑛 integers 𝑎 1 , 𝑎 2 , … , 𝑎 𝑛 ( 1 ≤ 𝑎𝑖 ≤ 109 ). It is Ch


file
guaranteed that all elements of 𝑎 are pairwise distinct.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 5 ⋅ 103.

Output

For each test case, output the sum of beauty over all subarrays of array 𝑎.
bi
Example
tw
input Copy

5
2
6 4 →
3
3 10 6
4
4 8 7 2
5
9 8 2 4 6
12
2 6 13 3 15 5 10 8 16 9 11 18

output Copy

1
2
8
16
232

Note
In the first test case:

The subarray [6] is already sorted, so its beauty is 0.


The subarray [4] is already sorted, so its beauty is 0.
You can sort the subarray [6, 4] in one operation by choosing 𝑙 = 1 and 𝑟 = 2. Its beauty is
equal to 1.

https://codeforces.com/problemset/problem/1827/b1
7/7/24, 2:14 PM Problem - 1827b1 - Codeforces
The sum of beauty over all subarrays of the given array is equal to 0 + 0 + 1 = 1.
In the second test case:

The subarray [3] is already sorted, so its beauty is 0.


The subarray [10] is already sorted, so its beauty is 0.
The subarray [6] is already sorted, so its beauty is 0.
The subarray [3, 10] is already sorted, so its beauty is 0.
You can sort the subarray [10, 6] in one operation by choosing 𝑙 = 1 and 𝑟 = 2. Its beauty
is equal to 2 − 1 = 1.
You can sort the subarray [3, 10, 6] in one operation by choosing 𝑙 = 2 and 𝑟 = 3. Its
beauty is equal to 3 − 2 = 1.

The sum of beauty over all subarrays of the given array is equal to 0 + 0 + 0 + 0 + 1 + 1 = 2.

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Jul/07/2024 14:13:58 UTC-3 (g1).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/problemset/problem/1827/b1
18 Problem I: In Case of an Invasion, Please. . .

I In Case of an Invasion, Please. . . Time limit: 3.5s

After Curiosity discovered not just water on Mars, but also


an aggressive, bloodthirsty bunch of aliens, the Louvain-la-
Neuve municipal government decided to take precautionary
measures; they built shelters in order to shelter everyone
in the city in the event of an extraterrestial attack.

Several alien-proof shelters have been erected throughout the city, where citizens can weather
an alien invasion. However, due to municipal regulations and local building codes the shelters
are limited in size. This makes it necessary for the government to assign every citizen a
shelter to calmly direct themselves towards in the rare event of a fleet of UFOs blotting out
the sun. Conditional on no shelter being assigned more people than it can fit, it is of the
utmost importance that the time it takes until everyone has arrived at a shelter is minimized.

We model Louvain-la-Neuve as a network of n locations at which people live, connected by


m bidirectional roads. Located at s points throughout the city are the shelters, each with
a given maximum capacity. What is the minimum amount of time it takes for everyone to
arrive at a shelter, when we assign people to shelters optimally?

The Louvain-la-Neuve municipal government has made sure that there is enough shelter
capacity for its citizens and all shelters can be reached from any location, i.e. it is always
possible to shelter everyone in some way.

Input

• On the first line are three integers, the number of locations 1 ≤ n ≤ 10 5 , roads 0 ≤ m ≤
2 · 105 , and shelters 1 ≤ s ≤ 10.

• Then follows a line with n integers 0 ≤ p i ≤ 109 , indicating the the number of people
living at location 1 ≤ i ≤ n.

• Then follow m lines containing three integers 1 ≤ u, v ≤ n and 1 ≤ w ≤ 10 9 indicating


that there is a bidirectional road connecting u and v that takes w time to traverse.
For any two locations there is at most one road connecting them directly, and no road
connects a location to itself.

• Finally follow s lines with two integers 1 ≤ s i ≤ n and 1 ≤ ci ≤ 109 , indicating that
there is a shelter with capacity c i at location si .

Output
Print the minimum amount of time it takes to shelter everyone.
Problem I: In Case of an Invasion, Please. . . 19

Sample Input 1 Sample Output 1


2 1 1 4
3 2
1 2 4
1 6

Sample Input 2 Sample Output 2


4 5 2 5
2 0 0 2
1 2 6
1 3 2
2 3 3
3 4 4
4 2 6
3 2
2 2

Sample Input 3 Sample Output 3


7 8 3 6
0 1 1 1 1 0 2
1 2 1
2 3 1
3 1 1
4 6 5
4 3 1
6 7 10
7 5 3
5 6 3
6 5
1 1
2 1

Sample Input 4 Sample Output 4


2 1 1 0
0 2
1 2 1000000000
2 2
PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST

1D Sokoban
C. 1D Sokoban
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so

you only consider integer positions on the line.
Vir
You start on a position 0. There are 𝑛 boxes, the 𝑖-th box is on a position 𝑎 𝑖. All positions of the as
su
boxes are distinct. There are also 𝑚 special positions, the 𝑗-th position is 𝑏𝑗. All the special yo
for
positions are also distinct. jus
vir
In one move you can go one position to the left or to the right. If there is a box in the direction of the
the
your move, then you push the box to the next position in that direction. If the next position is taken du
by another box, then that box is also pushed to the next position, and so on. You can't go
through the boxes. You can't pull the boxes towards you.

You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many
boxes on special positions as possible. Note that some boxes can be initially placed on special →
positions.
Yo

Input
The first line contains a single integer 𝑡 ( 1 ≤ 𝑡 ≤ 1000) — the number of testcases.
Then descriptions of 𝑡 testcases follow.

5
The first line of each testcase contains two integers 𝑛 and 𝑚 ( 1 ≤ 𝑛, 𝑚 ≤ 2 ⋅ 10 ) — the
number of boxes and the number of special positions, respectively. La

Ch
𝑛 distinct integers in the increasing order
The second line of each testcase contains file
𝑎1 , 𝑎2 , … , 𝑎𝑛 (−10 ≤ 𝑎1 < 𝑎2 < ⋯ < 𝑎𝑛 ≤ 109 ; 𝑎𝑖 ≠ 0) — the initial positions of the
9

boxes.

𝑚 distinct integers in the increasing order 𝑏1 , 𝑏2 , … , 𝑏𝑚


The third line of each testcase contains
(−109 ≤ 𝑏1 < 𝑏2 < ⋯ < 𝑏𝑚 ≤ 109 ; 𝑏𝑖 ≠ 0) — the special positions. →

The sum of 𝑛 over all testcases doesn't exceed 2 ⋅ 105. The sum of 𝑚 over all testcases doesn't bi
5
exceed 2 ⋅ 10 . tw

Output
For each testcase print a single integer — the maximum number of boxes that can be placed on
special positions. →

Example
input Copy

5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10

output Copy

https://codeforces.com/problemset/problem/1494/C
7/7/24, 12:57 PM Problem - 1494C - Codeforces
4
2
0
3
1

Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and
the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on
position −1 and push a box to −2. At the end, the boxes are on positions [−2, 6, 7, 11, 15],
respectively. Among them positions [−2, 6, 7, 15] are special, thus, the answer is 4.

In the second testcase you can push the box from −1 to −109, then the box from 1 to 109 and
obtain the answer 2.

The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring
them closer to special positions.

In the fourth testcase all the boxes are already on special positions, so you can do nothing and
still obtain the answer 3.

In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to
the right to have some box on position 10.

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Jul/07/2024 12:57:56 UTC-3 (g1).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/problemset/problem/1494/C
PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST

Stressful Training
D. Stressful Training
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Berland SU holds yet another training contest for its students today. 𝑛 students came, each of

them brought his laptop. However, it turned out that everyone has forgot their chargers!
Vir
Let students be numbered from 1 to 𝑛. Laptop of the 𝑖-th student has charge 𝑎 𝑖 at the beginning as
su
of the contest and it uses 𝑏𝑖 of charge per minute (i.e. if the laptop has 𝑐 charge at the beginning yo
of some minute, it becomes 𝑐 − 𝑏𝑖 charge at the beginning of the next minute). The whole for
jus
contest lasts for 𝑘 minutes. vir
the
the
Polycarp (the coach of Berland SU) decided to buy a single charger so that all the students du
would be able to successfully finish the contest. He buys the charger at the same moment the
contest starts.

Polycarp can choose to buy the charger with any non-negative (zero or positive) integer power
output. The power output is chosen before the purchase, it can't be changed afterwards. Let the →
chosen power output be 𝑥. At the beginning of each minute (from the minute contest starts to
Yo
the last minute of the contest) he can plug the charger into any of the student's laptops and use it
for some integer number of minutes. If the laptop is using 𝑏𝑖 charge per minute then it will
become 𝑏𝑖 − 𝑥 per minute while the charger is plugged in. Negative power usage rate means
that the laptop's charge is increasing. The charge of any laptop isn't limited, it can become
infinitely large. The charger can be plugged in no more than one laptop at the same time. →

The student successfully finishes the contest if the charge of his laptop never is below zero at the
La
beginning of some minute (from the minute contest starts to the last minute of the contest, zero
charge is allowed). The charge of the laptop of the minute the contest ends doesn't matter. Ch
file
Help Polycarp to determine the minimal possible power output the charger should have so that all
the students are able to successfully finish the contest. Also report if no such charger exists.

Input
5 5 →
The first line contains two integers 𝑛 and 𝑘 ( 1 ≤ 𝑛 ≤ 2 ⋅ 10 , 1 ≤ 𝑘 ≤ 2 ⋅ 10 ) — the number
of students (and laptops, correspondigly) and the duration of the contest in minutes.
bi
12
The second line contains 𝑛 integers 𝑎1 , 𝑎2 , … , 𝑎𝑛 ( 1 ≤ 𝑎𝑖 ≤ 10 ) — the initial charge of each
student's laptop.

The third line contains 𝑛 integers 𝑏1 , 𝑏2 , … , 𝑏𝑛 ( 1 ≤ 𝑏𝑖 ≤ 107 ) — the power usage of each
student's laptop.

Output
Print a single non-negative integer — the minimal possible power output the charger should have
so that all the students are able to successfully finish the contest.

If no such charger exists, print -1.

Examples
input Copy

2 4
3 2
4 2

output Copy

input Copy

1 5
4
2

https://codeforces.com/problemset/problem/1132/D
7/7/24, 12:57 PM Problem - 1132D - Codeforces

output Copy

input Copy

1 6
4
2

output Copy

input Copy

2 2
2 10
3 15

output Copy

-1

Note
Let's take a look at the state of laptops in the beginning of each minute on the first example with
the charger of power 5:

1. charge: [3, 2], plug the charger into laptop 1;


2. charge: [3 − 4 + 5, 2 − 2] = [4, 0], plug the charger into laptop 2;
3. charge: [4 − 4, 0 − 2 + 5] = [0, 3], plug the charger into laptop 1;
4. charge: [0 − 4 + 5, 3 − 2] = [1, 1].
The contest ends after the fourth minute.

However, let's consider the charger of power 4:

1. charge: [3, 2], plug the charger into laptop 1;


2. charge: [3 − 4 + 4, 2 − 2] = [3, 0], plug the charger into laptop 2;
3. charge: [3 − 4, 0 − 2 + 4] = [−1, 2], the first laptop has negative charge, thus, the first
student doesn't finish the contest.

In the fourth example no matter how powerful the charger is, one of the students won't finish the
contest.

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Jul/07/2024 12:57:04 UTC-3 (g1).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/problemset/problem/1132/D
PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST

Appleman and Toastman


A. Appleman and Toastman
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the →
Toastman, then they start to complete the following tasks: Vir
as
su
Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum yo
to the score. Then he gives the group to the Appleman. for
jus
Each time Appleman gets a group consisting of a single number, he throws this group out. vir
the
Each time Appleman gets a group consisting of more than one number, he splits the group the
du
into two non-empty groups (he can do it in any way) and gives each of them to Toastman.

After guys complete all the tasks they look at the score value. What is the maximum possible
value of score they can get?

Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, Yo
a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.

Output
Print a single integer — the largest possible score.

Examples
input Copy La

3 Ch
3 1 5 file

output Copy

26

input Copy →
1
gr
10

output Copy

10 →

Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and
adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two
groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group
[1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman
receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits
[3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When
Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it
out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he
will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the
optimal sequence of actions.

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Jul/07/2024 12:56:47 UTC-3 (g1).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/problemset/problem/461/A
PROBLEMS SUBMIT CODE MY SUBMISSIONS STATUS STANDINGS CUSTOM INVOCATION

Loppinha the boy who likes sopinha


E. Loppinha, the boy who likes sopinha
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Loppinha loves to go to the gym with his friends. He takes his training sessions very seriously, →
and he always follows his schedule very strictly. He just created a new plan where he has set
exactly what he is going to do at every single one of the N minutes of the session.

Loppinha loves having a sopinha (small soup) before training. The soup contains K grams of
protein. As you can imagine, he needs that protein to be able to endure his very tough exercises.
He is burning protein at every minute he is working out, and the amount of protein he burns in a
minute depends on how many minutes he has been working out without a minute of rest. If he
has trained for p minutes without resting, in the (p + 1)-th minute of workout he is going to burn
p + 1 grams of protein.
Of course, if he doesn't have enough protein at any moment he dies. For example, if he has 3
grams of protein at a moment and at that minute his workout requires 4, if he does the workout he
dies. Given his schedule and the amount of protein K he has before starting the training session,
Loppinha, who is willing to change some minutes of his workout, wants to know what is the
minimum amount of minutes he has to change from working out to resting so he does not die. Co

co
Input De
The first line of the input contains two integers N (1 ≤ N ≤ 450) and K (1 ≤ K ≤ 107), indicating
the number of minutes in Loppinha's training session and the amount of proteins in his soup. The
next line contains a string with N characters, either 0 or 1, where 0 indicates a minute of rest and →
a 1 indicates a minute of workout.
Vir
as
Output su
yo
Output a single integer - the minimum number of minutes Loppinha has to switch from workout to for
jus
rest so that he can finish his exercises without dying. vir
the
the
Examples du
input Copy

4 2
1101

output Copy →
1
Yo

input Copy

10 5
1101100111

output Copy

3 La

Ch
input Copy file

3 1
111

output Copy

2 →

Note
In the first test case, if he changes the second minute to rest, he will consume exactly 2 grams of
protein. Notice that if he changes the last minute to rest instead, he would need 3 grams of
protein to complete his workout session without dying.

https://codeforces.com/gym/101875/problem/E
PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST

Contest Start
A. Contest Start
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

There are 𝑛 people participating in some contest, they start participating in 𝑥 minutes intervals. →
That means the first participant starts at time 0, the second participant starts at time 𝑥, the Vir
third — at time 2 ⋅ 𝑥, and so on. as
su
yo
Duration of contest is 𝑡 minutes for each participant, so the first participant finishes the contest at for
jus
time 𝑡, the second — at time 𝑡 + 𝑥, and so on. When a participant finishes the contest, their vir
dissatisfaction equals to the number of participants that started the contest (or starting it now), but the
the
haven't yet finished it. du

Determine the sum of dissatisfaction of all participants.

Input
The first line contains a single integer 𝑘 ( 1 ≤ 𝑘 ≤ 1000) — the number of test cases. →

Each of the next 𝑘 lines contains three integers 𝑛, 𝑥, 𝑡 ( 1 ≤ 𝑛, 𝑥, 𝑡 ≤ 2 ⋅ 109 ) — the number of Yo
participants, the start interval and the contest duration.

Output
Print 𝑘 lines, in the 𝑖-th line print the total dissatisfaction of participants in the 𝑖-th test case.

Example
La
input Copy

4 Ch
file
4 2 5
3 1 2
3 3 10
2000000000 1 2000000000

output Copy

5
3 co
3
1999999999000000000 *1

Note
In the first example the first participant starts at 0 and finishes at time 5. By that time the second →
and the third participants start, so the dissatisfaction of the first participant is 2.

The second participant starts at time 2 and finishes at time 7. By that time the third the fourth
participants start, so the dissatisfaction of the second participant is 2.

The third participant starts at 4 and finishes at 9. By that time the fourth participant starts, so the
dissatisfaction of the third participant is 1.

The fourth participant starts at 6 and finishes at 11. By time 11 everyone finishes the contest, so
the dissatisfaction of the fourth participant is 0.

In the second example the first participant starts at 0 and finishes at time 2. By that time the
second participants starts, and the third starts at exactly time 2. So the dissatisfaction of the first
participant is 2.

The second participant starts at time 1 and finishes at time 3. At that time the third participant is
solving the contest.

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov


The only programming contests Web 2.0 platform

https://codeforces.com/problemset/problem/1539/A
PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST

Chat room
A. Chat room
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Vasya has recently learned to type and log on to the Internet. He immediately entered a chat →
room and decided to say hello to everybody. Vasya typed the word s. It is considered that Vasya Vir
managed to say hello if several letters can be deleted from the typed word so that it resulted in as
su
the word "hello". For example, if Vasya types the word " ahhellllloou", it will be yo
for
considered that he said hello, and if he types " hlelo", it will be considered that Vasya got jus
vir
misunderstood and he didn't manage to say hello. Determine whether Vasya managed to say the
hello by the given word s. the
du

Input
The first and only line contains the word s, which Vasya typed. This word consisits of small Latin
letters, its length is no less that 1 and no more than 100 letters.

Output
If Vasya managed to say hello, print " YES", otherwise print "NO". Yo

Examples
input Copy

ahhellllloou →
output Copy
La
YES
Ch
file
input Copy

hlelo

output Copy

NO →

gr

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Jul/07/2024 12:56:26 UTC-3 (g1).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/problemset/problem/58/A
PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST

Competitive Programmer
A. Competitive Programmer
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Bob is a competitive programmer. He wants to become red, and for that he needs a strict training →
regime. He went to the annual meeting of grandmasters and asked 𝑛 of them how much effort Vir
they needed to reach red. as
su
yo
"Oh, I just spent 𝑥𝑖 hours solving problems", said the 𝑖-th of them. for
jus
vir
Bob wants to train his math skills, so for each answer he wrote down the number of minutes ( the
60 ⋅ 𝑥𝑖 ), thanked the grandmasters and went home. Bob could write numbers with leading zeroes the
du
— for example, if some grandmaster answered that he had spent 2 hours, Bob could write
000120 instead of 120.
Alice wanted to tease Bob and so she took the numbers Bob wrote down, and for each of them
she did one of the following independently: →

rearranged its digits, or Yo


wrote a random number.

This way, Alice generated 𝑛 numbers, denoted 𝑦1 , ..., 𝑦𝑛 .


For each of the numbers, help Bob determine whether 𝑦𝑖 can be a permutation of a number →
divisible by 60 (possibly with leading zeroes).
La
Input
The first line contains a single integer 𝑛 ( 1 ≤ 𝑛 ≤ 418) — the number of grandmasters Bob Ch
file
asked.

Then 𝑛 lines follow, the 𝑖-th of which contains a single integer 𝑦𝑖 — the number that Alice wrote
down.

Each of these numbers has between 2 and 100 digits '0' through '9'. They can contain leading →
zeroes.
ch

Output
Output 𝑛 lines.

For each 𝑖, output the following. If it is possible to rearrange the digits of 𝑦𝑖 such that the resulting
number is divisible by 60, output " red" (quotes for clarity). Otherwise, output " cyan".

Example
input Copy

6
603
006
205
228
1053
0000000000000000000000000000000000000000000000

output Copy

red
red
cyan
cyan
cyan
red

Note
In the first example, there is one rearrangement that yields a number divisible by 60, and that is
360 .

https://codeforces.com/problemset/problem/1266/A
7/7/24, 12:56 PM Problem - 1266A - Codeforces
In the second example, there are two solutions. One is 060 and the second is 600.
In the third example, there are 6 possible rearrangments: 025, 052, 205, 250, 502 , 520 . None
of these numbers is divisible by 60.

In the fourth example, there are 3 rearrangements: 228, 282, 822.


In the fifth example, none of the 24 rearrangements result in a number divisible by 60.
In the sixth example, note that 000 … 0 is a valid solution.

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Jul/07/2024 12:56:07 UTC-3 (g1).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/problemset/problem/1266/A
������ ��� ��� ���� ����������� ������������ �������
������� ������� ��������� ��� ����
������� �� �������
����� ���� �����
������ ���� ������
���� ������ � �������
������ ������ ��� ���������

���� ���� ����� ���� ������� ������ �� � ��������������� �� ��� ������ n ������ ��������� �� ��� �� ������
����� ���� �������� �� ������ �� ���� ���� ��� ���� ��� ����

i��� ���� ��� �� ������� �� ��� ��� �������� ���� ��� ��� ti � �������� ��������� ����� ���� �� ��� ��������
����� ������� ������ ��� ����� ������ ��� ���� ���� ����� ��� ��������

������ �� i��� ���� ����� ci + d �������� ����� ci �� ��� ������� ���� �� ��� ������ ��� d �� ��� ������ ��
���� ������ ���� ��� ��� ���� ���� ���� ������ ��������� �� ������� ����� �� ������� ������� ��� ����
���� �� �� ������� ���������� �� ������� ci �������� �� �� ������� �� �� ��� ���� ��� � �� ������� ci + 1
�������� ��� �� ���

������ �� ����� ����� �� ��� ��������� ���� �� �������� ����� ��� ����� �� �� ��� �� ������ �� ����� ����
���� �� ����� ������ �� ������� ���� ������ ������� ����� ���� ���� �� ������� �� ������ ��� �����
�� ����� �� ������ ������� ������� ���� ��� ���� �� ����� �� ��� ��� ������� �� ���� ��� �� ����� ���
���� �� �������

����� ��� ��� ������� ���� �� �������� �� ��������� ���� ������� ����� ��� �������� �� ��� �������
���� ��� ��� �������� ���� ��� �������� ���� ��� ������� ��� ���� ����� ���� ������� ��� �������� �����
���� �� ���������� ���� ���� ��� ���� �� ����� �� ��� �������� ������� ���� ����� ���� ��� ������ ��������
��� ������ �� ���� � ��� ���� ��� ��� �� ����� �� ������ �� ����� �� �������� ���� ��� ��� ����� ����
��� � ��� ���� ������ ������ ������ ��� ���� ����� ���� ������ ������� ��� ���� �� ��� ���� ��������

�����

��� ���� ���� �������� ��� ���� ������� n �1 ≤ n ≤ 200000� � ������ �� ����� ���� ������ ������ �������
���� �� ��� ��������� n ����� �������� ��� ��������ci �1 ≤ ti ≤ 106 � 0 ≤ ci ≤ 109 � ��������� ��
ti ���
����� � ��� ���� ��� ��� i��� ���� ��� �� ������� ����� ��� ��� ������� ����� ������� t1 � � � � � tn ��� �����
�� �������������� ������ ���� ti ≤ tj ��� ��� i ≤ j �

������

����� n �������� ��������� �� ������� i��� ������ ������ �� � ��� ���� ������ ������ ������ i��� �����
�� ����� ��� ���� ��������� ���� ���� ��� ���� �� �������� ������ ��� �� �����

��������

����� ������
� � � �
� �
� �
� �
� � � � �
� �
� �
� �
� �

���� � �� ��
Problem I
Twenty Four, Again
Yes, we know . . . we’ve used Challenge 24 before for contest problems. In case you’ve never heard of
Challenge 24 (or have a very short memory) the object of the game is to take 4 given numbers (the base
values) and determine if there is a way to produce the value 24 from them using the four basic arithmetic
operations (and parentheses if needed). For example, given the four base values 3 5 5 2, you can produce
24 in many ways. Two of them are: 5*5-3+2 and (3+5)*(5-2). Recall that multiplication and division
have precedence over addition and subtraction, and that equal-precedence operators are evaluated left-to-right.
This is all very familiar to most of you, but what you probably don’t know is that you can grade the quality
of the expressions used to produce 24. In fact, we’re sure you don’t know this since we’ve just made it up.
Here’s how it works: A perfect grade for an expression is 0. Each use of parentheses adds one point to the
grade. Furthermore, each inversion (that is, a swap of two adjacent values) of the original ordering of the four
base values adds two points. The first expression above has a grade of 4, since two inversions are used to
move the 3 to the third position. The second expression has a better grade of 2 since it uses no inversions but
two sets of parentheses. As a further example, the initial set of four base values 3 6 2 3 could produce
an expression of grade 3 — namely (3+6+3)*2 — but it also has a perfect grade 0 expression — namely
3*6+2*3. Needless to say, the lower the grade the “better” the expression.
Two additional rules we’ll use: 1) you cannot use unary minus in any expression, so you can’t take the base
values 3 5 5 2 and produce the expression -3+5*5+2, and 2) division can only be used if the result is an
integer, so you can’t take the base values 2 3 4 9 and produce the expression 2/3*4*9.
Given a sequence of base values, determine the lowest graded expression resulting in the value 24. And by
the way, the initial set of base values 3 5 5 2 has a grade 1 expression — can you find it?

Input

Input consists of a single line containing 4 base values. All base values are between 1 and 100, inclusive.

Output

Display the lowest grade possible using the sequence of base values. If it is not possible to produce 24,
display impossible.

Sample Input 1 Sample Output 1


3 5 5 2 1

Sample Input 2 Sample Output 2


1 1 1 1 impossible

ECNA 2017 Problem I: Twenty Four, Again 17


PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST

Graph Without Long Directed Paths


F. Graph Without Long Directed Paths
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

You are given a connected undirected graph consisting of 𝑛 vertices and 𝑚 edges. There are no →
self-loops or multiple edges in the given graph. Vir
as
su
You have to direct its edges in such a way that the obtained directed graph does not contain any yo
paths of length two or greater (where the length of path is denoted as the number of traversed for
jus
edges). vir
the
the
Input du
The first line contains two integer numbers 𝑛 and 𝑚 ( 2 ≤ 𝑛 ≤ 2 ⋅ 105, 𝑛 − 1 ≤ 𝑚 ≤ 2 ⋅ 105 ) —
the number of vertices and edges, respectively.

The following 𝑚 lines contain edges: edge 𝑖 is given as a pair of vertices 𝑢𝑖, 𝑣𝑖 ( 1 ≤ 𝑢𝑖 , 𝑣𝑖 ≤ 𝑛,
𝑢𝑖 ≠ 𝑣𝑖 ). There are no multiple edges in the given graph, i. e. for each pair ( 𝑢𝑖 , 𝑣𝑖) there are no →
other pairs (𝑢𝑖 , 𝑣𝑖 ) and (𝑣𝑖 , 𝑢𝑖 ) in the list of edges. It is also guaranteed that the given graph is
Yo
connected (there is a path between any pair of vertex in the given graph).

Output
If it is impossible to direct edges of the given graph in such a way that the obtained directed graph
does not contain paths of length at least two, print " NO" in the first line. →

Otherwise print "YES" in the first line, and then print any suitable orientation of edges: a binary
La
string (the string consisting only of ' 0' and '1') of length 𝑚. The 𝑖-th element of this string should
be '0' if the 𝑖-th edge of the graph should be directed from 𝑢𝑖 to 𝑣𝑖, and ' 1' otherwise. Edges are Ch
file
numbered in the order they are given in the input.

Example
input Copy

6 5 →
1 5
2 1 df
1 4
3 1
6 1

output Copy →
YES
10100

Note
The picture corresponding to the first example:

https://codeforces.com/problemset/problem/1144/F
7/7/24, 12:58 PM Problem - 1144F - Codeforces

And one of possible answers:

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Jul/07/2024 12:58:30 UTC-3 (g1).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/problemset/problem/1144/F

You might also like