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 java.util.Arrays;
import java.util.Scanner;
public class Kvass526B {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int kegs = s.nextInt();
long glass = s.nextLong();
long sum =0l;
int arr[] = new int[kegs];
for(int i =0;i<kegs;i++) {
arr[i] = s.nextInt();
sum+=arr[i];
}
if(sum<glass) {
System.out.println(-1);
return;
}
Arrays.sort(arr);
long filled = 0l;
int min = arr[0];
for(int i = arr.length-1;i>=0;i--) {
if(arr[i]!=min) {
filled+=arr[i]-min;
arr[i] = min;
}
if(filled>=glass)
break;
}
// for(int o:arr) {
// System.out.print(o+" ");
// }
// System.out.println();
// System.out.println("Filled :"+filled);
if(filled>=glass) {
System.out.println(min);
return;
}
long remaining = glass-filled;
//System.out.println("Remaining : "+remaining);
long len = arr.length;
sum = min*len;
long ans = (sum-remaining)/arr.length;
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 |
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner input=new Scanner (System.in);
while(input.hasNext()) {
long sum=0;
int a=input.nextInt();
long min=999999999;
long b=input.nextLong();
int w[]=new int [a+1];
for(int i=1;i<=a;i++) {
w[i]=input.nextInt();
if(min>w[i]) {
min=w[i];
}
sum+=w[i];
}
if(sum<b) {
System.out.println(-1);
}else if(sum==b) {
System.out.println(0);
}else {
long tt=sum-b;
if(tt<min) {
System.out.println(0);
}else {
System.out.println(Math.min(tt/a, 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 itertools import cycle
if __name__ == '__main__':
n, s = [int(j) for j in input().split(' ')]
V = [int(j) for j in input().split(' ')]
V = sorted(V, reverse=True) + [0]
diffs = [V[i] - V[i+1] for i in range(len(V)-1)]
i = 0
last_i = len(V)-2
for i in range(len(V)-1):
v = V[i]
v_next = V[i+1]
diff = v - v_next
if i == last_i:
boch_vsego = (len(V) - 1)
if s % boch_vsego:
nado = s // boch_vsego + 1
else:
nado = s // boch_vsego
if V[i] < nado:
print(-1)
break
else:
print(V[i] - nado)
break
else:
s -= diff * (i+1)
if s <= 0:
print(V[-2])
break
| 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.math.BigInteger;
import java.util.*;
public class Main implements Runnable {
int maxn = (int)1e3+111;
int inf = (int)1e9;
long mod = (long)1e9+7;
int n,m,k;
long a[] = new long[maxn];
void solve() throws Exception {
n = in.iInt();
long s = in.lLong();
for (int i=1; i<=n; i++) {
a[i] = in.lLong();
}
long left = 0L;
long right = Long.MAX_VALUE;
long ans = -1;
while (left<=right) {
long mid = (left+right)/2L;
if (checkMid(mid,s)) {
left = mid + 1;
ans = mid;
} else {
right = mid - 1;
}
}
out.println(ans);
}
private boolean checkMid(long mid, long s) {
long sum = 0L;
for (int i=1; i<=n; i++) {
if (a[i]-mid<0) return false;
sum += Math.max(0L, a[i]-mid);
}
return (sum>=s);
}
class Pair {
int x;
int y;
public Pair(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;
Pair pair = (Pair) o;
return x == pair.x &&
y == pair.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
String fileInName = "";
static Throwable throwable;
public static void main (String [] args) throws Throwable {
Thread thread = new Thread(null, new Main(), "", (1 << 26));
thread.start();
thread.join();
thread.run();
if (throwable != null)
throw throwable;
}
FastReader in;
PrintWriter out;
public void run() {
try {
if (!fileInName.isEmpty()) {
in = new FastReader(new BufferedReader(new FileReader(fileInName+".in")));
out = new PrintWriter(new BufferedWriter(new FileWriter(fileInName+".out")));
} else {
in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
}
solve();
} catch(Exception e) {
throwable = e;
} finally {
out.close();
}
}
class FastReader {
BufferedReader bf;
StringTokenizer tk = null;
public FastReader(BufferedReader bf) {
this.bf = bf;
}
public Integer iInt() throws Exception {
return Integer.parseInt(sString());
}
public Long lLong() throws Exception {
return Long.parseLong(sString());
}
public Double dDouble() throws Exception {
return Double.parseDouble(sString());
}
public String sString () throws Exception {
if (tk==null || !tk.hasMoreTokens()) {
tk = new StringTokenizer(bf.readLine());
}
if (!tk.hasMoreTokens()) return sString();
else
return tk.nextToken();
}
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j = 0, k, n, s, sum = 0;
cin >> n >> s;
long long int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
sum = sum + a[i];
}
if (s > sum) {
cout << -1;
return 0;
}
if (s == sum) {
cout << 0;
return 0;
}
sort(a, a + n);
for (i = 0; i < n; i++) {
j = j + (a[i] - a[0]);
}
if (j >= s) {
cout << a[0];
return 0;
} else {
s = s - j;
if (s % n == 0) {
k = s / n;
} else {
k = s / n + 1;
}
cout << a[0] - k;
return 0;
}
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
from collections import deque
n,k=map(int,input().split())
l=list(map(int,input().split()))
if sum(l)<k:
print(-1)
else:
t=0
m=min(l)
for i in range(n):
t+=l[i]-m
l[i]=m
if t>=k:
print(m)
else:
k-=t
t=ceil(k/n)
print(m-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 | n, s = map(int, input().split())
v = [int(x) for x in input().split()]
t = sum(v)
if sum(v) < s:
print(-1)
exit(0)
b = min(v)
kol = t - b * n
s -= kol
if s <= 0:
print(b)
exit(0)
tmp = (s + n - 1) // n
print(b - tmp)
| 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 | /* package codechef; // don't place package name! */
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 Codechef
{
public static void main (String[] args) throws java.lang.Exception
{ int i,j,x;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s[]=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
long k=Long.parseLong(s[1]);
String ss[]=br.readLine().split(" ");
long a[]=new long[n];
long min=100000000000000l;
for(i=0;i<n;i++)
{ a[i]=Long.parseLong(ss[i]);
min=Math.min(min,a[i]);
}
long sum=0;
for(i=0;i<n;i++)
sum+=a[i]-min;
if(sum>=k)
System.out.println(min);
else
{ long temp;
if((k-sum)%n==0)
temp=(k-sum)/n;
else
temp=(k-sum)/n+1;
if(min-temp>=0)
System.out.println(min-temp);
else
System.out.println("-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 | #include <bits/stdc++.h>
using namespace std;
long long v[1009], n, s, sum;
long long maxx, minn;
int main() {
minn = 0x3f3f3f3f3f;
sum = 0;
scanf("%lld%lld", &n, &s);
for (int i = 0; i < n; i++) {
scanf("%lld", &v[i]);
sum += v[i];
minn = min(minn, v[i]);
}
if (sum < s) {
printf("-1\n");
return 0;
} else if (sum == s) {
printf("0\n");
return 0;
}
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += abs(minn - v[i]);
}
if (ans >= s) {
printf("%lld\n", minn);
return 0;
} else {
long long ss = s - ans;
ans = ceil(double(ss * 1.0) / (n * 1.0));
printf("%lld\n", minn - 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.*;
public class waw{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long s = sc.nextLong();
long[] t = new long[n];
long min;
t[0] = sc.nextLong();
min = t[0];
for(int i=1;i<n;i++){
t[i] = sc.nextLong();
if(min>t[i]) min = t[i];
}
long total = 0;
for(int i=0;i<n;i++){
total += t[i] - min;
}
if(total>=s) System.out.println(min);
else{
if(total + min*n<s) System.out.println("-1");
else{
long dif = s - total;
long rep = dif/n;
min -= rep;
if(dif%n!=0) min--;
System.out.println(min);
}
}
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
long m = sc.nextLong();
long[] arr = new long[n + 1];
long sum = 0;
long min = Integer.MAX_VALUE;
for (int i = 1; i <= n; i++) {
arr[i] = sc.nextLong();
sum += arr[i];
min = Math.min(min, arr[i]);
}
if (sum < m) {
System.out.println(-1);
} else {
long t = sum - min * n;
if (t >= m) {
System.out.println(min);
} else {
long tt = m - t;
long k = (tt - 1) / n + 1;
System.out.println(min - k);
}
}
}
}
}
| 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 int n, s;
long long int kegs[1002];
bool posible(long long int act) {
long long int suma = 0;
for (int i = 1; i <= n; i++) {
if (kegs[i] > act) {
suma += (kegs[i] - act);
} else {
if (kegs[i] < act) return false;
}
}
if (suma >= s) return true;
return false;
}
int main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) cin >> kegs[i];
long long int ini = 0, fin = 1000000000, mitad;
long long int res = -1;
while (ini <= fin) {
long long int mitad = (ini + fin) / 2;
if (posible(mitad)) {
res = mitad;
ini = mitad + 1;
} else {
fin = mitad - 1;
}
}
cout << res << 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 math
n, s =list(map(int, input().split()))
keg=list(map(int, input().split()))
keg.sort()
ans=-1
while(s>0):
cmin=keg[0]
if(keg[0]<keg[-1]):
for q in range(len(keg)):
if(s<1):
break
if(keg[q]>cmin):
if(s>keg[q]-cmin):
s-=keg[q]-cmin
keg[q]=cmin
else:
keg[q]-=s
s=0
else:
keg[0]-=math.ceil(s/len(keg))
s=0
if(keg[0]<0):
print(-1)
else:
print(keg[0])
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n,s=map(int,input().split())
v=list(map(int,input().split()))
if sum(v)<s:
print("-1")
else:
p=min(v)
k=0
for i in v:
if i>p:
k+=i-p
if k>=s:
print(p)
else:
x=s-k
if x%n==0:
print(p-x//n)
else:
print(p-x//n-1)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 |
no_of_glass , kvass_needed = map(int,input().split())
volume = list(map(int,input().split()))
volume.sort()
temp = list(volume)
kvass_filled = 0
flag = 0
for i in range(1,no_of_glass):
sub = volume[i] - volume[0]
if(kvass_filled >= kvass_needed ):
flag = 1
break
if(kvass_filled + sub > kvass_needed):
sub = kvass_needed - kvass_filled
volume[i] = volume[i] - sub
kvass_filled += sub
if(sum(temp) < kvass_needed):
print(-1)
elif(flag == 1):
print(volume[0])
else:
sub = (kvass_needed - kvass_filled) // no_of_glass
add = (kvass_needed - kvass_filled) % no_of_glass
if(add>0):
add = 1
volume[0] = volume[0] -sub - add
if(volume[0] <= 0):
print(0)
else:
print(volume[0])
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n, s = map(int, input().split())
arr = [int(x) for x in input().split()]
def get_extra(mn):
global arr, s
ret = 0
for x in arr:
if x > mn :
ret += x-mn
if ret >= s :
return True
else:
return False
if(sum(arr) < s):
print("-1")
exit()
start = 0
end = min(arr)
i = 50
while start<=end:
i = i-1
if(i == 0): break
mid = (start + end) // 2
if(get_extra(mid)):
start = mid
else:
end = mid-1
if get_extra(end):
print(end)
elif get_extra(mid):
print(mid)
elif get_extra(start):
print(start)
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 = list(map(int, input().split()))
a= list(map(int, input().split()))
if s > sum(a):
print(-1)
elif s == sum(a):
print(0)
else:
a = sorted(a)
total = sum(a)
b = [0 for _ in range(len(a))]
b[0] = a[0]
for i in range(1, len(a)):
b[i] = a[i] + b[i-1]
c = [0 for _ in range(len(a))]
c[0] = total - a[0] * (len(a))
for i in range(1, len(a)):
c[i] = total - b[i] - a[i] * (len(a)-i-1)
if s <= c[0]:
print(a[0])
else:
s -= c[0]
add = s // len(a) + bool(s%len(a))
final = a[0] - add
print(final)
| 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())
allCups = list(map(int, input().split()))
sumK = sum(allCups)
minK = min(allCups)
if sumK < s:
print(-1)
else:
if sumK - (minK * n) >= s:
print(minK)
else:
s = s - (sumK - (minK * n))
#print(s)
if s % n == 0:
print(minK - (s // n))
else:
print(minK - (s // n) - 1)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | from math import ceil
n, s = list(map(int, input().split()))
A = list(map(int, input().split()))
if sum(A) < s:
print(-1)
else:
x = min(A)
y = 0
for k in range(n):
y += (A[k] - x)
if y >= s:
print(x)
else:
a = s - y
print(x - ceil(a / 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 | from sys import stdin,stdout
from itertools import combinations
from collections import defaultdict
def listIn():
return list((map(int,stdin.readline().strip().split())))
def stringListIn():
return([x for x in stdin.readline().split()])
def intIn():
return (int(stdin.readline()))
def stringIn():
return (stdin.readline().strip())
if __name__=="__main__":
n,s=listIn()
v=listIn()
if sum(v)<s:
print(-1)
exit(0)
m=min(v)
v.sort()
total_diff=0
for i in range(n):
total_diff+=v[i]-m
#print(total_diff)
if s<=total_diff:
print(m)
else:
s-=total_diff
c=s//n
s-=n*c
m-=c
if s>0:
m-=1
print(m)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m;
cin >> n >> m;
long a[n];
long long sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
sort(a, a + n, greater<int>());
if (sum < m) {
cout << "-1\n";
} else if (sum == m) {
cout << "0\n";
} else {
long long avg = (sum - m) / n;
for (int i = 0; i < n; i++) {
if (m <= 0) break;
if (a[i] > avg) {
if (m > a[i] - avg) {
a[i] = avg;
m -= (a[i] - avg);
} else {
a[i] -= m;
m = 0;
}
}
}
if (m < 0) {
cout << "-1\n";
} else {
cout << *min_element(a, a + 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,raw_input().split())
v =[int(x) for x in raw_input().split()]
v.sort();
su =sum(v)
if su < s:
print -1
else:
df = su-s
df/=n
print min(df,v[0])
| PYTHON |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n,s=map(int,input().split())
a=list(map(int,input().split()))
mini=min(a)
sumi=0
for i in range(n):
sumi+=abs(a[i]-mini)
if(s<=sumi):
print(mini)
else:
if(sumi+n*mini>=s):
rest=s-sumi
if(rest%n==0):
part=rest//n
print(mini-part)
else:
part=rest//n
print(mini-part-1)
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 math, sys, itertools
def mp():
return list(map(int, input().split()))
def main():
n, s = mp()
a = mp()
sm = sum(a)
if sm < s:
print(-1)
return
ans = (sum(a) - s) // n
print(min(ans, min(a)))
deb = 0
if deb:
file = open('input.txt', 'r')
input = file.readline
else:
input = sys.stdin.readline
main() | PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int a[4];
int main() {
int n, i, j;
long long s, sum = 0, min = 1000000010, v, temp;
cin >> n >> s;
for (i = 0; i < n; i++) {
cin >> v;
if (v < min) min = v;
sum += v;
}
if (sum < s)
cout << -1 << endl;
else {
temp = min * n;
if ((sum - temp) >= s)
cout << min << endl;
else {
s -= (sum - temp);
temp = s / n;
if (s % n) temp++;
cout << min - temp << 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 | values = map(int, raw_input().split())
n, s = values[0], values[1]
arr = map(int, raw_input().split())
arr= sorted(arr)
def get_result(arr, n, s):
if sum(arr) < s:
return -1
mini = min(arr)
for i in range(len(arr)):
s -= arr[i] - mini
arr[i] = mini
if s <= 0:
return mini
if s % len(arr) != 0:
s -= s % len(arr)
s += len(arr)
return mini - (s / len(arr))
print get_result(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.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class B {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long y[] = new long[n];
long s = sc.nextLong();
int o = 0;
y[0]=sc.nextLong();
long sum1 = y[o];
for(int i=1;i<n;++i){
y[i]=sc.nextLong();
if(y[i]<y[o]){o=i;}
sum1+=y[i];
}
long sum = 0;
for(int i =0;i<n;++i){
if(i!=o){
sum+=(y[i]-y[o]);
}
}
if(sum1<s){
System.out.println(-1);
}
else if(sum>=s){
System.out.println(y[o]);
}
else{
s = s - sum;
if(s%n==0) {
long x = s / n;
System.out.println(y[o] - x);
}
else if (s<n){
System.out.println(y[o]-1);
}
else{
long x = (s / n)+1;
System.out.println(y[o] - x);
}
}
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
//tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new B().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long mod = 1e9 + 7;
vector<long long> v;
long long n, s;
bool check(long long x) {
long long sum = 0;
for (long long i = 0; i < n; i++) {
sum += (v[i] - x);
}
return sum >= s;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
cin >> s;
long long z;
long long sum = 0;
for (long long i = 0; i < n; i++) cin >> z, v.push_back(z), sum += z;
if (sum < s) {
cout << "-1";
return 0;
}
sort(v.begin(), v.end());
long long l = 0;
long long r = v[0];
while (r > l + 1) {
long long m = (l + r) / 2;
if (check(m)) {
l = m;
} else {
r = m;
}
}
if (!check(r))
cout << l << '\n';
else
cout << r;
}
| 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 math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
n,s=map(int, raw_input().split())
v=[]
v=map(int, raw_input().split())
mini=10000000000000000000000000000
for i in xrange(0,n):
mini=min(mini,v[i])
see=0
sumi=0
for i in xrange(0,n):
see=see+v[i]-mini
sumi=sumi+v[i]
if see>=s:
print(mini)
elif sumi<s:
print(-1)
else:
get=(int)((s-see+n-1)/n)
print(mini-get)
| 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() {
cin.tie(0)->sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long n, s;
cin >> n >> s;
vector<long long> v;
long long sum = 0;
for (int i = 1; i <= n; i++) {
long long x;
cin >> x;
v.push_back(x);
sum += x;
}
if (sum < s) {
cout << -1;
return 0;
}
sort(v.begin(), v.end());
for (int i = n - 1; i >= 0; i--) {
s = s - (v[i] - v[0]);
}
if (s < 0) {
cout << v[0] << '\n';
} else {
if (s % n == 0) {
cout << v[0] - s / n;
} else
cout << v[0] - s / n - 1;
}
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | from sys import stdin
import math
# main starts
n, s = list(map(int, stdin.readline().split()))
arr = list(map(int, stdin.readline().split()))
arr.sort()
mn = min(arr)
diff = 0
for i in range(1, n):
diff += arr[i] - mn
if diff >= s:
print(mn)
exit()
else:
if diff + mn * n >= s:
s -= diff
print(mn - math.ceil(s / 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=map(int,input().split())
a=list(map(int,input().split()))
if(sum(a)<s):
print(-1)
else:
x=0
m=min(a)
for i in range(n):
x+=(a[i]-m)
if(s<=x):
print(m)
else:
d=s-x
y=d//n
r=d%n
if(r!=0):
y+=1
print(m-y) | 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 | /*
Keep solving problems.
*/
import java.util.*;
import java.io.*;
public class CFA {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
private static long MOD = 1000L * 1000L * 1000L + 7;
private static final int[] dx = {0, -1, 0, 1};
private static final int[] dy = {1, 0, -1, 0};
private static final String yes = "Yes";
private static final String no = "No";
int n;
long s;
long[] arr;
void solve() throws IOException {
n = nextInt();
s = nextLong();
arr = nextLongArr(n);
long l = 0;
long h = MOD * MOD;
while (l < h) {
long mid = (l + h) / 2;
if (check(mid)) {
l = mid + 1;
}
else {
h = mid;
}
}
outln(l - 1);
}
boolean check(long level) {
for (long v : arr) {
if (v < level) {
return false;
}
}
long sum = 0;
for (long v : arr) {
sum += v - level;
}
return sum >= s;
}
void shuffle(int[] a) {
int n = a.length;
for(int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
long gcd(long a, long b) {
while(a != 0 && b != 0) {
long c = b;
b = a % b;
a = c;
}
return a + b;
}
private void outln(Object o) {
out.println(o);
}
private void out(Object o) {
out.print(o);
}
private void formatPrint(double val) {
outln(String.format("%.9f%n", val));
}
public CFA() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new CFA();
}
public long[] nextLongArr(int n) throws IOException{
long[] res = new long[n];
for(int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
public int[] nextIntArr(int n) throws IOException {
int[] res = new int[n];
for(int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
public String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
if (x < 0) return -x;
return x;
}
template <class T>
T sqr(T a) {
return a * a;
}
const double pi = acos(-1.0);
const double eps = 1e-8;
long long i, j, mn, x, z, sum, ar[100010], n, s;
int main() {
while (2 == scanf("%lld%lld", &n, &s)) {
mn = 1e18;
sum = 0LL;
for (i = 0; i < n; i++) {
scanf("%lld", &x);
mn = min(mn, x);
sum += x;
}
x = sum - (n * mn);
if (x >= s)
printf("%lld\n", mn);
else {
sum = sum - s;
if (sum < 0)
printf("-1\n");
else {
mn = sum / n;
printf("%lld\n", mn);
}
}
}
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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(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, ScanReader in, PrintWriter out) {
int n = in.scanInt();
long s = in.scanLong();
int[] vi = new int[n];
long tt = 0;
for (int i = 0; i < n; i++) tt += (vi[i] = in.scanInt());
int max = 1000000000;
for (int i = 0; i < n; i++) max = Math.min(max, vi[i]);
int low = 0;
int high = max;
int index = -1;
while (low <= high) {
int mid = (low + high) / 2;
long temp = 0;
for (int i = 0; i < n; i++) temp += ((long) vi[i] - mid);
if (temp >= s) {
low = mid + 1;
index = mid;
} else {
high = mid - 1;
}
}
out.println(index);
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
public long scanLong() {
long integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
}
}
| 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 f(a):
return a - MIN
n, s = list(map(int, input().split()))
v = list(map(int, input().split()))
if s > sum(v):
print(-1)
else:
MIN = min(v)
new_v = list(map(f, v))
S = sum(new_v)
if S >= s:
print(MIN)
else:
s -= S
k = (s - 1) // n + 1
print(MIN - k) | PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import math
n,c=map(int,input().split())
a=list(map(int,input().split()))
s=sum(a)
if s<c:
print(-1)
exit(0)
r=0
m=min(a)
for i in a:
r+=(i-m)
if r>=c :
print(m)
else:
print(m-(math.ceil((c-r)/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 | #------------------------------what is this I don't know....just makes my mess faster--------------------------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#----------------------------------Real game starts here--------------------------------------
'''
___________________THIS IS AESTROIX CODE________________________
KARMANYA GUPTA
'''
#_______________________________________________________________#
import math
def fact(x):
if x == 0:
return 1
else:
return x * fact(x-1)
def lower_bound(li, num): #return 0 if all are greater or equal to
answer = -1
start = 0
end = len(li)-1
while(start <= end):
middle = (end+start)//2
if li[middle] >= num:
answer = middle
end = middle - 1
else:
start = middle + 1
return answer #index where x is not less than num
def upper_bound(li, num): #return n-1 if all are small or equal
answer = -1
start = 0
end = len(li)-1
while(start <= end):
middle = (end+start)//2
if li[middle] <= num:
answer = middle
start = middle + 1
else:
end = middle - 1
return answer #index where x is not greater than num
def abs(x):
return x if x >=0 else -x
def binary_search(li, val, lb, ub):
ans = 0
while(lb <= ub):
mid = (lb+ub)//2
#print(mid, li[mid])
if li[mid] > val:
ub = mid-1
elif val > li[mid]:
lb = mid + 1
else:
ans = 1
break
return ans
#_______________________________________________________________#
for _ in range(1):
n, s = map(int, input().split())
levels = list(map(int, input().split()))
if sum(levels) < s:
print(-1)
else:
least = min(levels)
for i in range(n):
s -= levels[i]-least
levels[i] = least
if s <= 0:
print(least)
else:
print(least - (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;
const int N = 1000;
int n;
long long int sum, wanted;
int vol[N];
void read() {
cin >> n >> wanted;
for (int i = 0; i < n; i++) cin >> vol[i];
}
void init() {
for (int i = 0; i < n; i++) sum += vol[i];
}
int ans() {
for (int i = 1; i < n; i++) {
int dif = vol[i] - vol[0];
if (wanted <= dif) return vol[0];
wanted -= dif;
vol[i] = vol[0];
}
long long int q;
if (wanted % n == 0)
q = wanted / n;
else
q = (wanted / n) + 1;
return vol[0] - q;
}
int main() {
read();
init();
sort(vol, vol + n);
if (sum < wanted)
cout << -1 << endl;
else
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.math.*;
import java.io.*;
public class ok{
static FastReader scan=new FastReader();
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static LinkedList<Edge>edges[];
static boolean stdin = true;
static String filein = "input";
static String fileout = "output";
static int dx[] = { -1, 0, 1, 0 };
static int dy[] = { 0, 1, 0, -1 };
int dx_8[]={1,1,1,0,0,-1,-1,-1};
int dy_8[]={-1,0,1,-1,1,-1,0,1};
static char sts[]={'U','R','D','L'};
static boolean prime[];
static long LCM(long a,long b){
return (Math.abs(a*b))/gcd(a,b);
}
public static int upperBound(long[] array, int length, long value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = low+(high-low) / 2;
if ( array[mid]>value) {
high = mid ;
} else {
low = mid+1;
}
}
return low;
}
static long gcd(long a, long b) {
if(a!=0&&b!=0)
while((a%=b)!=0&&(b%=a)!=0);
return a^b;
}
static int countSetBits(int n)
{
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
return count;
}
static void sieve(long n)
{
prime = new boolean[(int)n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
}
static boolean isprime(long x)
{
for(long i=2;i*i<=x;i++)
if(x%i==0)
return false;
return true;
}
static int perm=0,FOR=0;
static boolean flag=false;
static int len=100000000;
static ArrayList<Pair>inters=new ArrayList<Pair>();
static StringBuilder sb;
static void swap(int i,int j,StringBuilder st)
{
char tmp=st.charAt(i);
st.setCharAt(i,st.charAt(j));
st.setCharAt(j,tmp);
}
private static int next(int[] arr, int target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to right side if target is
// greater.
if(arr[mid]==target)
return mid;
if (arr[mid] <target) {
start = mid + 1;
}
// Move left side.
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
//static boolean vis[][];
static long solve(int h,long n,int cur)
{
if(h==0)
return 0;
long half=1L<<(h-1);
if(n<=half)
{
if((cur^1)==0)
return 1+solve(h-1,n,0);
else
return 2*half+solve(h-1,n,0);
}
else
{
if((cur^1)==0)
return 2*half+solve(h-1,n-half,1);
else
return 1+solve(h-1,n-half,1);
}
}
static int dist[][];
public static String removeLeadingZeroes(String str) {
String strPattern = "^0+(?!$)";
str = str.replaceAll(strPattern, "");
return str;
}
static String gcd(String str1, String str2)
{
// If str1 length is less than
// that of str2 then recur
// with gcd(str2, str1)
if (str1.length() < str2.length()) {
return gcd(str2, str1);
}
// If str1 is not the
// concatenation of str2
else if (!str1.startsWith(str2)) {
return "";
}
else if (str2.isEmpty()) {
// GCD string is found
return str1;
}
else {
// Cut off the common prefix
// part of str1 & then recur
return gcd(str1.substring(str2.length()),
str2);
}
}
public static class comp1 implements Comparator<String>{
public int compare(String o1,String o2){
return o1.length()-o2.length();
}
}
public static class comp2 implements Comparator<String>{
public int compare(String o1,String o2){
return o1.compareTo(o2);
}
}
static StringBuilder a,b;
static void swap(int i,int j)
{
char tmp=a.charAt(i);
a.setCharAt(i,a.charAt(j));
a.setCharAt(j,tmp);
}
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) ==
(int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean vis[][]=new boolean[51][51];
static char board[][];
static int n,m;
static void dfs(int x,int y,int first,int second,char color)
{
if(x>=n||y>=m||y<0||x<0||board[x][y]!=color)
return;
// out.println(x+" "+y);
if(vis[x][y])
{
out.println("Yes");
out.close();
System.exit(0);
}
vis[x][y]=true;
if(x+1!=first||y!=second)
dfs(x+1,y,x,y,color);
if(x-1!=first||y!=second)
dfs(x-1,y,x,y,color);
if(x!=first||y-1!=second)
dfs(x,y-1,x,y,color);
if(x!=first||y+1!=second)
dfs(x,y+1,x,y,color);
}
public static void main(String[] args) throws Exception
{
//SUCK IT UP AND DO IT ALRIGHT
//scan=new FastReader("div7.in");
//out = new PrintWriter("div7.out");
// System.out.println(pow((long)1e9,(long)1e9));
//int elem[]={1,2,3,4,5};
//System.out.println(11111>10000);
int tt=1;
//tt=scan.nextInt();
//int st=scan.nextInt();
//System.out.println(body.containsKey(6));
outer:while(tt-->0)
{
int n=scan.nextInt();
long s=scan.nextLong();
long arr[]=new long[n];
long sum=0;
long mn=Long.MAX_VALUE;
long tmp=s;
for(int i=0;i<n;i++)
{
arr[i]=scan.nextLong();
mn=Math.min(mn,arr[i]);
sum+=arr[i];
}
if(s>sum){
out.println(-1);
out.close();
return;
}
for(int i=0;i<n;i++)
{
if(s>0)
{
if(s<arr[i]-mn){
out.println(mn);
out.close();
return;
}
// out.println(arr[i]-mn);
long x=arr[i]-mn;
arr[i]=mn;
s-=x;
}
else{
out.println(mn);
out.close();
return;
}
}
long k=s/n;
//out.println(s);
//if(tmp==980103855476L)
// out.println("FUCK");
if(s>0){
if(arr[0]>=Math.ceil(1.0*s/n))
out.println((int)(arr[0]-Math.ceil(1.0*s/n)));
else out.println(-1);
}
else out.println(arr[0]);
//arr[0]-=k;
//arr[0]-=(s%n);
//out.println(arr[0]);
}
out.close();
//SEE UP
}
static class special{
char c;
int idx;
special(char c,int idx)
{
this.c=c;
this.idx=idx;
}
}
static long binexp(long a,long n)
{
if(n==0)
return 1;
long res=binexp(a,n/2);
if(n%2==1)
return res*res*a;
else
return res*res;
}
static long powMod(long base, long exp, long mod) {
if (base == 0 || base == 1) return base;
if (exp == 0) return 1;
if (exp == 1) return base % mod;
long R = powMod(base, exp/2, mod) % mod;
R *= R;
R %= mod;
if ((exp & 1) == 1) {
return base * R % mod;
}
else return R % mod;
}
static double dis(double x1,double y1,double x2,double y2)
{
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long mod(long x,long y)
{
if(x<0)
x=x+(-x/y+1)*y;
return x%y;
}
public static long pow(long b, long e) {
long r = 1;
while (e > 0) {
if (e % 2 == 1) r = r * b ;
b = b * b;
e >>= 1;
}
return r;
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
FastReader(String filename)throws Exception
{
br=new BufferedReader(new FileReader(filename));
}
boolean hasNext(){
String line;
while(root.hasMoreTokens())
return true;
return false;
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
public int[] nextIntArray(int arraySize) {
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextInt();
}
return array;
}
}
static class Pair implements Comparable<Pair>{
public long x;
int y,z;
public Pair(int x1, int y1) {
x=x1;
y=y1;
}
public Pair(long x,int y,int z)
{
this.x=x;
this.y=y;
this.z=z;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
public String toString() {
return x + " " + y;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Pair t = (Pair)o;
return t.x == x && t.y == y;
}
public int compareTo(Pair o)
{
return (int) (o.x-x);
}
static class pair{
int i;
int j;
pair(int i,int j){
this.i=i;
this.j=j;
}}}
static class tuple{
int x,y,z;
tuple(int a,int b,int c){
x=a;
y=b;
z=c;
}
}
static class Edge{
int d,w;
Edge(int d,int w)
{
this.d=d;
this.w=w;
}
}
}
| 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>
int main() {
long long n, s, sum = 0, m = 9999999999;
scanf("%lld %lld", &n, &s);
for (int i = 0; i < n; i++) {
long long t;
scanf("%lld", &t);
m = m < t ? m : t;
sum += t;
}
if (sum < s)
printf("-1\n");
else
printf("%lld\n", m < (sum - s) / n ? m : (sum - s) / n);
return 0;
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n, s = map(int, input().split())
arr = list(map(int, input().split()))
sno = arr[0]
for i in range(0, len(arr)):
if arr[i] < sno:
sno = arr[i]
for i in range(0, len(arr)):
if arr[i]-sno < s:
s -= (arr[i]-sno)
arr[i] = sno
else:
print(sno)
exit()
if s == 0:
print(sno)
exit()
while s > 0:
if(s>1000*n):
s-=1000*n
sno-=1000
elif(s>100*n):
s -= 100*n
sno -= 100
elif(s>10*n):
s-=10*n
sno-=10
elif(s>0):
s-=n
sno-=1
if(sno<0):
print("-1")
exit()
print(sno)
| 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 N = 1e3 + 5;
const int M = 1e4 + 5;
int n, m, w, vis[N], l[N], r[N];
long long a[N], ans[N], s;
int main() {
cin >> n >> s;
long long sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s) {
cout << -1 << endl;
return 0;
} else if (sum == s) {
cout << 0 << endl;
return 0;
}
sort(a + 1, a + 1 + n);
int now = 1;
sum = s;
while (a[now] == a[1]) {
now++;
}
for (int i = n; i >= now; i--) {
sum -= (a[i] - a[1]);
}
if (sum) {
int cnt = 0;
while (sum > 0) {
sum -= n;
cnt++;
}
cout << a[1] - cnt << endl;
} else {
cout << a[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 | import java.io.*;
import java.util.*;
public class Sol
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
String t1[]=obj.readLine().split(" ");
long n=Long.parseLong(t1[0]);
long k=Long.parseLong(t1[1]);
String temp[]=obj.readLine().split(" ");
long arr[]=new long[(int)n];
long sum=0;
int min=Integer.MAX_VALUE;
for(int i=0;i<n;i++)
{
arr[i]=Integer.parseInt(temp[i]);
sum+=arr[i];
min=Math.min(min,(int)arr[i]);
}
long left= sum-k;
if(left<0)
System.out.println(-1);
else
{
long ans= left/n;
if(ans>min)
System.out.println(min);
else
System.out.println(ans);
}
// long avg=sum/n;
// for(int i=0;i<n;i++)
// {
// if(i==n-1)
// arr[i]= sum-((n-1)*avg);
// else
// arr[i]=avg;
// }
// long left=k;
// int j=(int)n-1;
// while(left!=0)
// {
// if(j<0)
// j=(int)n-1;
// arr[j]--;
// left--;
// j--;
// }
// int min=Integer.MAX_VALUE;
// for(int i=0;i<n;i++)
// min=Math.min(min,(int)arr[i]);
// 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 | __author__ = 'tanunia'
from sys import stdin
n, s = [int(x) for x in stdin.readline().strip().split()]
v = [int(x) for x in stdin.readline().strip().split()]
if sum(v) < s:
print -1
exit()
min_v = min(v)
for i in xrange(len(v)):
s -= (v[i] - min_v)
v[i] -= min_v
if s <= 0:
print min_v
exit()
if s % n > 0:
print min_v - s/n - 1
else:
print min_v - s/n | PYTHON |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n,s=map(int,input().split())
l=list(map(int,input().split()))
if sum(l)<s:
print('-1')
else:
l.sort()
ans=0
val=l[0]
check=0
for i in range(1,n):
ans+=l[i]-val
if n==1:
print(l[0]-s)
elif ans>=s:
print(val)
else:
remain=s-ans
we=remain//n
if remain%n>0:
we+=1
print(val-we)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import math
n, s = map(int, input().split())
arr = list(map(int, input().split()))
if sum(arr) < s:
print('-1')
else:
mn = min(arr)
sm = [x-mn for x in arr]
sum_arr = sum(sm)
if sum_arr >= s:
print(mn)
else:
s -= sum_arr
c = s/n
print(mn-math.ceil(c))
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.util.*;
public class Main {
static long a[]=new long [2000];
public static void main(String args[])
{
//System.out.println(1);
Scanner input=new Scanner(System.in);
int n;long k;
n=input.nextInt();k=input.nextLong();
long sum=0;
for(int i=1;i<=n;i++)
{
a[i]=input.nextLong();sum+=a[i];
}
Arrays.sort(a,1,1+n);
long ansmin=a[1];
long sub=sum-ansmin*n;
if(sub>=k)
{
System.out.println(ansmin);
}
else
{
k-=sub;
if(k>n*ansmin)
{
System.out.println(-1);
}
else
{
long num=k/n;
if(k%n==0)System.out.println(ansmin-k/n);
else
System.out.println(ansmin-k/n-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 |
def solve():
n, s = map(int, input().split())
v = list(map(int, input().split()))
sv = sum(v)
if sv < s:
print(-1)
return
mnf = min(v)
top = 0
for x in v:
top += max(0, x - mnf)
if top >= s:
print(mnf)
return
s -= top
mnf -= s // n
s %= n
if s > 0:
mnf -= 1
print(mnf)
solve()
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import math
n,s=map(int,input().split())
a=[int(s) for s in input().split()]
a.sort()
if sum(a)<s:
print(-1)
else:
sum=0
for i in range(n-1,0,-1):
sum+=a[i]-a[0]
if sum>=s:
print(a[0])
else:
print(a[0]-math.ceil((s-sum)/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, v = map(int, input().split())
a = [int(el) for el in input().split()]
a.sort()
mm = 0
for i in range(n):
mm += a[i] - a[0]
v = max(0, v - mm)
d = v // n
k = v % n
a[0] -= d
if k != 0:
a[0] -= 1
if a[0] < 0:
print(-1)
else:
print(a[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 | import java.util.*;
import java.io.*;
import java.lang.*;
public class programA {
public static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long [n1];
long R[] = new long [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
public static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long s = Long.parseLong(st.nextToken());
long arr[] = new long[(int)n];
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++) {
arr[i] = Long.parseLong(st.nextToken());
}
long sum = 0;
sort(arr, 0, n-1);
for(int i =0;i<n;i++) {
sum+= arr[i];
}
long sum2 = sum - n*(arr[0]);
if(s>sum)System.out.println("-1");
else if(s<=sum2)System.out.println(arr[0]);
else {
long rem = s-sum2;
long t2 = (long)Math.ceil((double)rem/(double)n);
long ans = arr[0] - t2;
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 | n,s=map(int,input().split())
list=[int(i) for i in input().split()]
list.sort(reverse=True)
if(sum(list)<s):
print("-1")
exit()
for i in range(n-1):
s-=(list[i]-list[n-1])
list[i]=list[n-1]
if(s<=0):
print(list[n-1])
exit()
if(s<=n):
print(list[0]-1)
exit()
else:
if(s%n):
print(list[0]-int(s/n)-1)
else:
print(list[0]-int(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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws IOException {
new B().solve();
}
public BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public StringTokenizer tok = null;
public String nextToken() throws IOException {
if (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(reader.readLine());
}
return tok.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public void solve() throws IOException {
int n = nextInt();
long s = nextLong();
long[] v = new long[n];
for (int i = 0; i < n; i++) {
v[i] = nextLong();
}
long vSum = Arrays.stream(v).sum();
if (s > vSum) {
System.out.println(-1);
return;
}
long vMin = Arrays.stream(v).min().orElse(0);
long expectedMin = (vSum - s) / n;
System.out.println(Math.min(expectedMin, vMin));
}
}
| 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
n, s = map(int, input().split())
v = list(map(int, input().split()))
least = min(v)
if sum(v) < s:
print(-1)
else:
for i in v:
s -= (i-least)
if s > 0:
least -= ((s+n-1)/n)
print(math.ceil(least))
| 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;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
vector<long long> a;
long long x;
for (long long i = 0; i < n; i++) {
cin >> x;
a.push_back(x);
}
long long sum = 0;
for (auto &i : a) sum += i;
if (sum < k)
cout << -1;
else {
long long mini = *min_element(a.begin(), a.end());
if (k > sum - n * mini)
cout << (sum - k) / n;
else {
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 | # python b.py < b.txt
import sys
from math import ceil
from os.path import isfile
fin, fout = (open("b.in"), open("b.out", "w")) if isfile("b.in") else (sys.stdin, sys.stdout)
def ris(): return map(int, fin.readline().split())
def ri(): return int(fin.readline())
n,s = ris()
v = sorted(ris(), reverse=True)
mn = v[-1]
mn_i = n-1
while mn_i>0 and mn == v[mn_i]:mn_i-=1
s-=sum(v[i]-mn for i in range(mn_i+1))
# print("sum = ", s)
if s > 0:
# print(" ceil(s/n) = ", ceil(s/n))
if mn >= ceil(s/n):mn-=ceil(s/n)
else: mn = -1
fout.write("%d\n" % mn)
fin.close(), fout.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 | import java.io.*;
import java.util.*;
public class Main {
private static final int MAX = Integer.MAX_VALUE;
private static final int MIN = Integer.MIN_VALUE;
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
Scanner scanner = new Scanner(System.in);
int n = in.nextInt();
long s = in.nextLong();
int [] a = new int[n];
long sum = 0;
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
sum+=a[i];
}
if(sum < s) {
out.println(-1);
}
else {
int max = a[0];
for(int i : a) {
max = Math.max(max,i);
}
int lo = 1;
int hi = 1_000_000_000;
while (lo<=hi) {
int med = (lo +hi)/2;
long ss = 0;
boolean ok = true;
for (int i = 0; i < a.length; i++) {
if(a[i] < med) ok = false;
else ss+=(a[i]-med);
}
if(ss < s) ok = false;
if(!ok) hi = med - 1;
else lo = 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;
if(sum>=s) return true;
}
return false;
}
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 | #########################################################################################################\
#########################################################################################################
###################################The_Apurv_Rathore#####################################################
#########################################################################################################
#########################################################################################################
import sys,os,io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_left , bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')
#for deep recursion__________________________________________-
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,p - 2, p)) % p
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
l.append(int(i))
n = n / i
if n > 2:
l.append(n)
c = dict(Counter(l))
return list(set(l))
# return c
def power(x, y, p) :
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
#____________________GetPrimeFactors in log(n)________________________________________
def sieveForSmallestPrimeFactor():
MAXN = 100001
spf = [0 for i in range(MAXN)]
spf[1] = 1
for i in range(2, MAXN):
spf[i] = i
for i in range(4, MAXN, 2):
spf[i] = 2
for i in range(3, math.ceil(math.sqrt(MAXN))):
if (spf[i] == i):
for j in range(i * i, MAXN, i):
if (spf[j] == j):
spf[j] = i
return spf
def getPrimeFactorizationLOGN(x):
spf = sieveForSmallestPrimeFactor()
ret = list()
while (x != 1):
ret.append(spf[x])
x = x // spf[x]
return ret
#____________________________________________________________
def SieveOfEratosthenes(n):
#time complexity = nlog(log(n))
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
def si():
return input()
def divideCeil(n,x):
if (n%x==0):
return n//x
return n//x+1
def ii():
return int(input())
def li():
return list(map(int,input().split()))
#__________________________TEMPLATE__________________OVER_______________________________________________________
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def solve():
n,s = li()
l = li()
l.sort()
ans = -1
low = 0
high = max(l)
def check(mid):
ans = 0
for i in l:
if i<mid:
return False
ans+=i-mid
if ans>=s:
return True
return False
while(low<=high):
mid = (low+high)//2
if check(mid):
low=mid+1
ans = mid
else:
high = mid-1
print(ans)
t = 1
# t = ii()
for _ in range(t):
solve()
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.io.*;
import java.util.*;
public class Main {
private static final int MAX = Integer.MAX_VALUE;
private static final int MIN = Integer.MIN_VALUE;
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
Scanner scanner = new Scanner(System.in);
int n = in.nextInt();
long s = in.nextLong();
int [] a = new int[n];
long sum = 0;
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
sum+=a[i];
}
if(sum < s) {
out.println(-1);
}
else {
int max = a[0];
for(int i : a) {
max = Math.max(max,i);
}
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 | if __name__ == "__main__":
n, desired_volume = map(int, raw_input().split())
volumes = list(map(int, raw_input().split()))
min_volume = min(volumes)
for v in volumes:
if desired_volume > 0:
desired_volume -= v - min_volume
else:
break
if desired_volume < 0:
print min_volume
else:
if desired_volume / n < min_volume:
print min_volume - desired_volume / n - (0 if desired_volume % n == 0 else 1)
elif desired_volume / n == min_volume and desired_volume % n == 0:
print 0
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 | #include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
long long A[N];
long long fnc(long long h, long long n) {
long long sum = 0;
for (__typeof((n)) i = (1); i <= (n); i++) {
if (A[i] >= h) sum += A[i] - h;
}
return sum;
}
void solve() {
long long n, k, x;
cin >> n >> k;
long long sum = 0;
long long mx = LLONG_MAX;
for (__typeof((n)) i = (1); i <= (n); i++) {
cin >> A[i];
sum += A[i];
mx = min(mx, A[i]);
}
if (k > sum) {
cout << -1 << '\n';
return;
}
long long ans = 0;
long long low = 0;
long long high = mx;
while (high - low >= 0) {
long long mid = (low + high) / 2;
if (fnc(mid, n) >= k) {
ans = mid;
low = mid + 1;
} else
high = mid - 1;
}
cout << ans << '\n';
}
signed main() {
long long t;
ios_base::sync_with_stdio(false);
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, m;
cin >> n >> m;
long long int a[n];
long long int minn = INT_MAX;
long long int sum = 0;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] < minn) minn = a[i];
sum += a[i];
}
long long int x = minn * n;
long long int y = sum - x;
if (sum < m)
cout << "-1" << endl;
else {
if (y >= m)
cout << minn << endl;
else {
m = m - y;
long long int z = ceil(m * 1.0 / n);
cout << minn - z << endl;
}
}
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, i, sum = 0, k = 0, m, d, c = 0;
cin >> n >> s;
long long arr[n];
for (i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
if (sum < s)
cout << "-1\n";
else if (sum == s)
cout << "0\n";
else {
sort(arr, arr + n);
m = arr[0];
for (i = 0; i < n; i++) {
if (arr[i] > m) {
k += arr[i] - m;
arr[i] -= m;
}
if (k >= s) {
c = 1;
break;
}
}
if (c == 1)
cout << arr[0] << "\n";
else {
d = s - k;
if (d % n == 0)
cout << arr[0] - (d / n) << "\n";
else {
cout << arr[0] - ((d / n) + 1) << "\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 sys
if __name__ == '__main__':
input = sys.stdin.read()
n, s, *a = list(map(int, input.split()))
a.sort()
minimum = a[0]
sum1 = 0
for i in range(n):
sum1 += a[i] - minimum
if s > sum1 + n *minimum:
print(-1)
elif s <= sum1:
print(minimum)
else:
print((n*minimum - s+sum1) //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 int n, k;
cin >> n >> k;
long long int a[n];
for (long long int i = 0; i < n; i = i + 1) cin >> a[i];
sort(a, a + n);
long long int extra = 0;
long long int sum = a[0];
for (long long int i = n - 1; i > 0; i = i - 1) {
extra += (a[i] - a[0]);
sum += a[i];
}
if (k > sum) {
cout << -1 << endl;
return 0;
}
if (k <= extra) {
cout << a[0] << endl;
return 0;
}
k = k - extra;
long long int q = k / n;
bool r = k % n;
long long int ans = a[0] - (q + r);
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 = [int(x) for x in input().split()]
v = [int(x) for x in input().split()]
b = [0 for i in range(n)]
rem = 0
if sum(v) < s:
print(-1)
else:
mn = min(v)
m = mn
for i in range(n):
b[i] = v[i] - mn
sm = sum(b)
if sm >= s:
print(mn)
else:
rem = s - sm
## print(rem)
times = rem // n
rem -= times * n
m -= times
if rem == 0:
print(m)
else:
print(m - 1)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.io.*;
import java.util.*;
/**
* @authod Rashedul Hasan Rijul
*/
public class Solution {
FastReader fastReader;
int n;
long s, sum;
long[] v;
public Solution() {
}
public Solution(FastReader fastReader) {
this.fastReader = fastReader;
}
public void solve() {
n = fastReader.nextInt();
s = fastReader.nextLong();
v = new long[n + 1];
sum = 0;
for (int i = 0; i < n; i++) {
v[i] = fastReader.nextLong();
sum += v[i];
}
if (sum < s) {
System.out.println("-1");
} else {
System.out.println(binarySearch());
}
}
private long binarySearch() {
long lo = 0, hi = sum, mid = 0;
while (lo < hi) {
mid = lo + hi;
mid /= 2;
if (possible(mid) == false) {
hi = mid - 1;
} else {
if (mid == lo) {
if (possible(mid + 1)) return mid + 1;
else return mid;
}
lo = mid;
}
}
return lo;
}
private boolean possible(long pivot) {
long rem = s;
for (int i = 0; i < n; i++) {
if (v[i] < pivot) return false;
rem -= (v[i] - pivot);
}
if (rem <= 0) return true;
return false;
}
public static void main(String args[]) throws FileNotFoundException {
// for testing in local
//FastReader fastReader = FastReader.getFileReader("in.txt");
FastReader fastReader = FastReader.getDefaultReader();
Solution soln = new Solution(fastReader);
soln.solve();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
public static FastReader getFileReader(String fileName) throws FileNotFoundException {
return new FastReader(new InputStreamReader(new FileInputStream(new File("in.txt"))));
}
public static FastReader getDefaultReader() throws FileNotFoundException {
return new FastReader();
}
public FastReader(InputStreamReader inputStreamReader) {
br = new BufferedReader(inputStreamReader);
}
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;
}
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n,s = [int(x) for x in input().split()]
L = [int(x) for x in input().split()]
S = sum(L)
mi = min(L)
equal = S-mi*n
if s > S:
print(-1)
else:
if s <= equal:
print(mi)
else:
print(mi-((s - equal-1)//n)-1)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n,s=map(int,input().split())
a=list(map(int,input().split()))
mn=min(a)
#print(sum(a))
if sum(a)<s :
print(-1)
else : print(min(mn,(sum(a)-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.*;
import java.util.*;
public class Main {
final static long mod = 1000000007;
static void debug(Object... args)
{System.out.println(Arrays.deepToString(args));}
public static void main(String[] args) throws Exception {
InputReader sc = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
Random gen = new Random();
int test = 1;//sc.nextInt();
StringBuilder sb = new StringBuilder();
while (test-- > 0) {
int n = sc.nextInt();
long k = sc.nextLong();
int [] ar = sc.nextIntArray(n);
long total = 0;
int min = Integer.MAX_VALUE;
for(int i=0;i<n;i++) {
total+=ar[i];
min = Math.min(min,ar[i]);
ar[i] *= -1;
}
if(total<k)
pw.println(-1);
else if(total==k)
pw.println(0);
else {
Arrays.sort(ar);
for (int i = 0; i < n; i++)
ar[i] *= -1;
long sum = 0;
for(int i=0;i<n;i++)
{
if(sum>=k)
break;
else
{
sum+=ar[i]-min;
}
}
if(sum>=k)
pw.println(min);
else
{
long rem = k-sum;
long times = (rem+n-1)/n;
pw.println(min-times);
}
}
}
pw.flush();
pw.close();
}
static class Data implements Comparable<Data> {
long x;
long y;
public Data(long m, long n) {
x = m;
y = n;
}
@Override
public int compareTo(Data o) {
return (int) (x-o.x);
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int [] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public long[] nextLongArray(int n) {
long [] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
public static int[] shuffle(int[] a, Random gen)
{ for(int i = 0, n = a.length;i < n;i++)
{ int ind = gen.nextInt(n-i)+i;
int d = a[i];
a[i] = a[ind];
a[ind] = d;
}
return a;
}
public static long[] shuffle(long[] a, Random gen)
{ for(int i = 0, n = a.length;i < n;i++)
{ int ind = gen.nextInt(n-i)+i;
long d = a[i];
a[i] = a[ind];
a[ind] = d;
}
return a;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
} | 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().strip().split())
v = list(map(int, input().strip().split()))
m = min(v)
for i in v:
if s > 0: s -= i - m
from math import ceil
if m * n < s: print(-1)
elif s <= 0: print(m)
else: print(m - ceil(s / n))
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 |
import java.util.Arrays;
import java.util.Scanner;
public class Substitute {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
long requi = s.nextLong();
long arr[] = new long[n];
long sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i] = s.nextLong();
}
Arrays.sort(arr);
if (sum < requi) System.out.println(-1);
else {
if(sum-arr[0]*n>=requi){
System.out.println(arr[0]);
return;
}
requi=requi-sum+arr[0]*n;
long k=requi/n;
if(requi%n!=0)k++;
System.out.println(arr[0]-k);
}
}
}
| JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | from math import ceil
n, s = map(int, input().split())
v = list(map(int, input().split()))
V = sum(v)
m = min(v)
if V<s:
answer = -1
else:
v.sort(reverse=True)
V_over = [0]*n
V_over[0] = v[0]-m
for i in range(1,n):
V_over[i] = V_over[i-1] + (v[i]-m)
if V_over[-1]<=s:
answer = m - ceil((s-V_over[-1])/n)
else:
answer = m
print(answer)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n, s = map(int, input().split())
v = [int(k) for k in input().split()]
v.sort(reverse=True)
for i in range(n):
s -= v[i] - v[-1]
v[i] = v[-1]
if s <= 0:
print(v[-1])
else:
if s > n * v[0]:
print(-1)
else:
print(v[0] - (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 | from fractions import gcd
from math import factorial, ceil, sqrt, atan2, log, pi, e, asin,acos, cos, sin, floor
from itertools import *
from fractions import Fraction
import string
import copy
import random
import bisect
from decimal import *
from collections import deque
from sys import *
digs = string.digits + string.ascii_letters
def id_generator(size=20, chars=string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def mp():
return map(int,str(raw_input()).split())
def check(mid,l,n,s):
ans=0
for i in range(n):
ans=ans+max(0,l[i]-mid)
if ans>=s:
return 1
return 0
mod=10**9+7
n,s=mp()
l=list(mp())
x=min(l)
if sum(l)<s:
print -1
exit()
ans=0
for i in range(n):
ans+=(l[i]-x)
if ans>=s:
print x
else:
print x-((s-ans-1)/n+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 | //package code;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.awt.Point;
/**
*
* @author prabhat // use stringbuilder,TreeSet, priorityQueue, x+y=(x&y + x|y);
*/
public class a
{
public static int n,max1=100005,k,m,q,a[],a1[],mat[][],id,depth[],parent[][],len=22,tin[],tout[],count=0,size[];
public static Point[] p1,p2,p;
public static long mod=(long)1e9+7,nCr[][],suf[][],ans=Long.MAX_VALUE,max=Long.MIN_VALUE,dp[][];
public static boolean isPrime[],visit[],valid[][];
public static HashSet<Integer> hash;
public static ArrayList<Integer> divisor[],prime,adj[],lpf;
public static ArrayList<Character> list[];
public static TreeSet<Integer> ts;
public static String s;
public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw;
static int[] x={0,1},y={1,0};
public static void main(String[] args) {
new Thread(null,null,"Prabhat Kumar",99999999)
{
public void run()
{
try
{
solve();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static void solve() throws IOException
{
InputReader in = new InputReader(System.in);
pw=new PrintWriter(System.out);
n=in.ii();
long s=in.il();
long[] a=in.ila(n);
long min=Long.MAX_VALUE;
for(long i:a)min=Math.min(min,i);
long low=0;
long high=min+1;
long ans=0;
while(low<high)
{
long mid=(low+high)/2;
long sum=0;
for(int i=0;i<n;i++)
{
sum+=Math.max(0L,a[i]-mid);
}
if(sum<s)high=mid;
else
{
ans=mid;
low=mid+1;
}
}
// pw.println("ans=="+ans);
long sum=0;
for(int i=0;i<n;i++)sum+=Math.max(0L,a[i]-ans);
if(sum<s)pw.println(-1);
else pw.println(ans);
pw.close();
}
static void dfs1(int cur,int par,TreeSet<Integer> ts)
{
if(visit[cur])return;
visit[cur]=true;
ts.add(cur);
for(int ver:adj[cur])
{
if(visit[ver])continue;
if(ver!=par)dfs1(ver,cur,ts);
}
}
static int palin(String s)
{
String s1=new StringBuilder(s).reverse().toString();
return s1.equals(s)==true?1:0;
}
static int dfs(int u, int p) {
//discover[count1++]=arr[u]; // it storing color or arr of vertex u
//discover[count1++]=u;
//tin[u] = count++;
int d1=1000000;
int d2=1000000;
int cnt=0;
//parent[u][0] = p==-1?0:p;
//size[u]++;
//for (int i = 1; i < len; i++)
// parent[u][i] = parent[parent[u][i-1]][i-1];
for (int v :adj[u])
if (v != p)
{
cnt++;
depth[v] = depth[u] + 1;
int y=dfs( v, u);
if(y<d1)
{
d2=d1;
d1=y;
}
else if(y<d2)
{
d2=y;
}
// size[u] += size[v];
}
if(cnt==0)return 1;
ans=Math.min(d1+d2,ans);
return 1+Math.min(d1,d2);
// if(adj[u].size()==1&&parent[u][0]!=0)last.add(u);
// tout[u] = count-1;
}
static boolean isParent(int parent, int child) {
return tin[parent] <= tin[child] && tout[child] <= tout[parent];
}
static int lca(int a, int b) {
if (isParent(a, b))
return a;
if (isParent(b, a))
return b;
for (int i = len - 1; i >= 0; i--)
if (!isParent(parent[a][i], b))
a = parent[a][i];
return parent[a][0];
}
static boolean valid(int i,int j)
{
return i>=1&&i<=n&&j>=1&&j<=m;
}
static int gcd(int a,int b)
{
while(a!=0)
{
int temp=a;
a=b%a;
b=temp;
}
return b;
}
static void Seive()
{
a1=new int[max1];
isPrime=new boolean[max1];
Arrays.fill(isPrime,true);
// prime=new ArrayList<>();
// divisor=new ArrayList[max1];
// for(int i=1;i<max1;i++)divisor[i]=new ArrayList<Integer>();
isPrime[0]=isPrime[1]=false;
for(int i=2;i<(max1);i++)
{
if(isPrime[i]){
// divisor[i].add(i);
// prime.add(i);
a1[i]=1;
for(int j=2*i;j<max1;j+=i)
{
a1[j]++;
isPrime[j]=false;
//divisor[j].add(i);
}
}
}
}
public static long pow(long n,long p)
{
long result = 1;
if(p==0)
return 1;
while(p!=0)
{
if(p%2==1)
result *= n;
p >>=1;
n*=n;
}
return result;
}
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private SpaceCharFilter filter;
byte inbuffer[] = new byte[1024];
int lenbuffer = 0, ptrbuffer = 0;
final int M = (int) 1e9 + 7;
int md=(int)(1e7+1);
int[] SMP=new int[md];
final double eps = 1e-6;
final double pi = Math.PI;
PrintWriter out;
String check = "";
InputStream obj = check.isEmpty() ? System.in : new ByteArrayInputStream(check.getBytes());
public InputReader(InputStream stream)
{
this.stream = stream;
}
int readByte() {
if (lenbuffer == -1) throw new InputMismatchException();
if (ptrbuffer >= lenbuffer) {
ptrbuffer = 0;
try {
lenbuffer = obj.read(inbuffer);
} catch (IOException e) {
throw new InputMismatchException();
}
}
if (lenbuffer <= 0) return -1;
return inbuffer[ptrbuffer++];
}
public int read()
{
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;
}
String is() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) // when nextLine, (isSpaceChar(b) && b!=' ')
{
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int ii() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public long il() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
boolean isSpaceChar(int c) {
return (!(c >= 33 && c <= 126));
}
int skip()
{
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
float nf() {
return Float.parseFloat(is());
}
double id() {
return Double.parseDouble(is());
}
char ic() {
return (char) skip();
}
int[] iia(int n) {
int a[] = new int[n];
for (int i = 0; i<n; i++) a[i] = ii();
return a;
}
long[] ila(int n) {
long a[] = new long[n];
for (int i = 0; i <n; i++) a[i] = il();
return a;
}
String[] isa(int n) {
String a[] = new String[n];
for (int i = 0; i < n; i++) a[i] = is();
return a;
}
long mul(long a, long b) { return a * b % M; }
/* long div(long a, long b)
{
return mul(a, pow(b, M - 2,M));
}
*/
double[][] idm(int n, int m) {
double a[][] = new double[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = id();
return a;
}
int[][] iim(int n, int m) {
int a[][] = new int[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j <m; j++) a[i][j] = ii();
return a;
}
public String readLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -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 | # n,s=[3,14]
# y=[3,4,5]
n,s=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]
if s>sum(y):print(-1)
elif s==sum(y):print(0)
else:
m=min(y)
bk=sum([i-m for i in y])
if s<=bk:print(m)#沑ζε¨ε°ζε°ηζζ±
else:
s1=s-bk
m-=s1//n
if s1%n!=0:m-=1
print(m)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n, s = list(map(int, input().split()))
v = list(map(int, input().split()))
total_kvass = sum(v)
if total_kvass < s:
print(-1)
exit(0)
r = min((total_kvass - s) // n, sorted(v)[0])
print(r)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void _dbg(const char* _s, T _h) {
cerr << _s << " = " << _h << "\n";
}
template <typename T, typename... Ts>
void _dbg(const char* _s, T _h, Ts... _t) {
int _b = 0;
while (((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',') cerr << *_s++;
cerr << " = " << _h << ",";
_dbg(_s + 1, _t...);
}
const int64_t INF = static_cast<int64_t>(1e9) + 7;
const int64_t LINF = INF * INF;
const int MAXN = static_cast<int>(1e6) + 17;
void solve() {
int n;
int64_t second;
cin >> n >> second;
vector<int> v(n);
int64_t sum = 0;
int right = INF;
for (auto& el : (v)) {
cin >> el;
sum += el;
right = min(right, el);
}
if (sum < second) {
cout << "-1\n";
return;
}
++right;
int left = 0;
while (right - left > 1) {
int mid = (left + right) / 2;
int64_t amount = 0;
for (int i = 0; i < n; ++i) {
amount += v[i] - mid;
}
if (amount >= second) {
left = mid;
} else {
right = mid;
}
}
cout << left;
}
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios_base::sync_with_stdio(false);
int q = 1;
for (int i = 0; i < q; ++i) {
solve();
}
}
| 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 solve(n, vol, kegs):
if sum(kegs) < vol:
return -1
totalVolume = sum(kegs)
allMin = min(kegs) * n
finalVolume = totalVolume - vol
if finalVolume >= allMin:
return min(kegs)
return int(finalVolume / n)
n, vol = list(map(int, input().split()))
kegs = list(map(int, input().split()))
print(solve(n, vol, 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.util.Scanner;
public class KvassAndTheFairNut2 {
static long sum = 0;
static long n = 0;
static long s = 0;
static long[] kegs = new long[1001];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextLong();
s = in.nextLong();
long Min = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
kegs[i] = in.nextLong();
sum += kegs[i];
Min = Math.min(kegs[i], Min);
}
if (sum < s) {
System.out.println(-1);
return;
}
long x = sum - Min * n;
s -= x;
if (s <= 0) {
System.out.println(Min);
return;
} else {
long lo = 0;
long hi = Min;
long res = 0;
while (lo <= hi) {
long mid = (hi - lo) / 2 + lo;
if (s - mid * n > 0) {
lo = mid + 1;
} else {
hi = mid - 1;
res = mid;
}
}
System.out.println(Min - res);
}
}
}
| JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | line1 = input()
tokens = line1.split()
n = int(tokens[0])
v = int(tokens[1])
line2 = input()
tokens = line2.split()
lst = []
for j in range(n):
lst.append(int(tokens[j]))
minv = min(lst)
total = sum(lst)
if total < v:
print('-1')
else:
minv = min(int((total - v ) / n), minv)
print(minv)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 |
import java.util.*;
public class test {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
long s,minn=Long.MAX_VALUE,temp=0,ans=-1,tot=0;
int n=sc.nextInt();
s=sc.nextLong();
Long arr[]=new Long[n+5];
for(int i=1;i<=n;i++)
{
arr[i]=sc.nextLong();
minn=Math.min(arr[i], minn);
tot+=arr[i];
}
if(tot<s)
{
System.out.println("-1");
return ;
}
for(int i=1;i<=n;i++)
temp+=(arr[i]-minn);
if(temp>=s)
{
System.out.println(minn);
return ;
}
s-=temp;
if(s%n==0)
ans=minn-(s/n);
else
ans=minn-(s/n)-1;
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 | n, s = list(map(int, input().split()))
V = list(map(int, input().split()))
Sum = sum(V)
Min = min(V)
if(Sum < s):
print(-1)
elif(Sum == s):
print(0)
else:
num = 0
for i in range(0, n):
num += V[i]-Min
if(num >= s):
print(Min)
else:
temp = s-num
ans = int(temp/n)
if(temp%n == 0):
print(Min-ans)
else:
print(Min-ans-1)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | def may_get(min_level, need, v):
for i in v:
if i < min_level:
return 0
need -= i - min_level
return need <= 0
def bin_find(l, r, need, v):
while l != r:
mid = (l + r + 1) // 2
if may_get(mid, need, v):
l = mid
else:
r = mid - 1
return l
def main():
n, s = map(int, input().split())
v = list(map(int, input().split()))
print(bin_find(-1, pow(10, 9), s, v))
if __name__ == '__main__':
main()
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, s;
cin >> n >> s;
vector<ll> v(n);
for (ll i = (0); i < (n); ++i) cin >> v[i];
ll m = *min_element((v).begin(), (v).end());
ll t = 0;
for (ll i = (0); i < (n); ++i) t += v[i];
t -= s;
if (t < 0) {
cout << -1 << endl;
} else {
cout << min(m, t / 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 | n,s=map(int,input().split())
A=list(map(int,input().split()))
a=0
ans=0
if(sum(A)<s):
ans=-1
else:
x=min(A)
for i in range(0,n):
if(A[i]!=x):
a+=A[i]-x
if(a>=s):
ans=x
else:
c=s-a+n-1
c=c//n
ans=x-c
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;
template <class T>
inline T bigMod(T p, T e, T M) {
long long ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T modInverse(T a, T M) {
return bigMod(a, M - 2, M);
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
a = abs(a);
b = abs(b);
return (a / gcd(a, b)) * b;
}
template <class T>
inline string int2String(T a) {
ostringstream str;
str << a;
return str.str();
}
const int dr[] = {0, 1, 0, -1, -1, 1, 1, -1, -2, -2, 2, 2, -1, -1, 1, 1};
const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1, -1, 1, -1, 1, -2, 2, -2, 2};
void fileIO() {}
int main() {
long long n, s;
scanf("%lld%lld", &n, &s);
vector<long long> v(n);
long long sum = 0;
for (int i = int(0); i < int(n); i++) scanf("%lld", &v[i]), sum += v[i];
if (sum < s) {
puts("-1");
return 0;
}
sort(v.begin(), v.end(), greater<int>());
sum = 0;
for (int i = int(0); i < int(n); i++) {
sum += v[i] - v.back();
v[i] = v.back();
if (sum >= s) {
printf("%lld\n", v.back());
return 0;
}
}
long long need = s - sum;
long long mn = (n + need - 1) / n;
printf("%lld\n", v.back() - mn);
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;
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;
for (long long int i = 0; i < n; i++) {
cin >> 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 sum1 = 0;
for (long long int i = 0; i < n; i++) {
sum1 = sum1 + abs(arr[i] - r);
}
if (sum1 >= s) {
cout << r << "\n";
return 0;
}
s = s - sum1;
long long int ans = (n * arr[0]) - 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 | import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long s = scanner.nextLong();
long array[] = new long[n];
long sum = 0, min = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
array[i] = scanner.nextLong();
min = Math.min(array[i], min);
sum += array[i];
}
if (sum < s){
System.out.println(-1);
return;
}
long currentSum = 0;
for (int i = 0; i < n; i++) {
currentSum += array[i] - min;
if (currentSum >= s){
System.out.println(min);
return;
}
array[i] = min;
}
double x = (double) (s - currentSum) / (double)n;
if(x == (int) x){
System.out.println(min - (int)x);
} else {
System.out.println(min - (int)x - 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 | 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()
if sum(ls) < 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(max(-1,ls[0] - ceil(s/n)))
if localsys:
print("\n\nTime Elased :",time.time() - start_time,"seconds")
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
void solve() {
long long i, j, flag = 0;
long long n, s;
cin >> n >> s;
long long a[n], sum = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s) {
cout << "-1\n";
return;
}
sort(a, a + n);
while (s != 0) {
long long p = a[n - 1] - a[0];
if (p < s) {
s -= p;
a[n - 1] = a[0];
} else {
a[n - 1] -= s;
s = 0;
}
sort(a, a + n);
if (a[n - 1] == a[0]) break;
}
if (a[n - 1] == a[0]) {
long long p = s / n, q = s % n;
for (i = 0; i < n; i++) a[i] -= p;
for (i = 0; i < q; i++) a[i]--;
sort(a, a + n);
cout << a[0] << endl;
} else
cout << a[0] << endl;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
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 | import java.io.*;
import java.util.*;
public class Main {
private InputStream is;
private PrintWriter pw;
static char[][] ch;
static int x1, x2, y1, y2, n, m, h, k;
static long dist[][];
static boolean boo[][];
void soln() {
is = System.in;
pw = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
pw.close();
pw.flush();
// tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Thread(null, new Runnable() {
public void run() {
try {
// new CODEFORCES().soln();
} catch (Exception e) {
System.out.println(e);
}
}
}, "1", 1 << 26).start();
new Main().soln();
}
long MOD = (1000000007);
void solve() {
int n = ni();
long s = nl();
int arr[] = na(n);
int min = 1000000100;
long sum = 0;
for(int i : arr) {sum += i;min = Math.min(min, i);}
long curr = 0;
if(sum<s) {
System.out.println("-1");
return;
}
for(int i : arr) {
curr += (i-min);
}
if(s<=curr) {
System.out.println(min);
return;
}
long val = (s-curr+n-1)/n;
System.out.println(min-val);
}
long pow(long x, long y, long MOD) {
long ans = 1;
while (y > 0) {
if (y % 2 == 0) {
x *= x;
x %= MOD;
y /= 2;
} else {
ans *= x;
ans %= MOD;
y -= 1;
}
}
return ans;
}
long gcd(long arr, long b) {
if (b == 0)
return arr;
return gcd(b, arr % b);
}
void printArray(long[] arr) {
for (long i : arr)
pw.print(i + " ");
pw.println();
}
static long min(long x, long y) {
return (x < y) ? x : y;
}
static class Pair implements Comparable<Pair> {
int x, y;
int i;
Pair(int arr, int b) {
x = arr;
y = b;
}
public int compareTo(Pair p) {
return Long.compare(this.x % m, p.x % m);
}
public String toString() {
return this.x + " " + this.y;
}
}
private void method(TreeMap<Integer, TreeSet<Integer>> treeMap, int x, int y, int m, SegmentTree tree, int i, int n) {
int l = Math.max(i-m+1, 0);
TreeSet<Integer> set = treeMap.get(x);
set.remove(i);
Integer start1 = set.floor(i);
Integer end1 = set.ceiling(i);
if(start1 == null)
start1 = -1;
if(end1 == null)
end1 = n;
start1++;
end1--;
l = Math.max(l, start1);
int r = Math.min(i, end1-m+1);
tree.update(l, r, -1, 0, 0, n-1);
if(set.size() == 0)
treeMap.remove(x);
l = Math.max(i-m+1, 0);
if(treeMap.containsKey(y))
set = treeMap.get(y);
else {
set = new TreeSet<>();
treeMap.put(y, set);
}
start1 = set.floor(i);
end1 = set.ceiling(i);
if(start1 == null)
start1 = -1;
if(end1 == null)
end1 = n;
start1++;
end1--;
l = Math.max(l, start1);
r = Math.min(i, end1-m+1);
tree.update(l, r, 1, 0, 0, n-1);
set.add(i);
}
private static class Queue {
int l = 0;
int r = 0;
Pair[] arr;
public Queue(int len) {
arr = new Pair[len];
}
public boolean isEmpty() {
return l == r;
}
public void add(Pair x) {
arr[r++] = x;
}
public Pair poll() {
return arr[l++];
}
public void clear() {
l = r = 0;
}
}
/*
* void bfs(int k) { while(!queries.isEmpty()) { int y = queries.poll();
* for(long i : amp[y]) { if(!b[i]) { D[i][k] = D[y][k]+1; queries.add(i); b[i]
* = true; } } } }
*/
/*
* int dfs(int x) { b[x] = true; //start[x] = time++; int ans = 1; for(int i :
* amp[x]) { if(!b[i]) { ans += dfs(i); } } //end[x] = time; if(x!= 0 &&
* ans%2==0 && (N-ans)%2==0) cost++;
*
* return ans; }
*/
/*
* void buildGraph(int n) { for (int i = 0; i < n; i++) { int x1 = ni() - 1, y1
* = ni() - 1; amp[x1].add(y1); amp[y1].add(x1); } }
*/
public static int[] shuffle(int[] arr, Random gen) {
for (int i = 0, n = arr.length; i < n; i++) {
int ind = gen.nextInt(n - i) + i;
int d = arr[i];
arr[i] = arr[ind];
arr[ind] = d;
}
return arr;
}
public class SegmentTree {
private long[] tree;
private long[] base;
private long[] lazyTree;
private int size;
private int n;
public SegmentTree(int n, long[] arr) {
this.base = arr;
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
size = 2 * (int) Math.pow(2, x) - 1;
tree = new long[size];
lazyTree = new long[size];
this.n = n;
build(0, 0, n - 1);
}
public void build(int id, int l, int r) {
if (l == r) {
tree[id] = base[l];
return;
}
int mid = ((l + r) >> 1);
build((id << 1) | 1, l, mid);
build((id << 1) + 2, mid + 1, r);
tree[id] = Math.max(tree[(id << 1) | 1], tree[(id << 1) + 2]);
}
private long query(int x, int y, int id, int l, int r) {
if (l > y || x > r)
return 0;
if (x <= l && r <= y) {
return tree[id];
}
int mid = ((l + r) >> 1);
shift(id);
long q1 = query(x, y, (id << 1) | 1, l, mid);
long q2 = query(x, y, (id << 1) + 2, mid + 1, r);
return Math.max(q1, q2);
}
private void update(int x, int y, long colour, int id, int l, int r) {
// System.out.println(l+" "+r+" "+x);
if (x > r || y < l)
return;
if (x <= l && r <= y) {
tree[id] += colour;
lazyTree[id] += colour;
return;
}
int mid = ((l + r) >> 1);
shift(id);
if (y <= mid)
update(x, y, colour, (id << 1) | 1, l, mid);
else if (x > mid)
update(x, y, colour, (id << 1) + 2, mid + 1, r);
else {
update(x, y, colour, (id << 1) | 1, l, mid);
update(x, y, colour, (id << 1) + 2, mid + 1, r);
}
tree[id] = Math.max(tree[(id << 1) | 1], tree[(id << 1) + 2]);
}
private void shift(int id) {
if (lazyTree[id] != 0) {
tree[(id << 1) | 1] += lazyTree[id];
lazyTree[(id << 1) | 1] += lazyTree[id];
tree[(id << 1) + 2] += lazyTree[id];
lazyTree[(id << 1) + 2] += lazyTree[id];
lazyTree[id] = 0;
}
}
}
// To Get Input
// Some Buffer Methods
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] treeMap = new char[n][];
for (int i = 0; i < n; i++)
treeMap[i] = ns(m);
return treeMap;
}
private int[] na(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = ni();
return arr;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj)
System.out.println(Arrays.deepToString(o));
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, s, A[1005], sum = 0;
scanf("%lld", &n), scanf("%lld", &s);
for (int i = 0; i < n; ++i) {
scanf("%lld", &A[i]);
sum += A[i];
}
if (sum < s) {
printf("-1\n");
return 0;
}
sort(A, A + n);
long long int remainSum = 0;
for (int i = 0; i < n; ++i) {
remainSum += (A[i] - A[0]);
}
if (s <= remainSum) {
printf("%lld\n", A[0]);
return 0;
}
s -= remainSum;
long long int div = s / n;
long long int rem = s % n;
if (rem == 0) {
printf("%lld\n", A[0] - div);
} else
printf("%lld\n", A[0] - div - 1);
return 0;
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | //package ;
import java.io.*;
import java.util.*;
public class Problem_1084B
{
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner();
PrintWriter pw = new PrintWriter(System.out);
int n=sc.nextInt();long s=sc.nextLong();
long[]a=new long[n];
long sum=0;
long lo=0,hi=Long.MAX_VALUE;
PriorityQueue<Long>pq=new PriorityQueue<>(Collections.reverseOrder());
for(int i=0;i<n;i++)
{
a[i]=sc.nextLong();
pq.add(a[i]);
sum+=a[i];
hi=Math.min(a[i], hi);
}
if(sum<s)
{
System.out.println(-1);
return;
}
// System.out.println(hi+" "+lo);
long ans=0;
for(int i=0;i<100;i++)
{
long mid=hi+lo>>1;
long tmp=0;
for(int j=0;j<n;j++)
{
if(a[j]<mid)
continue;
tmp+=a[j]-mid;
}
if(tmp>=s)
{
lo=mid+1;
ans=Math.max(mid, ans);
}
else
hi=mid-1;
}
pw.print(ans);
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
return br.readLine();
}
boolean hasnext() 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 | import java.util.*;
public class Kvass
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long n,sum=0;
n=sc.nextLong();
long d=sc.nextLong();
long m=1000000000;
for(long i=0;i<n;i++)
{
long s=sc.nextLong();
sum=sum+s;
if(m>s)
m=s;
}
if(d>sum)
System.out.println(-1);
else
System.out.println(Math.min((sum-d)/n,m));
}
} | 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;
void solve() {
long long i, j, flag = 0;
long long n, s;
cin >> n >> s;
long long a[n], sum = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s) {
cout << "-1\n";
return;
}
sort(a, a + n);
while (s != 0) {
long long p = a[n - 1] - a[0];
if (p < s) {
s -= p;
a[n - 1] = a[0];
} else {
a[n - 1] -= s;
s = 0;
}
sort(a, a + n);
if (a[n - 1] == a[0]) break;
}
if (a[n - 1] == a[0]) {
long long p = s / n, q = s % n;
for (i = 0; i < n; i++) a[i] -= p;
sort(a, a + n);
if (q > 0)
cout << a[0] - 1 << endl;
else
cout << a[0] << endl;
} else
cout << a[0] << endl;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
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 | #include <bits/stdc++.h>
using namespace std;
long long int n, s, a[111111], sum, minn = 11111111111, m, k;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> a[i];
minn = min(minn, a[i]);
sum += a[i];
}
if (sum < s) {
cout << "-1" << endl;
return 0;
}
for (int i = 0; i < n; i++) m += a[i] - minn;
if (m > s) {
cout << minn << endl;
return 0;
}
k = (s - m) / n;
if ((s - m) % n != 0) k++;
cout << minn - k;
}
| 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;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long int gcd(long long int x, long long int y) {
if (y == 0) return x;
return gcd(y, x % y);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
const long long int MAXN = (long long int)(1 * 1e6 + 5);
void solve() {
long long int n, tar;
cin >> n >> tar;
vector<long long int> v(n);
long long int sum = 0;
for (long long int i = 0; i < n; i++) cin >> v[i], sum += v[i];
if (sum < tar) {
cout << -1 << '\n';
return;
}
sort(v.begin(), v.end());
cout << min(((sum - tar) / n), v[0]) << '\n';
}
int main() {
fast();
long long int t = 1;
for (long long int i = 0; i < t; i++) {
solve();
}
}
| CPP |
Subsets and Splits