B. Kvass and the Fair Nut
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The Fair Nut likes kvass very much. On his birthday parents presented him n
kegs of kvass. There are vi liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by sliters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s
liters of kvass.
Input
The first line contains two integers n
and s (1≤n≤103, 1≤s≤1012) — the number of kegs and glass volume.
The second line contains n
integers v1,v2,…,vn (1≤vi≤109) — the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s
liters of kvass, print −1. Otherwise, print a single integer — how much kvass in the least keg can be.
Examples
Input
Copy
3 3 4 3 5Output
Copy
3Input
Copy
3 4 5 3 4Output
Copy
2Input
Copy
3 7 1 2 3Output
Copy
-1Note
In the first example, the answer is 3
, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3liters of kvass in each keg.
In the second example, the answer is 2
, the Fair Nut can take 3 liters from the first keg and 1liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7
liters, so the answer is −1.
题意:就是给你N个酒桶,每个酒桶有不同的酒,然后给你一个杯子k,问能不能倒满这个杯子,不能输出-1,能的话输出剩余酒桶中最多的为多少。
题解:一开始暴力,结果一直tle,后来发现是二分,时间不够了,实际上二分即可解决,下来又想想,发现一个巧妙思路 很简单就过了。
#include <bits/stdc++.h>
using namespace std;
const int maxx = 0x3f3f3f3f;
int main() {
long long int n,k,a,m=maxx,s=0;
cin>>n>>k;
for(long long int i=0; i<n; i++) {
cin>>a;
m=min(m,a);
s+=a;
}
if(s<k) {
cout<<-1;
} else {
cout<<min(m,(s-k)/n);
}
}