Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long n, s, a[10000], ans, sum, mn = 1e15; int main() { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; mn = min(mn, a[i]); } if (sum < s) { cout << "-1" << endl; } else { for (int i = 0; i < n; i++) if (a[i] > mn) s -= a[i] - mn; if (s <= 0) cout << mn << endl; else cout << fixed << setprecision(0) << mn - (ceil(s / (n * 1.))) << endl; } }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; class Timer { clock_t start; string name; public: Timer() { name = ""; start = clock(); } Timer(string s) { name = s; start = clock(); } ~Timer() { fprintf(stderr, "%s: %.3gs\n", name.c_str(), 1.0 * (clock() - start) / CLOCKS_PER_SEC); } }; const double EPS = 1e-9; const long double PI = acos(-1.0L); template <typename dtype> inline dtype sq(dtype a) { return a * a; } template <typename dtype1, typename dtype2> inline pair<dtype1, dtype2> mp(dtype1 a, dtype2 b) { return make_pair(a, b); } template <typename dtype1, typename dtype2> inline dtype1 safeMod(dtype1 a, dtype2 m) { return (a % m + m) % m; } template <typename dtype1, typename dtype2> inline bool isEq(dtype1 a, dtype2 b) { return abs(a - b) < EPS; } template <typename dtype1, typename dtype2, typename dtype3> inline bool isEq(dtype1 a, dtype2 b, dtype3 eps) { return abs(a - b) < eps; } template <typename dtype> inline dtype toRad(dtype deg) { return deg * PI / 180.0; } template <typename dtype> inline dtype toDeg(dtype rad) { return rad * 180.0 / PI; } template <typename dtype> inline bool isKthBitOn(dtype n, int k) { assert(n <= numeric_limits<dtype>::max()); assert(k <= numeric_limits<dtype>::digits); dtype ONE = 1; return bool((n & (ONE << k))); } template <typename dtype> inline void setKthBit(dtype& n, int k) { assert(n <= numeric_limits<dtype>::max()); assert(k <= numeric_limits<dtype>::digits); dtype ONE = 1; n = (n | (ONE << k)); } const int oo = 0x3f3f3f3f; const int MAX = 200010; const int MOD = 1000000007; const int precision = 10; int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, s, sum = 0; long long ara[MAX]; cin >> n >> s; long long mini = INT_MAX; for (int i = 0; i < n; i++) { cin >> ara[i]; sum += ara[i]; mini = min(mini, ara[i]); } if (sum < s) cout << -1; else { sum = 0; for (int i = 0; i < n; i++) { sum += ara[i] - mini; } s -= sum; s = max(s, 0LL); mini -= s / n + (s % n != 0); cout << mini << endl; } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.Scanner; public class B1084 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); long S = in.nextLong(); long total = 0; long min = Integer.MAX_VALUE; for (int n=0; n<N; n++) { long v = in.nextLong(); total += v; min = Math.min(min, v); } long answer = -1; if (total >= S) { long overMin = total - N*min; if (overMin >= S) { answer = min; } else { long rest = S - overMin; answer = min - (rest+N-1)/N; } } System.out.println(answer); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#In the name of GOD! n, s = map(int, input().split()) v = list(map(int, input().split())) mn = min(v) sm = cnt = 0 for i in range(n): sm += v[i] cnt += (v[i] - mn) if sm < s: print(-1) elif s <= cnt: print(mn) else: print(mn - ((s - cnt) // n) - min(1, (s - cnt) % n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
I = lambda: map(int, input().split()) n, s = I() V = list(I()) V_min = min(V) V = sum(V) print(-1 if V < s else min(V_min, (V-s)//n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long n, s, v[1234], mn, mx = 1e9, sum; long long liters(long long md) { long long tmp = 0; for (int i = 0; i < n; i++) tmp += max(v[i] - md, 0ll); return tmp; } int main() { cin >> n >> s; for (int i = 0; i < n; i++) cin >> v[i], sum += v[i], mx = min(mx, v[i]); if (sum < s) return puts("-1") * 0; while (mx > mn + 1ll) { long long md = (mn + mx) / 2ll; if (liters(md) >= s) mn = md; else mx = md; } for (long long i = mx; i >= mn; i--) { if (liters(i) >= s) { cout << i << endl; return 0; } } while (1) ; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
def func(s,n,vols): if s > sum(vols): return -1 minInd = 0 for i in range(1,len(vols)): if vols[i] < vols[minInd]: minInd = i minAmt = vols[minInd] for i in range(len(vols)): vols[i] -= minAmt remainingSum = sum(vols) if remainingSum >= s: return minAmt totalLeft = s - remainingSum if totalLeft % n == 0: minAmt -= (totalLeft//n) return minAmt else: reduction = totalLeft//n reduction += 1 return minAmt - reduction n, s = list(map(int,raw_input().split())) arr = list(map(int,raw_input().split())) print(func(s,n,arr))
PYTHON
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import sys input=sys.stdin.readline from collections import defaultdict as dc from collections import Counter from bisect import bisect_right, bisect_left import math from operator import itemgetter from heapq import heapify, heappop, heappush from queue import PriorityQueue as pq n,s=map(int,input().split()) l=list(map(int,input().split())) x=l[0] m=l[0] for i in range(1,n): x+=l[i] if l[i]<m: m=l[i] if s>x: print(-1) else: c=0 for i in range(n): if c>=s: break p=s-c c+=min(p,l[i]-m) if c==s: print(m) else: p=s-c x=p//n y=p%n if y==0: print(m-x) elif x==0: print(m-1) else: print(m-x-1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long int ok(long long int n, long long int k) { if (n % k == 0) return (n / k); else return (n / k) + 1; } long long int ll_sum(long long int n) { long long int sum = 0; while (n != 0) sum += (n % 10), n /= 10; return sum; } long long int str_sum(string s) { long long int sum = 0; for (long long int i = 0; s[i] != '\0'; i++) sum += (long long int)s[i] - 48; return sum; } long long int power(long long int n, long long int m) { long long int prod = 1; for (long long int i = 1; i <= m; i++) prod *= n; return prod; } bool isprime(long long int n) { if (n < 2) return 0; else if (n == 2) return 1; for (long long int i = 3; i * i <= n; i += 2) if (n % i == 0) return 0; return 1; } bool per_sq(long long int n) { double sq = sqrt(n); long long int sq1 = sqrt(n); if (sq - sq1 == 0) return 1; else return 0; } int main() { long long int a, b, c, d, i, j, k, l, m, n, o, p, q, r, u, w, e, f, g, h, t, ct = 0, ct1 = 0, ct2 = 0, ck = 0, ck1 = 0, ck2 = 0, ln, ln1, start, end, mid; long long int a1 = 0, a2 = 0, a3 = 0, a4 = 0, b1 = 0, b2 = 0, b3 = 0, b4 = 0, sum = 0, sum1 = 0, max1 = -1000000000000000005, max2 = -1000000000000000005, min1 = 1000000000000000005, min2 = 1000000000000000005; double x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4; char ch, ch1, ch2; ios ::sync_with_stdio(false); cin.tie(0); ; cin >> n >> m; vector<long long int> v; for (i = 0; i < n; i++) cin >> a, sum += a, v.push_back(a); if (sum < m) { cout << -1 << endl; return 0; } sort(v.begin(), v.end()); sum = 0; for (i = 0; i < n; i++) sum += (v[i] - v[0]); if (sum >= m) { cout << v[0] << endl; return 0; } a = (m - sum); a = ok(a, n); cout << v[0] - a << endl; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; while (cin >> n >> m) { long long num, sum = 0, minx = 1000000001; for (long long i = 0; i < n; i++) { cin >> num; sum += num; if (num < minx) minx = num; } if (sum < m) cout << -1 << endl; else if (minx < (sum - m) / n) { cout << minx << endl; } else cout << (sum - m) / n << endl; } }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long long int INF = 9e18 + 2e17; const int inf = 2e9; const double eps = 1e-10; int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; long long int s; cin >> n >> s; vector<int> v(n); long long int z = 0; for (int i = (int)0; i < (int)n; i++) { cin >> v[i]; z += v[i]; } if (z < s) cout << -1 << endl; else { long long int extra = 0; long long int mn = *min_element((v).begin(), (v).end()); for (int i = (int)0; i < (int)n; i++) if (v[i] > mn) extra += v[i] - mn; s -= extra; int flag = 0; if (s % n) flag = 1; if (s > 0) { s /= n; mn -= s; if (flag) mn--; } cout << mn << endl; } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.StringTokenizer; public class Main { private static long MODULO = 1000245997; private FastScanner in; private PrintWriter out; public static void main(String[] args) { try { (new Main()).run(); } catch (IOException e) { e.printStackTrace(); } } private void run() throws IOException { in = new FastScanner(System.in); out = new PrintWriter(System.out); int n = in.nextInt(); long s = in.nextLong(); int[] v = new int[n]; long sum = 0; long min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { long temp = in.nextLong(); min = Math.min(min, temp); sum+=temp; } s-=sum-n*min; if(s>0) min -=(s+n-1)/n; if(min<0){ out.println(-1); }else{ out.println(min); } out.close(); } private class FastScanner { BufferedReader bufferedReader; StringTokenizer stringTokenizer; FastScanner(InputStream inputStream) { this.bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); } FastScanner(File file) throws IOException { this.bufferedReader = new BufferedReader(new FileReader(file)); } public String nextLine() throws IOException { return bufferedReader.readLine(); } public String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { String line = bufferedReader.readLine(); if (line == null) return null; stringTokenizer = new StringTokenizer(line); } return stringTokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(this.next()); } public long nextLong() throws IOException { return Long.parseLong(this.next()); } public double nextDouble() throws IOException { return Double.parseDouble(this.next()); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import sys def fi(): return int(sys.stdin.readline()) def fi2(): return map(int, sys.stdin.readline().split()) def fi3(): return sys.stdin.readline() def fo(*args): for s in args: sys.stdout.write(str(s)+' ') sys.stdout.write('\n') INF=10**9+7 #main n,s=fi2() k=raw_input().split() k=[int(i) for i in k] if sum(k)<s: fo(-1) elif sum(k)==s: fo(0) else: mini=min(k) x=0 for i in range(n): x+=k[i]-mini if x>=s: fo(mini) else: s=s-x mini=mini-s/n if s%n!=0: mini-=1 fo(mini)
PYTHON
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s=map(int,input().split()) a=[int(i) for i in input().split()] mini=min(a) t=0 for i in range(n): t=t+a[i]-mini if(t>=s): print(mini) elif(s>sum(a)): print("-1") else: rem=s-t if(rem//n==rem/n): k=rem//n print(mini-k) else: k=rem//n+1 print(mini-k)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
a,b=map(int,input().split()) l=list(map(int,input().split())) summa=sum(l) need=0 if summa<b: print(-1) elif summa==b: print(0) else: need=summa-b print(min(min(l),need//a))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = input().split() n = int(n) s = int(s) arr = input().split() for i in range(n): arr[i] = int(arr[i]) minim = min(arr) count = arr.index(minim) max = 0 for i in range(n): max += arr[i] flag = 0 if s > max: print('-1') else: for i in range(n): s -= arr[i] - minim if s <= 0: print(minim) else: print(minim - (s + n - 1) // n)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; /** * @author Andrei Chugunov */ public class Main { private static class Solution { private void solve(FastScanner in, PrintWriter out) { int n = in.nextInt(); long s = in.nextLong(); int[] liters = new int[n]; long sum = 0; for (int i = 0; i < n; i++) { int tmp = in.nextInt(); liters[i] = tmp; sum +=tmp; } if (sum < s) { out.println(-1); } else if (sum == s){ out.println(0); } else { Arrays.sort(liters); int min = liters[0]; if (liters[n - 1] - s >= min) { out.println(min); } else { long current = 0; for (int i = 1; i < n; i++) { current+=(liters[i] - min); } if (current >= s) { out.println(min); } else { long also = s - current; while (also > 0) { also-=n; min--; } out.println(min); } } } } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); Solution solver = new Solution(); solver.solve(in, out); out.close(); } private static class FastScanner { private BufferedReader br; private StringTokenizer st; FastScanner(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream), 32768); st = null; } private String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private long[] readLongArray(int tokens) { long[] ret = new long[tokens]; for (int i = 0; i < tokens; i++) { ret[i] = nextLong(); } return ret; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void println(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s=[int(x) for x in input().split()] a=[int(x) for x in input().split()] sum=0 min=a[0] for i in range(n): if(min>a[i]): min=a[i] sum+=a[i] if(sum<s): print("-1") else: if(sum==s): print("0") else: if(s<=(sum-n*min)): print(min) else: s-=(sum-n*min) ans=(n*min-s)//n print(ans)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int s; cin >> s; long long int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); long long int smallest = arr[0]; long long int sum = 0; for (int i = 0; i < n; i++) { sum = sum + abs(smallest - arr[i]); } if (sum >= s) { cout << smallest; } else { s = s - sum; if (s > n * smallest) { cout << "-1"; } else { long long int factor = s / n; smallest = smallest - factor; if (s % n == 0) cout << smallest; else cout << smallest - 1; } } }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <class T> void na(vector<T>& a, int n) { a = vector<T>(n); for (T& i : a) cin >> i; } template <class T> void printVector(vector<T>& a) { for (T& i : a) cout << i << ' '; cout << '\n'; } template <class T> vector<T> shrink(vector<T>& x) { vector<T> vals = x; sort((vals).begin(), (vals).end()); (vals).resize(unique((vals).begin(), (vals).end()) - (vals).begin()); for (T& i : x) i = upper_bound((vals).begin(), (vals).end(), i) - vals.begin(); return vals; } vector<long long int> a; long long int s; bool f(long long int piv) { long long int cant = 0; for (long long int i : a) { if (i > piv) cant += i - piv; if (i < piv) return 0; } return cant >= s; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(); int n; cin >> n >> s; na(a, n); long long int sol = -1; long long int st = 0, nd = 1e13; while (st <= nd) { long long int piv = (st + nd) / 2; if (f(piv)) sol = piv, st = piv + 1; else nd = piv - 1; } cout << sol << '\n'; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
from sys import stdin, stdout n, s = map(int, stdin.readline().split()) v = list(map(int, stdin.readline().split())) ans = -1 if sum(v) < s: ans = -1 else: mnm = min(v) for i in range(n): if v[i] > mnm: if s - v[i] + mnm <= 0: ans = mnm s = 0 break else: s = s - v[i] + mnm v[i] = mnm if s != 0: ans = mnm - s // n if s % n > 0: ans -= 1 stdout.write("{}".format(ans))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = [int(x) for x in input().split(' ')] v = [int(x) for x in input().split(' ')] if sum(v) < s: ans = -1 else: ans = min(min(v), (sum(v) - s) // n) print(ans)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFLL = 1e18; const int MOD = 1e9 + 7; const int MAXN = 2e5 + 5; long long a[MAXN]; int n; long long s; int main() { scanf("%d", &n); scanf("%lld", &s); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); sort(a, a + n); for (int i = 1; i < n; i++) s -= a[i] - a[0]; if (s <= 0) { printf("%lld\n", a[0]); return 0; } if (n * a[0] < s) { printf("-1\n"); return 0; } a[0] -= (s + n - 1) / n; printf("%lld\n", a[0]); return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; const int mod = 1e9 + 7; long long n, a[1005]; long long s; int ck(long long x) { long long minn = x, now = 0, flag = 0; for (int i = 1; i <= n; i++) { if (a[i] >= x) { now += a[i] - x; } else { flag = 1; break; } } if (now < s || flag) return 0; return 1; } int main() { scanf("%lld%lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } long long l = 0, r = 1e12 + 1, ans = -1; while (l <= r) { long long mid = (l + r) >> 1; if (ck(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } printf("%lld\n", ans); return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s = map(int,raw_input().split(" ")) arr = map(int,raw_input().split(" ")) if sum(arr)<s: print -1 else: minv = min(arr) ss = sum(arr) if ss-minv*n >=s: print minv else: print (ss-s)/n
PYTHON
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
from os import path import sys,time # mod = int(1e9 + 7) # import re from math import ceil, floor,gcd,log,log2 ,factorial from collections import defaultdict ,Counter , OrderedDict , deque from itertools import combinations , groupby , zip_longest,permutations # from string import ascii_lowercase ,ascii_uppercase from bisect import * from functools import reduce from operator import mul maxx = float('inf') #----------------------------INPUT FUNCTIONS------------------------------------------# I = lambda :int(sys.stdin.buffer.readline()) tup= lambda : map(int , sys.stdin.buffer.readline().split()) lint = lambda :[int(x) for x in sys.stdin.buffer.readline().split()] S = lambda: sys.stdin.readline().strip('\n') grid = lambda r :[lint() for i in range(r)] stpr = lambda x : sys.stdout.write(f'{x}' + '\n') star = lambda x: print(' '.join(map(str, x))) localsys = 0 start_time = time.time() if (path.exists('input.txt')): sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w'); #left shift --- num*(2**k) --(k - shift) # input = sys.stdin.readline n ,s = tup() ls = lint() S = sum(ls) if s < s: print(-1) elif s ==S : print(0) else: ls.sort() x =0 for i in range(1 , n): if x <s: p= min(s-x, ls[i]-ls[0]) x+=p ls[i] = ls[i]-p else: break s-=x if s == 0: print(ls[0]) else: print(max(-1,ls[0] - ceil(s/n))) if localsys: print("\n\nTime Elased :",time.time() - start_time,"seconds")
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import sys input = sys.stdin.readline from collections import * def judge(x): s2 = sum(vi-x for vi in v) return s2>=s def binary_search(): l, r = 0, min(v) while l<=r: m = (l+r)//2 if judge(m): l = m+1 else: r = m-1 return r n, s = map(int, input().split()) v = list(map(int, input().split())) if sum(v)<s: print(-1) exit() print(binary_search())
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,v=(int(i)for i in input().split()) a=[int(i)for i in input().split()] mina=min(a) vm=sum(a)-n*mina if v<=vm: print(mina) elif sum(a)>=v>vm: print(mina+(-(v-vm))//n) else: print('-1')
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
from sys import stdin n,s = map(int,stdin.readline().split()) a = map(int,stdin.readline().split()) def solve(x): cur = 0 for i in a: if i < x: return 0 cur += (i-x) if cur >=s: return 1 return 0 if sum(a) < s: print -1 else: lo = 0 hi = 10**12 while hi > lo+1: mid = (lo + hi)/2 if solve(mid): lo = mid else: hi = mid while True: if solve(hi): break hi -=1 print hi
PYTHON
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class KvassAndTheFairNut3 { void solve() { int n = in.nextInt(); long s = in.nextLong(); int[] v = new int[n]; for (int i = 0; i < n; i++) v[i] = in.nextInt(); long min = v[0], sum = v[0]; for (int i = 1; i < n; i++) { min = Math.min(min, v[i]); sum += v[i]; } if (s > sum) { out.println(-1); return; } if (s <= sum - min * n) { out.println(min); return; } s -= sum - min * n; out.println(min - (s + n - 1) / n); } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new KvassAndTheFairNut3().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
from math import ceil import sys n, s = map(int, input().split()) v = list(map(int, input().split())) if s > sum(v): print(-1) sys.exit(0) m = min(v) s -= sum(v) - m * n if s < 0: print(m) sys.exit(0) print(m - ceil(s / n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.*; import java.io.*; import java.math.*; public class Prg4 { PrintWriter pw = new PrintWriter(System.out); Random rnd = new Random(); int a; void run(){ int a = ni(); long s = nl(), all=0; long[] m = new long[a], sum = new long[a]; for(int q=0; q<a; q++){ m[q] = nl(); all+=m[q]; } PyraSort.sort(m); sum[a-1] = m[a-1]; for(int q=a-2; q>=0; q--) sum[q] = sum[q+1]+m[q]; long o = 0; if(all<s) o = -1; else{ long l = 0, r = Integer.MAX_VALUE; for(; l+1!=r;){ long mid = (l+r)/2; int p = Arrays.binarySearch(m, mid); if(p<0) p = -p-1; if(p>=a){ r = mid; continue; } p = left(m, p); if(sum[p]-mid*(a-p)>=s) l = mid; else r = mid; } o = r-1; } pw.print(Math.min(o, m[0])); pw.flush(); } int left(long[] m, int p){ if(p==0 || m[p-1]!=m[p]) return p; if(m[0]==m[p]) return 0; long c = m[p]; int l=0, r = p; for(; !(m[p]==c && m[p-1]!=c);){ p = (r+l)/2; if(m[p]==c) r=p; else l = p; } return p; } static class PyraSort { private static int heapSize; public static void sort(long[] a) { buildHeap(a); while (heapSize > 1) { swap(a, 0, heapSize - 1); heapSize--; heapify(a, 0); } } private static void buildHeap(long[] a) { heapSize = a.length; for (int i = a.length / 2; i >= 0; i--) { heapify(a, i); } } private static void heapify(long[] a, int i) { int l = 2 * i + 2; int r = 2 * i + 1; int largest = i; if (l < heapSize && a[i] < a[l]) { largest = l; } if (r < heapSize && a[largest] < a[r]) { largest = r; } if (i != largest) { swap(a, i, largest); heapify(a, largest); } } private static void swap(long[] a, int i, int j) { a[i] ^= a[j] ^= a[i]; a[j] ^= a[i]; } } public static void main(String[] args) { new Prg4().run(); } InputStream is = System.in; private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private String nline(){ int b = readByte(); StringBuilder sb = new StringBuilder(); while (b!=10) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import sys, math from sys import stdin, stdout rem = 10 ** 9 + 7 inf=10**18 #sys.setrecursionlimit(10 ** 6 + 7) #from resource import *; setrlimit(RLIMIT_STACK, (RLIM_INFINITY, RLIM_INFINITY)) take = lambda: map(int, stdin.readline().split()) from heapq import heappush, heappop, heapify from collections import deque from bisect import * def help(i): p=0 for j in arr: if i>j: return False p+=(j-i) if p>=s: return True return False n,s=take() arr=take() if sum(arr)<s: print -1 exit() left=0 right=10**15 while(left<=right): mid=(left+right+1)/2 if left==right: break check=help(mid) if check==True: left=mid else: right=mid-1 print mid
PYTHON
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1025; long long p[MAX_N]; int main() { long long s, n, sum = 0; scanf("%lld%lld", &n, &s); for (int i = 1; i <= n; ++i) scanf("%lld", &p[i]), sum += p[i]; sort(p + 1, p + 1 + n); if (sum < s) { printf("-1\n"); return 0; } else { long long tmp = 0; for (int i = 1; i <= n; ++i) { tmp += p[i] - p[1]; } if (tmp >= s) { printf("%lld\n", p[1]); return 0; } else { tmp = s - tmp; long long k = (tmp - 1) / n + 1; printf("%lld\n", p[1] - k); } } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) v = list(map(int, input().split())) if sum(v) >= s: c = min(v) for i in range(n): s -= (v[i] - c) v[i] = c if s<=0: print(v[0]) else: print(v[0]-(s+n-1)//n) else: print(-1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long mod = (long long)1e17 + 7; long long n, s, v[1005], sum = 0, mn = mod; long long foo(long long mid) { long long r1 = sum - (n * mid); if (r1 >= s) return 1; else return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> s; for (long long i = 1; i <= n; ++i) { cin >> v[i]; sum += v[i]; mn = min(mn, v[i]); } if (sum < s) { cout << -1; return 0; } long long ans = -1, low = 0, high = mn; while (low <= high) { long long mid = (low + high) / 2; if (foo(mid)) { low = mid + 1; ans = max(ans, mid); } else { high = mid - 1; } } cout << ans; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) tab = sorted([int(x) for x in input().split()]) best = -1 if sum(tab) >= s: s -= sum(tab) - tab[0] * len(tab) best = min(tab[0], tab[0] - (s + len(tab) - 1) // len(tab)) print(best)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s = tuple(map(int, input().split())) min = 2000000000 arr = list(map(int, input().split())) for i in range(n): if (arr[i] < min): min = arr[i] sum = 0 for i in range(n): sum = sum + (arr[i] - min) flag = 0 if (sum >= s): print(min) else: tmp = s - sum tmpMin = tmp // n if(min < tmpMin): tmpMin = min sum = sum + (tmpMin * n) min = min - tmpMin if (sum >= s): print(min) flag = 1 else: while (min > 0): min = min - 1 sum = sum + n if (sum >= s): print(min) flag = 1 break if(flag == 0): print(-1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); long long n; cin >> n; long long s; cin >> s; long long sum = 0; long long mi; cin >> mi; sum += mi; for (long long i = 1; i < n; i++) { long long t; cin >> t; mi = min(mi, t); sum += t; } sum -= s; if (sum < 0) { cout << -1; return 0; } long long ans = sum / n; ans = min(ans, mi); cout << ans; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) a = [int(i) for i in input().split()] x = sum(a) if x >= s: print(min((x - s) // n, min(a))) else: print('-1')
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
def check(sum, n, x, s): if sum - n * x >= s: return True else: return False def main(): n, s = map(int, input().split()) sum = 0 minval = 999999999999 a = input().split() for i in range(n): sum += int(a[i]) minval = min(minval, int(a[i])) if sum < s: print(-1) else: l = 0 r = minval ans = 0 while l <= r: m = (l + r) // 2 if check(sum, n, m, s): l = m + 1 ans = max(ans, m) else: r = m - 1 print(ans) main()
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> int main() { int n = 0; long long s = 0; scanf("%d %lld", &n, &s); long long kegs[n]; memset(kegs, 0, sizeof(kegs)); long long least = -1; for (int i = 0; i < n; i++) { int v = 0; scanf("%d", &v); kegs[i] = v; least = (v < least || least == -1) ? v : least; } long long container = 0; long long total = 0; for (long long x : kegs) { container += (x - least); total += x; } if (s > total) { printf("%d\n", -1); return 0; } while (container < s) { container += n; least--; } printf("%lld\n", least); }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e18; const int inf = 1e9; const int MOD = 1e9 + 7; const int nax = 1000000 + 10; long long int n, arr[nax]; int main() { long long int s; cin >> n >> s; long long int mini = INF, tot = 0, left = 0; for (int i = 1; i <= n; i++) cin >> arr[i], tot += arr[i], mini = min(mini, arr[i]); for (int i = 1; i <= n; i++) left += abs(arr[i] - mini); if (tot < s) return cout << -1, 0; if (left > s) return cout << mini, 0; int dec = (s - left + n - 1) / n; cout << mini - dec << "\n"; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 5; long long v[maxn]; int main() { long long n, s; scanf("%lld%lld", &n, &s); long long sum = 0, minn = 1e9 + 7, x; for (long long i = 0; i < n; i++) scanf("%lld", &x), sum += x, minn = min(minn, x); if (sum < s) printf("-1\n"); else { long long ans = (sum - s) / n; printf("%d\n", min(minn, ans)); } }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; @SuppressWarnings("unused") public class Main { private FastScanner in; private PrintWriter out; final int MOD = (int)1e9+7; long ceil(long a, long b){return (a + b - 1) / b;} long gcd(long a, long b){return b == 0 ? a : gcd(b, a % b);} long lcm(long a, long b){return a / gcd(a, b) * b; /*γ‚ͺーバーフローに注意*/} void solve() { int n = in.nextInt(); long s = in.nextLong(); long[] v = new long[n]; long sum = 0, min = Long.MAX_VALUE; for(int i = 0; i < n; i++){ v[i] = in.nextLong(); sum += v[i]; min = Math.min(min, v[i]); } if(sum < s){ out.println("-1"); return; } for(int i = 0; i < n; i++){ s -= (v[i] - min); v[i] = min; } if(s <= 0){ out.println(min); }else{ out.println((min - 1 - ((s - 1) / n))); } } //end solve public static void main(String[] args) { new Main().m(); } private void m() { in = new FastScanner(System.in); out = new PrintWriter(System.out); /* try { String path = "output.txt"; out = new PrintWriter(new BufferedWriter(new FileWriter(new File(path)))); }catch (IOException e){ e.printStackTrace(); } */ solve(); out.flush(); in.close(); out.close(); } static class FastScanner { private Reader input; public FastScanner() {this(System.in);} public FastScanner(InputStream stream) {this.input = new BufferedReader(new InputStreamReader(stream));} public void close() { try { this.input.close(); } catch (IOException e) { e.printStackTrace(); } } public int nextInt() {return (int) nextLong();} public long nextLong() { try { int sign = 1; int b = input.read(); while ((b < '0' || '9' < b) && b != '-' && b != '+') { b = input.read(); } if (b == '-') { sign = -1; b = input.read(); } else if (b == '+') { b = input.read(); } long ret = b - '0'; while (true) { b = input.read(); if (b < '0' || '9' < b) return ret * sign; ret *= 10; ret += b - '0'; } } catch (IOException e) { e.printStackTrace(); return -1; } } public double nextDouble() { try { double sign = 1; int b = input.read(); while ((b < '0' || '9' < b) && b != '-' && b != '+') { b = input.read(); } if (b == '-') { sign = -1; b = input.read(); } else if (b == '+') { b = input.read(); } double ret = b - '0'; while (true) { b = input.read(); if (b < '0' || '9' < b) break; ret *= 10; ret += b - '0'; } if (b != '.') return sign * ret; double div = 1; b = input.read(); while ('0' <= b && b <= '9') { ret *= 10; ret += b - '0'; div *= 10; b = input.read(); } return sign * ret / div; } catch (IOException e) { e.printStackTrace(); return Double.NaN; } } public char nextChar() { try { int b = input.read(); while (Character.isWhitespace(b)) { b = input.read(); } return (char) b; } catch (IOException e) { e.printStackTrace(); return 0; } } public String nextStr() { try { StringBuilder sb = new StringBuilder(); int b = input.read(); while (Character.isWhitespace(b)) { b = input.read(); } while (b != -1 && !Character.isWhitespace(b)) { sb.append((char) b); b = input.read(); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); return ""; } } public int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public int[] nextIntArrayDec(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt() - 1; } return res; } public int[] nextIntArray1Index(int n) { int[] res = new int[n + 1]; for (int i = 0; i < n; i++) { res[i + 1] = nextInt(); } return res; } public long[] nextLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } public long[] nextLongArrayDec(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong() - 1; } return res; } public long[] nextLongArray1Index(int n) { long[] res = new long[n + 1]; for (int i = 0; i < n; i++) { res[i + 1] = nextLong(); } return res; } public double[] nextDoubleArray(int n) { double[] res = new double[n]; for (int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.stream.IntStream; import java.util.Arrays; import java.util.stream.LongStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UncheckedIOException; import java.util.OptionalLong; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author mikit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; LightScanner in = new LightScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BKvassAndTheFairNut solver = new BKvassAndTheFairNut(); solver.solve(1, in, out); out.close(); } static class BKvassAndTheFairNut { public void solve(int testNumber, LightScanner in, PrintWriter out) { int n = in.ints(); long s = in.longs(); long[] v = in.longs(n); long min = Arrays.stream(v).min().orElse(1); long sum = Arrays.stream(v).sum(); if (s > sum) { out.println(-1); } else { s -= (sum - min * n); if (s <= 0) { out.println(min); } else { out.println(min - ((s + n - 1L) / n)); } } } } static class LightScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public LightScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public String string() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new UncheckedIOException(e); } } return tokenizer.nextToken(); } public int ints() { return Integer.parseInt(string()); } public long longs() { return Long.parseLong(string()); } public long[] longs(int length) { return IntStream.range(0, length).mapToLong(x -> longs()).toArray(); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> void IO(bool inp, bool out) { if (inp) freopen("k.inp", "r", stdin); if (out) freopen("k.out", "w", stdout); } using namespace std; short n; long long a[1001], s; bool kt(long long u) { long long t = 0; for (short i = 1; i <= n; ++i) if (a[i] < u) return false; else t += a[i] - u; if (t >= s) return true; else return false; } int main() { IO(0, 0); scanf("%hd%I64d", &n, &s); for (short i = 1; i <= n; ++i) scanf("%I64d", &a[i]); long long l = 0, r = 1000000000, mid, kq = -1; while (l <= r) { mid = (l + r) >> 1; if (kt(mid)) kq = mid, l = mid + 1; else r = mid - 1; } printf("%I64d", kq); }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; bool check(vector<long long int> a, long long int mid, long long int k, long long int &ans) { long long int n = a.size(); long long int temp = 0; long long int c = a[n - 1]; ; for (long long int i = n - 1; i >= 0; i--) { if (a[i] >= mid) { temp += (a[i] - mid); c = min(c, mid); } } if (temp >= k) { ans = max(ans, min(a[0], c)); return true; } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, k; cin >> n >> k; vector<long long int> a(n); long long int sum = 0; for (long long int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } if (k > sum) { cout << -1; return 0; } sort(a.begin(), a.end()); long long int s = 0; long long int e = 1000000000; long long int ans = 0; while (s <= e) { long long int mid = s + (e - s) / 2; if (check(a, mid, k, ans)) { s = mid + 1; } else { e = mid - 1; } } cout << ans; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s=[int(x) for x in input().split(' ')] z=[int(x) for x in input().split(' ')] z.sort() if sum(z)<s: print(-1) elif n==1: print(z[0]-s) else: a,f=0,0 for x in z[1:]: a+=x-z[0] if a>=s: f=1 break if f: print(z[0]) else: s-=a if s%n==0: print(z[0]-s//n) else: print(z[0]-s//n-1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> int main() { int n; long long s; long long a[1005]; scanf("%d%lld", &n, &s); int i, j; long long min = 1e9; long long Sum = 0; int flag = 0; for (i = 0; i < n; i++) { scanf("%lld", &a[i]); if (a[i] < min) min = a[i]; Sum += a[i]; } if (s > Sum) flag = 2; long long sum = 0; for (i = 0; i < n; i++) { sum += a[i] - min; if (sum > s) { flag = 1; break; } } if (flag == 2) printf("-1\n"); else if (flag) printf("%lld\n", min); else { s = s - sum; if (s % n == 0) { min = min - s / n; } else { min = min - s / n - 1; } printf("%lld\n", min); } }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> int n, a[1001], mn = 1e9; long long s, cnt = 0; signed main() { scanf("%d %lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (mn > a[i]) mn = a[i]; } for (int i = 1; i <= n; i++) { cnt += a[i] - mn; } printf("%lld", (cnt >= s ? mn : (mn >= (s - cnt) / n + ((s - cnt) % n == 0 ? 0 : 1) ? mn - (s - cnt) / n - ((s - cnt) % n == 0 ? 0 : 1) : -1))); }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import sys from math import ceil def main(): n, s = map(int, sys.stdin.readline().split()) arr = list(map(int, sys.stdin.readline().split())) if s > sum(arr): print(-1, sep=' ', end='\n', file=sys.stdout, flush=False) else: count = 0 arr.sort(reverse=True) for i in range(n-1): if count == s: break if count+(arr[i]-arr[-1]) <= s: count += arr[i]-arr[-1] arr[i] = arr[-1] elif count+(arr[i]-arr[-1]) > s: count = s arr[i] -= (s-count) if count == s: print(arr[-1]) else: factor = (s-count)/n print(arr[-1]-ceil(factor), sep=' ', end='\n', file=sys.stdout, flush=False) if __name__ == '__main__': main()
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) v = list(map(int, input().split())) sm = sum(v) sm -= s if sm < 0: print(-1) else: mn = min(v) r = min(mn, sm // n) print(r)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; void Fast_Read_Out() { ios_base::sync_with_stdio(0); cin.tie(), cout.tie(); } void Random() { unsigned int seed; asm("rdtsc" : "=A"(seed)); srand(seed); } unsigned int Time() { unsigned int time = clock() / 1000.00; return time; } const int inf = int(1e9) + 123; long long a[1001]; int main() { Random(); Fast_Read_Out(); int n; long long s; cin >> n >> s; long long sum = 0; for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i]; if (s > sum) cout << -1 << endl, exit(0); sort(a + 1, a + n + 1); for (int i = 2; i <= n && s > 0; i++) s -= (a[i] - a[1]); if (s <= 0) cout << a[1], exit(0); if (s % n > 0) cout << a[1] - s / n - 1; else cout << a[1] - s / n << endl; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long a[100000]; int main() { long long n, s, sum = 0, ans, minn = 10000000000000; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; if (a[i] < minn) minn = a[i]; } if (sum < s) { cout << "-1" << endl; return 0; } long long temp = sum - s; temp = temp / n; if (minn < temp) ans = minn; else ans = temp; cout << ans << endl; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; stack<long long int> st; long long int binarySearch(long long int l, long long int r, long long int a[], long long int k) { if (l <= r) { long long int mid = l + (r - l) / 2; if (a[mid] == k) return mid; else if (a[mid] > k) return binarySearch(l, mid - 1, a, k); else return binarySearch(mid + 1, r, a, k); } else return -1; } void solve() { long long int n, s; cin >> n >> s; long long int a[n]; long long int sum = 0; for (long long int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } long long int v = *min_element(a, a + n); if (sum < s) cout << "-1"; else { for (long long int i = 0; i < n; i++) { s -= a[i] - v; } if (s <= 0) cout << v; else cout << v - (s + n - 1) / n; } return; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; while (t--) { solve(); } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
//package practice_codes; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.stream.Collectors; //import codes.Reference.i; public class KvassAndTheFairNut { public static void main(String[] args) { // TODO Auto-generated method stub long n = i.la()[1]; long a[]=i.la(); long min = Long.MAX_VALUE; for(int i=0;i<a.length;i++) { min=Math.min(min, a[i]); } long sum=0; long addn = 0; for(int i=0;i<a.length;i++) { addn+=a[i]; } if(addn<n) { System.out.println(-1); } else if(addn==n) System.out.println(0); else { long ans=0; for(int i=0;i<a.length;i++) { long diff = (a[i]-min); sum+=diff; if(sum>=n) { ans=min; break; } a[i]-=diff; } if(ans==0) { long val = (long) Math.ceil((n-sum)/(double)a.length); ans=a[0]-val; System.out.println(ans); } else System.out.println(ans); } } static private class i { //input taking methods static BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); public static String[] sa() { try { return b.readLine().split(" "); } catch (IOException e) { e.printStackTrace(); } return null; } public static long l() { try { return Long.parseLong(b.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } public static String s() { try { return b.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static long[] la() { return Arrays.stream(sa()).parallel().mapToLong(Long::parseLong).toArray(); } public static ArrayList<Long> lal() { return (ArrayList<Long>)Arrays.stream(la()).parallel().boxed().collect(Collectors.toList()); } public static StringBuilder so(String a) { return new StringBuilder(a); } } /* HashMap<String,Integer> map = new HashMap<String,Integer>(); if(!map.containsKey(key)) { map.put(key,value); } else { map.replace(key,map.get(key)); } */ static private class algo{ public static boolean isPrime(int a) { for(int i=2;i<a;i++) { if(a%i==0) { return false; } } return true; } static String reverse(String s) { return i.so(s).reverse().toString(); } static String replace(String s,int st,int e,String repl) { return i.so(s).replace(st, e, repl).toString(); } static String insert(String s,int pos,String ins) { return i.so(s).insert(pos, ins).toString(); } static long sod(long n) { long sum; for (sum = 0; n > 0; sum += n % 10, n /= 10); return sum; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a*b)/gcd(a, b); } static long sumOfSquares(long[] a) { return (long) Arrays.stream(a).mapToDouble(i->i*i).sum(); } //use of filter //Arrays.stream(a).filter(k->k==1).count(); // array variable->condition returning boolean . lot fof functionsa re available static String[] generateBinSeq(int cnt) { int loopvar=(int) Math.pow(2, cnt); String a[][] = new String[cnt+1][loopvar]; for(int i=1;i<=cnt;i++) { int temp = (int) Math.pow(2, i); for(int j=0;j<temp;j++) { if(i==1 || i==2) { if(i==1) { a[i][0]="0"; a[i][1]="1"; continue; } else { a[2][0]="00";a[2][1]="01";a[2][2]="10";a[2][3]="11"; continue; } } else { if(j<(temp/2)) { a[i][j] = a[i-1][j]+"0"; } else { a[i][j] = a[i-1][j%((temp+1)/2)]+"1"; } } } } return a[cnt]; } // USe this snippet for calling // int ndx = algo.bs(a,key,0,a.length-1); // if(ndx<0) // ndx=0; // if(ndx==a.length) // ndx=a.length-1; static public int bs(long a[],long key,int s,int e) { int m = (s+e)/2; if(s>e) return s; if(a[m]==key) return m; else if(a[m]>key) return bs(a,key,s,m-1); else return bs(a,key,m+1,e); } // USe this snippet for calling // int ndx = bsa(al,key,0,al.size()-1); // if(ndx<0) // ndx=0; // if(ndx==al.size()) // ndx=al.size()-1; static public int bsa(ArrayList<Long> a,long key,int s,int e) { int m = (s+e)/2; if(s>e) return s; if(a.get(m)==key) return m; else if(a.get(m)>key) return bsa(a,key,s,m-1); else return bsa(a,key,m+1,e); } static public long fact(long a) { long i,fact=1; long number=a; for(i=1;i<=number;i++){ fact=fact*i; } return fact; } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) throws Exception { //Scanner in = new Scanner(System.in); PrintWriter pw=new PrintWriter(System.out); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); InputReader in=new InputReader(System.in); int n=in.nextInt(); long s=in.nextLong(); long min=Long.MAX_VALUE; long sum=0; long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=in.nextLong(); min=Math.min(min,arr[i]); sum+=arr[i]; } if(sum<s){ pw.println(-1); }else{ for(int i=0;i<n && s>0;i++){ if(arr[i]!=min){ s-=(arr[i]-min); } } if(s<=0)pw.println(min); else{ long cnt=0; cnt=s/(long)n; if(s%(long)n!=0)cnt++; pw.println(min-cnt); } } pw.flush(); pw.close(); } static class Pair implements Comparable<Pair>{ int x; int y; Pair(int a,int b){ x=a; y=b; } @Override public int compareTo(Pair t){ if(x>t.x)return 1; else if(x==t.x){ if(y>=t.y)return 1; else return -1; } else return -1; } @Override public boolean equals(Object obj){ if(this==obj)return true; Pair that=(Pair)obj; if(this.x==that.x && this.y==that.y)return true; return false; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Try { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static long mod=1000000007; static BigInteger bigInteger = new BigInteger("1000000007"); static int n = (int)1e6; static boolean[] prime; static ArrayList<Integer> as; static HashSet<Integer> hs; static void sieve() { Arrays.fill(prime , true); prime[0] = prime[1] = false; for(int i = 2 ; i * i <= n ; ++i) { if(prime[i]) { for(int k = i * i; k< n ; k+=i) { prime[k] = false; } } } } static PrintWriter w = new PrintWriter(System.out); static char [][]sol; static int t2 = 0; static int t3 = 0; public static void main(String[] args) { InputReader sc = new InputReader(System.in); //PrintWriter w = new PrintWriter(System.out); prime = new boolean[n + 1]; sieve(); prime[1] = false; /* as = new ArrayList<>(); for(int i=2;i<=1000000;i++) { if(prime[i]) as.add(i); } */ /* long a = sc.nl(); BigInteger ans = new BigInteger("1"); for (long i = 1; i < Math.sqrt(a); i++) { if (a % i == 0) { if (a / i == i) { ans = ans.multiply(BigInteger.valueOf(phi(i))); } else { ans = ans.multiply(BigInteger.valueOf(phi(i))); ans = ans.multiply(BigInteger.valueOf(phi(a / i))); } } } w.println(ans.mod(bigInteger)); */ // MergeSort ob = new MergeSort(); // ob.sort(arr, 0, arr.length-1); // Student []st = new Student[x]; // st[i] = new Student(i,d[i]); //Arrays.sort(st,(p,q)->p.diff-q.diff); int x = sc.ni(); long s = sc.nl(); int []a = sc.nia(x); long sum = 0; for(int i=0;i<x;i++) { sum += (long) a[i]; } if(sum < s) w.println("-1"); else { MergeSort ob = new MergeSort(); ob.sort(a, 0, a.length-1); long temp = 0; for(int i=1;i<x;i++) { temp += (long) (a[i]-a[0]); } if(temp >= s) w.println(a[0]); else { long p = s-temp; long p2 = p % x; long p1 = p / x; if(p2==0) w.println(a[0] - p1); else w.println(a[0] - (p1+1)); } } w.close(); } static int digit(long x) { int p = 0; while(x > 0) { x /= 10; p++; } return p; } public static String f(String a,int b){ if(b==0) return ""; else if(b==1) return a; else{ if(b%2==0) return f(a,b/2)+f(a,b/2); else return a+f(a,b/2)+f(a,b/2); } } static class Student { int first; int sec; Student(int first,int sec) { this.first = first; this.sec = sec; } } public static long modMultiply(long one, long two) { return (one % mod * two % mod) % mod; } static long fx(int m) { long re = 0; for(int i=1;i<=m;i++) { re += (long) (i / gcd(i,m)); } return re; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static long phi(long nx) { // Initialize result as n double result = nx; // Consider all prime factors of n and for // every prime factor p, multiply result // with (1 - 1/p) for (int p = 0; as.get(p) * as.get(p) <= nx; ++p) { // Check if p is a prime factor. if (nx % as.get(p) == 0) { // If yes, then update n and result while (nx % as.get(p) == 0) nx /= as.get(p); result *= (1.0 - (1.0 / (double) as.get(p))); } } // If n has a prime factor greater than sqrt(n) // (There can be at-most one such prime factor) if (nx > 1) result *= (1.0 - (1.0 / (double) nx)); return (long)result; //return(phi((long)result,k-1)); } public static void primeFactors(long n) { //hs = new HashSet<>(); // Print the number of 2s that divide n long temp = n; //System.out.print(2 + " "); //n /= 2; // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 2; (long)i *(long)i<= n; i++) { // While i divides n, print i and divide n if (n%i == 0) { // System.out.print(i + " "); hs.add(i); //n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 /* if (n >= 2 && n*n <= temp) { hs.add(n); } */ } static int digitsum(int x) { int sum = 0; while(x > 0) { int temp = x % 10; sum += temp; x /= 10; } return sum; } static int countDivisors(int n) { int cnt = 0; for (int i = 1; i*i <=n; i++) { if (n % i == 0) { // If divisors are equal, // count only one if (n / i == i) cnt++; else // Otherwise count both cnt = cnt + 2; } } return cnt; } static boolean isprime(long n) { if(n == 2) return true; if(n == 3) return true; if(n % 2 == 0) return false; if(n % 3 == 0) return false; long i = 5; long w = 2; while(i * i <= n) { if(n % i == 0) return false; i += w; w = 6 - w; } return true; } static long log2(long value) { return Long.SIZE-Long.numberOfLeadingZeros(value); } public static int lowerBound(int[] array, int length, int value) { int low = 0; int high = length-1; while (low < high) { final int mid = (low + high) / 2; if (value <= array[mid]) { high = mid; } else { low = mid + 1; } } return low; } public static int upperBound(int[] array, int length, int value) { int low = 0; int high = length-1; while (low < high) { final int mid = (low + high) / 2; if (value >= array[mid]) { low = mid + 1; } else { high = mid; } } return low; } static boolean binarysearch(int []arr,int p,int n) { //ArrayList<Integer> as = new ArrayList<>(); //as.addAll(0,at); //Collections.sort(as); boolean re = false; int st = 0; int end = n-1; while(st <= end) { int mid = st + (end-st)/2; if(p > arr[mid]) { st = mid+1; } else if(p < arr[mid]) { end = mid-1; } else if(p == arr[mid]) { re = true; break; } } return re; } /* Java program for Merge Sort */ static class MergeSort { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } /* A utility function to print array of size n */ } public static int ip(String s){ return Integer.parseInt(s); } public static String ips(int s){ return Integer.toString(s); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) a = [int(i) for i in input().split()] if sum(a) < s: print(-1) exit() if sum(a) - n * min(a) >= s: print(min(a)) exit() s -= sum(a) - n * min(a) if s % n == 0: print(min(a) - s // n) else: print(min(a) - s // n - 1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,m=map(int,input().split()) l=list(map(int,input().split())) s=0 d=min(l) for i in l: s=s+i if(s<m): print(-1) else: print(min(d,(s-m)//n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) us = input().split() sum = 0 m = 10**9 + 1 for u in us: sum += int(u) if int(u) < m: m = int(u) if sum<s: print(-1) elif (sum-s)//n < m: print((sum-s)//n) else: print(m)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
a,b=map(int,input().split()) z=list(map(int,input().split())) s=sum(z);r=min(z) if b>s:print(-1) elif s-r*a>=b:print(r) else: f=b-(s-r*a) print((r-(f//a))-(0 if f%a==0 else 1))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.PrintWriter; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Scanner; public class BRound526 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); new BRound526 ().solve(in,out); in.close(); out.close(); } class Bucket { int id, val; public Bucket(int a, int b) { id = a; val = b; } } private void solve(Scanner in, PrintWriter out) { int N = in.nextInt(); long S = in.nextLong(); PriorityQueue<Long> pq = new PriorityQueue<>(); long smallest = Long.MAX_VALUE; for(int i=0; i<N; i++) { long x = in.nextLong(); pq.add(-x); smallest = Math.min(smallest, x); } while(S>0) { long vol = -pq.poll(); if(vol==smallest) break; long toSubtract = vol - smallest; S -= toSubtract; pq.add(-(vol - toSubtract)); } long answer = 0; if(S>0) { long need = (long) Math.ceil((double)S/(double)N); long allAre = smallest; if(allAre >= need) { answer = allAre-need; } else { answer = -1; } } else { answer = smallest; } System.out.println(answer); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import math, sys n, s = map(int, input().split()) minval = math.inf c = 0 l = list(map(int, input().split())) add = sum(l) if add < s: print(-1) sys.exit() for i in range(0, n): v = l[c] minval = min(v , minval) c+=1 var = (add - s) / n print(int(min(var , minval)))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s=map(int,input().split()) x=list(map(int,input().split())) m=-1 if sum(x)>=s: x=sorted(x,reverse=True) m=x[n-1] for i in x: if s<=0: break if i==m: import math m-=math.ceil(s/n) break else: s-=i-m print(m)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n = list(map(int,input().split())) s = n[1] n = n[0] a = list(map(int,input().split())) a = sorted(a,reverse = True) k = min(a) su = 0 if s > sum(a): print(-1) else: for i in range(len(a)): if a[i] > k: su += (a[i]-k) a[i] = k if su >= s: break if su < s: k = (n*k-(s-su))//n print(k) else: print(k)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
from math import ceil n,s=map(int,input().split()) l=[int(i) for i in input().split()] sm=sum(l) m=min(l) if s>sm: print(-1) exit() print(min(m,(sm-s)//n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.Arrays; import java.util.Scanner; public class KvassandtheFairNut { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); long s = scan.nextLong(); long[] a = new long[n]; long sum = 0; for (int i = 0; i < n; i++) { a[i] = scan.nextInt(); sum += a[i]; } if (sum < s) { System.out.println("-1"); } else { Arrays.sort(a); long r = a[0]; sum = 0; for (int i = 0; i < n; i++) { sum += a[i] - r; } if (sum >= s) { System.out.println(r); } else { sum = s - sum; sum =(long) Math.ceil((double)sum/n); System.out.println(r-sum); } } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n, s, sum = 0; cin >> n >> s; vector<long long int> u(n); for (long long int i = 0; i < n; i++) { cin >> u[i]; sum += u[i]; } if (sum < s) { cout << "-1" << endl; return 0; } sort(u.begin(), u.end()); for (long long int i = n - 1; i > 0; i--) { s -= (u[i] - u[0]); if (s <= 0) { cout << u[0] << endl; return 0; } } long long int d = s / n, r; if (s % n == 0) r = 0; else r = 1; cout << u[0] - d - r << endl; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
def get_ans(n, s, a): sum = 0 Min = int(1e9 + 1) for elem in a: sum += elem Min = min(Min, elem) if sum < s: return -1 k = (sum - s) // n return min(Min, k) n, s = map(int, input().split()) a = list(map(int, input().split())) print(get_ans(n, s, a))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.*; public class KvassAndTheFruitNut{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long s = sc.nextLong(); long[] arr = new long[n+1]; long sum = 0; for(int i=1; i<n+1; i++){ arr[i] = sc.nextLong(); sum += arr[i]; } long min =0; boolean flag = true; if(sum < s){ System.out.println(-1); flag = false; } else{ while(s > 0){ Arrays.sort(arr); min = arr[1]; long a = arr[n] - arr[1]; if(a==0){ if(n>s){ min = arr[1] - 1; break; } else{ min = arr[1] - (long)Math.ceil((double)((double)s/(double)n)); break; } } else{ if(a >= s){ arr[n] = arr[n] - s; min = arr[1]; break; } else{ arr[n] = arr[n] - a; s = s - a; } } } } if(flag){ System.out.println(min); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; public class Main { private static final int MAX = Integer.MAX_VALUE; private static final int MIN = Integer.MIN_VALUE; public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream); Scanner scanner = new Scanner(System.in); int n = in.nextInt(); long s = in.nextLong(); int [] a = new int[n]; long sum = 0; for (int i = 0; i < a.length; i++) { a[i] = in.nextInt(); sum+=a[i]; } if(sum < s) { out.println(-1); } else { int max = a[0]; for(int i : a) { max = Math.max(i,max); } int lo = 1; int hi = max; while (lo<=hi) { int med = lo + (hi-lo)/2; if (can(a,med,s)) { lo = med + 1; } else { hi = med - 1; } } out.println(hi); } out.flush(); } private static boolean can(int [] a, int min, long s) { long sum = 0; for (int i = 0; i < a.length; i++) { if(a[i] < min) return false; sum+=a[i]-min; } return sum>=s; } private static int binarySearch(int [] a, int min) { int lo = 0; int hi = a.length - 1; while(lo<=hi) { int med = lo+(hi-lo)/2; if(min > a[med]) { lo = med + 1; } else { hi = med - 1; } } return lo; } private static int [] freq(char [] c) { int [] f = new int[26]; for(char cc : c) { f[cc-'a']++; } return f; } private static void shuffle(int [] a) { for (int i = 0; i < a.length; i++) { int index = (int)(Math.random()*(i+1)); int temp = a[index]; a[index] = a[i]; a[i] = temp; } } private static int lowerBound(int [] a, int target) { int lo = 0; int hi = a.length - 1; while (lo<=hi) { int med = lo + (hi-lo)/2; if(target >= a[med]) { lo = med + 1; } else { hi = med - 1; } } return lo; } static int numberOfSubsequences(String a, String b) { int [] dp = new int[b.length()+1]; dp[0] = 1; for (int i = 0; i < a.length(); i++) { for (int j = b.length() - 1; j >=0;j--) { if(a.charAt(i) == b.charAt(j)) { dp[j+1]+=dp[j]; } } } return dp[dp.length - 1]; } static long count(String a, String b) { int m = a.length(); int n = b.length(); // Create a table to store // results of sub-problems long lookup[][] = new long[m + 1][n + 1]; // If first string is empty for (int i = 0; i <= n; ++i) lookup[0][i] = 0; // If second string is empty for (int i = 0; i <= m; ++i) lookup[i][0] = 1; // Fill lookup[][] in // bottom up manner for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { // If last characters are // same, we have two options - // 1. consider last characters // of both strings in solution // 2. ignore last character // of first string if (a.charAt(i - 1) == b.charAt(j - 1)) lookup[i][j] = lookup[i - 1][j - 1] + lookup[i - 1][j]; else // If last character are // different, ignore last // character of first string lookup[i][j] = lookup[i - 1][j]; } } return lookup[m][n]; } } class Point { private int x; private int y; public int getX() { return x; } public int getY() { return y; } public Point(int x,int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return x == point.x && y == point.y; } @Override public int hashCode() { return Objects.hash(x, y); } } class InputReader extends BufferedReader { StringTokenizer tokenizer; public InputReader(InputStream inputStream) { super(new InputStreamReader(inputStream), 32768); } public InputReader(String filename) { super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename))); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(readLine()); } catch (IOException e) { throw new RuntimeException(); } } return tokenizer.nextToken(); } public Integer nextInt(){ return Integer.valueOf(next()); } public Long nextLong() { return Long.valueOf(next()); } public Double nextDouble() { return Double.valueOf(next()); } static class OutputWriter extends PrintWriter { public OutputWriter(OutputStream outputStream) { super(outputStream); } public OutputWriter(Writer writer) { super(writer); } public OutputWriter(String filename) throws FileNotFoundException { super(filename); } public void close() { super.close(); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import math if __name__ == '__main__': n,s = [int(x) for x in input().split()] v = [int(x) for x in input().split()] v.sort() if sum(v) < s : print(-1) else : ans = -1 if s <= sum(v) - min(v)*n: print(min(v)) else : s -= sum(v) - min(v)*n v = [min(v)]*n if s%n == 0 : print(v[0] - s//n) else : print(v[0] - s//n - 1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#!/usr/bin/python3 n, s = map(int, input().split()) values = list(map(int, input().split())) m_v = min(values) total = sum(values) #no way if total < s: print ("{}".format(-1)) exit(0) if total - n * m_v >= s: # more than now print ("{}".format(m_v)) exit(0) # we have to decrease by rest all amount. lets count how many we loose rest = s - (total - n * m_v) r_p = rest % n r_q = rest // n if r_p: r_q += 1 print ("{}".format(m_v - r_q))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import heapq as h n, s = map(int, input().split()) v = list(map(int, input().split())) v.sort() if sum(v) < s: print(-1) else: h.heapify(v) i = -1 while i >= -n and s: s -= min(s, v[i] - v[0]) v[i] -= min(s, v[i] - v[0]) i -= 1 v[0] -= s // n + (1 if s % n else 0) print(v[0])
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s = map(int,input().split()) arr = list(map(int,input().split())) mn= min(arr) sm = sum(arr) if sm < s: print(-1) elif(sm == s): print(0) else: avg = (sm - s)//n print(min(mn,avg))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, s, a[1001], somma = 0, minimo = 1000000001; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; somma += a[i]; minimo = min(minimo, a[i]); } if (somma < s) cout << -1; else cout << min(minimo, (somma - s) / n); return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) arr = [int(x) for x in input().split()] if sum(arr) < s: print("-1") exit() mn = min(arr) extra = sum([x-mn for x in arr]) if extra >= s: print(mn) else: print(mn - ((s-extra+n-1)//n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
/** * Date: 11 Dec, 2018 * Link: * * @author Prasad-Chaudhari * @linkedIn: https://www.linkedin.com/in/prasad-chaudhari-841655a6/ * @git: https://github.com/Prasad-Chaudhari */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; public class newProgram1 { public static void main(String[] args) throws IOException { // TODO code application logic here FastIO in = new FastIO(); int n = in.ni(); long s = in.nl(); int a[] = in.gia(n); long min = Long.MAX_VALUE; long temp = s; for (int i : a) { min = Math.min(i, min); temp -= i; } if (temp > 0) { System.out.println("-1"); return; } for (int i : a) { s -= (i - min); } if (s <= 0) { System.out.println(min); } else { System.out.println((min * n - s) / n); } } static class FastIO { private final BufferedReader br; private final BufferedWriter bw; private String s[]; private int index; private StringBuilder sb; public FastIO() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8")); s = br.readLine().split(" "); sb = new StringBuilder(); index = 0; } public int ni() throws IOException { return Integer.parseInt(nextToken()); } public double nd() throws IOException { return Double.parseDouble(nextToken()); } public long nl() throws IOException { return Long.parseLong(nextToken()); } public String next() throws IOException { return nextToken(); } public String nli() throws IOException { try { return br.readLine(); } catch (IOException ex) { } return null; } public int[] gia(int n) throws IOException { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public int[] gia(int n, int start, int end) throws IOException { validate(n, start, end); int a[] = new int[n]; for (int i = start; i < end; i++) { a[i] = ni(); } return a; } public double[] gda(int n) throws IOException { double a[] = new double[n]; for (int i = 0; i < n; i++) { a[i] = nd(); } return a; } public double[] gda(int n, int start, int end) throws IOException { validate(n, start, end); double a[] = new double[n]; for (int i = start; i < end; i++) { a[i] = nd(); } return a; } public long[] gla(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public long[] gla(int n, int start, int end) throws IOException { validate(n, start, end); long a[] = new long[n]; for (int i = start; i < end; i++) { a[i] = nl(); } return a; } public int[][] gg(int n, int m) throws IOException { int adja[][] = new int[n + 1][]; int from[] = new int[m]; int to[] = new int[m]; int count[] = new int[n + 1]; for (int i = 0; i < m; i++) { from[i] = ni(); to[i] = ni(); count[from[i]]++; count[to[i]]++; } for (int i = 0; i <= n; i++) { adja[i] = new int[count[i]]; } for (int i = 0; i < m; i++) { adja[from[i]][--count[from[i]]] = to[i]; adja[to[i]][--count[to[i]]] = from[i]; } return adja; } public void print(String s) throws IOException { bw.write(s); } public void println(String s) throws IOException { bw.write(s); bw.newLine(); } private String nextToken() throws IndexOutOfBoundsException, IOException { if (index == s.length) { s = br.readLine().split(" "); index = 0; } return s[index++]; } private void validate(int n, int start, int end) { if (start < 0 || end >= n) { throw new IllegalArgumentException(); } } } static class Data implements Comparable<Data> { int a, b; public Data(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(Data o) { if (a == o.a) { return Integer.compare(b, o.b); } return Integer.compare(a, o.a); } public static void sort(int a[]) { Data d[] = new Data[a.length]; for (int i = 0; i < a.length; i++) { d[i] = new Data(a[i], 0); } Arrays.sort(d); for (int i = 0; i < a.length; i++) { a[i] = d[i].a; } } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; import java.lang.*; import java.math.*; //import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); Long s1 = s.nextLong(); Long w,min,idx; Long t; min =0L; Long sum = 0L; Vector<Long> v = new Vector<Long>(); for(int i = 0 ; i<n ;i++){ w = s.nextLong(); v.add(w); if(i==0){ min = w; //idx = 0; } if(min > w){ min = w; // idx = i; } sum = sum+w; } if(sum<s1){ System.out.println("-1"); } else if(sum == s1){ System.out.println("0"); } else{ int i = 0; int c = 0 ; t = s1; while(t > 0 && (c != n)){ if (v.elementAt(i) == min){ c++; } if(v.elementAt(i) != min){ t = t - (v.elementAt(i) - min) ; v.set(i, min ); //System.out.println(t); // count ++; //v.elementAt(i) = v.elementAt(i) -1 ; //t = t -min ; } i = (i+1) % n ; } if(c == n){ if(t % n == 0 ){ min = min - (t/n); } else{ min = min - (t/n) - 1; } } System.out.println(min); } //System.out.println(); /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
from math import ceil n,s=map(int,input().split()) a=list(map(int,input().split())) if s>sum(a): print(-1) else: a.sort(reverse=True); i=0 sofa=0; mn=min(a) while i<n and a[i]!=mn: sofa+=a[i]-mn i+=1 if sofa>=s: print(mn) else: diff=s-sofa print(mn-ceil(diff/n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long gcd1(long long a, long long b) { if (a == 0) return b; return gcd1(b % a, a); } long long modx(long long base, long long ex) { long long ans = 1LL, val = base; while (ex > 0LL) { if (ex & 1LL) ans = (ans * val) % 1000000009LL; val = (val * val) % 1000000009LL; ex = ex >> 1LL; } return ans; } int n; long long s, v[1005], minn, sum; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; minn = 20000000000000LL; for (int i = 1; i <= n; i++) { cin >> v[i]; minn = min(minn, v[i]); sum += v[i]; } if (sum < s) { cout << "-1" << endl; return 0; } for (int i = 1; i <= n && s > 0; i++) { s -= (v[i] - minn); v[i] = minn; } if (s <= 0) { cout << minn << endl; return 0; } if (s % n == 0) minn -= s / n; else minn -= (s / n + 1); cout << minn << endl; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1e3; const long long I = 1e18; int n; long long s, a[N], l = -1, r = I, c; bool C(long long k) { c = 0; for (int i = 0; i < n; i++) if (a[i] > k) c += a[i] - k; return c >= s; } int main() { cin >> n >> s; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) r = min(r, a[i]); while (l < r) if (C((l + r + 1) / 2)) l = (l + r + 1) / 2; else r = (l + r - 1) / 2; cout << l; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; public class Codeforces1084B { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long s = Long.parseLong(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] v = new int[n]; for (int i = 0; i < n; i++) { v[i] = Integer.parseInt(st.nextToken()); } int min = 1000000001; long total = 0; for (int i = 0; i < n; i++) { total += v[i]; min = Math.min(min, v[i]); } if (total < s) { System.out.println(-1); } else { //get to all kegs same first long firstpour = total - ((long) min)*((long) n); s -= firstpour; //so now we need s more liters //and each one has min in it if (s <= 0) { System.out.println(min); } else { if (s%n == 0) { System.out.println(min-(s/n)); } else { System.out.println(min-1-(s/n)); } } } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long arr[1010]; int main() { long long n; long long s; cin >> n >> s; long long mini = (long long)1e15; long long sum = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; mini = min(arr[i], mini); sum += arr[i]; } if (sum < s) { cout << -1; return 0; } long long u = 0; for (int i = 0; i < n; i++) { u += arr[i] - mini; } if (u >= s) { cout << mini; return 0; } sum -= u; s -= u; long long ans = sum - s; mini = ans / n; cout << mini; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
''' 3 3 4 3 5 ''' from math import * def solve(): n, s = map(int, input().split()) l = list(map(int, input().split())) answer = sum(l) if n == 1 and s == l[0]: return 0 if answer < s: return -1 minn = min(l) answer = 0 for i in range(len(l)): if l[i] > minn: if answer + l[i] - minn >= s: l[i] -= l[i] - minn return min(l) answer += l[i] - minn l[i] -= l[i] - minn ch = max(l) ll = len(l) p = ceil((s - answer) / len(l)) return ch - p print(solve())
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Scanner; import java.util.StringTokenizer; public class DIV526_B { static InputStream in = System.in ; PrintWriter out; private static byte[] inbuf = new byte[1024 * 1024]; public static int lenbuf = 0; public static int ptrbuf = 0; public static int MOD = 1000000007; public static void main(String args[]) { long n = nl(); long s = nl(); long[] arr = new long[(int) n]; long min = Integer.MAX_VALUE; long sum = 0; for (int i = 0; i < n; i++) { arr[i] = ni(); if (arr[i] < min) { min = arr[i]; } sum += arr[i]; } if ( sum < s) { System.out.println("-1"); } else { long diff = (sum - (min * n)); if (s <= diff) { System.out.println(min); } else { s = s - diff; long temp = (s/n); if (s % n != 0) { temp++; } long ans = min - temp; System.out.println(ans); } } } class Sorting implements Comparator<Song> { @Override public int compare(Song o1, Song o2) { if (o1.diff > o2.diff) { return -1; } else if (o1.diff < o2.diff) { return 1; } else { return 0; } } } class Song { long orig; long comp; long diff; Song(long a, long b, long c){ this.orig = a; this.comp = b; this.diff = c; } } class FastReader { BufferedReader br; StringTokenizer st; FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; } } private static int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = in.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private static boolean inSpaceChar(int c) { return !(c >= 33 && c <= 126); } private static int skip() { int b; while ((b = readByte()) != -1 && inSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(inSpaceChar(b))) { // when nextLine, (inSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(inSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private boolean oj = true; // private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = [int(x) for x in input().split()] data = [int(x) for x in input().split()] total = sum(data) diff = total - s if diff < 0: print(-1) else: # print(diff) print(min(diff // n, min(data)))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, left = map(int, input().split()) arr = list(map(int, input().split())) if sum(arr) < left: print(-1) else: mn = min(arr) extra = 0 for v in arr: extra += v - mn if extra >= left: print(mn) else: left -= extra stages = left // n if left % n != 0: stages += 1 print(mn - stages)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import math n,s = map(int,input().strip().split()) arr = list(map(int,input().strip().split())) arr = sorted(arr) c = 0 for i in range(n): c+=arr[i]-arr[0] #arr[i]=arr[i]-arr[0] if c>=s: print(arr[0]) else: k = n z = math.ceil((s-c)/k) if z<=arr[0]: print(arr[0]-z) else: print(-1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
def check(arr,n,temp,s): ans=0 for i in range(n): if arr[i]>temp: ans+=arr[i]-temp if ans>=s: return True else: return False n,s=map(int,raw_input().split(" ")) l=map(int,raw_input().split()) if sum(l)<s: print -1 exit() mn=min(l) l1=0 r1=mn fin=0 while l1<=r1: mid=(l1+r1)/2 if check(l,n,mid,s): fin=mid l1=mid+1 else: r1=mid-1 print fin
PYTHON
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long a[1005]; int main() { long long n, k, s = 0; scanf("%lld%lld", &n, &k); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); s += a[i]; } if (k > s) { printf("-1\n"); return 0; } if (n == 1) { printf("%lld\n", a[0] - k); return 0; } sort(a, a + n); long long ans = 0; for (int i = 1; i < n; i++) { ans += a[i] - a[0]; } if (ans < k) { ans = (k - ans) / n + ((k - ans) % n == 0 ? 0 : 1); printf("%lld\n", a[0] - ans); return 0; } else { printf("%lld\n", a[0]); return 0; } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import sys, os import math from collections import defaultdict as dd, deque from heapq import merge, heapify, heappop, heappush, nsmallest from bisect import bisect_left as bl, bisect_right as br, bisect if os.path.exists('input.txt'): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') n,s=map(int,input().split()) arr=list(map(int,input().split())) m=min(arr) temp=s arr.sort(reverse=True) ans=0 for i in range(n): if arr[i]>m: s-=(arr[i]-m) if temp>sum(arr): print(-1) elif s<0: print(m) else: print(m-(s+n-1)//n)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long s = in.nextLong(); int[] a = new int[n]; long amin = 1_000_000_001; for(int i=0; i<n; i++){ a[i] = in.nextInt(); if(a[i] < amin) { amin = a[i];} } long x = 0; for (int i = 0; i < n; i++){ if (a[i] > amin) { x += a[i]-amin; } } s -= x; if (s <= 0){ System.out.println(amin); System.exit(0); } if (s > (long)n*amin) { System.out.print("-1"); System.exit(0); } if (s % (long)n > 0){ amin -= s/ (long)n + 1;} else{ amin -= s/(long)n; } System.out.println(amin); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<26).start(); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=sc.nextInt(); long s=sc.nextLong(); long arr[]=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } long c=0; Arrays.sort(arr); long min=arr[0]; for(int i=0;i<n;i++){ c=c+arr[i]-min; arr[i]=min; } /* if(s>c){ while(s>c){ c=c+n; min--; if(min<0) break; } } */ if(s>c){ if ((s-c)<=min*n) min=(min*n-(s-c))/n; else min=-1; } // if(min<0) min=-1; w.println(min); w.close(); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import sys line = sys.stdin.readline().strip().split() n = int(line[0]) s = int(line[1]) ksum = 0 kmin = 10 ** 9 + 1 line = sys.stdin.readline().strip().split() for i in range (0, n): v = int(line[i]) ksum = ksum + v kmin = min(kmin, v) if ksum < s: print(-1) else: print(min(kmin, (ksum - s) // n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
# x = int(input()) # m, n = map(int, input().split()) # nums = list(map(int, input().split())) n,s=map(int,input().split()) a=list(map(int,input().split())) minn=0x3f3f3f3f sum=0 for i in range(n): minn=min(minn,a[i]) sum+=a[i] if sum<s: print(-1) else: print(int(min(minn,(sum-s)/n)))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, s; cin >> n >> s; vector<long long> v(n); long long sum = 0; long long mn = 1e9 + 7; for (int i = 0; i < n; ++i) { cin >> v[i]; sum += v[i]; mn = min(mn, v[i]); } if (sum < s) { cout << -1 << '\n'; return 0; } for (int i = 0; i < n; ++i) { long long p = min(s, v[i] - mn); v[i] -= min(s, v[i] - mn); s -= p; } cout << mn - s / n - !!(s % n) << '\n'; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import sys def solve(io): N = io.read_int() S = io.read_int() V = io.read_int_array(N) L = 0 R = 1000000000 good = -1 while L <= R: M = (L + R) // 2 if get_kvass(M, V) >= S: L = M + 1 good = M else: R = M - 1 io.println(min(good, *V)) def get_kvass(x, V): return sum([ val - x for val in V if val > x ]) # +---------------------+ # | TEMPLATE CODE BELOW | # | DO NOT MODIFY | # +---------------------+ class IO: in_stream = None out_stream = None raw = "" buf = [] pos = 0 def __init__(self, input_stream, output_stream): self.in_stream = input_stream self.out_stream = output_stream def read_to_buffer(self): self.raw = self.in_stream.readline().rstrip('\n') self.buf = self.raw.split() self.pos = 0 def read_string(self): while self.pos == len(self.buf): self.read_to_buffer() ans = self.buf[self.pos] self.pos += 1 return ans def read_int(self): return int(self.read_string()) def read_float(self): return float(self.read_string()) def read_string_array(self, N, offset=0): arr = [None] * offset for _ in range(0, N): arr.append(self.read_string()) return arr def read_int_array(self, N, offset=0): arr = [None] * offset for _ in range(0, N): arr.append(self.read_int()) return arr def read_float_array(self, N, offset=0): arr = [None] * offset for _ in range(0, N): arr.append(self.read_float()) return arr def read_line(self): while self.pos == len(self.buf): self.read_to_buffer() if self.pos > 0: raise ValueError("Cannot call read_line in the middle of a line.") self.pos = len(self.buf) return self.raw def print(self, s): self.out_stream.write(str(s)) def println(self, s): self.print(s) self.print('\n') def println_array(self, arr, sep = ' '): self.println(sep.join(str(x) for x in arr)) def flush_output(self): self.out_stream.flush() pythonIO = IO(sys.stdin, sys.stdout) solve(pythonIO) pythonIO.flush_output()
PYTHON3