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
import sys import math r = lambda: sys.stdin.readline() n, s = map(int,r().split()) arr = list(map(int, input().split())) cavas = 0 min_arr=min(arr) sum_arr=0 for i in range(n): sum_arr+=arr[i] cavas+=(arr[i]-min_arr) if s>sum_arr: print(-1) elif cavas>=s: print(min_arr) else: dd = ((s-cavas)/n) dd = math.ceil(dd) print(min_arr-dd)
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 long long INF = 100000000000000000; const long long MOD = 1000000007; const long long MAXN = 1005; long long dx[] = {0, 0, -1, 1, -1, -1, 1, 1}; long long dy[] = {1, -1, 0, 0, 1, -1, -1, 1}; vector<long long> v(MAXN); long long n, s; bool f(long long x) { long long sum = 0; for (int i = 0; i < n; ++i) { sum += (v[i] - x); } return (sum >= s); } void solve() { cin >> n >> s; long long lo = 0, hi = INF; for (int i = 0; i < n; ++i) { cin >> v[i]; hi = min(hi, v[i]); } long long ans = -1; while (lo <= hi) { long long mid = lo + hi >> 1; if (f(mid)) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long 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
arr = list(map(int,input().strip().split(' '))) n = arr[0] s = arr[1] ss = 0 arr = list(map(int,input().strip().split(' '))) ss = sum(arr) if(ss<s): print(-1) else: ss = 0 arr.sort() for i in range(n): ss+=(arr[i]-arr[0]) if(ss>s): print(arr[0]) else: t = arr[0] - (s-ss+n-1)//n print(t)
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 v[2000]; int main() { long long n, s, tot = 0; cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> v[i]; tot += v[i]; } if (tot <= s) { if (tot < s) cout << "-1\n"; else cout << "0\n"; return 0; } sort(v, v + n); long long carry = 0; for (int i = 1; i < n; ++i) carry += v[i] - v[0]; if (carry >= s) { cout << v[0] << endl; return 0; } long long extra = s - carry; long long mm = extra % n, div = extra / n; cout << (v[0] - div - (mm > 0)) << 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.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.LongSummaryStatistics; public class B { private static int cur, pos, len; private static int max, min, ans, res, sum; private static int ai, bi, ci, di; private static int[] aix, bix; private static int[][] aixx, bixx; private static long al, bl, cl, dl; private static long[] alx, blx; private static long[][] alxx, blxx; private static double ad, bd, cd, dd; private static double[] adx, bdx; private static double[][] adxx, bdxx; private static boolean ab, bb, cb, db; private static boolean[] abx, bbx; private static boolean[][] abxx, bbxx; private static char ac, bc, cc, dc; private static char[] acx, bcx; private static char[][] acxx, bcxx; private static String as, bs, cs, ds; private static InputReader in = new InputReader(System.in); private static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int n = in.nextInt(); long s = in.nextLong(); alx = new long[n]; LongSummaryStatistics statistics = new LongSummaryStatistics(); for (int i = 0; i < n; i++) { alx[i] = in.nextLong(); statistics.accept(alx[i]); } if (statistics.getSum() < s) { out.println("-1"); } else { long highPart = statistics.getSum() - statistics.getMin() * n; if (highPart >= s) { out.println(statistics.getMin()); } else { s -= highPart; out.println(statistics.getMin() - (s / n + ((s % n > 0) ? 1 : 0))); } } out.close(); } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public String next() { return readString(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private 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(); } private 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++]; } } }
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()) u = [int(x) for x in input().split()] su = sum(u) if su < s: print(-1) elif su == s: print(0) else: u.sort() print(min((su-s)//n, u[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
#include <bits/stdc++.h> using namespace std; long long n, i, a, s, t, m = 2e9; int main() { for (cin >> n >> s; i < n; i++) cin >> a, t += a, m = min(m, a); if (t < s) return cout << -1, 0; if (t - m * n < s) return cout << (t - s) / n, 0; cout << m; }
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.*; import java.io.*; public class CF1084B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long s = sc.nextLong(); long[] v = new long[n]; long sum = 0; for(int i = 0; i < n; i++) { v[i] = sc.nextLong(); sum += v[i]; } if(sum < s) { System.out.println(-1); System.exit(0); } Arrays.sort(v); long min = v[0]; for(int i = 1; i < n; i++) { s -= (v[i] - min); if(s <= 0) { System.out.println(min); System.exit(0); } } long more = 0; if(s % n == 0) more = s / n; else more = s / n + 1; System.out.println(min - more); } }
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 (ns, ss) = '3 4'.strip().split(' ') (ns, ss) = input().strip().split(' ') n = int(ns) s = int(ss) vstr = '5 3 4'.strip().split(' ') vstr = input().strip().split(' ') v = list(map(int, vstr)) min_v = min(v) for i in range(n): if v[i] > min_v: s -= (v[i]-min_v) v[i] -= (v[i]-min_v) if s <= 0: break if s <= 0: print(min_v) else: if n*min_v < s: print(-1) else: print(min_v - math.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
n, s = [int(x) for x in input().split()] barrel = [int(x) for x in input().split()] if sum(barrel) < s: print(-1) else: print(min(((sum(barrel) - s) // n), min(barrel)))
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, raw_input().split()) l = map(int, raw_input().split()) mini = min(l) if sum(l) < s: print -1 else: for i in l: s -= i - mini if s <= 0: print mini else: mini -= int(ceil(s/float(n))) print 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
/* * Date Created : 6/10/2020 * Have A Good Day ! */ import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.PriorityQueue; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.AbstractCollection; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Arpit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); OutputWriter out = new OutputWriter(outputStream); BKvassAndTheFairNut solver = new BKvassAndTheFairNut(); solver.solve(1, in, out); out.close(); } static class BKvassAndTheFairNut { public void solve(int testNumber, FastReader r, OutputWriter out) { int n = r.nextInt(); long s = r.nextLong(); PriorityQueue<Long> pq = new PriorityQueue<>(); long sum = 0; for (int i = 0; i < n; i++) { long ele = r.nextLong(); sum += ele; pq.add(ele); } if (sum < s) out.println(-1); else { long min = pq.poll(); long extra = 0; while (!pq.isEmpty()) extra += pq.poll() - min; if (extra >= s) out.println(min); else { s -= extra; min -= (long) Math.ceil((double) s / n); out.println(min); } } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(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 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 boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } 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(Object... objects) { for (int i = 0; i < objects.length; i++) { writer.print(objects[i]); if (i != objects.length - 1) writer.print(" "); } } public void println(Object... objects) { print(objects); 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
#include <bits/stdc++.h> using namespace std; bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { if (a.first == b.first) return (a.second < b.second); return (a.first < b.first); } vector<long long int> prime(1000005, 1); void p() { long long int i, j; for (i = 2; i <= 1000000; i++) { if (prime[i] == 1) { for (j = 2 * i; j <= 1000000; j += i) prime[j] = 0; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, s, i; cin >> n >> s; vector<long long int> a(n); for (i = 0; i < (n); i++) cin >> a[i]; sort(a.begin(), a.end()); long long int sum = 0; for (i = 0; i < (n); i++) sum += a[i]; if (sum < s) cout << -1 << "\n"; else { long long int ans = 0; long long int left = 0, mid; long long int right = a[0]; while (left <= right) { mid = left + (right - left) / 2; if (sum - mid * n >= s) { ans = mid; left = mid + 1; } else right = mid - 1; } cout << ans << "\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 java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); long v=sc.nextLong(); long sum=0; long min=Integer.MAX_VALUE; for(int i=0;i<n;i++) { int x=sc.nextInt(); sum+=x; min=Math.min(min, x); } if(sum<v) { System.out.println("-1"); }else { System.out.println(Math.min(min, (sum-v)/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 a[10001], minn = 0x7fffffff, sum = 0, n, s; long long fmin(long long k1, long long k2) { if (k1 < k2) return k1; else return k2; } int main() { scanf("%lld%lld", &n, &s); for (long long i = 1; i <= n; i++) { scanf("%lld", &a[i]); minn = fmin(minn, a[i]); sum += a[i]; } sum -= s; if (sum < 0) { printf("-1\n"); return 0; } if (minn * n >= sum) printf("%lld\n", sum / n); else printf("%lld\n", minn); 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 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) 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(ls[0] - (s+n -1)//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
#include <bits/stdc++.h> using namespace std; long long a[1050]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; long long s = 0; for (int i = 0; i < (n); i++) { cin >> a[i]; s += a[i]; } if (s < m) { cout << -1; return 0; } sort(a, a + n); reverse(a, a + n); long long ans = a[n - 1], cur = 0; for (int i = 0; i < (n); i++) { cur += a[i] - ans; if (cur >= m) break; } if (cur < m) { long long z = m - cur; if (z % n != 0) z = z / n + 1; else z /= n; ans -= z; } 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
import java.util.Scanner; public class CodeforcesFun { public static void main(String[] args) { Scanner myScanner = new Scanner(System.in); int n = myScanner.nextInt(); long s = myScanner.nextLong(); long[] arr = new long[n]; long min = Long.MAX_VALUE; for (int i = 0; i <= n - 1; i++) { arr[i] = myScanner.nextLong(); if (arr[i] < min) { min = arr[i]; } } long sumWithoutMin = 0; for (int i = 0; i <= n - 1; i++) { sumWithoutMin += arr[i] - min; } if (s <= sumWithoutMin) { System.out.println(min); } else { long div = (s - sumWithoutMin) / n; long mod = (s - sumWithoutMin) % n; long answ = mod == 0? min - div : min - div - 1; if (answ < 0) { System.out.println("-1"); } else System.out.println(answ); } } }
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 n, s=map(int, input().split()) a=[int(x) for x in input().split()] if (sum(a)<s): print (-1) else: m=min(a) free=0 for i in range(n): free+=(a[i]-m) a[i]-=m if (free>=s): print (m) sys.exit() s-=free print (m-math.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
#include <bits/stdc++.h> using namespace std; const int maxn = 1100; long long arr[maxn]; int n; long long s; int check(long long ck) { long long cur_sum = 0; for (int i = 1; i <= n; i++) { if (arr[i] < ck) return 0; cur_sum += (arr[i] - ck); } return cur_sum >= s; } int main() { long long sum = 0; scanf("%d%I64d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%I64d", &arr[i]); sum += arr[i]; } long long l = 0, r = sum, m; long long ret = -1; while (l <= r) { m = (l + r) >> 1; if (check(m)) { l = m + 1; ret = m; } else { r = m - 1; } } printf("%I64d\n", r); 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().strip().split()) v = list(map(int, input().strip().split())) v.sort() izd = 0 if(sum(v) <s): print(-1) else: for i in range(len(v)): izd += v[i] - v[0] if(izd >= s): print(v[0]) else: x = (s - izd)/n if(int(x)==x): print(v[0]-int(x)) else: print(v[0]-int(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
n = input().split(" ") data= [int(i) for i in input().split(" ")] s=sum(data) #print(s) if s<int(n[1]): s=-1 else: s-=int(n[1]) s//=int(n[0]) if min(data) <s: s= min(data) print(s)
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, k = list(map(int, input().split(' '))) a = list(map(int, input().split(' '))) minV = min(a) s = sum(a) if(k > s): print(-1) else: rest = s - minV * n if(rest >= k): print(minV) else: k -= rest sol = k // n if(k % n): sol += 1 print(minV - sol)
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 in() { return; } template <typename T, typename... Types> void in(T &a, Types &...b) { cin >> (a); in(b...); } void o() { return; } template <typename T, typename... Types> void o(T a, Types... b) { cout << (a); cout << ' '; o(b...); } bool sortin(const pair<long long int, long long int> &e, const pair<long long int, long long int> &f) { return (e.first < f.first); } bool POT(long long int x) { return x && (!(x & (x - 1))); } long long int i, j, k, l, m, n, p, q, r, a, b, c, x, y, z, ts, mn = 1e18, mod = 1e9 + 7; long long int ar[250005], br[250005], xr[250005], tem[250005]; int main() { { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; in(n, m); for (long long int i = 0; i <= n - 1; i++) { in(ar[i]); c += ar[i]; } if (c < m) { o(-1); return 0; } sort(ar, ar + n); x = c - ar[0] * n; if (x >= m) { o(ar[0]); return 0; } p = (m - x + n - 1) / n; o(ar[0] - p); }
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 cont[1005]; int main() { ios::sync_with_stdio(0); long long n, m; cin >> n >> m; long long sum = 0; long long Min = 1e18; for (int i = 0; i < n; i++) { cin >> cont[i]; sum += cont[i]; Min = min(Min, cont[i]); } if (sum < m) { cout << -1 << endl; exit(0); } int ans = 0; for (int i = 1 << 30; i; i >>= 1) { int test = ans + i; long long s = 0; for (int j = 0; j < n; j++) { s += cont[j] - test; } if (s >= m && test <= Min) { ans = test; } } cout << ans << 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
n, s = map(int, input().split()) a = list(map(int, input().split())) a.sort() d = sum(a) if d >= s: r = d - s ans = min(r // n, a[0]) else: ans = -1 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
import java.util.*; import java.lang.*; import java.io.*; public final class Kvass { static private Scanner sc = new Scanner(System.in); static public void main(String[] args) { int n = sc.nextInt(); long s = sc.nextLong(); long[] arr = new long[n]; long sum = 0; for(int i = 0;i<n; i++) { arr[i] = sc.nextLong(); sum+=arr[i]; } if(sum<s) { System.out.println(-1); } else { Arrays.sort(arr); long v = arr[0]; for(int i = 0;i<n; i++) { s-=(arr[i] - v); } if(s<=0) { System.out.println(v); } else { long z = (long)Math.floor(v-((s+n-1)/n)); System.out.println(z); } } } }
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 Kvass_Fair_Nut { public static void main (String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s[] =br.readLine().split(" "); int n = Integer.parseInt(s[0]); long g = Long.parseLong(s[1]); String v[] = br.readLine().split(" "); long sum = 0; long min = Integer.MAX_VALUE; for(int i=0;i<n;i++) { sum += Integer.parseInt(v[i]); if(min > Integer.parseInt(v[i])) { min = Integer.parseInt(v[i]); } } if(sum < g) { System.out.println(-1); return; } if(sum - (min*n) >= g) { System.out.println(min); return; } long remain = g - (sum - (min*n)); long eachGlass = remain/n; long finalGlass = remain%n; System.out.println(finalGlass == 0 ? min - eachGlass : min - eachGlass -1); } }
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.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class B_ { public static void main(String[] args) throws Exception { solve(); } // **SOLUTION** public static void solve() throws Exception { String[] ip = br.readLine().split(" "); int n = new Integer(ip[0]); long s = new Long(ip[1]); long[] arr = new long[n]; long sum = 0; long min = Integer.MAX_VALUE; ip = br.readLine().split(" "); for (int i = 0; i < arr.length; i++) { arr[i] = new Long(ip[i]); sum += arr[i]; min = Math.min(arr[i], min); } if (sum < s) { System.out.println(-1); return; } long have = 0; for (int i = 0; i < arr.length; i++) { have += (arr[i] > min) ? arr[i] - min : 0; } if (have >= s) { System.out.println(min); return; } long req = s - have; long redLevel = (req / n) + ((req % n == 0) ? 0 : 1); System.out.println(min - redLevel); } private static boolean check(long lev, long[] arr, long req) { long fill = 0; for (int i = 0; i < arr.length; i++) { fill += (arr[i] > lev) ? arr[i] - lev : 0; } if (fill >= req) return true; return false; } public static InputStreamReader r = new InputStreamReader(System.in); public static BufferedReader br = new BufferedReader(r); public static Scanner sc = new Scanner(System.in); }
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,k=map(int,raw_input().split()) N=map(int,raw_input().split()) if sum(N)<k: print -1 exit(0) x=min(N) ret=0 for i in N: ret+=i-x if ret>=k: print x exit(0) k-=ret t=k/n if k%n!=0: t+=1 print x-t
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; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; long long i, j, k, t, mn = 1e13, x, sum = 0, temp, ans, s, n, baki; cin >> n >> s; vector<long long> vec; for (int i = 0; i < n; i++) { cin >> x; sum += x; vec.push_back(x); mn = min(mn, x); } if (sum < s) { cout << -1; return 0; } else { k = 0; for (int i = 0; i < n; i++) { temp = vec[i] - mn; k += temp; } if (k >= s) { cout << mn; return 0; } else { baki = s - k; j = (baki + n - 1) / n; ans = mn - j; cout << 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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author llamaoo7 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(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, InputReader in, PrintWriter out) { int n = in.nextInt(); long s = in.nextLong(); long[] v = new long[n]; for (int i = 0; i < n; i++) v[i] = in.nextLong(); long t = 0; long m = Long.MAX_VALUE; for (int i = 0; i < n; i++) { t += v[i]; m = Math.min(m, v[i]); } if (s > t) { out.println("-1"); } else { long x = 0; for (int i = 0; i < n; i++) x += v[i] - m; if (x >= s) { out.println(m); } else { s -= x; out.println(m - (s / n + (s % n == 0 ? 0 : 1))); } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(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
#include <bits/stdc++.h> using namespace std; int n; long long int s, sum = 0, mn = 1e9 + 9; vector<long long int> arr(1e3 + 3); int main(void) { ios ::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (int i = 1; i <= n; ++i) { cin >> arr[i]; sum += arr[i]; mn = min(mn, arr[i]); } if (sum < s) { cout << -1 << "\n"; return 0; } sum = 0; for (int i = 1; i <= n; ++i) { sum += arr[i] - mn; } if (sum >= s) { cout << mn << "\n"; return 0; } s -= sum; mn -= s / n; if (s % n != 0) { --mn; } cout << mn << "\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 java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader in = new FastReader(); int n = in.nextInt(); long s = in.nextLong(); ArrayList<Integer> v = new ArrayList<>(); long ovallKvass = 0; for (int i = 0; i < n; i++) { int input = in.nextInt(); v.add(input); ovallKvass += input; } if (ovallKvass < s) { System.out.println("-1"); return; } Collections.sort(v); int min = v.get(0); if (v.size() == 1) { System.out.println(min - s); } else { for (int i = 1; i < v.size(); i++) { if (v.get(i) - min < s) { s -= (v.get(i) - min); } else { System.out.println(min); return; } } long x = s % v.size(); long sub = s / v.size(); if (x != 0) sub++; System.out.println(min - sub); } } }
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.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Washoum */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; inputClass in = new inputClass(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, inputClass sc, PrintWriter out) { int n = sc.nextInt(); long s = sc.nextLong(); long[] tab = new long[n]; long sum = 0; for (int i = 0; i < n; i++) { tab[i] = sc.nextLong(); sum += tab[i]; } if (sum < s) { out.println(-1); } else { Arrays.sort(tab); for (int i = 1; i < n && s > 0; i++) { s -= tab[i] - tab[0]; } if (s <= 0) out.println(tab[0]); else { if (s % n == 0) out.println(tab[0] - s / n); else out.println(tab[0] - s / n - 1); } } } } static class inputClass { BufferedReader br; StringTokenizer st; public inputClass(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(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 java.util.*; public class B{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); long nb = sc.nextLong(); long qt = sc.nextLong(); long min = sc.nextLong(); long surplus = 0; long enCours; for (int i=1;i<nb;i++){ enCours=sc.nextLong(); if (enCours<min){ surplus += i*(min-enCours); min = enCours; } else { surplus += enCours - min; } } qt-=surplus; if (qt>0){ min = min - (qt%nb==0?qt/nb:qt/nb+1); System.out.println((min>=0?min:-1)); } else { 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
from math import ceil n, s = [int(no) for no in input().split()] v = [int(no) for no in input().split()] min_v = min(v) current_available = sum([vi - min_v for vi in v]) if current_available >= s: print(min_v) else: rem = s - current_available # We have n kegs with min_v each # n * take >= rem # take >= rem/n # take = ceil(rem/n) # take must be <min_v req = ceil(rem / n) if req <= min_v: print(min_v - req) 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
import java.lang.reflect.Array; 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 min=1000000000; long sum=0; for (int i=0;i<n;i++){ int temp=in.nextInt(); if (temp<min){ min=temp; } sum+=temp; } sum-=s; if (sum<0){ System.out.println(-1); } else{ if ((sum/n)>=min){ System.out.println(min); } else{ System.out.println(sum/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
from math import floor n, s = tuple(int(i) for i in input().split(" ")) v_i = tuple(int(i) for i in input().split(" ")) v_sum = sum(v_i) if s>v_sum: print(-1) else: print(min(floor((v_sum - s)/n), min(v_i)))
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 MOD = 1000000007; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, k; cin >> n >> k; int a[n]; long long s = 0; for (int i = 0; i < (n); i++) { cin >> a[i]; s += a[i]; } if (k > s) { cout << "-1"; } else { long long min_ = *min_element(a, a + n); long long exc = 0; for (auto &i : a) exc += (i - min_); if (exc >= k) { cout << min_; } else { k -= exc; while (k > 0) { min_--; k -= n; } cout << min_; } } 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
""" this is a standard python template for codeforces task, repo: github.com/solbiatialessandro/pyComPro/codeforces """ stdin = lambda type_ = "int", sep = " ": list(map(eval(type_), raw_input().split(sep))) joint = lambda sep = " ", *args: sep.join(str(i) if type(i) != list else sep.join(map(str, i)) for i in args) def solve(n, target, A): if sum(A) < target: return -1 if sum(A) == target: return 0 res = min(A) for x in A: target -= (x - res) if target < 0: return res from operator import truediv from math import ceil return res - int(ceil(truediv(target, len(A)))) if __name__ == "__main__": """the solve(*args) structure is needed for testing purporses""" n, t = stdin() A = stdin() print solve(n, t, A)
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; map<pair<pair<long long int, long long int>, pair<long long int, long long int>>, long long int> pqr; map<pair<long long int, long long int>, long long int> xyz; map<long long int, long long int, greater<long long int>> yz; vector<pair<long long int, string>> a1; vector<pair<long long int, long long int>> a2; vector<pair<long long int, pair<long long int, long long int>>> a3; bool isSubSequence(string str1, string str2, long long int m, long long int n) { if (m == 0) return true; if (n == 0) return false; if (str1[m - 1] == str2[n - 1]) return isSubSequence(str1, str2, m - 1, n - 1); return isSubSequence(str1, str2, m, n - 1); } long long int fac[5005]; void output2(long long int t) { if (t > 2) { cout << "3" << " " << "1" << "\n"; cout << "3" << " " << "2" << "\n"; for (long long int i = 2; i < t - 1; i++) { cout << "3" << " " << i + 2 << "\n"; } } else { for (long long int i = 0; i < t - 1; i++) { cout << "1" << " " << i + 2 << "\n"; } } } long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int modInverse(long long int n, long long int p) { return power(n, p - 2, p); } long long int nCrModPFermat(long long int n, long long int r, long long int p, long long int fac[]) { if (r == 0) return 1; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, s; cin >> n >> s; long long int arr[n + 2]; long long int sum = 0; map<long long int, long long int> freq; for (long long int i = 0; i < n; i++) { cin >> arr[i]; freq[arr[i]]++; sum = sum + arr[i]; } if (sum < s) { cout << "-1\n"; return 0; } sort(arr, arr + n); long long int r = arr[0]; long long int d = n - freq[r]; long long int h = sum - freq[r] * r; long long int g = h - d * r; if (g >= s) { cout << r << "\n"; return 0; } long long int w = n * r; long long int q = sum - w; s = s - q; long long int ans = w - s; cout << ans / 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; int main() { long long n, s, mn = 2e9, sum = 0; cin >> n >> s; long long a[1100]; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; mn = min(mn, a[i]); } if (sum < s) return cout << -1, 0; if (sum == s) return cout << 0, 0; if (sum - mn * n < s) return cout << (sum - s) / n, 0; cout << mn; }
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 = sorted(list(map(int, input().split()))) if sum(v) < s: print(-1) else: remainder = (sum(v) - s) // n print(min(v[0], remainder))
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
var numbers = readline().split(' '); var howMany = numbers[0]; var amountToFill = numbers[1]; howMany = parseInt(howMany) amountToFill = parseInt(amountToFill) var numberOfK = readline().split(' '); var asd = howMany+amountToFill var sum = 0, sumCheck = 0, minSumCheck = 0, check,ass check = Math.min.apply(null,numberOfK) for( var i = 0; i < howMany; i++) { numberOfK[i] = parseInt(numberOfK[i]) sumCheck += numberOfK[i]; minSumCheck += numberOfK[i] - check if(amountToFill){ sum = (sumCheck - amountToFill)/howMany } if(howMany*amountToFill===sumCheck){ sum = numberOfK[i]-(amountToFill/howMany) } if(amountToFill < minSumCheck){ sum = check } if(amountToFill> sumCheck){ sum = -1 } } if(sum % 1 !== 0){ sum = Math.floor(sum) } print(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
n, s = map(int, input().split()) a = list(map(int, input().split())) suma, mina = sum(a), min(a) if suma < s: print(-1) else: print(min((suma - s) // n, mina))
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 = [int(x) for x in input().split()] lst = [int(x) for x in input().split()] tot = sum(lst) if (s>tot): print(-1) exit() elif (s==tot): print(0) exit() s-=(tot-min(lst)*n) if (s<=0): print(min(lst)) exit() else: x=min(lst) if (s<=n): print(x-1) elif (s//n)==s/n: print(x-s//n) else: print(x-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 math import sys input = sys.stdin.readline n, s = map(int, input().split()) v = list(map(int, input().split())) tot, mn = sum(v), min(v) if tot < s: ans = -1 else: res = 0 for i in range(n): if v[i] > mn: res += v[i] - mn v[i] -= mn if res >= s: ans = mn else: s -= res ans = mn - math.ceil(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() { 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; } sort(v.rbegin(), v.rend()); 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
(n,s)=map(int,input().split()) l=list(map(int,input().split())) sa=sum(l) sa-=s if sa<0: print(-1) else: sa//=n if min(l)<sa: print(min(l)) else: print(sa)
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, raw_input().split()) a = map(int, raw_input().split()) a.sort(reverse=1) if sum(a) < s: print - 1 else: x = sum(a) - n * a[-1] a = n * [a[-1]] if x >= s: print a[-1] else: y = int(ceil((s - x) / float(n))) print a[-1] - y
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
x = input() n = int(x.split(" ")[0]) s = int(x.split(" ")[1]) x = input() x = x.split(" ") arr = [int(i) for i in x] if sum(arr)<s: print("-1") else: imin = min(arr) x = (sum(arr) - s)//n print(min(imin, x))
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 minimum(int a, int b) { if (a > b) return b; else return a; } int main() { int n; long long int s; cin >> n >> s; int v[n]; int min = 1000000000; long long int total = 0; for (int i = 0; i < n; i++) { cin >> v[i]; total = total + v[i]; if (v[i] < min) min = v[i]; } if (s > total) { cout << -1 << endl; return 0; } sort(v, v + n); for (int i = n - 1; i >= 0; i--) { if (s > v[i] - min) { s = s - (v[i] - min); v[i] = min; } else { cout << min << endl; return 0; } } if (s > 0) { int x = min - s / n; int rem = s % n; if (rem == 0) cout << x; else cout << x - 1 << 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
n, m = map(int, input().strip().split()) kegs = list(map(int, input().strip().split())) import math if sum(kegs) < m : print(-1) else : import operator index, value = min(enumerate(kegs), key=operator.itemgetter(1)) res = m for i, k in enumerate(kegs): if i != index: res -= k-value if res <= 0: print(value) else: print(value-(math.ceil(res/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, raw_input().split()) v = map(int, raw_input().split()) if sum(v) >= s: m = min(v) c = 0 for x in v: c += x - m if c >= s: print m else: k = s - c h = (k / n) + (0 if k % n == 0 else 1) print m - h else: print -1
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 math def max_min_volume(n, s, arr): arr.sort(reverse=True) cur_min = arr[-1] while (s > 0) and arr[-1] and (cur_min >= 0): for i in range(n-1, -1, -1): diff = arr[i] - cur_min if diff <= s: s -= diff arr[i] -= diff else: arr[i] -= s s = 0 if s <= 0: break if s > n: cur_min -= s // n else: cur_min -= 1 if s > 0: return -1 return arr[-1] if __name__ == '__main__': n, s = list(map(int, input().strip().split())) arr = list(map(int, input().strip().split())) print(max_min_volume(n, s, arr))
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 binarySearch(long long arr[], long long l, long long r, long long x) { if (r >= l) { long long mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); if (arr[mid] < x) return binarySearch(arr, mid + 1, r, x); } return -1; } long long fact(long long n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } long long ncr(long long n, long long r) { if (r < 0 || r > n) return 0; if (r == 0) return 1; else return (n * ncr(n - 1, r - 1)) / r; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } bool isPowerOfTwo(long long n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); } long long mostFrequent(long long arr[], long long n) { unordered_map<long long, long long> hash; for (long long i = 0; i < n; i++) hash[arr[i]]++; long long max_count = 0, res = -1; for (auto i : hash) { if (max_count < i.second) { res = i.first; max_count = i.second; } } return max_count; } long long countSetBits(long long n) { long long count = 0; while (n) { count += n & 1; n >>= 1; } return count; } long long factors(long long n) { long long a = 0, z = 1; while (n % 2 == 0) { a++; n >>= 1; } z *= a + 1; for (long long i = 3; i <= sqrt(n); i += 2) { a = 0; while (n % i == 0) { a++; n = n / i; } z *= a + 1; } if (n > 2) z *= 2; return z - 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); std::cout << std::fixed; std::cout << std::setprecision(9); long long testcases = 1; while (testcases--) { long long n, s, k, a = 0, p = 1000000007; cin >> n >> s; for (long long i = 0; i < n; i++) { cin >> k; p = ((k > p) ? p : k); a += k; } if (a < s) cout << -1; else cout << ((p > (a - s) / n) ? (a - s) / n : p); } 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 long long MOD = 1e18; const long long N = 2e5 + 5; long long a[N]; int main() { long long i, j, k, l; long long n, m, t; cin >> n >> k; long long minx = MOD, ans = 0; for (i = 0; i < n; i++) scanf("%lld", &a[i]), minx = min(minx, a[i]); for (i = 0; i < n; i++) ans += a[i] - minx; if (k <= ans) return cout << minx << endl, 0; k -= ans; t = 0; m = k / n; t = k % n; if (k > n * minx) return cout << -1 << endl, 0; minx -= m; if (t) minx--; cout << minx << 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 main(): n, s = map(int, input().split()) a = list(map(int, input().split())) if sum(a) < s: return -1 if len(set(a)) != 1: a.sort(reverse = True) q = a[-1] for i in range(n): s -= (a[i] - q) if s <= 0: return q if s % n == 0: return q - (s//n) return q - (s//n) - 1 q = a[-1] if s % n == 0: return q - (s//n) return q - (s//n) - 1 if __name__=="__main__": print(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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Keggs { public static void main(String args[]) throws NumberFormatException, IOException { long n; long k; Scanner sc = new Scanner(new InputStreamReader(System.in)); BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); n = sc.nextLong(); // Integer.parseInt(null, bf.read()); k = sc.nextLong(); // Long.parseLong(null, bf.read()); long[] v = new long[(int) (n + 1)]; long minu = Long.MAX_VALUE; for (int i = 0; i < n; i++) { v[i] = sc.nextLong(); minu = Math.min(minu, v[i]); } long extra = 0; for (int i = 0; i < n; i++) { extra += (v[i] - minu); } long ans = -1; if (extra >= k) { ans = minu; } else { k -= extra; long div = k / n; long rem = k % n; if (div <= minu) { minu -= div; ans = minu; if (rem != 0) { ans--; } } } System.out.println(ans); } }
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
#526_B import sys import math s = int(sys.stdin.readline().rstrip().split(" ")[1]) lne = [int(i) for i in sys.stdin.readline().rstrip().split(" ")] n = min(math.floor((sum(lne) - s) / len(lne)), min(lne)) if n >= 0: print(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
n,s = [int(i) for i in input().split()] v = [int(i) for i in input().split()] v = sorted(v) sv = sum(v) if s > sv: print(-1) else: for i in v: s-= (i-v[0]) if s<= 0: print(v[0]) else: print((n*v[0]-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.io.*; public class new4 { public static void main(String args[])throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); String str1=br.readLine(); long n=Long.parseLong(str1.substring(0,str1.indexOf(' '))); long s=Long.parseLong(str1.substring(str1.indexOf(' ')+1)); String str2= br.readLine(); String[] s2=str2.split(" "); long[] v= new long[(int)n]; long sum=0; for(int i=0;i<n;i++) { v[i]=Long.parseLong(s2[i]); sum+=v[i]; } if(sum<s) System.out.println(-1); else if(sum==s) System.out.println(0); else { quicksort(v,0,(int)n-1); long current_volume=0; for(int i=0;i<n;i++) { current_volume+=v[i]-v[(int)n-1]; v[i]=v[(int)n-1]; } if(current_volume==s) System.out.println(v[0]); if(current_volume>s) System.out.println(v[(int)n-1]); if(current_volume<s) { long a=(s-current_volume)/n; if((s-current_volume)%n==0) System.out.println(v[0]-a); else System.out.println(v[0]-a-1); } } } public static void quicksort(long[] v,int start,int end) { if(start < end) { int partition_index = partition(v,start,end); quicksort(v,start, partition_index - 1); quicksort(v,partition_index + 1, end); } } public static int partition(long v[], int start, int end) { long t; long pivot = v[end]; int pindex= start; // setting partition index as start initially for(int i=start; i<end;i++) { if(v[i]>=pivot) { t=v[i]; //swapping a[i] with element at partition index v[i]=v[pindex]; v[pindex]=t; pindex++; } } t=v[pindex]; //swapping element at pindex and pivot v[pindex]=v[end]; v[end]=t; return(pindex); } }
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.util.Arrays; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Andy Phan */ public class p001084b { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long s = in.nextLong(); long[] a = new long[n]; long tot = 0; for(int i = 0; i < n; i++) { a[i] = in.nextLong(); tot += a[i]; } Arrays.sort(a); long min = a[0]; if(s > tot) {System.out.println(-1); return;} for(int i = n-1; i > 0; i--) { if(s <= a[i] - min) { System.out.println(min); return; } tot += a[i]; s -= a[i]-min; a[i] = min; } System.out.println(Math.max(min - s/n - ((s%n != 0) ? 1 : 0), 0)); } }
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
# Author: Harrison Whitner # Date: 02/13/19 # get the first line of input, which contains the number of kegs and size of glass n, s = list(map(int, input().split())) # get the next line which contains the keg amounts kegs = list(map(int, input().split())) # print -1 if there is not enough kvass if sum(kegs) < s: print(-1) # there is enough kvass else: # sort the keg amounts kegs.sort(reverse=True) i = 0 while s > 0: # find difference between ith keg and last keg (should be least) diff = kegs[i] - kegs[n - 1] # the ith keg still has more than the least if diff > 0: # then pull kvass from the ith keg until it is equal to the least s -= diff kegs[i] -= diff # increment i if i < n - 2: i += 1 # TODO: remove this debug pring # print('removed', diff, 'liter from the', i, 'th keg') # otherwise we have drained the kegs to be less than or equal to the starting least else: # if all kegs have the same amount and remaining s is greater than n if s > n: # take an equal amount from all kegs and remove the total from s temp = s // n s -= temp * n kegs = [k - temp for k in kegs] # else an equal amount cannot be removed from each keg else: # take 1 liter from the first keg and subtract 1 liter from the first keg # it is not necessary to go through and remove 1 liter from each keg until # s is 0, because we know that the least keg amount will be k[0]-1 because # s <= n kegs[0] -= 1 s = 0 # TODO: remove this debug pring # print('removed 1 liter from the', i+1, 'th keg') # TODO: remove this debug pring # print(kegs) # print the value of the keg with the least amount print(min(kegs))
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.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Today { public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); long s = sc.nextLong(); int a[] = new int[n]; int min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); min = Math.min(a[i], min); } long ans = 0; for (int i = 0; i < n; i++) if (a[i] > min) ans += (a[i] - min); if (ans >= s) { System.out.println(min); return; } else { long needed = s - ans; int nmin = min; double anss = (needed) / (n * 1.0); if (anss > min) { System.out.println(-1); return; } else { System.out.println(min - (long) Math.ceil(anss)); return; } } // System.out.println(-1); // pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(4000); } } }
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()) v = list(map(int, input().split())) if sum(v) < s: print(-1) exit(0) mv = min(v) for i in range(n): s -= v[i] - mv if s < 1: print(mv) exit(0) ans = mv - s // n if s % n != 0: ans -= 1 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
n, s = map(int, input().split()) A = list(map(int, input().split())) mn = 1e18 res = 0 for i in A : mn = min(i, mn) res += i if res < s : print(-1) elif s < res - mn*n : print(mn) else : s = s - (res - mn*n) print(mn - (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
#include <bits/stdc++.h> using namespace std; int main() { int n; long long s, sum = 0; cin >> n >> s; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } if (sum < s) { cout << -1; return 0; } long long c = 0; sort(a, a + n); if (n == 1) { cout << a[0] - s; return 0; } for (int i = 1; i < n; i++) { c += a[i] - a[0]; if (c >= s) { cout << a[0]; return 0; } a[i] = a[0]; } int i = 1, b; if ((s - c) % n == 0) b = (s - c) / n; else b = ((s - c) / n) + 1; if (a[0] - b < 0) cout << -1; else cout << a[0] - b; }
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; template <typename T> ostream& operator<<(ostream& os, vector<T>&& object) { for (auto& i : object) { os << i; } return os; } template <typename T> ostream& operator<<(ostream& os, vector<vector<T>>&& object) { for (auto& i : object) { os << i << endl; } return os; } const size_t MAX_N = (long long)2e5 + 10; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, s, sum = 0; cin >> n >> s; vector<long long> a(n); for (auto& i : a) { cin >> i; sum += i; } if (sum < s) { cout << "-1\n"; return 0; } sort(a.begin(), a.end(), less<long long>()); long long cmin = a[0]; long long accv = 0; for (auto& i : a) { accv += i - cmin; i = cmin; } if (accv >= s) { cout << cmin << "\n"; return 0; } else { cout << max(0LL, cmin - (long long)ceil(((long double)s - accv) / n)) << 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
""""""""""""""""""""""""""""""""""""""""""""" | author: mr.math - Hakimov Rahimjon | | e-mail: [email protected] | """"""""""""""""""""""""""""""""""""""""""""" #inp = open("lepus.in", "r"); input = inp.readline; out = open("lepus.out", "w"); print = out.write TN = 1 # =========================================== def solution(): from math import ceil n, s = map(int, input().split()) a = list(map(int, input().split())) ans = 0 if s > sum(a): ans = -1 else: v = min(a) for i in range(n): s -= a[i]-v if s <= 0: ans = v else: ans = ceil(v-(s+n-1)/n) print(ans) # =========================================== while TN != 0: solution() TN -= 1 # =========================================== #inp.close() #out.close()
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 _mp(): return map(int,raw_input().split()) n,s=_mp() a=_mp() tot=sum(a) if tot<s: print -1 else: z=min(a) left=tot-z*n if left>s: print z else: left=s-left k=left/n if left%n==0: print z-k else: print z-k-1
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 n, s, a[1010], sum, mim = 0x3f3f3f3f, l, r, mid, ans = 0; int can(long long x) { return (sum - n * x >= s ? 1 : 0); } int main() { cin >> n >> s; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; mim = min(mim, a[i]); } if (sum < s) { cout << -1; return 0; } l = 0; r = mim; while (l <= r) { mid = (l + r) / 2; if (can(mid)) { l = mid + 1; ans = max(ans, mid); } else r = mid - 1; } 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
import java.util.*; import java.io.*; // Solution public class Main { public static void main (String[] argv) { new Main(); } boolean test = false; final int MOD = 998244353; public Main() { FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in))); //FastReader in = new FastReader(new BufferedReader(new FileReader("Main.in"))); int n = in.nextInt(); long s = in.nextLong(); int[] a = new int[n]; long sum = 0L; int hi = 0, lo = 0; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); sum += a[i]; hi = max(hi, a[i]); } if (sum < s) { System.out.println(-1); return; } while (lo <= hi) { int mid = (lo + hi) / 2; if (ok(a, s, mid)) lo = mid + 1; else hi = mid - 1; } System.out.println(hi); } private boolean ok(int[] a, long s, int r) { for (int v : a) { if (v < r) return false; s -= v - r; } return s <= 0; } private long overlap(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { if (x1 > x3) return overlap(x3, y3, x4, y4, x1, y1, x2, y2); if (x3 > x2 || y4 < y1 || y3 > y2) return 0L; //(x3, ?, x2, ?) int yL = Math.max(y1, y3); int yH = Math.min(y2, y4); int xH = Math.min(x2, x4); return f(x3, yL, xH, yH); } //return #black cells in rectangle private long f(int x1, int y1, int x2, int y2) { long dx = 1L + x2 - x1; long dy = 1L + y2 - y1; if (dx % 2 == 0 || dy % 2 == 0 || (x1 + y1) % 2 == 0) return 1L * dx * dy / 2; return 1L * dx * dy / 2 + 1; } private int dist(int x, int y, int xx, int yy) { return abs(x - xx) + abs(y - yy); } private boolean less(int x, int y, int xx, int yy) { return x < xx || y > yy; } private int mul(int x, int y) { return (int)(1L * x * y % MOD); } private int add(int x, int y) { return (x + y) % MOD; } private int nBit1(int v) { int v0 = v; int c = 0; while (v != 0) { ++c; v = v & (v - 1); } return c; } private long abs(long v) { return v > 0 ? v : -v; } private int abs(int v) { return v > 0 ? v : -v; } private int common(int v) { int c = 0; while (v != 1) { v = (v >>> 1); ++c; } return c; } private void reverse(char[] a, int i, int j) { while (i < j) { swap(a, i++, j--); } } private void swap(char[] a, int i, int j) { char t = a[i]; a[i] = a[j]; a[j] = t; } private int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } private long gcd(long x, long y) { if (y == 0) return x; return gcd(y, x % y); } private int max(int a, int b) { return a > b ? a : b; } private long max(long a, long b) { return a > b ? a : b; } private int min(int a, int b) { return a > b ? b : a; } private long min(long a, long b) { return a > b ? b : a; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(BufferedReader in) { br = in; } String next() { while (st == null || !st.hasMoreElements()) { try { String line = br.readLine(); if (line == null || line.length() == 0) return ""; st = new StringTokenizer(line); } catch (IOException e) { return ""; //e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { return ""; //e.printStackTrace(); } return str; } } }
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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); long s = sc.nextLong(); int [] a = new int[n]; int min = Integer.MAX_VALUE; long sum = 0; for (int i = 0; i < n; ++i) { a[i] = sc.nextInt(); min = Math.min(min, a[i]); sum += a[i]; } if(sum < s) out.println(-1); else{ long tmp = 0; for (int i = 0; i < a.length; ++i) { tmp += a[i] - min; } s -= tmp; if(s > 0){ long res = (s + n - 1) / n; out.println(min - res); } else{ out.println(min); } } out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(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
#include <bits/stdc++.h> using namespace std; long long n, a[1005], s, mini, tot; int main() { cin >> n >> s; tot = 0; mini = 1e15; for (int i = 1; i <= n; i++) { cin >> a[i]; tot += a[i]; mini = min(a[i], mini); } if (tot < s) { cout << -1; return 0; } tot = tot - s; mini = min(tot / n, mini); cout << mini; }
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.*; import java.io.*; import static java.lang.Math.*; public class Main { static final long MOD = 1_000_000_007, INF = 1_000_000_000_000_000_000L; static final int INf = 1_000_000_000; static FastReader reader; static PrintWriter writer; public static void main(String[] args) { Thread t = new Thread(null, new O(), "Integer.MAX_VALUE", 100000000); t.start(); } static class O implements Runnable { public void run() { try { magic(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } public static class FastReader { BufferedReader br; StringTokenizer st; public 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int n,arr[]; static long s; static void magic() { reader = new FastReader(); writer = new PrintWriter(System.out, true); n = reader.nextInt(); s = reader.nextLong(); long sum = 0; arr = new int[n]; for(int i=0;i<n;++i) { arr[i] = reader.nextInt(); sum+=arr[i]; } if(s>sum) { writer.println(-1); } else { int low = 0, high = 1000000000,mid; while(low<high) { mid = (low+high)>>1; if(check(mid)) { if(!check(mid+1)) { low = mid; break; } else low = ++mid; } else high = --mid; } writer.println(low); } } static boolean check(int x) { long S = 0; for(int i=0;i<n;++i) { if(arr[i]<x) { return false; } else { S+=arr[i]-x; } } return S>=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
#include <bits/stdc++.h> const int N = 1e7 + 2 + 5, M = 1e3 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; int dxx[] = {-1, 1, -1, 1, -2, -2, 2, 2}; int dyy[] = {-2, -2, 2, 2, 1, -1, 1, -1}; using namespace std; long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); } long long lcm(long long x, long long y) { return ((x * y) / gcd(x, y)); } void file() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } vector<long long> v; int n; bool ok(int md, long long s) { long long ans = 0; for (int i = 0; i < n; i++) { s -= (v[i]); s += md; if (s <= 0) return 1; } return 0; } int main() { file(); long long s; cin >> n >> s; v.resize(n); long long sum = 0; for (int i = 0; i < n; i++) { cin >> v[i]; sum += v[i]; } if (sum < s) return cout << -1, 0; int st = 0, ed = 1e9, md, cur; while (st <= ed) { md = (st + ed) / 2; if (ok(md, s)) { cur = md; st = md + 1; } else ed = md - 1; } int nw = *min_element(((v).begin()), ((v).end())); cout << min(cur, nw) << '\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
# alpha = "abcdefghijklmnopqrstuvwxyz" from heapq import heappop, heappush t = 1#int(input()) for test in range(t): n,s = (map(int, input().split())) a = list(map(int, input().split())) if sum(a)<s: print(-1) continue heap = [] for i in range(n): heappush(heap, (-a[i],i)) if n==1: print(a[0]-s) continue count = 1 cur = heappop(heap) nex = heappop(heap) ans = -1 while s>0: # indices = [] # indices.append(cur[1]) while cur[0]==nex[0]: count+=1 # indices.append(nex[1]) if len(heap)==0: ans = ((-1*count*nex[0] - s)//count) s = 0 break nex = heappop(heap) s-= count*(-cur[0]+nex[0]) cur = nex # print(s, count, heap) # print(ans) if ans==-1: heappush(heap, nex) print(-max(heap)[0]) else: 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
import sys n, s = [int(s) for s in input().split()] array = [int(s) for s in input().split()] sorted_array = sorted(array) min_ = sorted_array[0] if sum(array) < s: print(-1) sys.exit(0) if sum(array) == s: print(0) sys.exit(0) if sum(array) - n * min_ >= s: print(min_) else: print(min_ - ((s - (sum(array) - n * min_)) // n + int((s - (sum(array) - n * min_)) % n > 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
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long SIZE = 100000; const int INF = 0x3f3f3f3f; const long long ll_INF = 0x3f3f3f3f3f3f3f3f; const long double PI = acos(-1); const long long MAXN = numeric_limits<long long>::max(); const long long MAX = 2000000; void disp(vector<long long> v) { for (auto u : v) cout << u << " "; cout << '\n'; } void solve() { long long n, s, tot = 0, x; cin >> n >> s; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; sort((v).begin(), (v).end()); for (long long i = 1; i < n; i++) { tot += v[i] - v[0]; } if (tot >= s) cout << v[0]; else { s -= tot; if (s % n == 0) x = s / n; else x = s / n + 1; if (x > v[0]) cout << -1; else cout << v[0] - x; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); 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
import java.util.Scanner; import java.util.HashMap; import java.util.Stack; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.lang.StringBuilder; public class idk { static Scanner scn = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub long n = scn.nextLong(); long k=scn.nextLong(); int arr[]=new int[(int) n]; long sum=0,min=Integer.MAX_VALUE; for(int i=0;i<n;i++) { arr[i]=scn.nextInt(); sum+=arr[i]; if(min>=arr[i]) min=arr[i]; } if(sum<k) System.out.println("-1"); else if(sum==k) System.out.println("0"); else { long sol=(sum-k)/n; if(sol<=min) System.out.println(sol); else 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
def foo(s,v): n = len(v) if sum(v) < s: return -1 mini = min(v) for i in range(n): s -= (v[i]-mini) if s <= 0: return mini #now common level is mini k = s//n s -= n*(k) mini -= k while s > 0: s -= n mini -= 1 return mini n,s = list(map(int,input().strip().split(" "))) v = list(map(int,input().strip().split(" "))) print(foo(s,v))
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 maxn = 1e5 + 10; long long n, v, k; int main() { cin >> n >> k; long long mi = 0x3f3f3f3f, sum = 0; for (int i = 1; i <= n; i++) { cin >> v; mi = min(mi, v); sum += v; } if (sum < k) printf("-1\n"); else { sum -= k; printf("%I64d\n", min(mi, sum / 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
R=lambda:map(int,input().split()) n,s=R() v=[*R()] m=min(v) s=max(0,s-sum(v)+n*m) print(max(-1,m+-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; #pragma GCC optimize("O2") vector<long long int> ar[1000001]; long long int vis[1000001], dis[1000001], level[1000001]; const int MAX_SIZE = 1000001; const int N = 2000010; const int mod = 1e9 + 7; vector<int> isprime(MAX_SIZE, true); vector<int> prime; vector<int> SPF(MAX_SIZE); long long int fact[N]; void manipulated_seive(int N) { isprime[0] = isprime[1] = false; for (int i = 2; i < N; ++i) { if (isprime[i]) { prime.push_back(i); SPF[i] = i; } for (int j = 0; j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; ++j) { isprime[i * prime[j]] = false; SPF[i * prime[j]] = prime[j]; } } } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } unordered_map<long long int, long long int> myp; void primeFactors(long long int n) { while (n % 2 == 0) { myp[2]++; n = n / 2; } for (long long int i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { myp[i]++; n = n / i; } } if (n > 2) myp[n]++; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int findlcm(long long int a, long long int b) { return a * b / gcd(a, b); } long long int power(long long int a, long long int b) { long long int res = 1; while (b) { if (b % 2) res *= a; a *= a; b /= 2; } return res; } long long int power_mod(long long int a, long long int b, long long int p) { long long int res = 1; while (b) { if (b % 2) { res *= a; res %= p; } a *= a; a %= p; b /= 2; } return res; } long long int mod_inverse(long long int x) { return power_mod(x, mod - 2, mod); } long long int nCr(long long int n, long long int r) { if (r == 0) return 1; long long int a = fact[n]; long long int b = mod_inverse(fact[n - r]); long long int c = mod_inverse(fact[r]); return (((a * b) % mod) * c) % mod; } void fun() { long long int n, s, a; cin >> n >> s; vector<long long int> v; long long int s1 = 0; for (long long int i = 1; i <= n; i++) { cin >> a; v.push_back(a); s1 += a; } if (s1 < s) { cout << -1 << '\n'; return; } sort(v.begin(), v.end()); long long int sum = 0; for (long long int i = 1; i < n; i++) { long long int dif = v[i] - v[0]; sum += dif; } if (sum >= s) { cout << v[0] << '\n'; return; } s -= sum; long long int ans = floor(v[0] - (s + n - 1) / n); cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); unsigned long long int t; t = 1; while (t--) { cout << fixed; cout << setprecision(10); fun(); } 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.*; import java.math.BigInteger; import java.io.*; public class Codeforces{ static class MyScanner{ BufferedReader br; StringTokenizer st; MyScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } MyScanner(FileReader fileReader){ br = new BufferedReader(fileReader); } String nn(){ while(st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } int ni(){ return Integer.parseInt(nn()); } double nd(){ return Double.parseDouble(nn()); } long nl(){ return Long.parseLong(nn()); } } private static PrintWriter out; public static void main(String[] args) throws IOException{ // FileReader fileReader = new FileReader(new File("JavaFile.txt")); // out = new PrintWriter(new PrintStream("JavaOutputFile.txt")); // MyScanner sc = new MyScanner(fileReader); MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.ni(); long s = sc.nl(); int[] ar = new int[n]; long total = 0; int curMin = Integer.MAX_VALUE; for(int i = 0; i < n; i++){ ar[i] = sc.ni(); total += ar[i]; curMin = Math.min(curMin, ar[i]); } out.println(getAns(n, ar, s, total, curMin)); out.close(); } private static long getAns(int n, int[] ar, long s, long total, int curMin){ if(total < s) return -1; for(int i = 0; i < n; i++){ s -= ar[i] - curMin; ar[i] = curMin; } // System.out.println(s + " " + Arrays.toString(ar)); if(s <= 0) return curMin; long req = s / n; if(s % n > 0) req++; return Math.max(0, ar[0] - req); } }
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.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class CF { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TASK solver = new TASK(); solver.solve(in, out); out.close(); } static class TASK { public void solve(Scanner in, PrintWriter out) { long n = in.nextInt(); long m = in.nextLong(); long a[] = new long[(int)n]; long Sum = 0; for(int i = 0; i < n; ++i) { a[i] = in.nextInt(); Sum += a[i]; } if(Sum < m) { out.print(-1); return; } Arrays.sort(a); long curSum = 0; for(int i = 0; i < n; ++i) { if(a[i] - a[0] > 0) { curSum += (a[i] - a[0]); a[i] -= a[0]; } } m = Math.max(m - curSum, 0); out.print(a[0] - (m + n - 1) / 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
def ans(a,n,s): total = (sum(a) - s) m = min(a) if total < 0: return -1 if total//n > m: return m return total//n n,s = map(int,raw_input().split()) arr = [int(i) for i in raw_input().split()] print(ans(arr,n,s))
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.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeSet; public class q54 { static final double EPS = 1e-9; static pair[] x = new pair[3]; static int ac1 = -1; static int ac2 = -1; static int noox = -1; static int nooy = -1; static int same = -1; static int grid[][] = new int[1001][1001]; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long s = sc.nextlong(); long x [] = new long [n]; long sum=0; long min = (int)1e9; for(int i=0;i<n;i++) { x[i]=sc.nextlong(); sum+=x[i]; min= Math.min(min, x[i]); } if(sum<s) { System.out.println(-1); return; } long cur=0; for(int i=0;i<n;i++) { if(x[i]>min) { cur+=x[i]-min; x[i]=min; } } if(cur>=s) { System.out.println(min); return; } long need = s-cur; long left =sum-cur; long hm = need/n; // System.out.println(need+" "+left+" "+hm); if(need%n!=0) { hm++; } // System.out.println(min); min-=hm; System.out.println(min); } public static class pair implements Comparable<pair> { int x, y; pair(int a, int b) { x = a; y = b; } public int compareTo(pair p) { if (Math.abs(x - p.x) > EPS) return x > p.x ? 1 : -1; if (Math.abs(y - p.y) > EPS) return y > p.y ? 1 : -1; return 0; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public long nextlong() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); long res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
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
if __name__ == "__main__": n,s=map(int, input().split()) ls=list(map(int, input().split())) m=sum(ls)-min(ls)*n if s<=m: print(min(ls)) elif s>sum(ls): print("-1") else: print((sum(ls)-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() { long long n, s, val; cin >> n >> s; vector<long long> vec; long long sumtot = 0; for (long long i = 0; i < n; i++) { cin >> val; sumtot += val; vec.push_back(val); } if (sumtot < s) { cout << -1; return 0; } sort(vec.begin(), vec.end()); long long value = (sumtot - s) / n; if (value < vec[0]) cout << value; else cout << vec[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
''' Thruth can only be found at one place - THE CODE ''' ''' Copyright 2018, SATYAM KUMAR''' from sys import stdin, stdout import cProfile, math from collections import Counter from bisect import bisect_left,bisect,bisect_right import itertools from copy import deepcopy from fractions import Fraction import sys, threading sys.setrecursionlimit(10**6) # max depth of recursion threading.stack_size(2**27) # new thread will get stack of such size printHeap = str() memory_constrained = False P = 10**9+7 import sys sys.setrecursionlimit(10000000) class Operation: def __init__(self, name, function, function_on_equal, neutral_value=0): self.name = name self.f = function self.f_on_equal = function_on_equal def add_multiple(x, count): return x * count def min_multiple(x, count): return x def max_multiple(x, count): return x sum_operation = Operation("sum", sum, add_multiple, 0) min_operation = Operation("min", min, min_multiple, 1e9) max_operation = Operation("max", max, max_multiple, -1e9) class SegmentTree: def __init__(self, array, operations=[sum_operation, min_operation, max_operation]): self.array = array if type(operations) != list: raise TypeError("operations must be a list") self.operations = {} for op in operations: self.operations[op.name] = op self.root = SegmentTreeNode(0, len(array) - 1, self) def query(self, start, end, operation_name): if self.operations.get(operation_name) == None: raise Exception("This operation is not available") return self.root._query(start, end, self.operations[operation_name]) def summary(self): return self.root.values def update(self, position, value): self.root._update(position, value) def update_range(self, start, end, value): self.root._update_range(start, end, value) def __repr__(self): return self.root.__repr__() class SegmentTreeNode: def __init__(self, start, end, segment_tree): self.range = (start, end) self.parent_tree = segment_tree self.range_value = None self.values = {} self.left = None self.right = None if start == end: self._sync() return self.left = SegmentTreeNode(start, start + (end - start) // 2, segment_tree) self.right = SegmentTreeNode(start + (end - start) // 2 + 1, end, segment_tree) self._sync() def _query(self, start, end, operation): if end < self.range[0] or start > self.range[1]: return None if start <= self.range[0] and self.range[1] <= end: return self.values[operation.name] self._push() left_res = self.left._query(start, end, operation) if self.left else None right_res = self.right._query(start, end, operation) if self.right else None if left_res is None: return right_res if right_res is None: return left_res return operation.f([left_res, right_res]) def _update(self, position, value): if position < self.range[0] or position > self.range[1]: return if position == self.range[0] and self.range[1] == position: self.parent_tree.array[position] = value self._sync() return self._push() self.left._update(position, value) self.right._update(position, value) self._sync() def _update_range(self, start, end, value): if end < self.range[0] or start > self.range[1]: return if start <= self.range[0] and self.range[1] <= end: self.range_value = value self._sync() return self._push() self.left._update_range(start, end, value) self.right._update_range(start, end, value) self._sync() def _sync(self): if self.range[0] == self.range[1]: for op in self.parent_tree.operations.values(): current_value = self.parent_tree.array[self.range[0]] if self.range_value is not None: current_value = self.range_value self.values[op.name] = op.f([current_value]) else: for op in self.parent_tree.operations.values(): result = op.f( [self.left.values[op.name], self.right.values[op.name]]) if self.range_value is not None: bound_length = self.range[1] - self.range[0] + 1 result = op.f_on_equal(self.range_value, bound_length) self.values[op.name] = result def _push(self): if self.range_value is None: return if self.left: self.left.range_value = self.range_value self.right.range_value = self.range_value self.left._sync() self.right._sync() self.range_value = None def __repr__(self): ans = "({}, {}): {}\n".format(self.range[0], self.range[1], self.values) if self.left: ans += self.left.__repr__() if self.right: ans += self.right.__repr__() return ans def display(string_to_print): stdout.write(str(string_to_print) + "\n") def primeFactors(n): #n**0.5 complex factors = dict() for i in range(2,math.ceil(math.sqrt(n))+1): while n % i== 0: if i in factors: factors[i]+=1 else: factors[i]=1 n = n // i if n>2: factors[n]=1 return (factors) def isprime(n): """Returns True if n is prime.""" if n < 4: return True if n % 2 == 0: return False if n % 3 == 0: return False i = 5 w = 2 while i * i <= n: if n % i == 0: return False i += w w = 6 - w return True def test_print(*args): if testingMode: print(args) def display_list(list1, sep=" "): stdout.write(sep.join(map(str, list1)) + "\n") def get_int(): return int(stdin.readline().strip()) def get_tuple(): return map(int, stdin.readline().split()) def get_list(): return list(map(int, stdin.readline().split())) memory = dict() def clear_cache(): global memory memory = dict() def cached_fn(fn, *args): global memory if args in memory: return memory[args] else: result = fn(*args) memory[args] = result return result # -------------------------------------------------------------- MAIN PROGRAM TestCases = False testingMode = False optimiseForReccursion = False #Can not be used clubbed with TestCases def comp(li): return len(li) def main(): n,k=get_tuple() li = get_list() sm = sum(li) res = 0 print(min(math.floor(Fraction(sm-k,n)),min(li))) if sm-k>=0 else print(-1) # --------------------------------------------------------------------- END if TestCases: for _ in range(get_int()): cProfile.run('main()') if testingMode else main() else: (cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()
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.*; public class vk18 { public static void main(String[]st) { Scanner scan=new Scanner(System.in); int n,i; long sum=0,k; n=scan.nextInt(); k=scan.nextLong(); long a[]=new long[n]; for(i=0;i<n;i++) { a[i]=scan.nextLong(); sum+=a[i]; } if(sum<k) System.out.println("-1"); else { Arrays.sort(a); i=(int)((sum-k)/n); System.out.println(Math.min(i,a[0])); } } }
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() { ios_base::sync_with_stdio; cin.tie(NULL); cout.tie(NULL); long long int i, j, sum = 0, n, m, min = 1000000001, ans, s; cin >> n >> s; long long int a[n]; for (i = 0; i < n; i++) { cin >> j; sum = sum + j; if (j <= min) min = j; } ans = (sum - s) / n; if (sum < s) { cout << -1; return 0; } if (ans >= min) cout << min; else 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
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; import javafx.util.*; public class Solution { static class com implements Comparator<Pair<Integer,Integer>>{ public int compare(Pair<Integer,Integer> a,Pair<Integer,Integer> b){ int x=a.getKey(); int y=b.getKey(); return x-y; } } public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); int n; long s; n=sc.nextInt(); s=sc.nextLong(); int A[]=new int[n]; long sum=0; for(int i=0;i<n;i++){ A[i]=sc.nextInt(); sum+=A[i]; } if(sum< s){ System.out.println(-1); } else{ Arrays.sort(A); int i=n-1; int j=1; int flag=0; for(i=n-1;i>=1;i--){ long x=((long)j)*((long)(A[i]-A[i-1])); j++; s=s-x; if(s<=0){ System.out.println(A[0]); flag=1; break; } } long z=0; if(s%n==0){ z=s/n; } else{ z=s/n; z++; } if(flag==0){ System.out.println(A[0]-z); } } } }
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() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long int n = 0, k = 0, sum = 0; cin >> n >> k; long long int arr[n]; for (int a = 0; a < n; a++) cin >> arr[a]; sort(arr, arr + n); for (int a = 0; a < n; a++) sum += arr[a] - arr[0]; cout << max(arr[0] - (max(k - sum, 0ll) / n + (max(k - sum, 0ll) % n != 0 ? 1ll : 0ll)), -1ll); }
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 main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); size_t n; int64_t s; std::cin >> n >> s; std::vector<int64_t> v(n); for (size_t i = 0; i < n; ++i) { std::cin >> v[i]; } const int64_t min = *min_element(v.begin(), v.end()); int64_t cur = 0; for (size_t i = 0; i < n; ++i) { cur += v[i] - min; } if (cur >= s) { std::cout << min << '\n'; return 0; } const int64_t need = (s - cur - 1) / int64_t(n) + 1; if (min < need) { std::cout << "-1\n"; } else { std::cout << min - need << '\n'; } }
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 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 lo = 1; int hi = 1_000_000_000; 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 java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()),i; long c=Long.parseLong(st.nextToken()),k=1000000005,j=0; st=new StringTokenizer(br.readLine()); long[] a=new long[n]; for(i=0;i<n;i++) { a[i]=Long.parseLong(st.nextToken()); } Arrays.sort(a); k=a[0]; //ArrayUtils.reverse(a); for(i=n-1;i>=0;i--) { if(a[i]>k) j+=a[i]-k; if(j>=c) { System.out.println(k); System.exit(0); } } c=c-j; if(k*n<c) System.out.println(-1); else { System.out.println(k-(c+n-1)/n); } } }
JAVA