message stringlengths 2 48.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 318 108k | cluster float64 8 8 | __index_level_0__ int64 636 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.
Omkar currently has n supports arranged in a line, the i-th of which has height a_i. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In 1 operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add 1 to each of their heights.
Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!
An array b is a subsegment of an array c if b can be obtained from c by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.
An array b_1, b_2, ..., b_n is called nondecreasing if b_iβ€ b_{i+1} for every i from 1 to n-1.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 100). Description of the test cases follows.
The first line of each test case contains an integer n (1 β€ n β€ 2 β
10^5) β the number of supports Omkar has.
The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (0 β€ a_{i} β€ 10^9) β the heights of the supports.
It is guaranteed that the sum of n over all test cases does not exceed 2 β
10^5.
Output
For each test case, output a single integer β the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.
Example
Input
3
4
5 3 2 5
5
1 2 3 5 3
3
1 1 1
Output
3
2
0
Note
The subarray with which Omkar performs the operation is bolded.
In the first test case:
* First operation:
[5, 3, 2, 5] β [5, 3, 3, 5]
* Second operation:
[5, 3, 3, 5] β [5, 4, 4, 5]
* Third operation:
[5, 4, 4, 5] β [5, 5, 5, 5]
In the third test case, the array is already nondecreasing, so Omkar does 0 operations.
Submitted Solution:
```
for _ in range(int(input())) :
n = int(input())
l = list(map(int,input().split()))
l.append(max(l))
ans = 0
mx = l[0]
temp = 0
for i in l :
if i >= mx :
mx = i
ans += temp
temp = 0
else :
temp = max(temp, mx-i)
print(ans)
``` | instruction | 0 | 82,775 | 8 | 165,550 |
No | output | 1 | 82,775 | 8 | 165,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.
Omkar currently has n supports arranged in a line, the i-th of which has height a_i. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In 1 operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add 1 to each of their heights.
Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!
An array b is a subsegment of an array c if b can be obtained from c by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.
An array b_1, b_2, ..., b_n is called nondecreasing if b_iβ€ b_{i+1} for every i from 1 to n-1.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 100). Description of the test cases follows.
The first line of each test case contains an integer n (1 β€ n β€ 2 β
10^5) β the number of supports Omkar has.
The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (0 β€ a_{i} β€ 10^9) β the heights of the supports.
It is guaranteed that the sum of n over all test cases does not exceed 2 β
10^5.
Output
For each test case, output a single integer β the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.
Example
Input
3
4
5 3 2 5
5
1 2 3 5 3
3
1 1 1
Output
3
2
0
Note
The subarray with which Omkar performs the operation is bolded.
In the first test case:
* First operation:
[5, 3, 2, 5] β [5, 3, 3, 5]
* Second operation:
[5, 3, 3, 5] β [5, 4, 4, 5]
* Third operation:
[5, 4, 4, 5] β [5, 5, 5, 5]
In the third test case, the array is already nondecreasing, so Omkar does 0 operations.
Submitted Solution:
```
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
ma=[]
if a[0]>a[1]:
ma.append(0)
for i in range(1,n-1):
if(a[i]>a[i-1] and a[i]>a[i+1]):
ma.append(i)
ans=0
if(len(ma)==0):
print(0)
continue
for i in range(len(ma)-1):
mi=min(a[ma[i]:ma[i+1]])
ans+=a[ma[i]]-mi
a[ma[i+1]]=max(a[ma[i+1]],a[ma[i]])
mi=min(a[ma[-1]:])
ans+=a[ma[-1]]-mi
print(ans)
``` | instruction | 0 | 82,776 | 8 | 165,552 |
No | output | 1 | 82,776 | 8 | 165,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city of D consists of n towers, built consecutively on a straight line. The height of the tower that goes i-th (from left to right) in the sequence equals hi. The city mayor decided to rebuild the city to make it beautiful. In a beautiful city all towers are are arranged in non-descending order of their height from left to right.
The rebuilding consists of performing several (perhaps zero) operations. An operation constitutes using a crane to take any tower and put it altogether on the top of some other neighboring tower. In other words, we can take the tower that stands i-th and put it on the top of either the (i - 1)-th tower (if it exists), or the (i + 1)-th tower (of it exists). The height of the resulting tower equals the sum of heights of the two towers that were put together. After that the two towers can't be split by any means, but more similar operations can be performed on the resulting tower. Note that after each operation the total number of towers on the straight line decreases by 1.
Help the mayor determine the minimum number of operations required to make the city beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5000) β the number of towers in the city. The next line contains n space-separated integers: the i-th number hi (1 β€ hi β€ 105) determines the height of the tower that is i-th (from left to right) in the initial tower sequence.
Output
Print a single integer β the minimum number of operations needed to make the city beautiful.
Examples
Input
5
8 2 7 3 1
Output
3
Input
3
5 2 1
Output
2
Submitted Solution:
```
from collections import defaultdict
def solve(arr):
dp = defaultdict(lambda:float('inf'))
s = 0
for i, n in enumerate(arr):
s += n
dp[(i, 1)] = s
for i in range(len(arr)):
#k start from 0 and increase
s = 0
for j in range(i, -1, -1):
s += arr[j]
for k in range(1, len(arr)):
if dp[(j-1, k)] <= s:
if j < i:
dp[(i, k+1)] = min(dp[(i, k+1)], s)
else:
dp[(i, k)] = min(dp[(i, k)], s)
res = 1
for (i, j) in dp:
if i == len(arr) - 1 and dp[(i, j)] < float('inf'):
res = max(j, res)
return len(arr) - res
n = input()
arr = list(map(int, input().split()))
print(solve(arr))
``` | instruction | 0 | 82,852 | 8 | 165,704 |
No | output | 1 | 82,852 | 8 | 165,705 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city of D consists of n towers, built consecutively on a straight line. The height of the tower that goes i-th (from left to right) in the sequence equals hi. The city mayor decided to rebuild the city to make it beautiful. In a beautiful city all towers are are arranged in non-descending order of their height from left to right.
The rebuilding consists of performing several (perhaps zero) operations. An operation constitutes using a crane to take any tower and put it altogether on the top of some other neighboring tower. In other words, we can take the tower that stands i-th and put it on the top of either the (i - 1)-th tower (if it exists), or the (i + 1)-th tower (of it exists). The height of the resulting tower equals the sum of heights of the two towers that were put together. After that the two towers can't be split by any means, but more similar operations can be performed on the resulting tower. Note that after each operation the total number of towers on the straight line decreases by 1.
Help the mayor determine the minimum number of operations required to make the city beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5000) β the number of towers in the city. The next line contains n space-separated integers: the i-th number hi (1 β€ hi β€ 105) determines the height of the tower that is i-th (from left to right) in the initial tower sequence.
Output
Print a single integer β the minimum number of operations needed to make the city beautiful.
Examples
Input
5
8 2 7 3 1
Output
3
Input
3
5 2 1
Output
2
Submitted Solution:
```
def solve(arr):
if len(arr) <= 1:
return 0
i, j = 0, 1
last_val = arr[i]
ops = 0
while j < len(arr):
val = 0
while j < len(arr) and val < last_val:
val += arr[j]
j += 1
if val >= last_val:
ops += j - 2 - i
i = j - 1
last_val = val
else:
ops += j - i - 1
return ops
n = input()
arr = list(map(int, input().split()))
print(solve(arr))
``` | instruction | 0 | 82,853 | 8 | 165,706 |
No | output | 1 | 82,853 | 8 | 165,707 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent. | instruction | 0 | 82,958 | 8 | 165,916 |
Tags: binary search, brute force, greedy, implementation, math
Correct Solution:
```
[n, k] = [int(x) for x in input().split()]
Data = type('Data', (object,), {'index': 0, 'value': 0})
data = [Data() for _ in range(k)]
for i in range(k):
[data[i].index, data[i].value] = [int(x) for x in input().split()]
data.sort(key=lambda x: x.index)
ans = max(data[0].value + data[0].index - 1, data[k-1].value + n - data[k-1].index)
for i in range(1, k):
L = data[i].index - data[i-1].index
minH = min(data[i].value, data[i-1].value)
maxV = max(data[i].value, data[i-1].value)
if minH + L < maxV:
ans = -1
break
ans = max(ans, (L + minH + maxV) // 2)
if ans < 0:
print("IMPOSSIBLE")
else:
print(ans)
``` | output | 1 | 82,958 | 8 | 165,917 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent. | instruction | 0 | 82,959 | 8 | 165,918 |
Tags: binary search, brute force, greedy, implementation, math
Correct Solution:
```
def f(d1, h1, d2, h2):
lo, hi = max(0, h1 - (d2-d1)), min(10**8, h1 + (d2-d1))
if h2 < lo or h2 > hi:
return False, float('-inf')
else:
if h1 < h2:
h1, d1 = h2, d1 + (h2-h1)
elif h1 > h2:
h2, d2 = h1, d2 - (h1-h2)
return True, h1 + (d2-d1)//2
def solve(a, n, m):
ans = float('-inf')
for i in range(m-1):
possible, maxheight = f(a[i][0], a[i][1], a[i+1][0], a[i+1][1])
if not possible:
return float('-inf')
else:
ans = max(ans, maxheight)
return max(ans, a[0][1] + a[0][0] - 1, a[-1][1] + n - a[-1][0])
n, m = map(int, input().split())
a = []
for i in range(m):
d, h = map(int, input().split())
a.append((d,h))
ans = solve(a, n, m)
if ans == float('-inf'):
print("IMPOSSIBLE")
else:
print(ans)
``` | output | 1 | 82,959 | 8 | 165,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent. | instruction | 0 | 82,960 | 8 | 165,920 |
Tags: binary search, brute force, greedy, implementation, math
Correct Solution:
```
n, m = map(int, input().split(' '))
l = [list(map(int, input().split(' '))) for _ in range(m)]
ans = max(l[0][0] + l[0][1] - 1, n - l[-1][0] + l[-1][1])
for i in range(1, m):
dd, dh = abs(l[i][0] - l[i - 1][0]), abs(l[i][1] - l[i - 1][1])
if dd < dh:
print('IMPOSSIBLE')
exit()
ans = max(ans, max(l[i][1], l[i - 1][1]) + ((dd - dh) >> 1))
print(ans)
``` | output | 1 | 82,960 | 8 | 165,921 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent. | instruction | 0 | 82,961 | 8 | 165,922 |
Tags: binary search, brute force, greedy, implementation, math
Correct Solution:
```
import sys
n, m = map(int, str.split(sys.stdin.readline()))
pd = ph = None
top = None
for _ in range(m):
d, h = map(int, str.split(sys.stdin.readline()))
if pd is None:
top = d - 1 + h
else:
if pd and d - pd < abs(h - ph):
print("IMPOSSIBLE")
exit()
delta = d - pd - 1 - abs(h - ph)
top = max(top, max(ph, h) + (delta // 2) + (delta % 2))
pd, ph = d, h
top = max(top, h + n - d)
print(top)
``` | output | 1 | 82,961 | 8 | 165,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent. | instruction | 0 | 82,962 | 8 | 165,924 |
Tags: binary search, brute force, greedy, implementation, math
Correct Solution:
```
n,m = map(int,input().split())
prevd,prevh = map(int,input().split())
maxi = (prevd-1)+prevh
check = 0
for i in range(m-1):
d,h = map(int,input().split())
if d-prevd>=abs(prevh-h):
k = ((d-prevd)-(abs(prevh-h)))//2
if maxi<max(prevh+k,h+k):
maxi = max(prevh+k,h+k)
prevd,prevh = d,h
else:
check = 1
break
if check:
print('IMPOSSIBLE')
else:
if maxi>=prevh+n-prevd:
print(maxi)
else:
print(prevh+n-prevd)
``` | output | 1 | 82,962 | 8 | 165,925 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent. | instruction | 0 | 82,963 | 8 | 165,926 |
Tags: binary search, brute force, greedy, implementation, math
Correct Solution:
```
R = lambda: map(int, input().split())
n, m = R()
ls = [tuple(R()) for _ in range(m)]
res = max(ls[0][1] + ls[0][0] - 1, ls[-1][-1] + n - ls[-1][0])
for i in range(1, m):
d, h = ls[i][0] - ls[i - 1][0], abs(ls[i][1] - ls[i - 1][1])
if d < h:
print('IMPOSSIBLE')
exit()
else:
res = max(res, (d - h) // 2 + max(ls[i][1], ls[i - 1][1]))
print(res)
``` | output | 1 | 82,963 | 8 | 165,927 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent. | instruction | 0 | 82,964 | 8 | 165,928 |
Tags: binary search, brute force, greedy, implementation, math
Correct Solution:
```
def get(d1, h1, d2, h2):
d_d = d2 - d1
if abs(h2 - h1) > d_d: return None
res = (d_d - abs(h2 - h1)) // 2 + max(h1, h2)
return res
n,m = tuple(map(int, input().split()))
l = [(0, 0)] * (m + 1)
for i in range(1, m + 1):
l[i] = tuple(map(int, input().split()))
maxi = 0
for i in range(1,m):
tmp = get(l[i][0], l[i][1], l[i + 1][0], l[i + 1][1])
if tmp == None:
print('IMPOSSIBLE');
exit(0)
if tmp > maxi: maxi = tmp
if l[m][1] + n - l[m][0] > maxi: maxi = l[m][1] + n - l[m][0]
if l[1][1] + l[1][0] - 1 > maxi: maxi = l[1][1] + l[1][0] - 1
print(maxi)
``` | output | 1 | 82,964 | 8 | 165,929 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent. | instruction | 0 | 82,965 | 8 | 165,930 |
Tags: binary search, brute force, greedy, implementation, math
Correct Solution:
```
n,m = map(int, input().split())
max_height = 0
prev_d, prev_h = [0,0]
for i in range(m) :
d,h = map(int, input().split())
if(i == 0) :
max_height = (d-1) + h
prev_d = d
prev_h = h
#crap - fix this
if(i == m-1) :
max_height = max(max_height, n-d+h)
continue
# print("calculating ", prev_d, prev_h,d,h)
intersection = (-1*(prev_d - prev_h) + (d + h))/2
day = (intersection - prev_h) + prev_d
# print("intersection: ", intersection, " at day ", day)
if(day > d or day < prev_d) :
# if(day > d) :
max_height = -1
break
max_height = max(max_height, int(intersection))
if(i == m-1) :
max_height = max(max_height, n-d+h)
prev_d = d
prev_h = h
if(max_height >= 0) :
print(max_height)
else :
print("IMPOSSIBLE")
#16160178
``` | output | 1 | 82,965 | 8 | 165,931 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.
Submitted Solution:
```
n,m=[int(x) for x in input().split()]
L=[[int(x) for x in input().split()] for z in range(m)]
maxh=max([x[1] for x in L])
def possible(a,b):
if abs(a[1]-b[1])>abs(a[0]-b[0]):
return False
return True
def maxp(a,b):
t=abs(a[0]-b[0])
t-=abs(a[1]-b[1])
m=max(a[1],b[1])
return m+(t//2)
poss=True
for i in range(len(L)-1):
if not possible(L[i],L[i+1]):
poss=False
break
maxh=max(maxh,maxp(L[i],L[i+1]))
maxh=max(maxh,L[0][0]-1+L[0][1],n-L[-1][0]+L[-1][1])
if poss:
print(maxh)
else:
print('IMPOSSIBLE')
``` | instruction | 0 | 82,966 | 8 | 165,932 |
Yes | output | 1 | 82,966 | 8 | 165,933 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.
Submitted Solution:
```
import math
n,m = map(int,input().split())
a = []
for i in range(m):
d,h = map(int,input().split())
a.append([d,h])
max_ = -1
c1 = 0
g = (a[0][0] - 1) + a[0][1]
if (g > max_):
max_ = g
for i in range(len(a)-1):
g = math.floor(((a[i+1][0] - a[i][0]) + a[i][1] + a[i+1][1]) / 2)
if ((a[i][1] > g) or (a[i+1][1] > g)):
c1 = -1
break
else:
if (g > max_):
max_ = g
g = (n - a[len(a)-1][0]) + a[len(a)-1][1]
if (g > max_):
max_ = g
if (c1 != -1):
print(max_)
else:
print('IMPOSSIBLE')
``` | instruction | 0 | 82,967 | 8 | 165,934 |
Yes | output | 1 | 82,967 | 8 | 165,935 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.
Submitted Solution:
```
n, m = map(int, input().split())
possible = True
day_prev, h_prev = map(int, input().split())
h_max = h_prev + (day_prev - 1)
for i in range(m - 1):
day, h = map(int, input().split())
if abs(h - h_prev) > day - day_prev:
possible = False
break
h_new = max(h, h_prev) + (day - day_prev - abs(h - h_prev)) // 2
h_max = max(h_max, h_new)
day_prev, h_prev = day, h
h_max = max(h_max, h_prev + (n - day_prev))
if possible:
print(h_max)
else:
print("IMPOSSIBLE")
``` | instruction | 0 | 82,968 | 8 | 165,936 |
Yes | output | 1 | 82,968 | 8 | 165,937 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.
Submitted Solution:
```
n, m = map(int, input().split())
d, h = [], []
for i in range(m):
di, hi = map(int, input().split())
d.append(di)
h.append(hi)
maximum = h[0]
if d[0] != 1:
d.insert(0, 1)
h.insert(0, -1)
m += 1
flag = 0
for i in range(1, m):
diff = d[i] - d[i - 1]
if h[i - 1] == -1:
if diff + h[i] > maximum:
maximum = diff + h[i]
else:
if abs(h[i] - h[i - 1]) > diff:
flag = 1
break
elif abs(h[i] - h[i - 1]) < diff:
p = diff - (abs(h[i - 1] - h[i]))
if max(h[i], h[i - 1]) + p // 2 > maximum:
maximum = p // 2 + max(h[i], h[i - 1])
else:
if max(h[i], h[i - 1]) > maximum:
maximum = max(h[i], h[i - 1])
if d[m - 1] < n:
if maximum < h[m - 1] + n - d[m - 1]:
maximum = h[m - 1] + n - d[m - 1]
if flag == 1:
print("IMPOSSIBLE")
else:
print(maximum)
``` | instruction | 0 | 82,969 | 8 | 165,938 |
Yes | output | 1 | 82,969 | 8 | 165,939 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.
Submitted Solution:
```
n, m = map(int, input().split())
d, h = [], []
for i in range(m):
di, hi = map(int, input().split())
d.append(di)
h.append(hi)
if d[0] != 0:
d.insert(0, 0)
h.insert(0, -1)
m += 1
flag = 0
maximum = 0
for i in range(1, m):
diff = d[i] - d[i - 1]
if h[i - 1] == -1:
if diff + h[i] > maximum:
maximum = diff + h[i]
else:
if abs(h[i] - h[i - 1]) > diff:
flag = 1
break
elif abs(h[i] - h[i - 1]) < diff:
p = diff - (abs(h[i - 1] - h[i]))
if p // 2 > maximum:
maximum = p // 2
else:
if max(h[i], h[i - 1]) > maximum:
maximum = max(h[i], h[i - 1])
if d[m - 1] < n:
if maximum < h[m - 1] + n - d[m - 1]:
maximum = h[m - 1] + n - d[m - 1]
if flag == 1:
print("IMPOSSIBLE")
else:
print(maximum)
``` | instruction | 0 | 82,970 | 8 | 165,940 |
No | output | 1 | 82,970 | 8 | 165,941 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.
Submitted Solution:
```
stroka=input().split()
n=int(stroka[0])
m=int(stroka[1])
f=False
lastDay=0
lastH=0
maxH=0
l=True
p=True
def maxHeight(deltaDays,deltaH):
maxHeight=0
if deltaH/deltaDays>1:
return False
for k in range(1,deltaDays):
if (deltaH+maxHeight+1)/(deltaDays-k)<=1:
maxHeight+=1
if deltaH/deltaDays<=1:
return maxHeight
for k in range(m):
stroka1=input().split()
thisDay=int(stroka1[0])
thisH=int(stroka1[1])
if p==True:
maxH=thisH
lastH=thisH
lastDay=thisDay
p=False
else:
if maxHeight(abs(thisDay-lastDay),abs(thisH-lastH))!=False:
if maxH<maxHeight(abs(thisDay-lastDay),abs(thisH-lastH)):
maxH=maxHeight(abs(thisDay-lastDay),abs(thisH-lastH))
else:
l=False
break
if k+1 not in range(m):
if n-thisDay+abs(thisH)>maxH:
maxH=n-thisDay+abs(thisH)
lastDay=thisDay
lastH=thisH
if l==False:
print("IMPOSSIBLE")
else:
print(maxH)
``` | instruction | 0 | 82,971 | 8 | 165,942 |
No | output | 1 | 82,971 | 8 | 165,943 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
def main():
n, m = map(int, input().split())
possible = True
heights = []
for i in range(m):
a, b = map(int, input().split())
heights.append((a, b))
maxi = -1
ans = 0
other = -1
for i in range(m-1):
if abs(heights[i][1] - heights[i+1][1]) > abs(heights[i][0] - heights[i+1][0]):
possible = False
break
else:
other = max(other, heights[i][1], heights[i+1][1])
maxi = ((heights[i+1][0] - heights[i][0]) + heights[i+1][1] + heights[i][1])//2
ans = max(maxi, ans)
if possible:
print(max(ans, other))
else:
print("IMPOSSIBLE")
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 82,972 | 8 | 165,944 |
No | output | 1 | 82,972 | 8 | 165,945 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| β€ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| β€ 1.
Input
The first line contains two space-separated numbers, n and m (1 β€ n β€ 108, 1 β€ m β€ 105) β the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 β€ di β€ n, 0 β€ hdi β€ 108) β the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
Output
If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.
Submitted Solution:
```
N, M = map(int, input().split())
H = []
D = []
for i in range(M):
d, h = map(int, input().split())
H.append(h)
D.append(d)
res = max(max(H), H[M - 1] + N - D[M - 1])
res = max(res, H[M - 1] + D[M - 1] - 1)
for i in range(M - 1):
dest = D[i + 1] - D[i]
diff = abs(H[i + 1] - H[i])
if diff > dest:
res = float('inf')
break
else:
res = max(res, (dest - diff) // 2 + max(H[i + 1], H[i]))
if res == float('inf'):
print("IMPOSSIBLE")
else:
print(res)
``` | instruction | 0 | 82,973 | 8 | 165,946 |
No | output | 1 | 82,973 | 8 | 165,947 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
Input
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
Output
Print a single number β the maximum number of diamonds Joe can steal.
Examples
Input
2 3 1
2 3
Output
0
Input
3 2 2
4 1 3
Output
2
Note
In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket. | instruction | 0 | 83,107 | 8 | 166,214 |
Tags: greedy
Correct Solution:
```
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if n % 2 == 0:
print('0')
else:
print(min(m // (n // 2 + 1) * k, min(a[::2])))
# Made By Mostafa_Khaled
``` | output | 1 | 83,107 | 8 | 166,215 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
Input
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
Output
Print a single number β the maximum number of diamonds Joe can steal.
Examples
Input
2 3 1
2 3
Output
0
Input
3 2 2
4 1 3
Output
2
Note
In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket. | instruction | 0 | 83,108 | 8 | 166,216 |
Tags: greedy
Correct Solution:
```
n,m,k=map(int,input().split())
a=[int(x) for x in input().split()]
if n%2==0:
print(0)
else:
print(min( min(a[::2]), m//(n//2+1)*k))
``` | output | 1 | 83,108 | 8 | 166,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
Input
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
Output
Print a single number β the maximum number of diamonds Joe can steal.
Examples
Input
2 3 1
2 3
Output
0
Input
3 2 2
4 1 3
Output
2
Note
In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket. | instruction | 0 | 83,109 | 8 | 166,218 |
Tags: greedy
Correct Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=998244353
EPS=1e-6
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
n,m,k=value()
a=array()
if(n%2==0):
print(0)
elif(m<ceil(n/2)):
print(0)
else:
maxLoot=inf
for i in range(n):
if(i%2==0):
maxLoot=min(maxLoot,a[i])
per_min=m//(ceil(n/2))
ans=per_min*k
print(min(ans,maxLoot))
``` | output | 1 | 83,109 | 8 | 166,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
Input
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
Output
Print a single number β the maximum number of diamonds Joe can steal.
Examples
Input
2 3 1
2 3
Output
0
Input
3 2 2
4 1 3
Output
2
Note
In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket. | instruction | 0 | 83,110 | 8 | 166,220 |
Tags: greedy
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if n == 1:
print(min(a[0], m * k))
exit()
if n % 2 == 0:
print(0)
exit()
turn = (m // ((n + 1) // 2)) * k
print(min(turn, min(a[::2])))
``` | output | 1 | 83,110 | 8 | 166,221 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
Input
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
Output
Print a single number β the maximum number of diamonds Joe can steal.
Examples
Input
2 3 1
2 3
Output
0
Input
3 2 2
4 1 3
Output
2
Note
In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket. | instruction | 0 | 83,111 | 8 | 166,222 |
Tags: greedy
Correct Solution:
```
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if n % 2 == 0:
print('0')
else:
print(min(m // (n // 2 + 1) * k, min(a[::2])))
``` | output | 1 | 83,111 | 8 | 166,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
Input
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
Output
Print a single number β the maximum number of diamonds Joe can steal.
Examples
Input
2 3 1
2 3
Output
0
Input
3 2 2
4 1 3
Output
2
Note
In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket.
Submitted Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if n == 1:
raise Exception('hoge')
print(min(a[0], m * k))
exit()
if n % 2 == 0 or n - 1 > m:
print(0)
exit()
turn = (m // (n - 1)) * k
print(min(turn, a[0], a[-1]))
``` | instruction | 0 | 83,112 | 8 | 166,224 |
No | output | 1 | 83,112 | 8 | 166,225 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
Input
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
Output
Print a single number β the maximum number of diamonds Joe can steal.
Examples
Input
2 3 1
2 3
Output
0
Input
3 2 2
4 1 3
Output
2
Note
In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket.
Submitted Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if n == 1:
print(min(a[0], m * k))
exit()
if n <= 2 or n % 2 == 0 or min(a[0], a[-1]) == 0 or n - 1 > m:
print(0)
exit()
turn = (m // (n - 1)) * k
print(min(turn, a[0], a[-1]))
``` | instruction | 0 | 83,113 | 8 | 166,226 |
No | output | 1 | 83,113 | 8 | 166,227 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
Input
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
Output
Print a single number β the maximum number of diamonds Joe can steal.
Examples
Input
2 3 1
2 3
Output
0
Input
3 2 2
4 1 3
Output
2
Note
In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket.
Submitted Solution:
```
n,m,k=map(int,input().split())
a=[int(x) for x in input().split()]
if n%2==0:
print(0)
else:
print(min( min(a[::2]), k*m//(n//2+1)))
``` | instruction | 0 | 83,114 | 8 | 166,228 |
No | output | 1 | 83,114 | 8 | 166,229 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
Input
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
Output
Print a single number β the maximum number of diamonds Joe can steal.
Examples
Input
2 3 1
2 3
Output
0
Input
3 2 2
4 1 3
Output
2
Note
In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket.
Submitted Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if n == 1:
print(min(a[0], m * k))
exit()
if n % 2 == 0 or n - 1 > m:
print(0)
exit()
turn = (m // (n - 1)) * k
print(min(turn, min(a[::2])))
``` | instruction | 0 | 83,115 | 8 | 166,230 |
No | output | 1 | 83,115 | 8 | 166,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
<image>
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 β€ n β€ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 β€ wi β€ 106 for each 1 β€ i β€ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Examples
Input
5
1 1 2 3 3
Output
2
Input
4
0 1 2 3
Output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two. | instruction | 0 | 83,795 | 8 | 167,590 |
Tags: greedy
Correct Solution:
```
N = int(1e6) + 20
n = int(input())
m = [0] * N
k = 1e6 + 1
for x in map(int, input().split()):
m[x] += 1
k = min(k, x)
result = 0
while k < N - 1:
v = m[k]
m[k + 1] += v >> 1
result += v & 0x1
k += 1
print(result + m[-1])
``` | output | 1 | 83,795 | 8 | 167,591 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
<image>
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 β€ n β€ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 β€ wi β€ 106 for each 1 β€ i β€ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Examples
Input
5
1 1 2 3 3
Output
2
Input
4
0 1 2 3
Output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two. | instruction | 0 | 83,797 | 8 | 167,594 |
Tags: greedy
Correct Solution:
```
from collections import *
import sys
import math
from functools import reduce
def factors(n):
return set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
def li():return [int(i) for i in input().rstrip('\n').split(' ')]
def st():return input().rstrip('\n')
def val():return int(input())
def stli():return [int(i) for i in input().rstrip('\n')]
def persquare(x):
return 1 if x**0.5 == int(x**0.5) else 0
n = val()
l = sorted(li())
currlist = [0 for i in range(10**6 + 22)]
for i in l:
currlist[i] += 1
tot = 0
# print(currlist[:10])
for i in range(10**6 + 21):
while currlist[i]>1:
temp = 2**int(math.log2(currlist[i]))
currlist[i] -= temp
currlist[i + 1] += temp//2
# print(currlist[:10])
print(sum(currlist))
``` | output | 1 | 83,797 | 8 | 167,595 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
<image>
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 β€ n β€ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 β€ wi β€ 106 for each 1 β€ i β€ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Examples
Input
5
1 1 2 3 3
Output
2
Input
4
0 1 2 3
Output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two. | instruction | 0 | 83,802 | 8 | 167,604 |
Tags: greedy
Correct Solution:
```
l=[0]*1100001
input()
for i in map(int,input().split()):l[i]+=1
for i in range(1100000):
l[i],l[i+1]=l[i]%2,l[i+1]+l[i]//2
print(sum(l))
``` | output | 1 | 83,802 | 8 | 167,605 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
<image>
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 β€ n β€ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 β€ wi β€ 106 for each 1 β€ i β€ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Examples
Input
5
1 1 2 3 3
Output
2
Input
4
0 1 2 3
Output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
Submitted Solution:
```
import math
n = int(input())
w = [int(i) for i in input().split()]
# w = [1000000] * 1000000
mxN = 1000000 + math.ceil(math.log2(1000000)) + 1
btStr = [0] * mxN
# print(btStr)
# mx = 0
for i in w:
btStr[i] += 1
# print(j)
# print(mx)
steps = 0
for i in range(mxN - 1):
btStr[i + 1] += btStr[i] // 2
btStr[i] = btStr[i] % 2
steps += btStr[i]
# for i in btStr:
# if i == 1:
# steps += 1
#
print(steps)
``` | instruction | 0 | 83,805 | 8 | 167,610 |
Yes | output | 1 | 83,805 | 8 | 167,611 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
<image>
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 β€ n β€ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 β€ wi β€ 106 for each 1 β€ i β€ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Examples
Input
5
1 1 2 3 3
Output
2
Input
4
0 1 2 3
Output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
Submitted Solution:
```
n = input()
a = [0] * 1000021
ans = 0
b = 0
for u in map(int, input().split()):
a[u] += 1
for u in a:
b += u
if b & 1:
ans += 1
b >>= 1
print(ans)
``` | instruction | 0 | 83,806 | 8 | 167,612 |
Yes | output | 1 | 83,806 | 8 | 167,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
<image>
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 β€ n β€ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 β€ wi β€ 106 for each 1 β€ i β€ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Examples
Input
5
1 1 2 3 3
Output
2
Input
4
0 1 2 3
Output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
Submitted Solution:
```
n = int(input())
w_arr = list(map(int, input().split()))
w_arr.sort()
col = [0 for i in range(10**6 + 100)]
last_el = w_arr[0]
for j in range(len(w_arr)):
col[w_arr[j]] += 1
if w_arr[j] != last_el:
last_el = w_arr[j]
a = col[w_arr[j - 1]]
col[w_arr[j - 1] + 1] += a // 2
col[w_arr[j - 1]] = a % 2
last_el = w_arr[j]
a = col[w_arr[j - 1]]
col[w_arr[j - 1] + 1] += a // 2
col[w_arr[j - 1]] = a % 2
for i in range(1, 10**6 + 99):
col[i], col[i + 1] = col[i] % 2, col[i + 1] + col[i] // 2
print(col[:20])
print(len(col) - col.count(0))
``` | instruction | 0 | 83,808 | 8 | 167,616 |
No | output | 1 | 83,808 | 8 | 167,617 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
<image>
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 β€ n β€ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 β€ wi β€ 106 for each 1 β€ i β€ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Examples
Input
5
1 1 2 3 3
Output
2
Input
4
0 1 2 3
Output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
Submitted Solution:
```
n = int(input())
w_arr = list(map(int, input().split()))
w_arr.sort()
col = {}
last_el = w_arr[0]
for j, el in enumerate(w_arr):
if el not in col:
col[el] = 0
col[el] += 1
if el != last_el:
last_el = el
bin_el = str(bin(col[w_arr[j - 1]]))[2:]
col[w_arr[j - 1]] = 0
for i in range(len(bin_el)):
if bin_el[i] == '1':
col[w_arr[j - 1] + len(bin_el) - i - 1] = 1
bin_el = str(bin(col[w_arr[j - 1]]))[2:]
col[w_arr[j - 1]] = 0
for i in range(len(bin_el)):
if bin_el[i] == '1':
col[w_arr[j - 1] + len(bin_el) - i - 1] = 1
count = 0
for key in col:
if col[key] != 0:
count += 1
print(count)
``` | instruction | 0 | 83,809 | 8 | 167,618 |
No | output | 1 | 83,809 | 8 | 167,619 |
Provide a correct Python 3 solution for this coding contest problem.
problem
AOR Co., Ltd. (Association Of Return home) has $ N $ employees.
Employee $ i $ wants to use the elevator to go down to the $ 1 $ floor and arrives in front of the elevator on the $ F_i $ floor at time $ t_i $. You decide to remotely control an elevator that has only $ 1 $ on the $ 1 $ floor at time $ 0 $ and send all employees to the $ 1 $ floor. The elevator can only carry up to $ D $ people. Elevators can move upstairs or stay in place per unit time. Employee $ i $ only takes the elevator if the elevator is on the $ F_i $ floor at time $ t_i $ and does not exceed capacity. When I can't get on the elevator at time $ t_i $, I'm on the stairs to the $ 1 $ floor. The time it takes to get on and off can be ignored.
Find the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $.
output
Output the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $. Also, output a line break at the end.
Example
Input
2 2
2 2
3 3
Output
5 | instruction | 0 | 84,182 | 8 | 168,364 |
"Correct Solution:
```
# AOJ 2832 All Japan Association of Return home
# Python3 2018.7.12 bal4u
n, d = map(int, input().split())
ans = t0 = f0 = num = 0
for i in range(n):
t, f = map(int, input().split())
f -= 1
df = f-f0
if df < 0: df = -df
dt = t - t0
if dt < df: ans = -1; break
if dt >= f0+f:
ans += num*f0
num = 0
else:
if num+1 > d: ans = -1; break
ans += num*dt
f0, t0 = f, t
num += 1
print(-1 if ans < 0 else ans+num*f0)
``` | output | 1 | 84,182 | 8 | 168,365 |
Provide a correct Python 3 solution for this coding contest problem.
problem
AOR Co., Ltd. (Association Of Return home) has $ N $ employees.
Employee $ i $ wants to use the elevator to go down to the $ 1 $ floor and arrives in front of the elevator on the $ F_i $ floor at time $ t_i $. You decide to remotely control an elevator that has only $ 1 $ on the $ 1 $ floor at time $ 0 $ and send all employees to the $ 1 $ floor. The elevator can only carry up to $ D $ people. Elevators can move upstairs or stay in place per unit time. Employee $ i $ only takes the elevator if the elevator is on the $ F_i $ floor at time $ t_i $ and does not exceed capacity. When I can't get on the elevator at time $ t_i $, I'm on the stairs to the $ 1 $ floor. The time it takes to get on and off can be ignored.
Find the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $.
output
Output the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $. Also, output a line break at the end.
Example
Input
2 2
2 2
3 3
Output
5 | instruction | 0 | 84,183 | 8 | 168,366 |
"Correct Solution:
```
N, D = map(int, input().split())
time = 0
floor = 1
num = 0
result = 0
for _ in [0]*N:
t, f = map(int, input().split())
if floor-1+f-1 <= t-time:
result += num * (floor-1)
num = 1
elif abs(floor-f) <= t-time and num < D:
result += num * abs(t-time)
num += 1
else:
print(-1)
break
time, floor = t, f
else:
result += num * (floor-1)
print(result)
``` | output | 1 | 84,183 | 8 | 168,367 |
Provide a correct Python 3 solution for this coding contest problem.
problem
AOR Co., Ltd. (Association Of Return home) has $ N $ employees.
Employee $ i $ wants to use the elevator to go down to the $ 1 $ floor and arrives in front of the elevator on the $ F_i $ floor at time $ t_i $. You decide to remotely control an elevator that has only $ 1 $ on the $ 1 $ floor at time $ 0 $ and send all employees to the $ 1 $ floor. The elevator can only carry up to $ D $ people. Elevators can move upstairs or stay in place per unit time. Employee $ i $ only takes the elevator if the elevator is on the $ F_i $ floor at time $ t_i $ and does not exceed capacity. When I can't get on the elevator at time $ t_i $, I'm on the stairs to the $ 1 $ floor. The time it takes to get on and off can be ignored.
Find the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $.
output
Output the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $. Also, output a line break at the end.
Example
Input
2 2
2 2
3 3
Output
5 | instruction | 0 | 84,184 | 8 | 168,368 |
"Correct Solution:
```
n, d = map(int, input().split())
lst = sorted([list(map(int, input().split())) for _ in range(n)]) + [[10 ** 20, 1]]
cnt = 0
time = 0
floor = 1
ans = 0
for i in range(n):
t, f = lst[i]
if f - floor > t - time or cnt >= d:
print(-1)
break
ans += cnt * (t - time)
cnt += 1
time = t
floor = f
next_t, next_f = lst[i + 1]
if time + (floor - 1) + (next_f - 1) <= next_t:
ans += cnt * (floor - 1)
cnt = 0
time += floor - 1
floor = 1
else:
print(ans)
``` | output | 1 | 84,184 | 8 | 168,369 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
AOR Co., Ltd. (Association Of Return home) has $ N $ employees.
Employee $ i $ wants to use the elevator to go down to the $ 1 $ floor and arrives in front of the elevator on the $ F_i $ floor at time $ t_i $. You decide to remotely control an elevator that has only $ 1 $ on the $ 1 $ floor at time $ 0 $ and send all employees to the $ 1 $ floor. The elevator can only carry up to $ D $ people. Elevators can move upstairs or stay in place per unit time. Employee $ i $ only takes the elevator if the elevator is on the $ F_i $ floor at time $ t_i $ and does not exceed capacity. When I can't get on the elevator at time $ t_i $, I'm on the stairs to the $ 1 $ floor. The time it takes to get on and off can be ignored.
Find the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $.
output
Output the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $. Also, output a line break at the end.
Example
Input
2 2
2 2
3 3
Output
5
Submitted Solution:
```
N, D = map(int, input().split())
time = 0
floor = 1
num = 0
result = 0
for _ in [0]*N:
t, f = map(int, input().split())
if floor-1+f-1 <= t-time:
result += num * (floor-1)
num = 1
elif abs(floor-f) <= t-time and num < D:
result += num * abs(floor-f)
num += 1
else:
print(-1)
break
time, floor = t, f
else:
result += num * (floor-1)
print(result)
``` | instruction | 0 | 84,185 | 8 | 168,370 |
No | output | 1 | 84,185 | 8 | 168,371 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
AOR Co., Ltd. (Association Of Return home) has $ N $ employees.
Employee $ i $ wants to use the elevator to go down to the $ 1 $ floor and arrives in front of the elevator on the $ F_i $ floor at time $ t_i $. You decide to remotely control an elevator that has only $ 1 $ on the $ 1 $ floor at time $ 0 $ and send all employees to the $ 1 $ floor. The elevator can only carry up to $ D $ people. Elevators can move upstairs or stay in place per unit time. Employee $ i $ only takes the elevator if the elevator is on the $ F_i $ floor at time $ t_i $ and does not exceed capacity. When I can't get on the elevator at time $ t_i $, I'm on the stairs to the $ 1 $ floor. The time it takes to get on and off can be ignored.
Find the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $.
output
Output the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $. Also, output a line break at the end.
Example
Input
2 2
2 2
3 3
Output
5
Submitted Solution:
```
N, D = map(int, input().split())
time = 0
floor = 1
num = 0
result = 0
for _ in [0]*N:
t, f = map(int, input().split())
if floor-1+f-1 <= t-time:
result += num * (floor-1)
num = 1
elif abs(floor-f) <= t-time:
result += num * abs(floor-f)
num += 1
else:
print(-1)
break
time, floor = t, f
else:
result += num * (floor-1)
print(result)
``` | instruction | 0 | 84,186 | 8 | 168,372 |
No | output | 1 | 84,186 | 8 | 168,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have got a shelf and want to put some books on it.
You are given q queries of three types:
1. L id β put a book having index id on the shelf to the left from the leftmost existing book;
2. R id β put a book having index id on the shelf to the right from the rightmost existing book;
3. ? id β calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id will be leftmost or rightmost.
You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so ids don't repeat in queries of first two types.
Your problem is to answer all the queries of type 3 in order they appear in the input.
Note that after answering the query of type 3 all the books remain on the shelf and the relative order of books does not change.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow. The i-th line contains the i-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).
It is guaranteed that there is at least one query of type 3 in the input.
In each query the constraint 1 β€ id β€ 2 β
10^5 is met.
Output
Print answers to queries of the type 3 in order they appear in the input.
Examples
Input
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1
Output
1
1
2
Input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
Output
0
2
1
Note
Let's take a look at the first example and let's consider queries:
1. The shelf will look like [1];
2. The shelf will look like [1, 2];
3. The shelf will look like [1, 2, 3];
4. The shelf looks like [1, 2, 3] so the answer is 1;
5. The shelf will look like [4, 1, 2, 3];
6. The shelf looks like [4, 1, 2, 3] so the answer is 1;
7. The shelf will look like [5, 4, 1, 2, 3];
8. The shelf looks like [5, 4, 1, 2, 3] so the answer is 2.
Let's take a look at the second example and let's consider queries:
1. The shelf will look like [100];
2. The shelf will look like [100, 100000];
3. The shelf will look like [100, 100000, 123];
4. The shelf will look like [101, 100, 100000, 123];
5. The shelf looks like [101, 100, 100000, 123] so the answer is 0;
6. The shelf will look like [10, 101, 100, 100000, 123];
7. The shelf will look like [10, 101, 100, 100000, 123, 115];
8. The shelf looks like [10, 101, 100, 100000, 123, 115] so the answer is 2;
9. The shelf will look like [10, 101, 100, 100000, 123, 115, 110];
10. The shelf looks like [10, 101, 100, 100000, 123, 115, 110] so the answer is 1. | instruction | 0 | 84,243 | 8 | 168,486 |
Tags: implementation
Correct Solution:
```
n=int(input())
ln=0
rn=0
ids={}
bel={}
for i in range(n):
s=input().split()
op=s[0]
id=int(s[1])
if op=='L' or op=='R':
if op=='L':
ln+=1
ids[id]=ln
bel[id]='l'
else:
rn+=1
ids[id]=rn
bel[id]='r'
else:
if bel[id]=='l':
l=ln-ids[id]
r=ids[id]+rn-1
print(min(l,r))
else:
r=rn-ids[id]
l=ids[id]+ln-1
print(min(l,r))
# if op=='L' or op=='R':
# if op=='L':
# ln+=1
# ids[id]=-ln
# else:
# rn+=1
# ids[id]=rn
# else:
# if ids[id]<0:
# ll=ln-abs(ids[id])+1
# rr=abs(ids[id])+rn
# print(min(ll,rr)-1)
# else:
# rr=rn-abs(ids[id])+1
# ll=abs(ids[id])+rn
# print(min(ll,rr)-1)
``` | output | 1 | 84,243 | 8 | 168,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have got a shelf and want to put some books on it.
You are given q queries of three types:
1. L id β put a book having index id on the shelf to the left from the leftmost existing book;
2. R id β put a book having index id on the shelf to the right from the rightmost existing book;
3. ? id β calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id will be leftmost or rightmost.
You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so ids don't repeat in queries of first two types.
Your problem is to answer all the queries of type 3 in order they appear in the input.
Note that after answering the query of type 3 all the books remain on the shelf and the relative order of books does not change.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow. The i-th line contains the i-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).
It is guaranteed that there is at least one query of type 3 in the input.
In each query the constraint 1 β€ id β€ 2 β
10^5 is met.
Output
Print answers to queries of the type 3 in order they appear in the input.
Examples
Input
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1
Output
1
1
2
Input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
Output
0
2
1
Note
Let's take a look at the first example and let's consider queries:
1. The shelf will look like [1];
2. The shelf will look like [1, 2];
3. The shelf will look like [1, 2, 3];
4. The shelf looks like [1, 2, 3] so the answer is 1;
5. The shelf will look like [4, 1, 2, 3];
6. The shelf looks like [4, 1, 2, 3] so the answer is 1;
7. The shelf will look like [5, 4, 1, 2, 3];
8. The shelf looks like [5, 4, 1, 2, 3] so the answer is 2.
Let's take a look at the second example and let's consider queries:
1. The shelf will look like [100];
2. The shelf will look like [100, 100000];
3. The shelf will look like [100, 100000, 123];
4. The shelf will look like [101, 100, 100000, 123];
5. The shelf looks like [101, 100, 100000, 123] so the answer is 0;
6. The shelf will look like [10, 101, 100, 100000, 123];
7. The shelf will look like [10, 101, 100, 100000, 123, 115];
8. The shelf looks like [10, 101, 100, 100000, 123, 115] so the answer is 2;
9. The shelf will look like [10, 101, 100, 100000, 123, 115, 110];
10. The shelf looks like [10, 101, 100, 100000, 123, 115, 110] so the answer is 1. | instruction | 0 | 84,244 | 8 | 168,488 |
Tags: implementation
Correct Solution:
```
n = int(input())
arr = {}
mi = 0
ma = 0
t, i = input().split()
arr[i] = 0
for _ in range(n-1):
t, i = input().split()
if t == 'L':
mi -= 1
arr[i] = mi
elif t == 'R':
ma += 1
arr[i] = ma
else:
print(min(ma-arr[i], arr[i]-mi))
``` | output | 1 | 84,244 | 8 | 168,489 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have got a shelf and want to put some books on it.
You are given q queries of three types:
1. L id β put a book having index id on the shelf to the left from the leftmost existing book;
2. R id β put a book having index id on the shelf to the right from the rightmost existing book;
3. ? id β calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id will be leftmost or rightmost.
You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so ids don't repeat in queries of first two types.
Your problem is to answer all the queries of type 3 in order they appear in the input.
Note that after answering the query of type 3 all the books remain on the shelf and the relative order of books does not change.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow. The i-th line contains the i-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).
It is guaranteed that there is at least one query of type 3 in the input.
In each query the constraint 1 β€ id β€ 2 β
10^5 is met.
Output
Print answers to queries of the type 3 in order they appear in the input.
Examples
Input
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1
Output
1
1
2
Input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
Output
0
2
1
Note
Let's take a look at the first example and let's consider queries:
1. The shelf will look like [1];
2. The shelf will look like [1, 2];
3. The shelf will look like [1, 2, 3];
4. The shelf looks like [1, 2, 3] so the answer is 1;
5. The shelf will look like [4, 1, 2, 3];
6. The shelf looks like [4, 1, 2, 3] so the answer is 1;
7. The shelf will look like [5, 4, 1, 2, 3];
8. The shelf looks like [5, 4, 1, 2, 3] so the answer is 2.
Let's take a look at the second example and let's consider queries:
1. The shelf will look like [100];
2. The shelf will look like [100, 100000];
3. The shelf will look like [100, 100000, 123];
4. The shelf will look like [101, 100, 100000, 123];
5. The shelf looks like [101, 100, 100000, 123] so the answer is 0;
6. The shelf will look like [10, 101, 100, 100000, 123];
7. The shelf will look like [10, 101, 100, 100000, 123, 115];
8. The shelf looks like [10, 101, 100, 100000, 123, 115] so the answer is 2;
9. The shelf will look like [10, 101, 100, 100000, 123, 115, 110];
10. The shelf looks like [10, 101, 100, 100000, 123, 115, 110] so the answer is 1. | instruction | 0 | 84,245 | 8 | 168,490 |
Tags: implementation
Correct Solution:
```
'''input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
'''
from sys import stdin, stdout
def myfunction(mydict, current, second):
a = mydict[second]
if a[2] == 'R':
return min( current[1] - a[1], current[0] + a[1] - 1)
else:
return min(current[0] - a[0], current[1] + a[0] - 1)
q = int(stdin.readline())
mydict = dict()
current = [0, 0, 0]
while q > 0:
first, second = stdin.readline().split()
if first == 'L':
mydict[second] = [current[0] + 1, current[1], 'L']
current = [current[0] + 1, current[1], 'L']
elif first == 'R':
mydict[second] = [current[0], current[1] + 1, 'R']
current = [current[0], current[1] + 1, 'R']
else:
print(myfunction(mydict, current, second))
#print(current)
q -= 1
``` | output | 1 | 84,245 | 8 | 168,491 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have got a shelf and want to put some books on it.
You are given q queries of three types:
1. L id β put a book having index id on the shelf to the left from the leftmost existing book;
2. R id β put a book having index id on the shelf to the right from the rightmost existing book;
3. ? id β calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id will be leftmost or rightmost.
You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so ids don't repeat in queries of first two types.
Your problem is to answer all the queries of type 3 in order they appear in the input.
Note that after answering the query of type 3 all the books remain on the shelf and the relative order of books does not change.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow. The i-th line contains the i-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).
It is guaranteed that there is at least one query of type 3 in the input.
In each query the constraint 1 β€ id β€ 2 β
10^5 is met.
Output
Print answers to queries of the type 3 in order they appear in the input.
Examples
Input
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1
Output
1
1
2
Input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
Output
0
2
1
Note
Let's take a look at the first example and let's consider queries:
1. The shelf will look like [1];
2. The shelf will look like [1, 2];
3. The shelf will look like [1, 2, 3];
4. The shelf looks like [1, 2, 3] so the answer is 1;
5. The shelf will look like [4, 1, 2, 3];
6. The shelf looks like [4, 1, 2, 3] so the answer is 1;
7. The shelf will look like [5, 4, 1, 2, 3];
8. The shelf looks like [5, 4, 1, 2, 3] so the answer is 2.
Let's take a look at the second example and let's consider queries:
1. The shelf will look like [100];
2. The shelf will look like [100, 100000];
3. The shelf will look like [100, 100000, 123];
4. The shelf will look like [101, 100, 100000, 123];
5. The shelf looks like [101, 100, 100000, 123] so the answer is 0;
6. The shelf will look like [10, 101, 100, 100000, 123];
7. The shelf will look like [10, 101, 100, 100000, 123, 115];
8. The shelf looks like [10, 101, 100, 100000, 123, 115] so the answer is 2;
9. The shelf will look like [10, 101, 100, 100000, 123, 115, 110];
10. The shelf looks like [10, 101, 100, 100000, 123, 115, 110] so the answer is 1. | instruction | 0 | 84,246 | 8 | 168,492 |
Tags: implementation
Correct Solution:
```
l,r=1,0
d={}
for _ in[0]*int(input()):
o,i=input().split()
if o=='L':l-=1;d[i]=l
elif o=='R':r+=1;d[i]=r
else:print(min(d[i]-l,r-d[i]))
#JSR
``` | output | 1 | 84,246 | 8 | 168,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have got a shelf and want to put some books on it.
You are given q queries of three types:
1. L id β put a book having index id on the shelf to the left from the leftmost existing book;
2. R id β put a book having index id on the shelf to the right from the rightmost existing book;
3. ? id β calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id will be leftmost or rightmost.
You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so ids don't repeat in queries of first two types.
Your problem is to answer all the queries of type 3 in order they appear in the input.
Note that after answering the query of type 3 all the books remain on the shelf and the relative order of books does not change.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow. The i-th line contains the i-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).
It is guaranteed that there is at least one query of type 3 in the input.
In each query the constraint 1 β€ id β€ 2 β
10^5 is met.
Output
Print answers to queries of the type 3 in order they appear in the input.
Examples
Input
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1
Output
1
1
2
Input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
Output
0
2
1
Note
Let's take a look at the first example and let's consider queries:
1. The shelf will look like [1];
2. The shelf will look like [1, 2];
3. The shelf will look like [1, 2, 3];
4. The shelf looks like [1, 2, 3] so the answer is 1;
5. The shelf will look like [4, 1, 2, 3];
6. The shelf looks like [4, 1, 2, 3] so the answer is 1;
7. The shelf will look like [5, 4, 1, 2, 3];
8. The shelf looks like [5, 4, 1, 2, 3] so the answer is 2.
Let's take a look at the second example and let's consider queries:
1. The shelf will look like [100];
2. The shelf will look like [100, 100000];
3. The shelf will look like [100, 100000, 123];
4. The shelf will look like [101, 100, 100000, 123];
5. The shelf looks like [101, 100, 100000, 123] so the answer is 0;
6. The shelf will look like [10, 101, 100, 100000, 123];
7. The shelf will look like [10, 101, 100, 100000, 123, 115];
8. The shelf looks like [10, 101, 100, 100000, 123, 115] so the answer is 2;
9. The shelf will look like [10, 101, 100, 100000, 123, 115, 110];
10. The shelf looks like [10, 101, 100, 100000, 123, 115, 110] so the answer is 1. | instruction | 0 | 84,247 | 8 | 168,494 |
Tags: implementation
Correct Solution:
```
n = int(input())
s, e = 0, 0
c = True
w = dict()
for _ in range(n):
q, d = input().split()
if q == "R":
if c:
s += 1
c = False
e += 1
w[d] = e
elif q == "L":
if c:
e -= 1
c = False
s -= 1
w[d] = s
else:
i = w[d]
if i > e:
i -= n
print(min(e - i, i - s))
``` | output | 1 | 84,247 | 8 | 168,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have got a shelf and want to put some books on it.
You are given q queries of three types:
1. L id β put a book having index id on the shelf to the left from the leftmost existing book;
2. R id β put a book having index id on the shelf to the right from the rightmost existing book;
3. ? id β calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id will be leftmost or rightmost.
You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so ids don't repeat in queries of first two types.
Your problem is to answer all the queries of type 3 in order they appear in the input.
Note that after answering the query of type 3 all the books remain on the shelf and the relative order of books does not change.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow. The i-th line contains the i-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).
It is guaranteed that there is at least one query of type 3 in the input.
In each query the constraint 1 β€ id β€ 2 β
10^5 is met.
Output
Print answers to queries of the type 3 in order they appear in the input.
Examples
Input
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1
Output
1
1
2
Input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
Output
0
2
1
Note
Let's take a look at the first example and let's consider queries:
1. The shelf will look like [1];
2. The shelf will look like [1, 2];
3. The shelf will look like [1, 2, 3];
4. The shelf looks like [1, 2, 3] so the answer is 1;
5. The shelf will look like [4, 1, 2, 3];
6. The shelf looks like [4, 1, 2, 3] so the answer is 1;
7. The shelf will look like [5, 4, 1, 2, 3];
8. The shelf looks like [5, 4, 1, 2, 3] so the answer is 2.
Let's take a look at the second example and let's consider queries:
1. The shelf will look like [100];
2. The shelf will look like [100, 100000];
3. The shelf will look like [100, 100000, 123];
4. The shelf will look like [101, 100, 100000, 123];
5. The shelf looks like [101, 100, 100000, 123] so the answer is 0;
6. The shelf will look like [10, 101, 100, 100000, 123];
7. The shelf will look like [10, 101, 100, 100000, 123, 115];
8. The shelf looks like [10, 101, 100, 100000, 123, 115] so the answer is 2;
9. The shelf will look like [10, 101, 100, 100000, 123, 115, 110];
10. The shelf looks like [10, 101, 100, 100000, 123, 115, 110] so the answer is 1. | instruction | 0 | 84,248 | 8 | 168,496 |
Tags: implementation
Correct Solution:
```
n = int(input())
L =[0]*(n+1)
R = [0]*(n+1)
S = [0] * (n+1)
d = dict()
A =[0]*(n+1)
for j in range(1,n+1):
c,val = input().split()
A[j] = [c,int(val)]
val = int(val)
if(c=="L"):
S[j]=1+S[j-1]
L[j] = 1 + L[j - 1]
R[j] = R[j - 1]
d[val] = j
elif (c == "R"):
S[j] = 1 + S[j - 1]
L[j] = L[j - 1]
R[j] = 1 + R[j - 1]
d[val] = j
else:
S[j] = S[j-1]
L[j] = L[j - 1]
R[j] = R[j - 1]
ints = d[val]
left = L[j] - L[ints]
right = R[j] - R[ints]
sums = S[ints]-1
#print(left,right,sums)
if(A[ints][0] == "L"):
#print("l")
print(min(left,right+sums))
if(A[ints][0] == "R"):
#print("r")
print(min(right,left+sums))
``` | output | 1 | 84,248 | 8 | 168,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have got a shelf and want to put some books on it.
You are given q queries of three types:
1. L id β put a book having index id on the shelf to the left from the leftmost existing book;
2. R id β put a book having index id on the shelf to the right from the rightmost existing book;
3. ? id β calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id will be leftmost or rightmost.
You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so ids don't repeat in queries of first two types.
Your problem is to answer all the queries of type 3 in order they appear in the input.
Note that after answering the query of type 3 all the books remain on the shelf and the relative order of books does not change.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow. The i-th line contains the i-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).
It is guaranteed that there is at least one query of type 3 in the input.
In each query the constraint 1 β€ id β€ 2 β
10^5 is met.
Output
Print answers to queries of the type 3 in order they appear in the input.
Examples
Input
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1
Output
1
1
2
Input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
Output
0
2
1
Note
Let's take a look at the first example and let's consider queries:
1. The shelf will look like [1];
2. The shelf will look like [1, 2];
3. The shelf will look like [1, 2, 3];
4. The shelf looks like [1, 2, 3] so the answer is 1;
5. The shelf will look like [4, 1, 2, 3];
6. The shelf looks like [4, 1, 2, 3] so the answer is 1;
7. The shelf will look like [5, 4, 1, 2, 3];
8. The shelf looks like [5, 4, 1, 2, 3] so the answer is 2.
Let's take a look at the second example and let's consider queries:
1. The shelf will look like [100];
2. The shelf will look like [100, 100000];
3. The shelf will look like [100, 100000, 123];
4. The shelf will look like [101, 100, 100000, 123];
5. The shelf looks like [101, 100, 100000, 123] so the answer is 0;
6. The shelf will look like [10, 101, 100, 100000, 123];
7. The shelf will look like [10, 101, 100, 100000, 123, 115];
8. The shelf looks like [10, 101, 100, 100000, 123, 115] so the answer is 2;
9. The shelf will look like [10, 101, 100, 100000, 123, 115, 110];
10. The shelf looks like [10, 101, 100, 100000, 123, 115, 110] so the answer is 1. | instruction | 0 | 84,249 | 8 | 168,498 |
Tags: implementation
Correct Solution:
```
import sys
q = int(input())
l, r = -1, 1
pos = [0] * 200001
k = 0
for line in sys.stdin:
t, a = line.split()
a = int(a)
if k == 0:
pos[a] = 0
else:
if t == 'L':
pos[a] = l
l -= 1
elif t == 'R':
pos[a] = r
r += 1
else:
print(min(abs(pos[a] - l), abs(pos[a] - r)) - 1)
k += 1
``` | output | 1 | 84,249 | 8 | 168,499 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have got a shelf and want to put some books on it.
You are given q queries of three types:
1. L id β put a book having index id on the shelf to the left from the leftmost existing book;
2. R id β put a book having index id on the shelf to the right from the rightmost existing book;
3. ? id β calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id will be leftmost or rightmost.
You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so ids don't repeat in queries of first two types.
Your problem is to answer all the queries of type 3 in order they appear in the input.
Note that after answering the query of type 3 all the books remain on the shelf and the relative order of books does not change.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
Then q lines follow. The i-th line contains the i-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).
It is guaranteed that there is at least one query of type 3 in the input.
In each query the constraint 1 β€ id β€ 2 β
10^5 is met.
Output
Print answers to queries of the type 3 in order they appear in the input.
Examples
Input
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1
Output
1
1
2
Input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
Output
0
2
1
Note
Let's take a look at the first example and let's consider queries:
1. The shelf will look like [1];
2. The shelf will look like [1, 2];
3. The shelf will look like [1, 2, 3];
4. The shelf looks like [1, 2, 3] so the answer is 1;
5. The shelf will look like [4, 1, 2, 3];
6. The shelf looks like [4, 1, 2, 3] so the answer is 1;
7. The shelf will look like [5, 4, 1, 2, 3];
8. The shelf looks like [5, 4, 1, 2, 3] so the answer is 2.
Let's take a look at the second example and let's consider queries:
1. The shelf will look like [100];
2. The shelf will look like [100, 100000];
3. The shelf will look like [100, 100000, 123];
4. The shelf will look like [101, 100, 100000, 123];
5. The shelf looks like [101, 100, 100000, 123] so the answer is 0;
6. The shelf will look like [10, 101, 100, 100000, 123];
7. The shelf will look like [10, 101, 100, 100000, 123, 115];
8. The shelf looks like [10, 101, 100, 100000, 123, 115] so the answer is 2;
9. The shelf will look like [10, 101, 100, 100000, 123, 115, 110];
10. The shelf looks like [10, 101, 100, 100000, 123, 115, 110] so the answer is 1. | instruction | 0 | 84,250 | 8 | 168,500 |
Tags: implementation
Correct Solution:
```
m = [0]*(2*10**5 + 1)
n = int(input())
input().split()
l = -1
r = 1
for i in range(n - 1):
p, b_id = input().split()
b_id = int(b_id)
if p == "L":
m[b_id] = l
l -= 1
elif p == "R":
m[b_id] = r
r += 1
elif p == "?":
print(int(min(r - m[b_id], m[b_id] - l) - 1))
``` | output | 1 | 84,250 | 8 | 168,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little boy Petya loves stairs very much. But he is bored from simple going up and down them β he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.
Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.
One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
Input
The first line contains two integers n and m (1 β€ n β€ 109, 0 β€ m β€ 3000) β the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 β€ di β€ n) β the numbers of the dirty stairs (in an arbitrary order).
Output
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
Examples
Input
10 5
2 4 8 3 6
Output
NO
Input
10 5
2 4 5 7 9
Output
YES | instruction | 0 | 84,533 | 8 | 169,066 |
Tags: implementation, sortings
Correct Solution:
```
k,n=map(int,input().split())
if n==0:
print('YES')
exit()
l=[int(i) for i in input().split()]
#l is dirty
from collections import Counter
c=Counter(l)
l.sort()
for i in range(2,n):
if l[i]==l[i-1]+1 and l[i-1]==l[i-2]+1:
print('NO')
exit()
if c[k] or c[1]:
print('NO')
exit()
print('YES')
``` | output | 1 | 84,533 | 8 | 169,067 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.