message stringlengths 2 30.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 237 109k | cluster float64 10 10 | __index_level_0__ int64 474 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. | instruction | 0 | 5,130 | 10 | 10,260 |
Tags: binary search, greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
def solve(mid):
ans = 0
cnt = 0
tmp = []
for i in range(n):
if info[i][1] < mid:
ans += info[i][0]
elif mid < info[i][0]:
ans += info[i][0]
cnt += 1
else:
tmp.append(info[i][0])
tmp.sort(reverse = True)
nokori = (n+1) // 2 - cnt
for i in tmp:
if nokori > 0:
ans += mid
nokori -= 1
else:
ans += i
if ans <= s and nokori <= 0:
return True
else:
return False
q = int(input())
ans = [0]*q
for qi in range(q):
n, s = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n)]
ok = 0
ng = s + 1
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if solve(mid):
ok = mid
else:
ng = mid
ans[qi] = ok
print('\n'.join(map(str, ans)))
``` | output | 1 | 5,130 | 10 | 10,261 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. | instruction | 0 | 5,131 | 10 | 10,262 |
Tags: binary search, greedy, sortings
Correct Solution:
```
import sys
import bisect
input=sys.stdin.readline
def checker(rem_,lower_):
sum_=0
a=[]
b=[]
for i in range(n):
if lower[i][1]>=rem_:
a.append(lower[i][0])
else:
b.append(lower[i][0])
a.sort(reverse=True)
if len(a) > n//2 and sum([max(j, rem_) for j in a[:n//2+1]]) + sum(a[n//2+1:]) + sum(b) <= s:
return(True)
return(False)
t=int(input())
for i in range(t):
lower=[]
higher=[]
n,s=map(int,input().split())
for i in range(n):
l,r=map(int,input().split())
lower.append([l,r])
higher.append(l)
higher.sort()
if sum(higher)==s:
print(higher[n//2])
else:
beg=1
end=1000000001
while end-beg>1:
mid=(beg+end)//2
#print(beg,end)
if checker(mid,lower):
beg=mid
else:
end=mid
print(beg)
``` | output | 1 | 5,131 | 10 | 10,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. | instruction | 0 | 5,132 | 10 | 10,264 |
Tags: binary search, greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
N, S = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(N)]
Query.append((N, S, LR))
for N, S, LR in Query:
G = []
for L, _ in LR:
G.append(L)
G.sort()
l = G[N//2]
r = S+1
while r - l > 1:
m = (l+r)//2
A = []
B = []
C = []
for L, R in LR:
if L <= m <= R:
C.append(L)
elif R < m:
A.append(L)
else:
B.append(L)
C.sort()
if len(A) < N//2+1 <= len(A)+len(C):
if sum(A)+sum(B)+sum(C[:N//2-len(A)]) + m*(len(A)+len(C)-N//2) <= S:
l = m
else:
r = m
else:
r = m
print(l)
``` | output | 1 | 5,132 | 10 | 10,265 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. | instruction | 0 | 5,133 | 10 | 10,266 |
Tags: binary search, greedy, sortings
Correct Solution:
```
from sys import stdin
from operator import itemgetter, attrgetter
def check(salary, mid, s):
cnt = 0
for i in range(len(salary)):
if salary[i][0] > mid:
s -= salary[i][0]
cnt += 1
elif salary[i][0] <= mid and salary[i][1] >= mid:
if cnt < (len(salary) + 1) // 2:
s -= mid
cnt += 1
else:
s -= salary[i][0]
elif salary[i][1] < mid:
s -= salary[i][0]
if cnt >= (len(salary) + 1) // 2 and s >= 0:
return True
else:
return False
t = int(stdin.buffer.readline())
for _ in range(t):
salary = list()
n, s = map(int, stdin.buffer.readline().split())
ansL, ansR = 0x3f3f3f3f3f3f3f3f, -1
for i in range(n):
l, r = map(int, stdin.buffer.readline().split())
ansL = min(ansL, l)
ansR = max(ansR, r)
salary.append((l, r))
salary = sorted(salary, key=itemgetter(0), reverse=True)
l = ansL
r = ansR
ans = None
while l <= r:
mid = (l + r) // 2
if check(salary, mid, s):
ans = mid
l = mid + 1
else:
r = mid - 1
print(ans)
``` | output | 1 | 5,133 | 10 | 10,267 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. | instruction | 0 | 5,134 | 10 | 10,268 |
Tags: binary search, greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
def solve(mid):
ans = 0
cnt = 0
tmp = []
for i in range(n):
l, r = info[i]
if r < mid:
ans += l
elif mid < l:
ans += l
cnt += 1
else:
tmp.append(l)
tmp.sort(reverse = True)
nokori = (n+1) // 2 - cnt
for i in tmp:
if nokori > 0:
ans += mid
nokori -= 1
else:
ans += i
if ans <= s and nokori <= 0:
return True
else:
return False
q = int(input())
for _ in range(q):
n, s = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n)]
ok = 0
ng = s + 1
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if solve(mid):
ok = mid
else:
ng = mid
print(ok)
``` | output | 1 | 5,134 | 10 | 10,269 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. | instruction | 0 | 5,135 | 10 | 10,270 |
Tags: binary search, greedy, sortings
Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
def bisearch_max(mn, mx, func):
ok = mn
ng = mx
while ok+1 < ng:
mid = (ok+ng) // 2
if func(mid):
ok = mid
else:
ng = mid
return ok
def check(m):
midcnt = N//2 + 1
A1, A2, A3 = [], [], []
cnt = k = 0
for l, r in LR:
if r < m:
A1.append((l, r))
k += l
elif m <= l:
A2.append((l ,r))
cnt += 1
k += l
else:
A3.append((l, r))
k += l
if k > K:
return False
if cnt+len(A3) < midcnt:
return False
if cnt >= midcnt and k <= K:
return True
A3.sort(reverse=True)
for l, r in A3:
cnt += 1
k += m - l
if k > K:
return False
if cnt >= midcnt and k <= K:
return True
return False
for _ in range(INT()):
N, K = MAP()
LR = []
for i in range(N):
l, r = MAP()
LR.append((l, r))
res = bisearch_max(0, 10**9+1, check)
print(res)
``` | output | 1 | 5,135 | 10 | 10,271 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. | instruction | 0 | 5,136 | 10 | 10,272 |
Tags: binary search, greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
import heapq
sys.setrecursionlimit(100000)
def getN():
return int(input())
def getList():
return list(map(int, input().split()))
def solve():
ls, rs = [], []
n, money = getList()
sals = []
for _ in range(n):
a, b = getList()
sals.append((a, b))
# ls.sort()
# rs.sort()
ans_mx = 10**10
ans_mn = 0
while(ans_mx - ans_mn > 1):
# print(ans_mx, ans_mn)
tmp = 0
heap = []
mid = (ans_mn + ans_mx) // 2
for sal in sals:
if sal[1] < mid:
tmp += sal[0]
else:
heapq.heappush(heap, (-sal[0], -sal[1]))
# print(len(heap))
if len(heap) < (n + 1) // 2:
ans_mx = mid
continue
high = 0
tgt = n // 2
# print(heap)
while(heap):
sal_cur = heapq.heappop(heap)
if high <= tgt:
tmp += max(mid, -sal_cur[0])
high += 1
else:
tmp += -sal_cur[0]
if tmp <= money:
ans_mn = mid
else:
ans_mx = mid
# print(tmp)
# ================================
# print(ans_mx, ans_mn)
tmp = 0
heap = []
mid = ans_mx
# print("mid", mid)
for sal in sals:
if sal[1] < mid:
tmp += sal[0]
else:
heapq.heappush(heap, (-sal[0], -sal[1]))
if len(heap) < (n + 1) // 2:
print(ans_mn)
return
high = 0
tgt = n // 2
# print(heap)
while (heap):
sal_cur = heapq.heappop(heap)
# print(sal_cur)
if high <= tgt:
tmp += max(mid, -sal_cur[0])
high += 1
else:
tmp += -sal_cur[0]
# print(tmp)
if tmp <= money:
print(mid)
return
else:
print(ans_mn)
return
# print(tmp)
def main():
t = getN()
for times in range(t):
solve()
if __name__ == "__main__":
main()
"""
1
3 26
10 12
1 4
10 11
1
1 100
1 1
1
3 6
1 1000
2 1000
3 1000
1
9 100
2 4
3 5
8 100
25 100
2 39
1 2
23 40
1 20
2 10
"""
``` | output | 1 | 5,136 | 10 | 10,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
Submitted Solution:
```
#!/usr/bin/env python3
import sys
INF = 10**9
sys.setrecursionlimit(10**8)
input = sys.stdin.buffer.readline
t = int(input())
for i in range(t):
n, s = [int(item) for item in input().split()]
lr = []
for j in range(n):
l, r = [int(item) for item in input().split()]
lr.append((l, r))
lft = 0; rgt = 10**9+1
while rgt - lft > 1:
mid = (lft + rgt) // 2
minimum_cost = 0
takable = []
for l, r in lr:
minimum_cost += l
if mid <= r:
takable.append(max(l - mid, 0) - l)
if len(takable) < (n // 2 + 1):
rgt = mid
continue
takable.sort()
cost = sum(takable[:n//2 + 1]) + minimum_cost + mid * (n // 2 + 1)
if cost <= s:
lft = mid
else:
rgt = mid
print(lft)
``` | instruction | 0 | 5,137 | 10 | 10,274 |
Yes | output | 1 | 5,137 | 10 | 10,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# 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")
# ------------------------------
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
def ctd(chr): return ord(chr)-ord("a")
mod = 998244353
INF = float('inf')
# ------------------------------
def main():
for _ in range(N()):
n, s = RL()
arr = [RLL() for _ in range(n)]
mid = n//2+1
def c(num):
now = 0
total = 0
tmp = []
for l, r in arr:
if r<num:
total+=l
elif l>num:
total+=l
now+=1
else:
tmp.append([l, r])
dif = mid-now
tmp.sort(key=lambda a: a[0], reverse=1)
for i in range(len(tmp)):
if i+1<=dif:
total+=num
now+=1
else:
total+=tmp[i][0]
return now>=mid and total<=s
l, r = 0, s
while l<r:
m = (l+r+1)//2
if c(m):
l = m
else:
r = m-1
print(l)
if __name__ == "__main__":
main()
``` | instruction | 0 | 5,138 | 10 | 10,276 |
Yes | output | 1 | 5,138 | 10 | 10,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
Submitted Solution:
```
a=int(input())
ans=[]
import sys
input=sys.stdin.readline
def checker(ans,mid,n,s):
g1=n//2
c1=0
c2=0
c3=0
cost=0
t1=[]
for i in range(len(ans)):
if(ans[i][0]<=mid<=ans[i][1]):
t1.append([ans[i][0],ans[i][1]])
elif(ans[i][1]<mid):
c3+=1
cost+=ans[i][0]
else:
c1+=1
cost+=ans[i][0]
if(c1>g1 or c3>g1):
if(c3>g1):
return 0;
else:
return 2;
else:
left=g1-c3
leftr=g1-c1
for i in range(left):
cost+=t1[i][0]
cost+=(leftr+1)*mid
if(cost<=s):
return 1;
else:
return 0;
import math
for i in range(a):
n,s=map(int,input().split())
ans=[]
mini=math.inf
maxa=0
for i in range(n):
x,y=map(int,input().split())
ans.append([x,y])
mini=min(x,y,mini)
maxa=max(x,y,maxa)
ans.sort()
maxa+=5
value=0
while(mini<maxa):
mid=(mini+maxa)//2
t=checker(ans,mid,n,s)
if(t==0):
maxa=mid
else:
if(t==1):
value=max(value,mid)
mini=mid+1
print(value)
``` | instruction | 0 | 5,139 | 10 | 10,278 |
Yes | output | 1 | 5,139 | 10 | 10,279 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
Submitted Solution:
```
import bisect
import os, sys, atexit
from io import BytesIO, StringIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
_OUTPUT_BUFFER = StringIO()
sys.stdout = _OUTPUT_BUFFER
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
def solve():
t = int(input())
for _ in range(t):
n,s = map(int,input().split())
ranges = []
for _ in range(n):
l,r = map(int,input().split())
ranges.append([r,l])
ranges.sort()
sumli =[0]
li,ri=[],[]
for r,l in ranges:
sumli.append(sumli[-1]+l)
li.append(l)
ri.append(r)
left,right = 0,ri[n//2]
# print(ri,li)
while left!=right:
mid = (left+right+1)//2
total = 0
# print ("binaries",left,mid,right)
riIndex = bisect.bisect_left(ri,mid)
if (n-riIndex>n//2):
total += sumli[riIndex]
# print (total)
sortedLi = sorted(li[riIndex:])
# print ("sorted li ",sortedLi)
liIndex = bisect.bisect_left(sortedLi,mid)
total+=sum(sortedLi[liIndex:])
# print (total)
countN2 = max((n+1)//2-(len(sortedLi)-liIndex),0)
total += countN2*mid+sum(sortedLi[:liIndex-countN2])
# print (total)
if total>s:
right = mid-1
else:
if countN2==0:
left = sortedLi[-(n//2+1)]
else:
left = mid
else:
right = mid-1
print ((left+right)//2)
try:
solve()
except Exception as e:
print (e)
# solve()
``` | instruction | 0 | 5,140 | 10 | 10,280 |
Yes | output | 1 | 5,140 | 10 | 10,281 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
Submitted Solution:
```
t = int(input())
#n number of employee
#s amt of money
#take in the lowest one for the first n//2(sorted)
#then take in the highest possible from the top.
for i in range(t):
n,s = map(int,input().split())
salaryrange = [[] for c in range(n)]
for b in range(n):
l,r = map(int,input().split())
salaryrange[b] = [l,r]
salaryrange.sort(key=lambda x: x[1])
totalpaid = 0
if len(salaryrange) >1:
for c in range(n//2):
totalpaid += salaryrange[c][0]
for d in range(n//2,n):
totalpaid += salaryrange[n//2][1]
if totalpaid > s:
print(int(salaryrange[n//2][1] -((totalpaid - s )/(n - n//2))))
else:
print(salaryrange[n//2][1])
else:
print(s)
``` | instruction | 0 | 5,141 | 10 | 10,282 |
No | output | 1 | 5,141 | 10 | 10,283 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
Submitted Solution:
```
import operator
def divide_into_lsts(salaries_ranges, median):
below = []
middle = []
above = 0
count_above = 0
for k, v in salaries_ranges.items():
if k[0] < median and k[1] < median:
#duplicates or more
for i in range(v):
below.append(k)
elif k[0] > median and k[1] > median:
above += k[0]
count_above += 1
else:
#duplicates or more
for i in range(v):
middle.append(k)
return below, middle, above, count_above
def calculate_min_salary_sum(below, middle, above, median, e):
min_salary_sum = 0
if len(middle) != 0:
middle.sort(key = operator.itemgetter(0))
if len(below) <= e:
n = e - len(below)
for i in range(n):
if len(middle) == 0:
break
else:
below.append(middle[0])
middle.pop(0)
for i in range(len(below)):
min_salary_sum += below[i][0]
if len(middle) > 0:
min_salary_sum += median * len(middle)
min_salary_sum += above
return min_salary_sum
t = int(input())
salaries_ranges_lst = []
p_lst = []
sum_max_lst = []
# loading data
for i in range(t):
p, sum_max = map(int, input().split())
p_lst.append(p)
sum_max_lst.append(sum_max)
salaries_ranges = {}
for j in range(p):
salary_min, salary_max = map(int, input().split())
if (salary_min, salary_max) in salaries_ranges:
salaries_ranges[(salary_min, salary_max)] += 1
else:
salaries_ranges[(salary_min, salary_max)] = 1
salaries_ranges_lst.append(salaries_ranges)
for i in range(t):
# define new variables using created collections
p = p_lst[i]
sum_max = sum_max_lst[i]
salaries_ranges = salaries_ranges_lst[i]
#define new variables
median_idx = p // 2
real_sum_max = 0
median_lst = []
for k in salaries_ranges.keys():
real_sum_max += k[1]
median_lst.append(k[1])
median_lst = sorted(median_lst)
if real_sum_max <= sum_max:
print(median_lst[median_idx])
elif p == 1 and real_sum_max > sum_max:
print(sum_max)
else:
median = median_lst[median_idx]
too_low = 0
too_high = median
min_salary_sum = real_sum_max
while too_low != too_high:
below, middle, above, count_above = divide_into_lsts(salaries_ranges, median)
e = median_idx
min_salary_sum = calculate_min_salary_sum(below, middle, above, median, e)
if min_salary_sum > sum_max:
# print("a")
too_high = median
median = int(too_low + (too_high - too_low) // 2)
elif min_salary_sum <= sum_max:
too_low = median
median = int(too_low + (too_high - too_low) // 2)
if too_high - too_low <= 1:
too_high = too_low
median = too_low
print(median)
``` | instruction | 0 | 5,142 | 10 | 10,284 |
No | output | 1 | 5,142 | 10 | 10,285 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
Submitted Solution:
```
def fun1(m,l,s):
cl=0
cr=0
l1=[]
n=len(l)
l2=[]
for i in range(n):
if l[i][1]<m:
s-=l[i][0]
cl+=1
elif l[i][0]>m:
s-=l[i][0]
cr+=1
else:
l1.append(l[i][1])
if cl>n//2 or cr>n//2:
return(0)
elif (s<(sum(l1[:(n//2-cl)])+m*(1+n//2-cr))):
return(0)
else:
return(1)
def search(low,high,l,su):
while(low<high):
mid=(low+high)//2
if fun1(mid,l,su):
low=mid+1
else:
high=mid
if fun1(low,l,su):
return(low)
return(low-1)
for _ in range(int(input())):
n,su=[int(j) for j in input().split()]
l=[]
l2=[]
for i in range(n):
inp=[int(j) for j in input().split()]
l.append(inp)
l2.append(inp[0])
l.sort()
low=sorted(l2)[n//2]
print(search(low,1+10**9,l,su))
``` | instruction | 0 | 5,143 | 10 | 10,286 |
No | output | 1 | 5,143 | 10 | 10,287 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
Submitted Solution:
```
t = int(input())
#n number of employee
#s amt of money
#take in the lowest one for the first n//2(sorted)
#then take in the highest possible from the top.
for i in range(t):
n,s = map(int,input().split())
salaryrange = [[] for c in range(n)]
for b in range(n):
l,r = map(int,input().split())
salaryrange[b] = [l,r]
salaryrange.sort(key=lambda x: x[1])
totalpaid = 0
if len(salaryrange) >1:
for c in range(n//2):#0->n//2-1
totalpaid += salaryrange[c][0]
ans = salaryrange[n // 2][1]
totalpaid += ans
counter = 1
for d in range(n//2+1,n):#n//2+1->n-1
if salaryrange[d][0]>=salaryrange[n//2][1]:
totalpaid += salaryrange[d][0]
else:
totalpaid += ans
counter += 1
if totalpaid > s:
print(int(ans-((totalpaid-s)/(counter))))
else:
print(ans)
else:
print(s)
``` | instruction | 0 | 5,144 | 10 | 10,288 |
No | output | 1 | 5,144 | 10 | 10,289 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Boris really likes numbers and even owns a small shop selling interesting numbers. He has n decimal numbers Bi. Cost of the number in his shop is equal to the sum of costs of its digits. You are given the values cd, where cd is the cost of the digit d. Of course, Boris is interested in that numbers he owns have the maximum cost possible.
Recently Boris got hold of the magical artifact A, which can allow him to increase the cost of his collection. Artifact is a string, consisting of digits and '?' symbols. To use the artifact, Boris must replace all '?' with digits to get a decimal number without leading zeros (it is also not allowed to get number 0). After that, the resulting number is added to all numbers Bi in Boris' collection. He uses the artifact exactly once.
What is the maximum cost of the collection Boris can achieve after using the artifact?
Input
First line contains artifact A, consisting of digits '0'β'9' and '?' symbols (1 β€ |A| β€ 1000). Next line contains n β the amount of numbers in Boris' collection (1 β€ n β€ 1000). Next n lines contain integers Bi (1 β€ Bi < 101000). A doesn't start with '0'.
Last line contains ten integers β costs of digits c0, c1, ..., c9 (0 β€ ci β€ 1000).
Output
Output one integer β the maximum possible cost of the collection after using the artifact.
Examples
Input
42
3
89
1
958
0 0 1 1 2 2 3 3 4 4
Output
4
Input
?5?
4
2203
5229
276
6243
2 1 6 1 1 2 5 2 2 3
Output
62
Note
In the second sample input, the optimal way is to compose the number 453. After adding this number, Boris will have numbers 2656, 5682, 729 and 6696. The total cost of all digits in them is equal to 18 + 15 + 11 + 18 = 62.
Submitted Solution:
```
numbers = [2203,5229,276,6243]
values = [2,1,6,1,1,2,5,2,2,3]
def costs(numbers):#this is the function to calculate the costs
cost = 0
for i in numbers:
for single in str(i):
value = values[int(single)]
cost += value
return cost
def highest_cost(length,place,artifact,numbers):
'''length means number of digits in the integer, place means that the given number is in which place, in hundred or tenth or single, artifact meass the number given
numbers are the list of numbers to calculate
'''
results = []
Artifact = artifact * place
if length == 3:
for x in range(1,10):
for y in range(10):
if place == 10:
number = x * 100 + Artifact + y
results.append(costs([i + number for i in numbers]))
elif place == 1:
number = x * 100 + y * 10 + Artifact
results.append(costs([i + number for i in numbers]))
elif place == 100:
number = Artifact + x * 10 + y
results.append(costs([i + number for i in numbers]))
elif length == 2:
for x in range(10):
if place == 10:
number = Artifact + x
results.append(costs([i + number for i in numbers]))
elif place == 1:
number = x * 10 + Artifact
results.append(costs([i + number for i in numbers]))
elif place == 0:
for y in range(10):
number = x * 10 + y
results.append(costs([i + number for i in numbers]))
elif length == 1:
for x in range(1, 10):
results.append(costs([i + x for i in numbers]))
return sorted(results,reverse=True)
def get_qmark(length,place,artifact,numbers):
'''length means number of digits in the integer, place means that the given number is in which place, in hundred or tenth or single, artifact meass the number given
numbers are the list of numbers to calculate
'''
needed_number = highest_cost(length=length,place=place,numbers=numbers,artifact=artifact)[0] # get the highest cost number
global answer
l = []
Artifact = artifact * place
if length == 3:
for x in range(10):
for y in range(10):
if place == 10:
number = x * 100 + Artifact + y
elif place == 1:
number = x * 100 + y * 10 + Artifact
elif place == 100:
number = Artifact + x * 10 + y
cost = costs([i + number for i in numbers])
if cost == needed_number:
l.append(x)
l.append(y)
elif length == 2:
for x in range(10):
if place == 10:
number = Artifact + x
cost = costs([i + number for i in numbers])
if cost == needed_number:
l.append(x)
elif place == 1:
number = x * 10 + Artifact
cost = costs([i + number for i in numbers])
if cost == needed_number:
l.append(x)
elif place == 0:
for y in range(10):
number = x * 10 + y
cost = costs([i + number for i in numbers])
if cost == needed_number:
l.append(x)
elif length == 1:
for x in range(1, 10):
cost = costs([i + x for i in numbers])
if cost == needed_number:
l.append(x)
return l
print (get_qmark(length=3,place=10,artifact=5,numbers=numbers))
``` | instruction | 0 | 5,530 | 10 | 11,060 |
No | output | 1 | 5,530 | 10 | 11,061 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema.
<image>
However, one day, the No. 68 Cinema runs out of changes (they don't have 50-yuan notes currently), but Nephren still wants to start their business. (Assume that yuan is a kind of currency in Regulu Ere.)
There are three types of customers: some of them bring exactly a 50-yuan note; some of them bring a 100-yuan note and Nephren needs to give a 50-yuan note back to him/her; some of them bring VIP cards so that they don't need to pay for the ticket.
Now n customers are waiting outside in queue. Nephren wants to know how many possible queues are there that they are able to run smoothly (i.e. every customer can receive his/her change), and that the number of 50-yuan notes they have after selling tickets to all these customers is between l and r, inclusive. Two queues are considered different if there exists a customer whose type is different in two queues. As the number can be large, please output the answer modulo p.
Input
One line containing four integers n (1 β€ n β€ 105), p (1 β€ p β€ 2Β·109), l and r (0 β€ l β€ r β€ n).
Output
One line indicating the answer modulo p.
Examples
Input
4 97 2 3
Output
13
Input
4 100 0 4
Output
35
Note
We use A, B and C to indicate customers with 50-yuan notes, customers with 100-yuan notes and customers with VIP cards respectively.
For the first sample, the different possible queues that there are 2 50-yuan notes left are AAAB, AABA, ABAA, AACC, ACAC, ACCA, CAAC, CACA and CCAA, and the different possible queues that there are 3 50-yuan notes left are AAAC, AACA, ACAA and CAAA. So there are 13 different queues satisfying the first sample. Similarly, there are 35 different queues satisfying the second sample.
Submitted Solution:
```
def mult(m1, m2):
p = len(m1)
q = len(m2)
r = len(m2[0])
m3 = [[0] * r for _ in range(p)]
for x in range(p):
for y in range(r):
for z in range(q):
m3[x][y] += m1[x][z] * m2[z][y]
m3[x][y] %= pio
return m3
def binpow(mat, x):
if x == 0:
return E
elif x % 2:
return mult(mat, binpow(mult(mat, mat), x // 2))
else:
return binpow(mult(mat, mat), x // 2)
n, pio, l, r = map(int, input().split())
matrix = [[0] * (n+1) for _ in range(n+1)]
E = [[0]*(n+1) for _ in range(n+1)]
for i in range(n+1):
E[i][i] = 1
for i in range(n+1):
for j in range(n+1):
if abs(i - j) <= 1:
matrix[i][j] = 1
start = [[1]]
for _ in range(n):
start.append([0])
matrix = binpow(matrix, n)
start = mult(matrix, start)
ans = 0
for p in range(l, r + 1):
ans += start[p][0]
print(ans)
``` | instruction | 0 | 5,568 | 10 | 11,136 |
No | output | 1 | 5,568 | 10 | 11,137 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin. | instruction | 0 | 5,861 | 10 | 11,722 |
Tags: dp, implementation
Correct Solution:
```
n = int(input())
nums = sorted(list(map(int, input().split())))
ideal = 0
countNeg = 0
for i in range(n):
if nums[i] > 0:
ideal += nums[i] - 1
elif nums[i] < 0:
ideal += abs(-1 - nums[i])
countNeg += 1
else:
ideal += 1
if countNeg % 2 != 0 and 0 not in nums:
ideal += 2
print(ideal)
``` | output | 1 | 5,861 | 10 | 11,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin. | instruction | 0 | 5,862 | 10 | 11,724 |
Tags: dp, implementation
Correct Solution:
```
s=0
n=0
z=0
t=int(input())
l=[int(i) for i in input().split()]
for i in l:
if i==0:
z+=1
if i<0:
s+=abs(i)-1
n+=1
if i>0:
s+=abs(i-1)
if z==0:
if n%2==0:
print(s)
else:
print(s+2)
else:
print(s+z)
``` | output | 1 | 5,862 | 10 | 11,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin. | instruction | 0 | 5,863 | 10 | 11,726 |
Tags: dp, implementation
Correct Solution:
```
import math
import sys
import collections
# imgur.com/Pkt7iIf.png
def getdict(n):
d = {}
if type(n) is list:
for i in n:
if i in d:
d[i] += 1
else:
d[i] = 1
else:
for i in range(n):
t = ii()
if t in d:
d[t] += 1
else:
d[t] = 1
return d
def cdiv(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a*b) // math.gcd(a, b)
n = ii()
d = li()
r = n = z = 0
for i in d:
r += abs(abs(i) - 1)
z += (i == 0)
n += (i < 0)
print(r) if n%2 == 0 or z > 0 else print(r + 2)
``` | output | 1 | 5,863 | 10 | 11,727 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin. | instruction | 0 | 5,864 | 10 | 11,728 |
Tags: dp, implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
neg = 0
ans = 0
zeros = 0
for i in range(n):
if a[i]>0:
ans+=a[i]-1
elif a[i]==0:
zeros+=1
else:
neg+=1
ans+=abs(-1 - a[i])
if (neg%2)!=0:
if zeros>0:
ans+=1
zeros-=1
else:
ans+=2
ans+=zeros
print(ans)
``` | output | 1 | 5,864 | 10 | 11,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin. | instruction | 0 | 5,865 | 10 | 11,730 |
Tags: dp, implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
is_ = False
p = 0
m = 0
for i in a:
if i > 0: m += i - 1;
elif i < 0:
m += -i - 1
if not(is_): is_ = True;
else: is_ = False
else:
m += 1
p += 1
if is_ and p < 1: print(m + 2);
else: print(m);
``` | output | 1 | 5,865 | 10 | 11,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin. | instruction | 0 | 5,866 | 10 | 11,732 |
Tags: dp, implementation
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
ans = 0
neg = 0
zero = 0
# set to 1
for i in range(n):
if a[i] < 0:
ans += abs(a[i] + 1)
a[i] = -1
neg += 1
elif a[i] > 0:
ans += abs(a[i] - 1)
a[i] = 1
else: zero += 1
# sign
if neg % 2 == 1:
if zero > 0:
ans += zero
else:
ans += 2
else: ans += zero
# print(a)
print(ans)
``` | output | 1 | 5,866 | 10 | 11,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin. | instruction | 0 | 5,867 | 10 | 11,734 |
Tags: dp, implementation
Correct Solution:
```
n = int(input())
l = [*map(int, input().split())]
neg = [-e for e in l if e < 0]
zer = l.count(0)
pos = [e for e in l if e > 0]
res = sum(pos) - len(pos) + zer
if neg: res += sum(neg) - len(neg)
if len(neg) & 1 and zer == 0: res += 2
print(res)
``` | output | 1 | 5,867 | 10 | 11,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin. | instruction | 0 | 5,868 | 10 | 11,736 |
Tags: dp, implementation
Correct Solution:
```
n = int(input())
x = input()
y = x.split()
z = [int(d) for d in y]
no_negatives = []
no_positives = []
no_zeroes = []
count = 0
for i in z:
if i>0:
no_positives.append(i)
elif i<0:
no_negatives.append(i)
else:
no_zeroes.append(i)
if len(no_negatives)%2 != 0:
if len(no_zeroes) != 0:
for i in no_negatives:
count = count + abs(abs(i) -1)
for i in no_zeroes:
count = count + 1
else:
for i in no_negatives:
count = count + abs(abs(i) -1)
for i in no_zeroes:
count = count + 1
count = count +2
elif len(no_negatives) %2 ==0:
for i in no_negatives:
count = count + abs(abs(i)-1)
for i in no_zeroes:
count = count + 1
for i in no_positives:
if i ==0:
pass
else:
count = count + i-1
print(count)
``` | output | 1 | 5,868 | 10 | 11,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin.
Submitted Solution:
```
n = input()
int_list = map(int, input().split())
count = 0
zeroes = 0
negatives = 0
for num in int_list:
if (num == 0):
zeroes += 1
else:
count += (abs(num) - 1)
if (num < 0):
negatives += 1
if (zeroes == 0 and negatives%2 != 0):
count +=2
else:
count += zeroes
print(count)
``` | instruction | 0 | 5,869 | 10 | 11,738 |
Yes | output | 1 | 5,869 | 10 | 11,739 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
pos = []
neg = []
zero = []
coin = 0
for num in a:
if num > 0:
pos.append(num)
elif num==0:
zero.append(num)
else:
neg.append(num)
for num in pos:
coin += num -1
for num in neg:
coin += -1 - num
if len(zero)>0:
coin += len(zero)
elif len(neg)%2==1:
coin += 2
print(coin)
``` | instruction | 0 | 5,870 | 10 | 11,740 |
Yes | output | 1 | 5,870 | 10 | 11,741 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
ans = 0
cnt = 0
cnt2 = 0
for i in range(n):
if a[i] < 0:
ans += abs(a[i]) - 1
a[i] = -1
cnt2 += 1
elif a[i] == 0:
cnt += 1
else:
ans += a[i] - 1
a[i] = 1
if cnt2 % 2 == 1 and cnt == 0:
ans += 2
print(ans)
else:
ans += cnt
print(ans)
``` | instruction | 0 | 5,871 | 10 | 11,742 |
Yes | output | 1 | 5,871 | 10 | 11,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
count = neg = flag = 0
for num in a:
if num > 0:
count += num - 1
elif num == 0:
count += 1
flag += 1
else:
count += -num - 1
neg += 1
if neg % 2 == 0 or flag:
print(count)
else:
print(count + 2)
``` | instruction | 0 | 5,872 | 10 | 11,744 |
Yes | output | 1 | 5,872 | 10 | 11,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin.
Submitted Solution:
```
negativecounter=0
noo=0
steps=0
n=int(input())
arr=list(map(int,input().split()))
for i in arr:
if(i<0):
negativecounter+=1
if(i==0):
noo+=1
arr.sort()
if(negativecounter%2==0):
for i in range(n):
if(arr[i]>1):
steps+=arr[i]-1
elif(arr[i]==0):
steps+=1
elif(arr[i]<-1):
steps+=-1*(arr[i]+1)
else:
steps+=0
else:
steps+=abs(arr[0])+1
for i in range(1,n):
if(arr[i]>1):
steps+=arr[i]-1
elif(noo%2==1 and arr[i]==0):
steps-=1
elif(arr[i]==0):
steps+=1
elif(arr[i]<-1):
steps+=-1*(arr[i]+1)
else:
steps+=0
print(steps)
``` | instruction | 0 | 5,873 | 10 | 11,746 |
No | output | 1 | 5,873 | 10 | 11,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
a.sort()
nv,count=0,0
for i in range(n):
if a[i]<0 and nv==0 and i!=n-1:
nv+=1
count+=abs(a[i]-(-1))
elif a[i]<0 and nv==1 and i!=n-1:
nv=0
count+=abs(a[i]-(-1))
elif a[i]<0 and nv==0 and i==n-1:
count+=abs(a[i])+2
elif a[i]>=0 and nv==1:
nv=0
count+=abs(a[i]+1)
else:
count+=abs(a[i]-1)
print(count)
``` | instruction | 0 | 5,874 | 10 | 11,748 |
No | output | 1 | 5,874 | 10 | 11,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin.
Submitted Solution:
```
def main():
n=int(input())
ip=[int(item) for item in input().split(" ")]
n_coins=0
neg_cnt=0
for i in range(len(ip)):
if ip[i]==0:
n_coins+=1
elif ip[i]>0:
n_coins+=ip[i]-1
elif ip[i]<0:
n_coins+=-1-ip[i]
neg_cnt+=1
if neg_cnt%2==1:
n_coins+=2
print(n_coins)
if __name__=="__main__":
main()
``` | instruction | 0 | 5,875 | 10 | 11,750 |
No | output | 1 | 5,875 | 10 | 11,751 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n numbers a_1, a_2, ..., a_n. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract 1 from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to 1, in other words, we want a_1 β
a_2 ... β
a_n = 1.
For example, for n = 3 and numbers [1, -3, 0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1, -1, -1]. And 1β
(-1) β
(-1) = 1.
What is the minimum cost we will have to pay to do that?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9) β the numbers.
Output
Output a single number β the minimal number of coins you need to pay to make the product equal to 1.
Examples
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
Note
In the first example, you can change 1 to -1 or -1 to 1 in 2 coins.
In the second example, you have to apply at least 4 operations for the product not to be 0.
In the third example, you can change -5 to -1 in 4 coins, -3 to -1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1 coin.
Submitted Solution:
```
n=int(input())
a=[int(i) for i in input().split()]
d={}
g=0
for i in a:
if i in d:
d[i]+=1
else:
d[i]=1
i=0
while(i<n):
if a[i]>1:
temp=a[i]
a[i]=1
g+=temp-1
if a[i]<-1:
temp=a[i]
a[i]=-1
g+=-1-temp
if a[i]==0:
if a.count(-1)%2!=0 :
a[i]=-1
g+=1
else:
a[i]=1
g+=1
if i==n-1 and a.count(-1)%2!=0:
g+=2
i+=1
print(g)
``` | instruction | 0 | 5,876 | 10 | 11,752 |
No | output | 1 | 5,876 | 10 | 11,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | instruction | 0 | 6,151 | 10 | 12,302 |
Tags: implementation
Correct Solution:
```
n,m = [int(x) for x in input().split()]
listaDividas = []
for i in range(101):
listaDividas.append([0,0])
for i in range(m):
individado,agiota,quantidade = [int(x) for x in input().split()]
listaDividas[individado][0] += quantidade
listaDividas[agiota][1] += quantidade
quantidadeTotal = 0
for i in range(101):
quantidadeTotal += max(listaDividas[i][0]-listaDividas[i][1],0)
print(quantidadeTotal)
``` | output | 1 | 6,151 | 10 | 12,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | instruction | 0 | 6,152 | 10 | 12,304 |
Tags: implementation
Correct Solution:
```
n,m=input().strip().split(" ")
n,m=[int(n),int(m)]
ag=[0 for _ in range(n)]
for i in range(m):
a,b,c=input().strip().split(" ")
a,b,c=[int(a),int(b),int(c)]
ag[a-1]+=c
ag[b-1]-=c
sum=0
for i in range(n):
if ag[i]>0:
sum+=ag[i]
print(sum)
``` | output | 1 | 6,152 | 10 | 12,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | instruction | 0 | 6,153 | 10 | 12,306 |
Tags: implementation
Correct Solution:
```
n,m = map(int, input().split())
debt=[0]*(n+1)
for i in range(m):
a,b,c = map(int, input().split())
debt[a]-=c
debt[b]+=c
ans=0
for i in debt:
if i>0:
ans+=i
print(ans)
``` | output | 1 | 6,153 | 10 | 12,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | instruction | 0 | 6,154 | 10 | 12,308 |
Tags: implementation
Correct Solution:
```
class CodeforcesTask376BSolution:
def __init__(self):
self.result = ''
self.n_m = []
self.debts = []
def read_input(self):
self.n_m = [int(x) for x in input().split(" ")]
for x in range(self.n_m[1]):
self.debts.append([int(y) for y in input().split(" ")])
def process_task(self):
people_balance = [0] * self.n_m[0]
for d in self.debts:
people_balance[d[0] - 1] -= d[2]
people_balance[d[1] - 1] += d[2]
self.result = str(sum([abs(x) for x in people_balance]) // 2)
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask376BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
``` | output | 1 | 6,154 | 10 | 12,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | instruction | 0 | 6,155 | 10 | 12,310 |
Tags: implementation
Correct Solution:
```
import sys
def readInputs():
global n,m,lAdj, counts
n,m = map(int,f.readline().split())
lAdj = [[] for _ in range(n)]
counts = n*[0]
for _ in range(m):
(a,b,c) = map(int,f.readline().split())
lAdj[a-1] += [(b-1,c)]
#print(lAdj)
def solve():
if(m == 0):
return 0
for i in range(n):
for cple in lAdj[i]:
succ = cple[0]
debt = cple[1]
counts[i] -= debt
counts[succ] += debt
sum = 0
for i in range(n):
if(counts[i]<0):
sum += abs(counts[i])
return sum
def tests():
global f
bFail = False
for i in range(1,3):
# inputs
f = open("test"+str(i)+".txt")
readInputs()
# output expected
lEx = list(map(int,f.readline().split()))
lEx.sort()
resExpect = str(lEx)
# output
res = str(solve())
if(res != resExpect):
print("TEST",i,"FAILED !"+" Found :",res,", Expected :",resExpect)
bFail = True
else:
print("TEST",i,"PASSED")
if(not bFail):
print("\n--- ALL OF THE "+str(i)+" TESTS HAVE SUCCEEDED ---")
def main():
global f
i = 2
if(i == 1):
tests()
return
f = sys.stdin
readInputs()
print(solve())
main()
``` | output | 1 | 6,155 | 10 | 12,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | instruction | 0 | 6,156 | 10 | 12,312 |
Tags: implementation
Correct Solution:
```
def I(): return list(map(int, input().split()))
n, m = I()
list_of_persons = [0]*(n+1)
sum = 0
for i in range(m):
x, y, z = I()
list_of_persons[x] += z
list_of_persons[y] -= z
for i in range(n+1):
if list_of_persons[i] > 0:
sum += list_of_persons[i]
print(sum)
``` | output | 1 | 6,156 | 10 | 12,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | instruction | 0 | 6,157 | 10 | 12,314 |
Tags: implementation
Correct Solution:
```
n, m = map(int, input().split())
balance = [0] * (n+1)
for i in range(m):
a, b, c = map(int, input().split())
balance[a] -= c
balance[b] += c
debt = 0
for b in balance:
if b < 0:
debt += abs(b)
print(debt)
``` | output | 1 | 6,157 | 10 | 12,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | instruction | 0 | 6,158 | 10 | 12,316 |
Tags: implementation
Correct Solution:
```
n,m=map(int,input().split())
d=[0]*n
for i in range(m):
a,b,c=map(int,input().split())
d[a-1]-=c
d[b-1]+=c
ans=0
for i in range(n):
if d[i]>0: ans+=d[i]
print(ans)
``` | output | 1 | 6,158 | 10 | 12,317 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
Submitted Solution:
```
n, m = [int(x) for x in input().split()]
gives = [0 for i in range(n+1)]
receives = [0 for i in range(n+1)]
for i in range(m):
a,b,c = [int(x) for x in input().split()]
gives[a] += c
receives[b] += c
opt = 0
for i in range(n+1):
diff = gives[i] - receives[i]
if diff > 0:
opt += diff
print(opt)
``` | instruction | 0 | 6,159 | 10 | 12,318 |
Yes | output | 1 | 6,159 | 10 | 12,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
Submitted Solution:
```
n,m = (map(int,input().split()))
s = [0] * n
for i in range(m):
a,b,c = (map(int,input().split()))
s[a-1] -= c
s[b-1] += c
print(sum(s[i] for i in range(n) if s[i] > 0))
``` | instruction | 0 | 6,160 | 10 | 12,320 |
Yes | output | 1 | 6,160 | 10 | 12,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
Submitted Solution:
```
n, m = map(int, input().split())
d = [0 for i in range(n)]
for i in range(m):
a, b, c = map(int, input().split())
d[a-1] -= c
d[b-1] += c
print(sum(i for i in d if i > 0))
``` | instruction | 0 | 6,161 | 10 | 12,322 |
Yes | output | 1 | 6,161 | 10 | 12,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
Submitted Solution:
```
__author__ = 'asmn'
n, m = tuple(map(int, input().split()))
l = [0] * n
for i in range(m):
a, b, c = tuple(map(int, input().split()))
l[a - 1] += c
l[b - 1] -= c
print(sum(abs(x) for x in l)//2)
``` | instruction | 0 | 6,162 | 10 | 12,324 |
Yes | output | 1 | 6,162 | 10 | 12,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
Submitted Solution:
```
from collections import defaultdict
n, m = map(int, input().split())
s = defaultdict(lambda: 0)
for i in range(m):
a, b, c = map(int, input().split())
s[a]+=c
s[b]-=c
print(s)
print(sum(i for i in s.values() if i>0))
``` | instruction | 0 | 6,163 | 10 | 12,326 |
No | output | 1 | 6,163 | 10 | 12,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
Submitted Solution:
```
from collections import defaultdict
def debt(g):
for i in g.keys():
l = len(g[i])
for k in range(l):
t =g[i][k][1]
u = g[i][k][0]
if u in g.keys():
for j in g[u]:
t -=j[1]
g[i][k][1] = t
return dict(g)
n , m = map(int,input().split())
d = defaultdict(list)
for _ in range(m):
a,b,c = map(int,input().split())
d[a].append([b,c])
a = debt(d)
s = 0
print(a)
for i , j in a.items():
l = len(a[i])
for k in range(l):
s+=j[k][1]
print(s)
``` | instruction | 0 | 6,164 | 10 | 12,328 |
No | output | 1 | 6,164 | 10 | 12,329 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
Submitted Solution:
```
import sys
import math
import collections
import bisect
from collections import deque as queue
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip()
for t in range(1):
n,m=get_ints()
counter=dict()
for i in range(m):
a,b,c=get_ints()
if a in counter:
counter[a]-=c
elif b in counter:
counter[b]+=c
else:
counter[a]=-c
counter[b]=+c
cost=0
for i in counter:
val=counter[i]
if val<=0:
cost+=abs(val)
print(cost)
``` | instruction | 0 | 6,165 | 10 | 12,330 |
No | output | 1 | 6,165 | 10 | 12,331 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
Input
The first line contains two integers n and m (1 β€ n β€ 100; 0 β€ m β€ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 β€ ai, bi β€ n; ai β bi; 1 β€ ci β€ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
Output
Print a single integer β the minimum sum of debts in the optimal rearrangement.
Examples
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
Note
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
Submitted Solution:
```
from collections import defaultdict as dd
d=dd(lambda:0)
from sys import stdin
stdin.readline
def mp(): return list(map(int, stdin.readline().strip().split()))
def it():return int(stdin.readline().strip())
from math import pi,sqrt
n,m=mp()
try:
for _ in range(m):
a,b,deb=mp()
d[a]+=deb
d[b]-=deb
print(max(d.values()))
except:
print(0)
``` | instruction | 0 | 6,166 | 10 | 12,332 |
No | output | 1 | 6,166 | 10 | 12,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.
Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2i - 1 liters and costs ci roubles. The number of bottles of each type in the store can be considered infinite.
You want to buy at least L liters of lemonade. How many roubles do you have to spend?
Input
The first line contains two integers n and L (1 β€ n β€ 30; 1 β€ L β€ 109) β the number of types of bottles in the store and the required amount of lemonade in liters, respectively.
The second line contains n integers c1, c2, ..., cn (1 β€ ci β€ 109) β the costs of bottles of different types.
Output
Output a single integer β the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.
Examples
Input
4 12
20 30 70 90
Output
150
Input
4 3
10000 1000 100 10
Output
10
Input
4 3
10 100 1000 10000
Output
30
Input
5 787787787
123456789 234567890 345678901 456789012 987654321
Output
44981600785557577
Note
In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.
In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.
In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles. | instruction | 0 | 6,301 | 10 | 12,602 |
Tags: bitmasks, dp, greedy
Correct Solution:
```
from sys import stdin, stdout
sze = 100
INF = float('inf')
n, l = map(int, stdin.readline().split())
c = list(map(int, stdin.readline().split()))
may = [1 for i in range(sze)]
for i in range(n - 1, -1, -1):
for j in range(i):
if c[i] > c[j] * 2 ** (i - j):
may[i] = 0
ans = INF
cnt = 0
value = 0
for i in range(n - 1, -1, -1):
if not may[i]:
continue
v = l - value
if not v % (2 ** i):
ans = min(ans, cnt + v * c[i] // (2 ** i))
else:
ans = min(ans, cnt + (v // (2 ** i) + 1) * c[i])
cnt += v // (2 ** i) * c[i]
value += (v // (2 ** i)) * (2 ** i)
stdout.write(str(ans))
``` | output | 1 | 6,301 | 10 | 12,603 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.