Problem - E - Codeforces
Problem - E - Codeforces
HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN
PROBLEMS SUBMIT CODE MY SUBMISSIONS STATUS HACKS ROOM STANDINGS CUSTOM INVOCATION
Vlad also wants the graph to be connected, which might not be the case initially. In order to → Virtual participation
satisfy that, he can do the following two types of operations on the array:
Virtual contest is a way to take part in past
contest, as close as possible to participation
1. Choose some element ai and increment it by 1. on time. It is supported only ICPC mode for
2. Choose some element ai and decrement it by 1 (possible only if ai > 0 ). virtual contests. If you've seen these
problems, a virtual contest is not for you -
solve these problems in the archive. If you
It can be proven that there exists a finite sequence of operations such that the graph will be just want to solve some problem from a
connected. So, can you please help Vlad find the minimum possible number of operations to do contest, a virtual contest is not for you -
solve this problem in the archive. Never use
that and also provide the way how to do that? someone else's code, read the tutorials or
communicate with other person during a
Input virtual contest.
There are several test cases in the input data. The first line contains a single integer t ( Start virtual contest
1 ≤ t ≤ 1000) — the number of test cases. This is followed by the test cases description.
4
5
1 2 3 4 5
2
0 2
2
3 12
4
3 0 0 0
output Copy
0
1 2 3 4 5
2
2 2
1
3 11
3
3 1 1 1
Note
In the first test case, the graph is already connected.
In the second test case, we can increment 0 twice and end up with the array [2, 2]. Since
2&2 = 2 > 0 , the graph is connected. It can be shown that one operation is not enough.
In the third test case, we can decrement 12 once and we end up with an array [3, 11].
3&11 = 3 > 0 hence the graph is connected. One operation has to be done since the graph is
Supported by