message stringlengths 2 20.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 1.95k 109k | cluster float64 17 17 | __index_level_0__ int64 3.91k 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The last stage of Football World Cup is played using the play-off system.
There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.
Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.
Input
The only line contains three integers n, a and b (2 ≤ n ≤ 256, 1 ≤ a, b ≤ n) — the total number of teams, and the ids of the teams that Arkady is interested in.
It is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.
Output
In the only line print "Final!" (without quotes), if teams a and b can meet in the Final.
Otherwise, print a single integer — the number of the round in which teams a and b can meet. The round are enumerated from 1.
Examples
Input
4 1 2
Output
1
Input
8 2 6
Output
Final!
Input
8 7 5
Output
2
Note
In the first example teams 1 and 2 meet in the first round.
In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds.
In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.
Submitted Solution:
```
import math
n,a,b=map(int,input().split(' '))
rounds=math.log(n,2)
r=int(rounds)
for i in range(1,int(rounds)+1):
a=math.ceil(a/2)
b=math.ceil(b/2)
if(a==b):
if(i==r):
print('Final!')
break
else:
print(i)
break
``` | instruction | 0 | 83,926 | 17 | 167,852 |
Yes | output | 1 | 83,926 | 17 | 167,853 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The last stage of Football World Cup is played using the play-off system.
There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.
Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.
Input
The only line contains three integers n, a and b (2 ≤ n ≤ 256, 1 ≤ a, b ≤ n) — the total number of teams, and the ids of the teams that Arkady is interested in.
It is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.
Output
In the only line print "Final!" (without quotes), if teams a and b can meet in the Final.
Otherwise, print a single integer — the number of the round in which teams a and b can meet. The round are enumerated from 1.
Examples
Input
4 1 2
Output
1
Input
8 2 6
Output
Final!
Input
8 7 5
Output
2
Note
In the first example teams 1 and 2 meet in the first round.
In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds.
In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.
Submitted Solution:
```
n, a, b = map(int, input().split())
d = bin((a-1)^(b-1))
r = len(d) - d.find('1', 2)
print('Final!' if r == (n-1).bit_length() else r)
``` | instruction | 0 | 83,927 | 17 | 167,854 |
Yes | output | 1 | 83,927 | 17 | 167,855 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The last stage of Football World Cup is played using the play-off system.
There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.
Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.
Input
The only line contains three integers n, a and b (2 ≤ n ≤ 256, 1 ≤ a, b ≤ n) — the total number of teams, and the ids of the teams that Arkady is interested in.
It is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.
Output
In the only line print "Final!" (without quotes), if teams a and b can meet in the Final.
Otherwise, print a single integer — the number of the round in which teams a and b can meet. The round are enumerated from 1.
Examples
Input
4 1 2
Output
1
Input
8 2 6
Output
Final!
Input
8 7 5
Output
2
Note
In the first example teams 1 and 2 meet in the first round.
In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds.
In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.
Submitted Solution:
```
t=input().split()
n=int(t[0])
a=int(t[1])
b=int(t[2])
i=n
k=0
while i>0:
k=k+1
i=i//2
k=k-1
x=k
if a<b:
i=n//2
while i>0:
if a<=i and b>i:
break
x=x-1
i=i//2
if x==k:
print("Final!")
else:
print(x)
else:
i=n//2
while i>0:
if b<=i and a>i:
break
x=x-1
i=i//2
if x==k:
print("Final")
else:
print(x)
``` | instruction | 0 | 83,928 | 17 | 167,856 |
No | output | 1 | 83,928 | 17 | 167,857 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The last stage of Football World Cup is played using the play-off system.
There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.
Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.
Input
The only line contains three integers n, a and b (2 ≤ n ≤ 256, 1 ≤ a, b ≤ n) — the total number of teams, and the ids of the teams that Arkady is interested in.
It is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.
Output
In the only line print "Final!" (without quotes), if teams a and b can meet in the Final.
Otherwise, print a single integer — the number of the round in which teams a and b can meet. The round are enumerated from 1.
Examples
Input
4 1 2
Output
1
Input
8 2 6
Output
Final!
Input
8 7 5
Output
2
Note
In the first example teams 1 and 2 meet in the first round.
In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds.
In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.
Submitted Solution:
```
n,a,b=map(int,input().split())
l=list(range(1,n+1))
def getRoundNumber(l,n,a,b,count):
if len(l)==2:
return 10000
temp=[]
for i in range(0,len(l)-1,2):
if l[i]==a and l[i+1]==b:
return count
elif l[i]==a and l[i+1]!=b:
temp.append(l[i])
elif l[i]!=a and l[i+1]==b:
temp.append(l[i+1])
else:
temp.append(l[i])
n=n//2
return getRoundNumber(temp,n,a,b,count+1)
if a>b:
count=getRoundNumber(l,n,b,a,1)
if count==10000:
print('Final!')
else:
print(count)
else:
count=getRoundNumber(l,n,a,b,1)
if count==10000:
print('Final!')
else:
print(count)
``` | instruction | 0 | 83,929 | 17 | 167,858 |
No | output | 1 | 83,929 | 17 | 167,859 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The last stage of Football World Cup is played using the play-off system.
There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.
Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.
Input
The only line contains three integers n, a and b (2 ≤ n ≤ 256, 1 ≤ a, b ≤ n) — the total number of teams, and the ids of the teams that Arkady is interested in.
It is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.
Output
In the only line print "Final!" (without quotes), if teams a and b can meet in the Final.
Otherwise, print a single integer — the number of the round in which teams a and b can meet. The round are enumerated from 1.
Examples
Input
4 1 2
Output
1
Input
8 2 6
Output
Final!
Input
8 7 5
Output
2
Note
In the first example teams 1 and 2 meet in the first round.
In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds.
In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.
Submitted Solution:
```
import math
n,a,b=input().split()
n=int(n)
a=int(a)
b=int(b)
x=n//2
if((a<x and b>x)or(a>x and b<x)):
print("Final!")
else:
print(math.ceil(abs(math.ceil(a/2)-math.ceil(b/2))/2)+1)
``` | instruction | 0 | 83,930 | 17 | 167,860 |
No | output | 1 | 83,930 | 17 | 167,861 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The last stage of Football World Cup is played using the play-off system.
There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.
Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.
Input
The only line contains three integers n, a and b (2 ≤ n ≤ 256, 1 ≤ a, b ≤ n) — the total number of teams, and the ids of the teams that Arkady is interested in.
It is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.
Output
In the only line print "Final!" (without quotes), if teams a and b can meet in the Final.
Otherwise, print a single integer — the number of the round in which teams a and b can meet. The round are enumerated from 1.
Examples
Input
4 1 2
Output
1
Input
8 2 6
Output
Final!
Input
8 7 5
Output
2
Note
In the first example teams 1 and 2 meet in the first round.
In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds.
In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.
Submitted Solution:
```
n,a,b=map(int,input().split())
cnt=0
while(n>1):
cnt+=1
n=n//2
cnt1=0
k=abs(a-b)
while(k>=1):
cnt1+=1
k=k//2
if cnt1==cnt:
print("Final!")
elif (a%2!=0 and b%2==0) or (a%2==0 and b%2!=0):
cnt1+=1
if cnt1==cnt:
print("Final!")
else:
print(cnt1)
else:
print()
``` | instruction | 0 | 83,931 | 17 | 167,862 |
No | output | 1 | 83,931 | 17 | 167,863 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
When you heard that this year's KUPC can be joined as a team, you decided to talk to your friends and join as a team.
Eventually, $ 4 $ people, including you, got together.
Individual strength is expressed in ratings, and $ 4 $ person ratings are $ a $, $ b $, $ c $, and $ d $, respectively.
You decided to split into $ 2 $ teams of $ 2 $ people each.
At this time, the strength of the team is defined by the sum of the ratings of the $ 2 $ people who make up the team. In addition, the difference in ability between teams is defined by the absolute value of the difference in strength of each team.
You want to divide the teams so that the difference in ability between the teams is as small as possible.
Find the minimum value of the difference in ability between teams when the teams are divided successfully.
Constraint
* $ 1 \ leq a, b, c, d \ leq 2799 $
* All inputs are integers
* * *
input
Input is given from standard input in the following format.
$ a $ $ b $ $ c $ $ d $
output
Output the minimum difference in ability between teams in an integer $ 1 $ line.
* * *
Input example 1
2 1 3 4
Output example 1
0
If you team up with the $ 1 $ and $ 3 $ people, and the $ 2 $ and $ 4 $ people, the strength of the team will be $ 5 $ and the difference in ability will be $ 0 $. Obviously this is the smallest.
* * *
Input example 2
64 224 239 1024
Output example 2
625
Example
Input
2 1 3 4
Output
0 | instruction | 0 | 84,187 | 17 | 168,374 |
"Correct Solution:
```
def main():
A= list(map( int, input().split()))
ans = abs(A[0]+A[1]-A[2]-A[3])
for i in range(4):
for j in range(4):
if i == j:
continue
now = 0
for t in range(4):
if t == i or t == j:
now += A[t]
else:
now -= A[t]
if abs( now) < ans:
ans = abs(now)
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 84,187 | 17 | 168,375 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
When you heard that this year's KUPC can be joined as a team, you decided to talk to your friends and join as a team.
Eventually, $ 4 $ people, including you, got together.
Individual strength is expressed in ratings, and $ 4 $ person ratings are $ a $, $ b $, $ c $, and $ d $, respectively.
You decided to split into $ 2 $ teams of $ 2 $ people each.
At this time, the strength of the team is defined by the sum of the ratings of the $ 2 $ people who make up the team. In addition, the difference in ability between teams is defined by the absolute value of the difference in strength of each team.
You want to divide the teams so that the difference in ability between the teams is as small as possible.
Find the minimum value of the difference in ability between teams when the teams are divided successfully.
Constraint
* $ 1 \ leq a, b, c, d \ leq 2799 $
* All inputs are integers
* * *
input
Input is given from standard input in the following format.
$ a $ $ b $ $ c $ $ d $
output
Output the minimum difference in ability between teams in an integer $ 1 $ line.
* * *
Input example 1
2 1 3 4
Output example 1
0
If you team up with the $ 1 $ and $ 3 $ people, and the $ 2 $ and $ 4 $ people, the strength of the team will be $ 5 $ and the difference in ability will be $ 0 $. Obviously this is the smallest.
* * *
Input example 2
64 224 239 1024
Output example 2
625
Example
Input
2 1 3 4
Output
0 | instruction | 0 | 84,188 | 17 | 168,376 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
from collections import deque, Counter
def getN():
return int(input())
def getList():
return list(map(int, input().split()))
import math
def main():
a,b,c,d = getList()
ans = min([abs(a+b-c-d), abs(a+c-b-d), abs(a+d-b-c)]
)
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 84,188 | 17 | 168,377 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
When you heard that this year's KUPC can be joined as a team, you decided to talk to your friends and join as a team.
Eventually, $ 4 $ people, including you, got together.
Individual strength is expressed in ratings, and $ 4 $ person ratings are $ a $, $ b $, $ c $, and $ d $, respectively.
You decided to split into $ 2 $ teams of $ 2 $ people each.
At this time, the strength of the team is defined by the sum of the ratings of the $ 2 $ people who make up the team. In addition, the difference in ability between teams is defined by the absolute value of the difference in strength of each team.
You want to divide the teams so that the difference in ability between the teams is as small as possible.
Find the minimum value of the difference in ability between teams when the teams are divided successfully.
Constraint
* $ 1 \ leq a, b, c, d \ leq 2799 $
* All inputs are integers
* * *
input
Input is given from standard input in the following format.
$ a $ $ b $ $ c $ $ d $
output
Output the minimum difference in ability between teams in an integer $ 1 $ line.
* * *
Input example 1
2 1 3 4
Output example 1
0
If you team up with the $ 1 $ and $ 3 $ people, and the $ 2 $ and $ 4 $ people, the strength of the team will be $ 5 $ and the difference in ability will be $ 0 $. Obviously this is the smallest.
* * *
Input example 2
64 224 239 1024
Output example 2
625
Example
Input
2 1 3 4
Output
0 | instruction | 0 | 84,189 | 17 | 168,378 |
"Correct Solution:
```
a = list(map(int,input().split()))
print(min(abs(sum(a)-(a[0]+a[i])*2) for i in range(1,4)))
``` | output | 1 | 84,189 | 17 | 168,379 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
When you heard that this year's KUPC can be joined as a team, you decided to talk to your friends and join as a team.
Eventually, $ 4 $ people, including you, got together.
Individual strength is expressed in ratings, and $ 4 $ person ratings are $ a $, $ b $, $ c $, and $ d $, respectively.
You decided to split into $ 2 $ teams of $ 2 $ people each.
At this time, the strength of the team is defined by the sum of the ratings of the $ 2 $ people who make up the team. In addition, the difference in ability between teams is defined by the absolute value of the difference in strength of each team.
You want to divide the teams so that the difference in ability between the teams is as small as possible.
Find the minimum value of the difference in ability between teams when the teams are divided successfully.
Constraint
* $ 1 \ leq a, b, c, d \ leq 2799 $
* All inputs are integers
* * *
input
Input is given from standard input in the following format.
$ a $ $ b $ $ c $ $ d $
output
Output the minimum difference in ability between teams in an integer $ 1 $ line.
* * *
Input example 1
2 1 3 4
Output example 1
0
If you team up with the $ 1 $ and $ 3 $ people, and the $ 2 $ and $ 4 $ people, the strength of the team will be $ 5 $ and the difference in ability will be $ 0 $. Obviously this is the smallest.
* * *
Input example 2
64 224 239 1024
Output example 2
625
Example
Input
2 1 3 4
Output
0 | instruction | 0 | 84,190 | 17 | 168,380 |
"Correct Solution:
```
a,b,c,d=map(int,input().split())
ans=1e100
for x in (b,c,d):
ans=min(ans,abs(a+x*2-(b+c+d)))
print(ans)
``` | output | 1 | 84,190 | 17 | 168,381 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
When you heard that this year's KUPC can be joined as a team, you decided to talk to your friends and join as a team.
Eventually, $ 4 $ people, including you, got together.
Individual strength is expressed in ratings, and $ 4 $ person ratings are $ a $, $ b $, $ c $, and $ d $, respectively.
You decided to split into $ 2 $ teams of $ 2 $ people each.
At this time, the strength of the team is defined by the sum of the ratings of the $ 2 $ people who make up the team. In addition, the difference in ability between teams is defined by the absolute value of the difference in strength of each team.
You want to divide the teams so that the difference in ability between the teams is as small as possible.
Find the minimum value of the difference in ability between teams when the teams are divided successfully.
Constraint
* $ 1 \ leq a, b, c, d \ leq 2799 $
* All inputs are integers
* * *
input
Input is given from standard input in the following format.
$ a $ $ b $ $ c $ $ d $
output
Output the minimum difference in ability between teams in an integer $ 1 $ line.
* * *
Input example 1
2 1 3 4
Output example 1
0
If you team up with the $ 1 $ and $ 3 $ people, and the $ 2 $ and $ 4 $ people, the strength of the team will be $ 5 $ and the difference in ability will be $ 0 $. Obviously this is the smallest.
* * *
Input example 2
64 224 239 1024
Output example 2
625
Example
Input
2 1 3 4
Output
0 | instruction | 0 | 84,191 | 17 | 168,382 |
"Correct Solution:
```
num_list = sorted(list(map(int,input().split()))) #標準入力をして小さい順にしリストに入れる
num1 = num_list[0] + num_list[3] #リストの1番大きい数と1番小さい数を足す
num2 = num_list[1] + num_list[2] #リストの2番目と三番目を足す
print(max(num1,num2) - min(num1,num2)) #2つの数の大きい方から小さい方を引く
``` | output | 1 | 84,191 | 17 | 168,383 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
When you heard that this year's KUPC can be joined as a team, you decided to talk to your friends and join as a team.
Eventually, $ 4 $ people, including you, got together.
Individual strength is expressed in ratings, and $ 4 $ person ratings are $ a $, $ b $, $ c $, and $ d $, respectively.
You decided to split into $ 2 $ teams of $ 2 $ people each.
At this time, the strength of the team is defined by the sum of the ratings of the $ 2 $ people who make up the team. In addition, the difference in ability between teams is defined by the absolute value of the difference in strength of each team.
You want to divide the teams so that the difference in ability between the teams is as small as possible.
Find the minimum value of the difference in ability between teams when the teams are divided successfully.
Constraint
* $ 1 \ leq a, b, c, d \ leq 2799 $
* All inputs are integers
* * *
input
Input is given from standard input in the following format.
$ a $ $ b $ $ c $ $ d $
output
Output the minimum difference in ability between teams in an integer $ 1 $ line.
* * *
Input example 1
2 1 3 4
Output example 1
0
If you team up with the $ 1 $ and $ 3 $ people, and the $ 2 $ and $ 4 $ people, the strength of the team will be $ 5 $ and the difference in ability will be $ 0 $. Obviously this is the smallest.
* * *
Input example 2
64 224 239 1024
Output example 2
625
Example
Input
2 1 3 4
Output
0 | instruction | 0 | 84,192 | 17 | 168,384 |
"Correct Solution:
```
A = sorted(list(map(int, input().split())))
print(abs(A[3]+A[0]-A[1]-A[2]))
``` | output | 1 | 84,192 | 17 | 168,385 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
When you heard that this year's KUPC can be joined as a team, you decided to talk to your friends and join as a team.
Eventually, $ 4 $ people, including you, got together.
Individual strength is expressed in ratings, and $ 4 $ person ratings are $ a $, $ b $, $ c $, and $ d $, respectively.
You decided to split into $ 2 $ teams of $ 2 $ people each.
At this time, the strength of the team is defined by the sum of the ratings of the $ 2 $ people who make up the team. In addition, the difference in ability between teams is defined by the absolute value of the difference in strength of each team.
You want to divide the teams so that the difference in ability between the teams is as small as possible.
Find the minimum value of the difference in ability between teams when the teams are divided successfully.
Constraint
* $ 1 \ leq a, b, c, d \ leq 2799 $
* All inputs are integers
* * *
input
Input is given from standard input in the following format.
$ a $ $ b $ $ c $ $ d $
output
Output the minimum difference in ability between teams in an integer $ 1 $ line.
* * *
Input example 1
2 1 3 4
Output example 1
0
If you team up with the $ 1 $ and $ 3 $ people, and the $ 2 $ and $ 4 $ people, the strength of the team will be $ 5 $ and the difference in ability will be $ 0 $. Obviously this is the smallest.
* * *
Input example 2
64 224 239 1024
Output example 2
625
Example
Input
2 1 3 4
Output
0 | instruction | 0 | 84,193 | 17 | 168,386 |
"Correct Solution:
```
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
A = LIST()
A.sort()
a = A[0] + A[3]
b = A[2] + A[1]
# print(A)
print(abs(a - b))
``` | output | 1 | 84,193 | 17 | 168,387 |
Provide a correct Python 3 solution for this coding contest problem.
Problem statement
When you heard that this year's KUPC can be joined as a team, you decided to talk to your friends and join as a team.
Eventually, $ 4 $ people, including you, got together.
Individual strength is expressed in ratings, and $ 4 $ person ratings are $ a $, $ b $, $ c $, and $ d $, respectively.
You decided to split into $ 2 $ teams of $ 2 $ people each.
At this time, the strength of the team is defined by the sum of the ratings of the $ 2 $ people who make up the team. In addition, the difference in ability between teams is defined by the absolute value of the difference in strength of each team.
You want to divide the teams so that the difference in ability between the teams is as small as possible.
Find the minimum value of the difference in ability between teams when the teams are divided successfully.
Constraint
* $ 1 \ leq a, b, c, d \ leq 2799 $
* All inputs are integers
* * *
input
Input is given from standard input in the following format.
$ a $ $ b $ $ c $ $ d $
output
Output the minimum difference in ability between teams in an integer $ 1 $ line.
* * *
Input example 1
2 1 3 4
Output example 1
0
If you team up with the $ 1 $ and $ 3 $ people, and the $ 2 $ and $ 4 $ people, the strength of the team will be $ 5 $ and the difference in ability will be $ 0 $. Obviously this is the smallest.
* * *
Input example 2
64 224 239 1024
Output example 2
625
Example
Input
2 1 3 4
Output
0 | instruction | 0 | 84,194 | 17 | 168,388 |
"Correct Solution:
```
nums = list(int(x) for x in input().split())
nums.sort()
team_a = 0
team_b = 0
team_a += nums[0]
team_a += nums[3]
team_b += nums[1]
team_b += nums[2]
print(abs(team_a -team_b ))
``` | output | 1 | 84,194 | 17 | 168,389 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick.
Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, however, claimed that the main culprit is some other student p_a.
After that, the teacher came to student p_a and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}.
This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.
After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.
You don't know the first student who was caught by the teacher. However, you know all the numbers p_i. Your task is to find out for every student a, who would be the student with two holes in the badge if the first caught student was a.
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 1000) — the number of the naughty students.
The second line contains n integers p_1, ..., p_n (1 ≤ p_i ≤ n), where p_i indicates the student who was reported to the teacher by student i.
Output
For every student a from 1 to n print which student would receive two holes in the badge, if a was the first student caught by the teacher.
Examples
Input
3
2 3 2
Output
2 2 3
Input
3
1 2 3
Output
1 2 3
Note
The picture corresponds to the first example test case.
<image>
When a = 1, the teacher comes to students 1, 2, 3, 2, in this order, and the student 2 is the one who receives a second hole in his badge.
When a = 2, the teacher comes to students 2, 3, 2, and the student 2 gets a second hole in his badge. When a = 3, the teacher will visit students 3, 2, 3 with student 3 getting a second hole in his badge.
For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.
Submitted Solution:
```
n=int(input())
p=[*map(int,input().split())]
r=[]
for i in range(n):
s=[1]*n
while s[i]:
s[i]=0
i=p[i]-1
r+=[i+1]
print(*r)
``` | instruction | 0 | 84,219 | 17 | 168,438 |
Yes | output | 1 | 84,219 | 17 | 168,439 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick.
Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, however, claimed that the main culprit is some other student p_a.
After that, the teacher came to student p_a and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}.
This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.
After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.
You don't know the first student who was caught by the teacher. However, you know all the numbers p_i. Your task is to find out for every student a, who would be the student with two holes in the badge if the first caught student was a.
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 1000) — the number of the naughty students.
The second line contains n integers p_1, ..., p_n (1 ≤ p_i ≤ n), where p_i indicates the student who was reported to the teacher by student i.
Output
For every student a from 1 to n print which student would receive two holes in the badge, if a was the first student caught by the teacher.
Examples
Input
3
2 3 2
Output
2 2 3
Input
3
1 2 3
Output
1 2 3
Note
The picture corresponds to the first example test case.
<image>
When a = 1, the teacher comes to students 1, 2, 3, 2, in this order, and the student 2 is the one who receives a second hole in his badge.
When a = 2, the teacher comes to students 2, 3, 2, and the student 2 gets a second hole in his badge. When a = 3, the teacher will visit students 3, 2, 3 with student 3 getting a second hole in his badge.
For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.
Submitted Solution:
```
def main():
n = int(input())
data = list(map(int , input().split()))
sub = [0] * n
for i in range(n):
temp = sub.copy()
x = i
while not temp[x]:
temp[x] += 1
x = data[x]-1
print(x+1 ,end = ' ')
main()
``` | instruction | 0 | 84,220 | 17 | 168,440 |
Yes | output | 1 | 84,220 | 17 | 168,441 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick.
Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, however, claimed that the main culprit is some other student p_a.
After that, the teacher came to student p_a and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}.
This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.
After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.
You don't know the first student who was caught by the teacher. However, you know all the numbers p_i. Your task is to find out for every student a, who would be the student with two holes in the badge if the first caught student was a.
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 1000) — the number of the naughty students.
The second line contains n integers p_1, ..., p_n (1 ≤ p_i ≤ n), where p_i indicates the student who was reported to the teacher by student i.
Output
For every student a from 1 to n print which student would receive two holes in the badge, if a was the first student caught by the teacher.
Examples
Input
3
2 3 2
Output
2 2 3
Input
3
1 2 3
Output
1 2 3
Note
The picture corresponds to the first example test case.
<image>
When a = 1, the teacher comes to students 1, 2, 3, 2, in this order, and the student 2 is the one who receives a second hole in his badge.
When a = 2, the teacher comes to students 2, 3, 2, and the student 2 gets a second hole in his badge. When a = 3, the teacher will visit students 3, 2, 3 with student 3 getting a second hole in his badge.
For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.
Submitted Solution:
```
n = int(input())
arr = list(map(lambda x: x - 1, map(int, input().split())))
for i in range(n):
colours = [0 for _ in range(n)]
cur_index = i
while colours[cur_index] < 1:
colours[cur_index] += 1
cur_index = arr[cur_index]
print(cur_index + 1, end=' ')
``` | instruction | 0 | 84,221 | 17 | 168,442 |
Yes | output | 1 | 84,221 | 17 | 168,443 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick.
Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, however, claimed that the main culprit is some other student p_a.
After that, the teacher came to student p_a and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}.
This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.
After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.
You don't know the first student who was caught by the teacher. However, you know all the numbers p_i. Your task is to find out for every student a, who would be the student with two holes in the badge if the first caught student was a.
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 1000) — the number of the naughty students.
The second line contains n integers p_1, ..., p_n (1 ≤ p_i ≤ n), where p_i indicates the student who was reported to the teacher by student i.
Output
For every student a from 1 to n print which student would receive two holes in the badge, if a was the first student caught by the teacher.
Examples
Input
3
2 3 2
Output
2 2 3
Input
3
1 2 3
Output
1 2 3
Note
The picture corresponds to the first example test case.
<image>
When a = 1, the teacher comes to students 1, 2, 3, 2, in this order, and the student 2 is the one who receives a second hole in his badge.
When a = 2, the teacher comes to students 2, 3, 2, and the student 2 gets a second hole in his badge. When a = 3, the teacher will visit students 3, 2, 3 with student 3 getting a second hole in his badge.
For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.
Submitted Solution:
```
def dfs(start):
if color[start] == True:
print(start + 1, end=' ')
return
else:
color[start] = True
dfs(mas[start] - 1)
n = int(input())
mas = [int(s) for s in input().split()]
for i in range(n):
color = [False] * n
dfs(i)
``` | instruction | 0 | 84,222 | 17 | 168,444 |
Yes | output | 1 | 84,222 | 17 | 168,445 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick.
Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, however, claimed that the main culprit is some other student p_a.
After that, the teacher came to student p_a and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}.
This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.
After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.
You don't know the first student who was caught by the teacher. However, you know all the numbers p_i. Your task is to find out for every student a, who would be the student with two holes in the badge if the first caught student was a.
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 1000) — the number of the naughty students.
The second line contains n integers p_1, ..., p_n (1 ≤ p_i ≤ n), where p_i indicates the student who was reported to the teacher by student i.
Output
For every student a from 1 to n print which student would receive two holes in the badge, if a was the first student caught by the teacher.
Examples
Input
3
2 3 2
Output
2 2 3
Input
3
1 2 3
Output
1 2 3
Note
The picture corresponds to the first example test case.
<image>
When a = 1, the teacher comes to students 1, 2, 3, 2, in this order, and the student 2 is the one who receives a second hole in his badge.
When a = 2, the teacher comes to students 2, 3, 2, and the student 2 gets a second hole in his badge. When a = 3, the teacher will visit students 3, 2, 3 with student 3 getting a second hole in his badge.
For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.
Submitted Solution:
```
from collections import defaultdict
#start the code from here
# no of egdes..
# simple DFS EDITED VERSION......
def dfs(stack,visited,g):
while stack:
# print(stack,visited)
p=stack.pop()
if visited[p]!=2:
visited[p]+=1
if visited[p]==2:
# print(p+1,"pp")
return p+1
# print("yes")
for i in g[p]:
# print(i,"kya dikkat h")
if visited[i]!=2:
stack.append(i)
# print(stack)
t=int(input())
g=defaultdict(list)
l=list(map(int,input().split()))
for i in range(t):
a,b=i+1,l[i]
a=a-1
b=b-1
g[a].append(b)
print(g)
ans=[]
for i in range(t):
visited=[0]*t
stack=[i]
ans.append(dfs(stack,visited,g))
print(*ans)
``` | instruction | 0 | 84,223 | 17 | 168,446 |
No | output | 1 | 84,223 | 17 | 168,447 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick.
Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, however, claimed that the main culprit is some other student p_a.
After that, the teacher came to student p_a and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}.
This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.
After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.
You don't know the first student who was caught by the teacher. However, you know all the numbers p_i. Your task is to find out for every student a, who would be the student with two holes in the badge if the first caught student was a.
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 1000) — the number of the naughty students.
The second line contains n integers p_1, ..., p_n (1 ≤ p_i ≤ n), where p_i indicates the student who was reported to the teacher by student i.
Output
For every student a from 1 to n print which student would receive two holes in the badge, if a was the first student caught by the teacher.
Examples
Input
3
2 3 2
Output
2 2 3
Input
3
1 2 3
Output
1 2 3
Note
The picture corresponds to the first example test case.
<image>
When a = 1, the teacher comes to students 1, 2, 3, 2, in this order, and the student 2 is the one who receives a second hole in his badge.
When a = 2, the teacher comes to students 2, 3, 2, and the student 2 gets a second hole in his badge. When a = 3, the teacher will visit students 3, 2, 3 with student 3 getting a second hole in his badge.
For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.
Submitted Solution:
```
n= int(input())
x = list(map(int,input().split()))
final = set()
def recurse(y):
if x[y] in final:
print(x[y],end=" ")
else:
final.add(x[y])
print("final :",final)
recurse(x[y]-1)
for i in range(len(x)):
final.add(i+1)
recurse(i)
print()
``` | instruction | 0 | 84,224 | 17 | 168,448 |
No | output | 1 | 84,224 | 17 | 168,449 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick.
Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, however, claimed that the main culprit is some other student p_a.
After that, the teacher came to student p_a and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}.
This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.
After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.
You don't know the first student who was caught by the teacher. However, you know all the numbers p_i. Your task is to find out for every student a, who would be the student with two holes in the badge if the first caught student was a.
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 1000) — the number of the naughty students.
The second line contains n integers p_1, ..., p_n (1 ≤ p_i ≤ n), where p_i indicates the student who was reported to the teacher by student i.
Output
For every student a from 1 to n print which student would receive two holes in the badge, if a was the first student caught by the teacher.
Examples
Input
3
2 3 2
Output
2 2 3
Input
3
1 2 3
Output
1 2 3
Note
The picture corresponds to the first example test case.
<image>
When a = 1, the teacher comes to students 1, 2, 3, 2, in this order, and the student 2 is the one who receives a second hole in his badge.
When a = 2, the teacher comes to students 2, 3, 2, and the student 2 gets a second hole in his badge. When a = 3, the teacher will visit students 3, 2, 3 with student 3 getting a second hole in his badge.
For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.
Submitted Solution:
```
from collections import defaultdict
def bg(g,v,p=[]):
if (v in g[v]) or (v in p):
return v
p+=[v]
b=bg(g,*g[v],p)
if b:return(b)
n=int(input())
l=list(map(int,input().split()))
g=defaultdict(list)
for i in range(n):
g[i+1].append(l[i])
for node in range(1,n+1):
p=[0]*n
for node in g:print(bg(g,node),end=" ")
``` | instruction | 0 | 84,225 | 17 | 168,450 |
No | output | 1 | 84,225 | 17 | 168,451 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick.
Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, however, claimed that the main culprit is some other student p_a.
After that, the teacher came to student p_a and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}.
This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.
After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.
You don't know the first student who was caught by the teacher. However, you know all the numbers p_i. Your task is to find out for every student a, who would be the student with two holes in the badge if the first caught student was a.
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 1000) — the number of the naughty students.
The second line contains n integers p_1, ..., p_n (1 ≤ p_i ≤ n), where p_i indicates the student who was reported to the teacher by student i.
Output
For every student a from 1 to n print which student would receive two holes in the badge, if a was the first student caught by the teacher.
Examples
Input
3
2 3 2
Output
2 2 3
Input
3
1 2 3
Output
1 2 3
Note
The picture corresponds to the first example test case.
<image>
When a = 1, the teacher comes to students 1, 2, 3, 2, in this order, and the student 2 is the one who receives a second hole in his badge.
When a = 2, the teacher comes to students 2, 3, 2, and the student 2 gets a second hole in his badge. When a = 3, the teacher will visit students 3, 2, 3 with student 3 getting a second hole in his badge.
For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.
Submitted Solution:
```
n=int(input())
s=input().split()
l=[0]+[int(s[i]) for i in range(n)]
res=[0 for i in range(n+1)]
mita=[False for i in range(n+1)]
for i in range(1,n+1):
buf=[]
while True:
if mita[i]:
for j in buf:
res[j]=i
break
buf.append(i)
i=l[i]
try:
ind=buf.index(i)
except:
ind=-1
if ind!=-1:
for j in range(ind):
res[buf[j]]=i
mita[buf[j]]=True
for j in range(ind,len(buf)):
res[buf[j]]=buf[j]
mita[buf[j]]=True
break
for i in range(1,n+1):
print(res[i],end=" ")
print("")
``` | instruction | 0 | 84,226 | 17 | 168,452 |
No | output | 1 | 84,226 | 17 | 168,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.
Rules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better).
We know that the i-th competitor will always have score x_i when he codes the first task and y_i when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest.
Zibi wants all competitors to write a contest with each other. However, there are m pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in?
Input
The first line contains two integers n and m (2 ≤ n ≤ 300 000, 0 ≤ m ≤ 300 000) — the number of participants and the number of pairs of people who will not write a contest together.
Each of the next n lines contains two integers x_i and y_i (-10^9 ≤ x_i, y_i ≤ 10^9) — the scores which will the i-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both x_i and y_i same.
Each of the next m lines contain two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once.
Output
Output n integers — the sum of scores for all participants in the same order as they appear in the input.
Examples
Input
3 2
1 2
2 3
1 3
1 2
2 3
Output
3 0 3
Input
3 3
1 2
2 3
1 3
1 2
2 3
1 3
Output
0 0 0
Input
5 3
-1 3
2 4
1 1
3 5
2 2
1 4
2 3
3 5
Output
4 14 4 16 10
Note
In the first example, there will be only one team consisting of persons 1 and 3. The optimal strategy for them is to assign the first task to the 3-rd person and the second task to the 1-st person, this will lead to score equal to 1 + 2 = 3.
In the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case... | instruction | 0 | 84,227 | 17 | 168,454 |
Tags: constructive algorithms, greedy, math, sortings
Correct Solution:
```
import sys, io
stdin = io.FileIO(sys.stdin.fileno())
input = iter(stdin.read().splitlines()).__next__
n, m = map(int, input().split())
x, y = [0] * n, [0] * n
for i in range(n):
x[i], y[i] = map(int, input().split())
t = list(range(n))
t.sort(key=lambda i: x[i] - y[i])
pre = [0] * (n + 1)
for i in range(n):
pre[i] = pre[i - 1] + x[t[i]]
suf = 0
ans = [0] * n
for i in range(n - 1, -1, -1):
ans[t[i]] = pre[i - 1] + suf + i * y[t[i]] + (n - i - 1) * x[t[i]]
suf += y[t[i]]
for _ in range(m):
u, v = map(int, input().split())
d = min(x[u - 1] + y[v - 1], y[u - 1] + x[v - 1])
ans[u - 1] -= d
ans[v - 1] -= d
io.FileIO(sys.stdout.fileno(), 'w').write(b' '.join(str(v).encode() for v in ans))
``` | output | 1 | 84,227 | 17 | 168,455 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.
Rules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better).
We know that the i-th competitor will always have score x_i when he codes the first task and y_i when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest.
Zibi wants all competitors to write a contest with each other. However, there are m pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in?
Input
The first line contains two integers n and m (2 ≤ n ≤ 300 000, 0 ≤ m ≤ 300 000) — the number of participants and the number of pairs of people who will not write a contest together.
Each of the next n lines contains two integers x_i and y_i (-10^9 ≤ x_i, y_i ≤ 10^9) — the scores which will the i-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both x_i and y_i same.
Each of the next m lines contain two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once.
Output
Output n integers — the sum of scores for all participants in the same order as they appear in the input.
Examples
Input
3 2
1 2
2 3
1 3
1 2
2 3
Output
3 0 3
Input
3 3
1 2
2 3
1 3
1 2
2 3
1 3
Output
0 0 0
Input
5 3
-1 3
2 4
1 1
3 5
2 2
1 4
2 3
3 5
Output
4 14 4 16 10
Note
In the first example, there will be only one team consisting of persons 1 and 3. The optimal strategy for them is to assign the first task to the 3-rd person and the second task to the 1-st person, this will lead to score equal to 1 + 2 = 3.
In the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case... | instruction | 0 | 84,228 | 17 | 168,456 |
Tags: constructive algorithms, greedy, math, sortings
Correct Solution:
```
import sys
import io
n,m = [int(x) for x in sys.stdin.buffer.readline().split()]
s = sys.stdin.buffer.read()
inp = []
sign = 1
numb = 0
for i in range(len(s)):
if s[i]>=48:
numb = 10*numb + s[i]-48
else:
if s[i]==45:
sign = -1
elif s[i]!=13:
inp.append(sign*numb)
numb = 0
sign = 1
if s[-1]>=48:
inp.append(sign*numb)
order = sorted(range(n),key=lambda i:inp[2*i]-inp[2*i+1])
score = [0]*n
val = sum(inp[1:2*n:2])
for ind in range(n):
i = order[ind]
# Do second problem together with order[:ind]
# Do first problem together with order[ind:]
score[i] += val + inp[2*i+1]*(ind-1) + inp[2*i]*(n-ind-1)
val += inp[2*i]-inp[2*i+1]
for _ in range(m):
u = inp[2*n+2*_]-1
v = inp[2*n+2*_+1]-1
s = min(inp[2*u]+inp[2*v+1],inp[2*v]+inp[2*u+1])
score[u] -= s
score[v] -= s
sys.stdout = io.StringIO()
print(*score)
sys.__stdout__.buffer.write(sys.stdout.getvalue().encode('ascii'))
``` | output | 1 | 84,228 | 17 | 168,457 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.
Rules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better).
We know that the i-th competitor will always have score x_i when he codes the first task and y_i when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest.
Zibi wants all competitors to write a contest with each other. However, there are m pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in?
Input
The first line contains two integers n and m (2 ≤ n ≤ 300 000, 0 ≤ m ≤ 300 000) — the number of participants and the number of pairs of people who will not write a contest together.
Each of the next n lines contains two integers x_i and y_i (-10^9 ≤ x_i, y_i ≤ 10^9) — the scores which will the i-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both x_i and y_i same.
Each of the next m lines contain two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once.
Output
Output n integers — the sum of scores for all participants in the same order as they appear in the input.
Examples
Input
3 2
1 2
2 3
1 3
1 2
2 3
Output
3 0 3
Input
3 3
1 2
2 3
1 3
1 2
2 3
1 3
Output
0 0 0
Input
5 3
-1 3
2 4
1 1
3 5
2 2
1 4
2 3
3 5
Output
4 14 4 16 10
Note
In the first example, there will be only one team consisting of persons 1 and 3. The optimal strategy for them is to assign the first task to the 3-rd person and the second task to the 1-st person, this will lead to score equal to 1 + 2 = 3.
In the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case... | instruction | 0 | 84,229 | 17 | 168,458 |
Tags: constructive algorithms, greedy, math, sortings
Correct Solution:
```
#!/usr/bin/env python
from __future__ import division, print_function
import io
import os
import sys
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
else:
from io import BytesIO as StringIO
sys.stdout, stream = io.IOBase(), StringIO()
sys.stdout.flush = lambda: os.write(1, stream.getvalue()) and not stream.truncate(0) and stream.seek(0)
sys.stdout.write = stream.write if sys.version_info[0] < 3 else lambda s: stream.write(s.encode())
input, flush = sys.stdin.readline, sys.stdout.flush
ind = 0
numbers = []
num, sign = 0, True
for char in os.read(0, os.fstat(0).st_size):
if char >= 48:
num = 10 * num + char - 48
elif char == 45:
sign = False
elif char != 13:
numbers.append(num if sign else -num)
num, sign = 0, True
if char >= 48:
numbers.append(num if sign else -num)
getnum = iter(numbers).__next__
def main():
n, m = getnum(), getnum()
c = [0] * n
a = [[getnum(), getnum(), i] for i in range(n)]
for _ in range(m):
x, y = getnum(), getnum()
s = min(a[x - 1][0] + a[y - 1][1], a[x - 1][1] + a[y - 1][0])
c[x - 1] -= s
c[y - 1] -= s
a.sort(key=lambda x: x[1] - x[0])
sum_x = sum(i[0] for i in a)
sum_y = 0
for i in range(n):
sum_x -= a[i][0]
c[a[i][2]] += sum_x + ((n - i - 1) * a[i][1]) + sum_y + (i * a[i][0])
sum_y += a[i][1]
print(*c)
if __name__ == '__main__':
main()
``` | output | 1 | 84,229 | 17 | 168,459 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.
Rules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better).
We know that the i-th competitor will always have score x_i when he codes the first task and y_i when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest.
Zibi wants all competitors to write a contest with each other. However, there are m pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in?
Input
The first line contains two integers n and m (2 ≤ n ≤ 300 000, 0 ≤ m ≤ 300 000) — the number of participants and the number of pairs of people who will not write a contest together.
Each of the next n lines contains two integers x_i and y_i (-10^9 ≤ x_i, y_i ≤ 10^9) — the scores which will the i-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both x_i and y_i same.
Each of the next m lines contain two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once.
Output
Output n integers — the sum of scores for all participants in the same order as they appear in the input.
Examples
Input
3 2
1 2
2 3
1 3
1 2
2 3
Output
3 0 3
Input
3 3
1 2
2 3
1 3
1 2
2 3
1 3
Output
0 0 0
Input
5 3
-1 3
2 4
1 1
3 5
2 2
1 4
2 3
3 5
Output
4 14 4 16 10
Note
In the first example, there will be only one team consisting of persons 1 and 3. The optimal strategy for them is to assign the first task to the 3-rd person and the second task to the 1-st person, this will lead to score equal to 1 + 2 = 3.
In the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case... | instruction | 0 | 84,230 | 17 | 168,460 |
Tags: constructive algorithms, greedy, math, sortings
Correct Solution:
```
import sys, io
BUF_SIZ = 65536
input = iter(io.BufferedReader(io.FileIO(sys.stdin.fileno()), BUF_SIZ).read().splitlines()).__next__
n, m = map(int, input().split())
x, y = [0] * n, [0] * n
for i in range(n):
x[i], y[i] = map(int, input().split())
t = list(range(n))
t.sort(key=lambda i: x[i] - y[i])
pre = [0] * (n + 1)
for i in range(n):
pre[i] = pre[i - 1] + x[t[i]]
suf = 0
ans = [0] * n
for i in range(n - 1, -1, -1):
ans[t[i]] = pre[i - 1] + suf + i * y[t[i]] + (n - i - 1) * x[t[i]]
suf += y[t[i]]
for _ in range(m):
u, v = map(int, input().split())
d = min(x[u - 1] + y[v - 1], y[u - 1] + x[v - 1])
ans[u - 1] -= d
ans[v - 1] -= d
out = b' '.join(str(v).encode() for v in ans)
io.BufferedWriter(io.FileIO(sys.stdout.fileno(), 'w'), BUF_SIZ).write(out)
``` | output | 1 | 84,230 | 17 | 168,461 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.
Rules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better).
We know that the i-th competitor will always have score x_i when he codes the first task and y_i when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest.
Zibi wants all competitors to write a contest with each other. However, there are m pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in?
Input
The first line contains two integers n and m (2 ≤ n ≤ 300 000, 0 ≤ m ≤ 300 000) — the number of participants and the number of pairs of people who will not write a contest together.
Each of the next n lines contains two integers x_i and y_i (-10^9 ≤ x_i, y_i ≤ 10^9) — the scores which will the i-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both x_i and y_i same.
Each of the next m lines contain two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once.
Output
Output n integers — the sum of scores for all participants in the same order as they appear in the input.
Examples
Input
3 2
1 2
2 3
1 3
1 2
2 3
Output
3 0 3
Input
3 3
1 2
2 3
1 3
1 2
2 3
1 3
Output
0 0 0
Input
5 3
-1 3
2 4
1 1
3 5
2 2
1 4
2 3
3 5
Output
4 14 4 16 10
Note
In the first example, there will be only one team consisting of persons 1 and 3. The optimal strategy for them is to assign the first task to the 3-rd person and the second task to the 1-st person, this will lead to score equal to 1 + 2 = 3.
In the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case... | instruction | 0 | 84,231 | 17 | 168,462 |
Tags: constructive algorithms, greedy, math, sortings
Correct Solution:
```
import io
import os
import sys
class FastI(io.BytesIO):
""" FastIO for PyPy3 by Pajenegod """
def __init__(self):
self.newlines = 0
def read(self, b=b'\n'):
while b:
b, ptr = os.read(0, (1 << 13) + os.fstat(0).st_size), self.tell()
self.seek(0, 2), self.write(b), self.seek(ptr)
return super(FastI, self).read() if self.stream.tell() else self.getvalue()
def readline(self, b=b'\n'):
while b and self.newlines == 0:
b, ptr = os.read(0, (1 << 13) + os.fstat(0).st_size), self.tell()
self.seek(0, 2), self.write(b), self.seek(ptr)
self.newlines += b.count(b'\n')
self.newlines -= 1
return super(FastI, self).readline()
def readnumbers(self, var=int):
""" Read numbers till EOF. Use var to change type. """
numbers, b = [], self.read()
num, sign = var(0), 1
for char in b:
if char >= b'0' [0]:
num = 10 * num + char - 48
elif char == b'-' [0]:
sign = -1
elif char != b'\r' [0]:
numbers.append(sign * num)
num, sign = var(0), 1
if b and b[-1] >= b'0' [0]:
numbers.append(sign * num)
return numbers
sys.stdin, sys.stdout, stream = FastI(), io.IOBase(), io.BytesIO()
sys.stdout.flush = lambda: os.write(1, stream.getvalue()) and not stream.truncate(0) and stream.seek(0)
sys.stdout.write = lambda s: stream.write(s.encode())
input, flush = sys.stdin.readline, sys.stdout.flush
def main():
n, m = map(int, input().split())
c = [0] * n
a = [[int(j) for j in input().split()] + [i] for i in range(n)]
for _ in range(m):
x, y = map(int, input().split())
s = min(a[x - 1][0] + a[y - 1][1], a[x - 1][1] + a[y - 1][0])
c[x - 1] -= s
c[y - 1] -= s
a.sort(key=lambda x: x[1] - x[0])
sum_x = sum(i[0] for i in a)
sum_y = 0
for i in range(n):
sum_x -= a[i][0]
c[a[i][2]] += sum_x + ((n - i - 1) * a[i][1]) + sum_y + (i * a[i][0])
sum_y += a[i][1]
print(*c)
if __name__ == '__main__':
main()
``` | output | 1 | 84,231 | 17 | 168,463 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.
Rules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better).
We know that the i-th competitor will always have score x_i when he codes the first task and y_i when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest.
Zibi wants all competitors to write a contest with each other. However, there are m pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in?
Input
The first line contains two integers n and m (2 ≤ n ≤ 300 000, 0 ≤ m ≤ 300 000) — the number of participants and the number of pairs of people who will not write a contest together.
Each of the next n lines contains two integers x_i and y_i (-10^9 ≤ x_i, y_i ≤ 10^9) — the scores which will the i-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both x_i and y_i same.
Each of the next m lines contain two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once.
Output
Output n integers — the sum of scores for all participants in the same order as they appear in the input.
Examples
Input
3 2
1 2
2 3
1 3
1 2
2 3
Output
3 0 3
Input
3 3
1 2
2 3
1 3
1 2
2 3
1 3
Output
0 0 0
Input
5 3
-1 3
2 4
1 1
3 5
2 2
1 4
2 3
3 5
Output
4 14 4 16 10
Note
In the first example, there will be only one team consisting of persons 1 and 3. The optimal strategy for them is to assign the first task to the 3-rd person and the second task to the 1-st person, this will lead to score equal to 1 + 2 = 3.
In the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case... | instruction | 0 | 84,232 | 17 | 168,464 |
Tags: constructive algorithms, greedy, math, sortings
Correct Solution:
```
from __future__ import division, print_function
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import BytesIO, IOBase
# Pajenegod's fastIO for PyPy2 and PyPy3
class FastI:
bufflines = 0
def __init__(self, fd=0, buffersize=2**14):
self.stream = stream = BytesIO()
def read2buffer():
curpos,s = stream.tell(), os.read(fd, buffersize + os.fstat(fd).st_size)
return [s, stream.seek(0,2), stream.write(s), stream.seek(curpos)][0]
self.read2buffer = read2buffer
def read(self):
while self.read2buffer(): pass
return self.stream.read()
def readline(self):
while self.bufflines == 0:
s = self.read2buffer()
self.bufflines += s.count(b'\n') + (not s)
self.bufflines -= 1
return self.stream.readline()
def input(self): return self.readline().rstrip(b'\r\n')
class FastO(IOBase):
def __init__(self, fd=1):
stream = BytesIO()
self.flush = lambda: os.write(fd, stream.getvalue()) + stream.truncate(0)
self.write = stream.write if py2 else lambda s: stream.write(s.encode())
sys.stdin, sys.stdout = FastI(), FastO()
input = sys.stdin.input
def main():
n, m = map(int, input().split())
c = [0] * n
a = [[int(j) for j in input().split()] + [i] for i in range(n)]
for _ in range(m):
x, y = map(int, input().split())
s = min(a[x - 1][0] + a[y - 1][1], a[x - 1][1] + a[y - 1][0])
c[x - 1] -= s
c[y - 1] -= s
a.sort(key=lambda x: x[1] - x[0])
sum_x = sum(i[0] for i in a)
sum_y = 0
for i in range(n):
sum_x -= a[i][0]
c[a[i][2]] += sum_x + ((n - i - 1) * a[i][1]) + sum_y + (i * a[i][0])
sum_y += a[i][1]
print(*c)
if __name__ == '__main__':
main()
``` | output | 1 | 84,232 | 17 | 168,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.
Rules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better).
We know that the i-th competitor will always have score x_i when he codes the first task and y_i when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest.
Zibi wants all competitors to write a contest with each other. However, there are m pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in?
Input
The first line contains two integers n and m (2 ≤ n ≤ 300 000, 0 ≤ m ≤ 300 000) — the number of participants and the number of pairs of people who will not write a contest together.
Each of the next n lines contains two integers x_i and y_i (-10^9 ≤ x_i, y_i ≤ 10^9) — the scores which will the i-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both x_i and y_i same.
Each of the next m lines contain two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once.
Output
Output n integers — the sum of scores for all participants in the same order as they appear in the input.
Examples
Input
3 2
1 2
2 3
1 3
1 2
2 3
Output
3 0 3
Input
3 3
1 2
2 3
1 3
1 2
2 3
1 3
Output
0 0 0
Input
5 3
-1 3
2 4
1 1
3 5
2 2
1 4
2 3
3 5
Output
4 14 4 16 10
Note
In the first example, there will be only one team consisting of persons 1 and 3. The optimal strategy for them is to assign the first task to the 3-rd person and the second task to the 1-st person, this will lead to score equal to 1 + 2 = 3.
In the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case... | instruction | 0 | 84,233 | 17 | 168,466 |
Tags: constructive algorithms, greedy, math, sortings
Correct Solution:
```
import io
import os
import sys
class FastI():
""" FastIO for PyPy3 by Pajenegod """
def __init__(self):
self.stream = io.BytesIO()
self.newlines = 0
def read1(self):
b, ptr = os.read(0, (1 << 13) + os.fstat(0).st_size), self.stream.tell()
self.stream.seek(0, 2)
self.stream.write(b)
self.stream.seek(ptr)
return b
def read(self):
while self.read1():
pass
return self.stream.read() if self.stream.tell() else self.stream.getvalue()
def readline(self):
while self.newlines == 0:
b = self.read1()
self.newlines += b.count(b'\n') + (not b)
self.newlines -= 1
return self.stream.readline()
def readnumbers(self, var=int):
""" Read numbers till EOF. Use var to change type. """
numbers, b = [], self.read()
num, sign = var(0), 1
for char in b:
if char >= b'0' [0]:
num = 10 * num + char - 48
elif char == b'-' [0]:
sign = -1
elif char != b'\r' [0]:
numbers.append(sign * num)
num, sign = var(0), 1
if b and b[-1] >= b'0' [0]:
numbers.append(sign * num)
return numbers
sys.stdin, sys.stdout, stream = FastI(), io.IOBase(), io.BytesIO()
sys.stdout.flush = lambda: os.write(1, stream.getvalue()) and not stream.truncate(0) and stream.seek(0)
sys.stdout.write = lambda s: stream.write(s.encode())
input, flush = sys.stdin.readline, sys.stdout.flush
def main():
n, m = map(int, input().split())
c = [0] * n
a = [[int(j) for j in input().split()] + [i] for i in range(n)]
for _ in range(m):
x, y = map(int, input().split())
s = min(a[x - 1][0] + a[y - 1][1], a[x - 1][1] + a[y - 1][0])
c[x - 1] -= s
c[y - 1] -= s
a.sort(key=lambda x: x[1] - x[0])
sum_x = sum(i[0] for i in a)
sum_y = 0
for i in range(n):
sum_x -= a[i][0]
c[a[i][2]] += sum_x + ((n - i - 1) * a[i][1]) + sum_y + (i * a[i][0])
sum_y += a[i][1]
print(*c)
if __name__ == '__main__':
main()
``` | output | 1 | 84,233 | 17 | 168,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.
Rules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better).
We know that the i-th competitor will always have score x_i when he codes the first task and y_i when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest.
Zibi wants all competitors to write a contest with each other. However, there are m pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in?
Input
The first line contains two integers n and m (2 ≤ n ≤ 300 000, 0 ≤ m ≤ 300 000) — the number of participants and the number of pairs of people who will not write a contest together.
Each of the next n lines contains two integers x_i and y_i (-10^9 ≤ x_i, y_i ≤ 10^9) — the scores which will the i-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both x_i and y_i same.
Each of the next m lines contain two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once.
Output
Output n integers — the sum of scores for all participants in the same order as they appear in the input.
Examples
Input
3 2
1 2
2 3
1 3
1 2
2 3
Output
3 0 3
Input
3 3
1 2
2 3
1 3
1 2
2 3
1 3
Output
0 0 0
Input
5 3
-1 3
2 4
1 1
3 5
2 2
1 4
2 3
3 5
Output
4 14 4 16 10
Note
In the first example, there will be only one team consisting of persons 1 and 3. The optimal strategy for them is to assign the first task to the 3-rd person and the second task to the 1-st person, this will lead to score equal to 1 + 2 = 3.
In the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case... | instruction | 0 | 84,234 | 17 | 168,468 |
Tags: constructive algorithms, greedy, math, sortings
Correct Solution:
```
import sys
s = sys.stdin.buffer.read()
n = m = -1
numb = 0
for i in range(len(s)):
if s[i]>=48:
numb = 10*numb + s[i]-48
else:
if n==-1:
n = numb
else:
m = numb
break
numb = 0
inp = []
numb = 0
sign = 1
for i in range(i+2,len(s)):
if s[i]>=48:
numb = 10*numb + s[i]-48
else:
if s[i]==45:
sign = -1
elif s[i]!=13:
inp.append(sign*numb)
numb = 0
sign = 1
if s[-1]>=48:
inp.append(sign*numb)
order = sorted(range(n),key=lambda i:inp[2*i]-inp[2*i+1])
score = [0]*n
val = sum(inp[1:2*n:2])
for ind in range(n):
i = order[ind]
# Do second problem together with order[:ind]
# Do first problem together with order[ind:]
score[i] += val + inp[2*i+1]*(ind-1) + inp[2*i]*(n-ind-1)
val += inp[2*i]-inp[2*i+1]
for _ in range(m):
u = inp[2*n+2*_]-1
v = inp[2*n+2*_+1]-1
s = min(inp[2*u]+inp[2*v+1],inp[2*v]+inp[2*u+1])
score[u] -= s
score[v] -= s
sys.stdout.write(' '.join(str(x) for x in score))
``` | output | 1 | 84,234 | 17 | 168,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class.
Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where ai represents both the laziness level of the i-th student and the difficulty of his task.
The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007.
Input
The first line of input contains integer n (1 ≤ n ≤ 100 000) — the number of tasks. The next n lines contain exactly one integer number ai (1 ≤ ai ≤ 100 000) — both the difficulty of the initial task and the laziness of the i-th students.
Output
Print the minimum total time to finish all tasks modulo 10 007.
Example
Input
2
1
3
Output
6
Note
In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
Submitted Solution:
```
#!/usr/bin/env python
#-*-coding:utf-8 -*-
n=int(input())
A=sorted(int(input())for _ in range(n))
B=10007
s=0
for i in range(n):s=(A[i]*A[-1-i]+s)%B
print(s)
``` | instruction | 0 | 84,673 | 17 | 169,346 |
Yes | output | 1 | 84,673 | 17 | 169,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0. | instruction | 0 | 85,008 | 17 | 170,016 |
Tags: binary search, interactive
Correct Solution:
```
class Solver:
def solve(self):
self.num_people = int(input())
if self.num_people % 4 == 2:
return -1
return self.find_zero_pair()
def find_zero_pair(self):
begin = 1
end = self.num_people // 2 + 1
begin_value = self.func(begin)
if begin_value == 0:
return begin
while begin < end:
mid = (begin + end) // 2
mid_value = self.func(mid)
if mid_value == 0:
return mid
elif begin_value * mid_value > 0:
begin = mid + 1
else:
end = mid - 1
return begin
def func(self, pos):
opposite = (pos - 1 + self.num_people // 2) % self.num_people + 1
return self.get_value(pos) - self.get_value(opposite)
def get_value(self, pos):
print('? {}'.format(pos))
value = int(input())
return value
solver = Solver()
pair = solver.solve()
print('! {}'.format(pair))
``` | output | 1 | 85,008 | 17 | 170,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0. | instruction | 0 | 85,009 | 17 | 170,018 |
Tags: binary search, interactive
Correct Solution:
```
# this sequence is a bit scary
# 8
# 1 2 3 2 3 2 1 0
import sys
#sys.stdin=open("data.txt")
#input=sys.stdin.readline
got=[10**18]*100005
def getnum(i):
if got[i]==10**18:
print("? %d"%i)
sys.stdout.flush()
got[i]=int(input())
return got[i]
n=int(input())
if n%4==2:
# the opposite person has a different parity
print("! -1")
else:
lo=1
hi=n//2+1
t1=getnum(lo)
t2=getnum(hi)
lo2=t1-t2
hi2=t2-t1
if lo2==0:
print("! 1")
else:
# binary search
# let's hope that 1 <= mid <= n/2
while lo<hi:
mid=(lo+hi)//2
#print(lo,hi,mid)
mid2=getnum(mid)-getnum(mid+n//2)
if mid2==0:
print("! %d"%mid)
break
if (lo2>0) == (mid2>0):
lo=mid+1
else:
hi=mid-1
else:
print("! %d"%lo)
sys.stdout.flush()
``` | output | 1 | 85,009 | 17 | 170,019 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0. | instruction | 0 | 85,010 | 17 | 170,020 |
Tags: binary search, interactive
Correct Solution:
```
from sys import stdout
n = int(input())
if n % 4 == 2:
print("! -1")
exit(0)
print("?", 1)
stdout.flush()
a = int(input())
print("?", 1 + n // 2)
stdout.flush()
b = int(input())
if a == b:
print("!", 1)
exit(0)
l = 1
r = 1 + n // 2
while(l != r):
mid = ( l + r ) // 2
print("?", mid)
stdout.flush()
c = int(input())
print("?", mid + n // 2)
stdout.flush()
d = int(input())
if c == d:
print("!", mid)
exit(0)
if a < b:
if c < d:
l = mid + 1
else:
r = mid
else:
if c > d:
l = mid + 1
else:
r = mid
print("!", l)
``` | output | 1 | 85,010 | 17 | 170,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0. | instruction | 0 | 85,011 | 17 | 170,022 |
Tags: binary search, interactive
Correct Solution:
```
from sys import stdout
n = int(input())
if n % 4 == 2:
print('!', -1)
exit(0)
l = 1
r = l + n // 2
memo = [-1] * (n + 1)
def check(i):
if memo[i] == -1:
print('?', i)
stdout.flush()
memo[i] = int(input())
return memo[i]
while r >= l:
a = check(l)
b = check(l + n // 2)
if a == b:
print('!', l)
exit(0)
mid = (l + r) >> 1
c = check(mid)
d = check(mid + n // 2)
if c == d:
print('!', mid)
exit(0)
if (a < b and c < d) or (a > b and c > d):
l = mid + 1
else:
r = mid
``` | output | 1 | 85,011 | 17 | 170,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0. | instruction | 0 | 85,012 | 17 | 170,024 |
Tags: binary search, interactive
Correct Solution:
```
from sys import stdout
n = int(input())
if n % 4 == 2:
print("! -1")
exit(0)
print("?", 1)
stdout.flush()
a = int(input())
print("?", 1 + n // 2)
stdout.flush()
b = int(input())
if a == b:
print("!", 1)
exit(0)
l = 1
r = 1 + n // 2
while(l != r):
mid = ( l + r ) // 2
print("?", mid)
stdout.flush()
c = int(input())
print("?", mid + n // 2)
stdout.flush()
d = int(input())
if c == d:
print("!", mid)
exit(0)
if a < b:
if c < d:
l = mid + 1
else:
r = mid
else:
if c > d:
l = mid + 1
else:
r = mid
print("!", l)
# Made By Mostafa_Khaled
``` | output | 1 | 85,012 | 17 | 170,025 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0. | instruction | 0 | 85,013 | 17 | 170,026 |
Tags: binary search, interactive
Correct Solution:
```
#!/usr/bin/env python3
import sys
def ask(i):
print('?', i + 1)
sys.stdout.flush()
a_i = int(input())
return a_i
def answer(i):
print('!', i + 1 if i != -1 else -1)
sys.exit()
def has_intersection(l1, r1, l2, r2):
if l1 <= l2 and r2 <= r1:
return True
if l2 <= l1 and r1 <= r2:
return True
return False
n = int(input())
assert n >= 2 and n % 2 == 0
if (n // 2) % 2 == 1:
answer(-1)
else:
assert n % 4 == 0
l1 = 0
r1 = n // 2
a_l1 = ask(l1)
a_r1 = ask(r1)
if a_l1 == a_r1:
answer(0)
a_l2 = a_r1
a_r2 = a_l1
# print('binary search [', l1, ',', r1, ') ->', (l1 + r1) // 2, file=sys.stderr)
while True:
m1 = (l1 + r1) // 2
m2 = (m1 + n // 2) % n
a_m1 = ask(m1)
a_m2 = ask(m2)
if a_m1 == a_m2:
answer(m1)
if has_intersection(a_l1, a_m1, a_l2, a_m2):
r1 = m1
a_r1 = a_m1
a_r2 = a_m2
else:
assert has_intersection(a_m1, a_r1, a_m2, a_r2)
l1 = m1
a_l1 = a_m1
a_l2 = a_m2
# print('binary search [', l1, ',', r1, ') ->', (l1 + r1) // 2, file=sys.stderr)
assert False
``` | output | 1 | 85,013 | 17 | 170,027 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0. | instruction | 0 | 85,014 | 17 | 170,028 |
Tags: binary search, interactive
Correct Solution:
```
import sys
def ask(x):
print('? %d'%x)
sys.stdout.flush()
x=int(input())
return x
n=int(input())
t=n//2
if t&1:
print('! -1')
sys.stdout.flush()
sys.exit()
l=1
r=n
while l<r:
mid=(l+r)>>1
if ask(mid)>=ask((mid+t-1)%n+1):
r=mid
else:
l=mid+1
print('! %d'%l)
sys.stdout.flush()
``` | output | 1 | 85,014 | 17 | 170,029 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0. | instruction | 0 | 85,015 | 17 | 170,030 |
Tags: binary search, interactive
Correct Solution:
```
USE_STDIO = True
import sys
if not USE_STDIO:
try: import mypc
except: pass
def get_two(x, n):
print('?', x)
sys.stdout.flush()
a = int(input())
x += n // 2
if x > n:
x -= n
print('?', x)
sys.stdout.flush()
b = int(input())
return a, b
def answer(x):
print('!', x)
sys.stdout.flush()
def main():
n, = map(int, input().split(' '))
if n % 4 != 0:
answer(-1)
return
x, y = get_two(1, n)
if x == y:
answer(1)
return
l, r = 2, n // 2
for _ in range(30):
mid = (l + r) // 2
xn, yn = get_two(mid, n)
if xn == yn:
answer(mid)
return
if (yn - xn) * (y - x) > 0:
l = mid + 1
else:
r = mid - 1
answer(-1)
if __name__ == '__main__':
main()
``` | output | 1 | 85,015 | 17 | 170,031 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0.
Submitted Solution:
```
import sys
def printans(i):
print("! %d"%i, flush=True)
sys.exit(0)
def fullgetdiff(i, m):
print("? %d"%i, flush=True)
ai = int(input())
print("? %d"%(i+m), flush=True)
aim = int(input())
return ai-aim
n = int(input())
if n//2%2:
printans(-1)
getdiff = lambda i: fullgetdiff(i, n//2)
d1 = getdiff(1)
if d1==0:
printans(1)
lo, hi = 2, n//2
while lo <= hi:
mid = (lo+hi)//2
dmid = getdiff(mid)
if dmid == 0:
printans(mid)
if d1*dmid > 0:
lo = mid+1
else:
hi = mid-1
``` | instruction | 0 | 85,016 | 17 | 170,032 |
Yes | output | 1 | 85,016 | 17 | 170,033 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0.
Submitted Solution:
```
n=int(input())
if n%4==2:
print('!', '-1')
exit()
def qry(i):
print('?', i+1, flush=True)
a=int(input())
return a
def qry2(i):
a=qry(i+n//2)-qry(i)
if a==0:
print('!', i+1)
exit()
return a
a=qry2(0)
lb,rb=1,n//2-1
while lb<=rb:
mb=(lb+rb)//2
b=qry2(mb)
if (a>0)==(b>0):
lb=mb+1
else:
rb=mb-1
``` | instruction | 0 | 85,017 | 17 | 170,034 |
Yes | output | 1 | 85,017 | 17 | 170,035 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0.
Submitted Solution:
```
def inp():
return input()
def sep():
return map(int, inp().strip().split(" "))
def lis():
return list(sep())
def N():
return int(inp())
def testcase(t):
for p in range(t):
solve()
def solve():
n=N()
diff=n//2
print("?",1,flush=True)
x1=N()
print("?",1+diff,flush=True)
x2=N()
if x1==x2:
print("!",1,flush=True)
return
d1=x2-x1
l=1
h=1+ diff
i=2
while(i<=60):
m=(l+h)//2
print("?",m, flush=True)
x1 = N()
print("?",m + diff, flush=True)
x2 = N()
if x1 == x2:
print("!",m,flush=True)
return
d2=x2-x1
if d2*d1>0:
l=m
else:
h=m
d1=d2
i+=1
print("!",-1,flush=True)
return
solve()
# testcase(int(inp()))
``` | instruction | 0 | 85,018 | 17 | 170,036 |
No | output | 1 | 85,018 | 17 | 170,037 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0.
Submitted Solution:
```
# this sequence is a bit scary
# 8
# 1 2 3 2 3 2 1 0
import sys
#sys.stdin=open("data.txt")
#input=sys.stdin.readline
got=[10**18]*100005
def getnum(i):
if got[i]==10**18:
print(i)
sys.stdout.flush()
got[i]=int(input())
return got[i]
n=int(input())
if n%4==2:
# the opposite person has a different parity
print("! -1")
else:
lo=1
hi=n//2+1
t1=getnum(lo)
t2=getnum(hi)
lo2=t1-t2
hi2=t2-t1
if lo2==0:
print("! 1")
else:
# binary search
# let's hope that 1 <= mid <= n/2
while lo<hi:
mid=(lo+hi)//2
#print(lo,hi,mid)
mid2=getnum(mid)-getnum(mid+n//2)
if mid2==0:
print("! %d"%mid)
break
if (lo2>0) == (mid2>0):
lo=mid+1
else:
hi=mid-1
else:
print("! %d"%lo)
sys.stdout.flush()
``` | instruction | 0 | 85,019 | 17 | 170,038 |
No | output | 1 | 85,019 | 17 | 170,039 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0.
Submitted Solution:
```
import sys
n = int(input())
print('? 0')
sys.stdout.flush()
b0 = int(input())
print('? ' + n//2)
sys.stdout.flush()
bn2 = int(input())
bn2 -= b0
b0 = -bn2
if (bn2) % 2 == 1:
print('! -1')
sys.stdout.flush()
else:
l = 0;
r = n//2
while bn2 != 0 or b0 != 0:
m = (r-l)//2
print('? '+m)
sys.stdout.flush()
c = int(input())
print('? '+(m+n//2))
sys.stdout.flush()
d = int(input())
if c-d<0 and b0<0 or c-d>0 and b0>0:
b0 = c-d
l = m
else:
bn2 = c-d
r = m
if bn2 == 0:
print('! '+r)
sys.stdout.flush()
else:
print('! '+l)
sys.stdout.flush()
``` | instruction | 0 | 85,020 | 17 | 170,040 |
No | output | 1 | 85,020 | 17 | 170,041 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109).
When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students.
In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0.
Submitted Solution:
```
import sys
def ask(x):
print(x)
sys.stdout.flush()
x=int(input())
return x
n=int(input())
if (n>>1)&1:
print('! -1')
sys.exit()
l=1
r=n
while l<r:
mid=(l+r)>>1
if ask(mid)>=ask((mid+n//2)%n):
r=mid
else:
l=mid+1
print('! %d'%l)
``` | instruction | 0 | 85,021 | 17 | 170,042 |
No | output | 1 | 85,021 | 17 | 170,043 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is a_i.
You have to form k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than k (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 5. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).
It is possible that some students not be included in any team at all.
Your task is to report the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the number of students and the maximum number of teams, correspondingly.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is a programming skill of the i-th student.
Output
Print one integer — the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
Examples
Input
5 2
1 2 15 15 15
Output
5
Input
6 1
36 4 1 25 9 16
Output
2
Input
4 4
1 10 100 1000
Output
4 | instruction | 0 | 85,058 | 17 | 170,116 |
Tags: dp, sortings, two pointers
Correct Solution:
```
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
cnt=[0]*n
for i in range(n):
while i+cnt[i]<n and a[cnt[i]+i]-a[i]<=5:
cnt[i]+=1
dp=[[0]*(k+1) for i in range(n+1)]
for i in range(n):
for j in range(k+1):
dp[i+1][j]=max(dp[i+1][j],dp[i][j])
if j+1<=k and i+cnt[i]<=n:
dp[i+cnt[i]][j+1]=max(dp[i+cnt[i]][j+1],dp[i][j]+cnt[i])
print(dp[n][k])
``` | output | 1 | 85,058 | 17 | 170,117 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is a_i.
You have to form k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than k (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 5. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).
It is possible that some students not be included in any team at all.
Your task is to report the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the number of students and the maximum number of teams, correspondingly.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is a programming skill of the i-th student.
Output
Print one integer — the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
Examples
Input
5 2
1 2 15 15 15
Output
5
Input
6 1
36 4 1 25 9 16
Output
2
Input
4 4
1 10 100 1000
Output
4 | instruction | 0 | 85,059 | 17 | 170,118 |
Tags: dp, sortings, two pointers
Correct Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from array import array
def main():
n,k=map(int,input().split())
a=sorted(map(int,input().split()))
dp=[array("i",[0]*(k+1)) for _ in range(n+1)]
l=1
for i in range(1,n+1):
while l<n+1 and a[l-1]-a[i-1]<=5:
for j in range(1,k+1):
dp[l][j]=max(dp[l][j],dp[l-1][j],dp[i-1][j-1]+l-i+1)
l+=1
print(max(max(i) for i in dp))
# 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")
if __name__ == "__main__":
main()
``` | output | 1 | 85,059 | 17 | 170,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is a_i.
You have to form k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than k (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 5. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).
It is possible that some students not be included in any team at all.
Your task is to report the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the number of students and the maximum number of teams, correspondingly.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is a programming skill of the i-th student.
Output
Print one integer — the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
Examples
Input
5 2
1 2 15 15 15
Output
5
Input
6 1
36 4 1 25 9 16
Output
2
Input
4 4
1 10 100 1000
Output
4 | instruction | 0 | 85,060 | 17 | 170,120 |
Tags: dp, sortings, two pointers
Correct Solution:
```
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
i = 0
j = 0
ans = 0
start = [0] * n
for _ in range(n):
while j < i and a[i] - a[j] > 5:
j += 1
start[i] = j
i += 1
dp = [([0] * (k + 1)) for _ in range(n)]
for i in range(1, k+1):
dp[0][i] = 1
for i in range(1, n):
for j in range(1, k+1):
lastgroup = 0
if start[i] >= 1:
lastgroup = dp[start[i]-1][j-1]
dp[i][j] = max(dp[i-1][j], lastgroup + (i - start[i]) + 1)
print(dp[-1][-1])
``` | output | 1 | 85,060 | 17 | 170,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is a_i.
You have to form k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than k (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 5. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).
It is possible that some students not be included in any team at all.
Your task is to report the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the number of students and the maximum number of teams, correspondingly.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is a programming skill of the i-th student.
Output
Print one integer — the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
Examples
Input
5 2
1 2 15 15 15
Output
5
Input
6 1
36 4 1 25 9 16
Output
2
Input
4 4
1 10 100 1000
Output
4 | instruction | 0 | 85,061 | 17 | 170,122 |
Tags: dp, sortings, two pointers
Correct Solution:
```
import sys
import math
input = sys.stdin.readline
from functools import cmp_to_key;
def pi():
return(int(input()))
def pl():
return(int(input(), 16))
def ti():
return(list(map(int,input().split())))
def ts():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
mod = 998244353;
f = [];
def fact(n,m):
global f;
f = [1 for i in range(n+1)];
f[0] = 1;
for i in range(1,n+1):
f[i] = (f[i-1]*i)%m;
def fast_mod_exp(a,b,m):
res = 1;
while b > 0:
if b & 1:
res = (res*a)%m;
a = (a*a)%m;
b = b >> 1;
return res;
def inverseMod(n,m):
return fast_mod_exp(n,m-2,m);
def ncr(n,r,m):
if r == 0: return 1;
return ((f[n]*inverseMod(f[n-r],m))%m*inverseMod(f[r],m))%m;
def main():
D();
dp = [];
def D():
[n,k] = ti();
a = ti();
a = sorted(a);
cnt = [0 for i in range(n)];
for i in range(n):
c = 0;
for j in range(i,n):
if a[j]-a[i] <= 5: c+=1;
else:break;
cnt[i] = c;
global dp;
dp = [[0 for j in range(k+1)] for i in range(n+1)];
ans = 0;
for i in range(n):
for j in range(k+1):
dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
if j+1 <= k:
dp[i+cnt[i]][j+1] = max(dp[i+cnt[i]][j+1], dp[i][j]+cnt[i]);
print(dp[n][k]);
main();
``` | output | 1 | 85,061 | 17 | 170,123 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is a_i.
You have to form k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than k (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 5. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).
It is possible that some students not be included in any team at all.
Your task is to report the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the number of students and the maximum number of teams, correspondingly.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is a programming skill of the i-th student.
Output
Print one integer — the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
Examples
Input
5 2
1 2 15 15 15
Output
5
Input
6 1
36 4 1 25 9 16
Output
2
Input
4 4
1 10 100 1000
Output
4 | instruction | 0 | 85,062 | 17 | 170,124 |
Tags: dp, sortings, two pointers
Correct Solution:
```
import heapq
arr = input()
N,K = [int(x) for x in arr.split(' ')]
arr = input()
arr = [int(x) for x in arr.split(' ')]
arr.sort()
data = [[0]*N for _ in range(K)]
left = 0
right = 0
res = 0
while left<N and right<N:
if arr[right]>arr[left]+5:
#res = max(res,right-left)
left += 1
else:
data[0][right] = max(data[0][right-1],right-left+1)
right += 1
#res = max(res,right-left)
for i in range(K):
data[i][0] = 1
#print(data)
for j in range(1,K):
left = 0
right = 0
res = 0
while left<N and right<N:
if arr[right]>arr[left]+5:
left += 1
else:
#print(left,right)
if left >= 1 and right>=1:
data[j][right] = max(data[j][right-1],data[j][right],data[j-1][left-1] + right-left+1)
elif left==0 and right>=1:
data[j][right] = max(data[j][right-1],data[j][right],right-left+1)
right += 1
#print(data)
print(data[K-1][N-1])
``` | output | 1 | 85,062 | 17 | 170,125 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.