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 |
---|---|---|---|---|---|
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | import sys
input=sys.stdin.readline
n,q=list(map(int,input().split()))
s=list(input())
a=[0]
b=0
for i in range(n):
b=b+int(s[i])
a.append(b)
mod=10**9+7
for i in range(q):
b,c=list(map(int,input().split()))
d=a[c]-a[b-1]
e=c-b+1-d
f=(pow(2,d,mod)-1)*pow(2,e,mod)
print((f)%mod) | PYTHON3 |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1, mod = 1e9 + 7;
int n, q;
long long arr[N];
long long pref[N];
long long binpow(long long a, long long s) {
long long ans = 1;
while (s) {
if (s & 1) {
ans *= a;
ans %= mod;
}
a *= a;
a %= mod;
s >>= 1;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
char c;
cin >> c;
if (c == '1') arr[i] = 1;
pref[i] = pref[i - 1] + arr[i];
}
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
long long len = r - l + 1, cnt = pref[r] - pref[l - 1];
cout << ((binpow(2, cnt) - 1) * binpow(2, len - cnt)) % mod << endl;
}
}
| CPP |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long n, q, sum[100004], mod = 1e9 + 7;
string s;
long long get_sum(long long i, long long j) {
if (i == 0) return sum[j];
return sum[j] - sum[i - 1];
}
long long expo(long long v, long long e) {
if (v == 0) return 0;
if (e == 0) return 1;
if (e == 1) return v;
long long x = expo(v, e / 2);
x = (x * x) % mod;
if (e % 2 == 1) {
x = (x * v) % mod;
}
return x;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> q;
cin >> s;
sum[0] = (long long)(s[0] == '1');
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + (long long)(s[i] == '1');
}
for (int i = 0; i < q; i++) {
long long a, b;
cin >> a >> b;
a--;
b--;
long long ones = get_sum(a, b);
long long finaln = (expo(2, ones) - 1 + mod) % mod;
long long ans = ((expo(2, b - a + 1 - ones) - 1) * finaln + finaln) % mod;
while (ans < 0) {
ans = (ans + mod) % mod;
}
cout << ans << endl;
}
return 0;
}
| CPP |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | #include <bits/stdc++.h>
const long long mod = 1e9 + 7;
using namespace std;
long long a[100005] = {0};
string str;
long long quickpow(long long a, long long n) {
long long ans = 1;
long long tmp = a % mod;
while (n) {
if (n & 1) {
ans = ((ans) % mod * (tmp) % mod) % mod;
}
tmp = ((tmp % mod) * (tmp) % mod) % mod;
n >>= 1;
}
return ans % mod;
}
int main() {
long long n, q;
cin >> n >> q;
cin >> str;
long long len = str.size();
for (long long i = 0; i < len; i++) {
if (str[i] == '1') {
a[i + 1] = a[i] + 1;
} else {
a[i + 1] = a[i];
}
}
long long l, r;
while (q--) {
cin >> l >> r;
long long b = (r - l + 1) % mod;
long long c = b - a[r] + a[l - 1];
cout << (quickpow(2, b) % mod - quickpow(2, c) % mod + mod) % mod << endl;
}
}
| CPP |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, q, cnt[100005];
long long ans, x, y;
char s[100005];
long long pow(long long a, long long x) {
long long b = 1;
while (x > 0) {
if (x % 2) b = (b * a) % 1000000007;
x = x / 2;
a = (a * a) % 1000000007;
}
return b;
}
int main() {
int i, l, r;
scanf("%d%d", &n, &q);
scanf("%s", s + 1);
for (i = 1; i <= n; i++) {
cnt[i] = cnt[i - 1];
if (s[i] == '1') cnt[i]++;
}
while (q--) {
scanf("%d%d", &l, &r);
x = cnt[r] - cnt[l - 1];
y = r - l + 1 - x;
x = pow(2, x) - 1;
y = pow(2, y) - 1;
if (x < 0) x = 1000000007 - 1;
if (y < 0) y = 1000000007 - 1;
ans = (x + (x * y) % 1000000007) % 1000000007;
printf("%I64d\n", ans);
}
return 0;
}
| CPP |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | import configparser
import math
import sys
input = sys.stdin.readline
def mod_exp(base, exp, mod):
# cur_iter = base^(2^0)) = base^1 = base
cur_iter = base
cur_iter %= mod
result = 1
while exp > 0:
if (1 & exp) > 0:
result = result * cur_iter
result %= mod
cur_iter *= cur_iter
cur_iter %= mod
exp = exp >> 1
return result % mod
def mod_mul(a, b, mod):
return ((a%mod) * (b%mod))%mod
def mod_add(a, b, mod):
return ((a%mod) + (b%mod))%mod
def main():
mod = int(1e9) + 7
n, q = [int(x) for x in input().split(' ')]
s = input().strip()
cnt0 = [0 for i in range(n)]
cnt1 = [0 for i in range(n)]
cnt0[0] = int((s[0]=='0'))
cnt1[0] = int((s[0]=='1'))
for i in range(1, n):
cnt0[i] = (s[i]=='0') + cnt0[i-1]
cnt1[i] = (s[i]=='1') + cnt1[i-1]
for i in range(q):
l, r = [int(x) for x in input().split(' ')]
l -= 1
r -= 1
x = cnt0[r]
if l != 0:
x -= cnt0[l-1]
y = cnt1[r]
if l != 0:
y -= cnt1[l-1]
ans = mod_exp(2, y, mod) - 1
if x > 0:
ans += mod_mul(ans, mod_exp(2, x, mod)-1, mod)
ans %= mod
print(ans)
if __name__ == '__main__':
main()
| PYTHON3 |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long mod2 = 998244353;
long long mod3 = 1000003;
long long mod4 = 998244853;
long long mod5 = 1000000009;
long long inf = 1LL << 62;
double pi = 3.141592653589793238462643383279L;
double eps = 1e-14;
int dh[4] = {1, -1, 0, 0};
int dw[4] = {0, 0, 1, -1};
int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
long long gcd(long long a, long long b) {
if (a < b) swap(a, b);
if (b == 0) return a;
if (a % b == 0) return b;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
long long c = gcd(a, b);
return a * b / c;
}
long long Pow(long long n, long long k) {
long long ret = 1;
long long now = n;
while (k > 0) {
if (k & 1) ret *= now;
now *= now;
k /= 2;
}
return ret;
}
long long beki(long long n, long long k, long long md) {
long long ret = 1;
long long now = n;
now %= md;
while (k > 0) {
if (k % 2 == 1) {
ret *= now;
ret %= md;
}
now *= now;
now %= md;
k /= 2;
}
return ret;
}
long long gyaku(long long n, long long md) { return beki(n, md - 2, md); }
long long popcount(long long n) {
long long ret = 0;
long long u = n;
while (u > 0) {
ret += u % 2;
u /= 2;
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
string s;
cin >> s;
int a[n];
for (long long i = 0; i < n; i++) a[i] = s[i] - '0';
int sum[n + 1];
sum[0] = 0;
for (long long i = 0; i < n; i++) {
sum[i + 1] = sum[i] + a[i];
}
for (long long i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
long long cnt = sum[r + 1] - sum[l];
long long ans = beki(2, r - l + 1, mod) - 1 + mod;
cnt = r - l + 1 - cnt;
ans += -beki(2, cnt, mod) + 1 + mod;
ans %= mod;
cout << ans << endl;
}
}
| CPP |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
long long sqr(long long n) { return n * n; }
const int MOD = 1e9 + 7;
long long binpow(long long a, long long n) {
if (n == 0) return 1;
if (n % 2 != 0)
return (binpow(a, n - 1) * a) % MOD;
else {
long long b = binpow(a, n / 2) % MOD;
return (b * b) % MOD;
}
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void solve() {
int n, q;
string s;
cin >> n >> q >> s;
vector<int> pr_0(n + 1), pr_1(n + 1);
for (int i = 1; i <= n; i++) {
int x = s[i - 1] - '0';
if (x)
pr_1[i]++;
else
pr_0[i]++;
pr_1[i] += pr_1[i - 1];
pr_0[i] += pr_0[i - 1];
}
int l, r;
while (q--) {
cin >> l >> r;
int o = pr_1[r] - pr_1[l - 1];
int z = pr_0[r] - pr_0[l - 1];
long long ans = binpow(2, o) - 1;
ans += (ans * (binpow(2, z) - 1)) % MOD;
ans %= MOD;
cout << ans << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| CPP |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int MOD = 1000000007;
int N, Q;
char c[MAXN];
int S[MAXN];
long long modpow(long long base, long long expo) {
base %= MOD;
long long res = 1;
while (expo > 0) {
if (expo & 1) res = res * base % MOD;
base = base * base % MOD;
expo >>= 1;
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> Q;
cin >> c + 1;
for (int i = (1); i <= (N); i++) S[i] = S[i - 1] + (c[i] == '1');
while (Q--) {
int L, R;
cin >> L >> R;
long long a = S[R] - S[L - 1];
long long b = (R - L + 1) - a;
cout << (modpow(2, a) - 1 + MOD) % MOD * modpow(2, b) % MOD << "\n";
}
}
| CPP |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | import java.util.*;
public class Main{
public static int MOD=1000000007;
public static long[] powers;
public static long solve(int[] a,int x,int y){
int ones=a[y]-a[x-1],zeros=y-x+1-ones;
long onesum=powers[ones]-1;
long zerosum=(onesum*(powers[zeros]))%MOD;
return zerosum;
}
public static void main(String[] args){
powers=new long[100001];
powers[0]=1;
for(int i=1;i<100001;i++){
powers[i]=(2*powers[i-1])%MOD;
}
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int q=s.nextInt();
s.nextLine();
String str=s.nextLine();
int[] a=new int[n+1];
for(int i=1;i<=n;i++){
if(str.charAt(i-1)=='1'){
a[i]=a[i-1]+1;
}else a[i]=a[i-1];
}
while(q-->0){
int x=s.nextInt();
int y=s.nextInt();
System.out.println(solve(a,x,y));
}
}
} | JAVA |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | import java.io.*;
import java.util.*;
public class C {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) throws IOException {
readInput();
out.close();
}
public static void readInput() throws IOException {
// br = new BufferedReader(new FileReader(".in"));
// out = new PrintWriter(new FileWriter(".out"));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
int[][] p = new int[n+1][2];
String s = br.readLine();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == '1') {
p[i+1][1] = 1;
}
else p[i+1][0] = 01;
p[i+1][0] += p[i][0];
p[i+1][1] += p[i][1];
}
while(q-->0) {
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
long res = pow(p[r][1]-p[l-1][1])-1 + mod;
res %= mod;
res *= pow(p[r][0] - p[l-1][0]);
out.println(res % mod);
}
}
static long mod = (int)1e9+7;
static long pow(int e) {
if (e == 0) return 1;
long r = pow(e/2);
r = r*r%mod;
if (e % 2 ==1) return 2 * r % mod;
return r;
}
}
| JAVA |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | import java.lang.*;
import java.util.*;
import java.io.*;
public class CF1335{
static long power(long x, long y, long p)
{
long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long calculate(int ones,int zeros){
if(ones==0){
return 0;
}
long ans = 0;
long mod = 1000000007;
ans = power(2,ones,mod);
ans--;
long val = ans;
long ans2 = power(2,zeros,mod);
ans2--;
ans2 = ((ans2%mod)*(val%mod))%mod;
ans = ((ans%mod)+(ans2%mod))%mod;
return ans;
}
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = new Integer(st.nextToken());
int q = new Integer(st.nextToken());
StringBuffer sb = new StringBuffer();
String a = br.readLine();
int[] ones = new int[a.length()+1];
int[] zeros = new int[a.length()+1];
for(int i=0;i<a.length();i++){
if(a.charAt(i)=='1'){
ones[i+1]=ones[i]+1;
zeros[i+1]=zeros[i];
}
else{
zeros[i+1]=zeros[i]+1;
ones[i+1]=ones[i];
}
}
while(q-->0){
StringTokenizer st1 = new StringTokenizer(br.readLine());
int w = new Integer(st1.nextToken()),v = new Integer(st1.nextToken());
sb.append(calculate(ones[v]-ones[w-1],zeros[v]-zeros[w-1])).append("\n");
}
System.out.println(sb);
}
}
| JAVA |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, q, x, y, z, h;
string s;
long long a[100005], b[100005];
void init() {
b[0] = 1;
for (long long(i) = (1); (i) <= (n); ++(i)) b[i] = b[i - 1] * 2 % 1000000007;
}
int main() {
cin >> n >> q;
cin >> s;
init();
for (long long(i) = (0); (i) < (n); ++(i)) a[i] = a[i - 1] + s[i] - 48;
while (q--) {
cin >> x >> y;
if (x == 1)
z = a[y - 1];
else
z = a[y - 1] - a[x - 2];
h = y - x + 1;
long long sum = (b[h] - b[h - z] + 1000000007) % 1000000007;
cout << sum << endl;
}
}
| CPP |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | import sys
input = sys.stdin.readline
mod = 10**9+7
n,q = map(int,input().split())
t= input()
zero=[0]*(n)
ones=[0]*(n)
if t[0]=='0':
zero[0]=1
else:
ones[0]=1
for j in range(1,n):
if t[j]=='0':
zero[j]=zero[j-1]+1
ones[j]=ones[j-1]
else:
ones[j]= ones[j-1]+1
zero[j] = zero[j-1]
ans=[]
for j in range(q):
a,b = map(int,input().split())
a-=1
b-=1
if a==0:
totalone=ones[b]
else:
totalone=ones[b]-ones[a-1]
if a==0:
totalzero=zero[b]
else:
totalzero=zero[b]-zero[a-1]
x=totalone+totalzero
ans.append(str((pow(2, x, mod) - pow(2, totalzero, mod) + mod) % mod))
sys.stdout.write("\n".join(ans))
| PYTHON3 |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 |
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.InputMismatchException;
/**
* @author thesparkboy
*
*/
public class A3 {
static int[] bit;
static int n, MOD = (int) 1e9 + 7;
static void update(int idx, int val) {
while (idx <= n) {
bit[idx] += val;
idx += idx & -idx;
}
}
static long query(int idx) {
long sum = 0;
while (idx > 0) {
sum += bit[idx];
idx -= idx & -idx;
}
return sum;
}
static long power(long x, long y) {
long p = MOD;
long res = 1;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int t = 1;
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
n = in.readInt();
int q = in.readInt();
String str = in.readString();
char c[] = str.toCharArray();
long arr[] = new long[n];
bit = new int[n + 1];
for (int j = 0; j < n; j++) {
arr[j] = c[j] - '0';
update(j + 1, (int) arr[j]);
}
while (q-- > 0) {
int l = in.readInt();
int r = in.readInt();
long num1 = (int) (query(r) - query(l - 1));
long num0 = (r - l + 1) - num1;
// int len = r - l + 1;
long ans = (((power(2, num0)) * (power(2, num1) - 1))) % MOD;
sb.append(ans);
sb.append("\n");
}
System.out.println(sb);
}
out.close();
}
private static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
long sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
}
private static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
} | JAVA |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | import java.util.*;
import java.io.*;
public class Banhmi {
/************************ SOLUTION STARTS HERE ************************/
static class MM { // MM (Modular Math) class
static final long mod = (long) (1e9) + 7; // Default
static long sub(long a, long b) {return (a - b + mod) % mod;}
static long mul(long a, long b) {return ((a % mod) * (b % mod)) % mod;}
static long add(long a, long b) {return (a + b) % mod;}
static long div(long a, long b) {return mul(a, modInverse(b));}
static long modInverse(long n) {return modPow(n, mod - 2);} // Fermat's little theorem
static long modPow(long a , long b) {
long pow = 1;
while(b > 0) {
if((b & 1L) == 1)
pow = ((pow * a) % mod);
a = ((a * a) % (mod));
b >>= 1;
}
return pow;
}
}
private static void solve() {
int n = nextInt();
int q = nextInt();
int val[] = new int[n + 1];
char str[] = nextLine().toCharArray();
for(int i = 0; i < n; i++)
val[i + 1] = val[i] + (str[i] - '0');
while(q-->0) {
int l = nextInt();
int r = nextInt();
int len = r - l + 1;
int ones = val[r] - val[l - 1];
int zeroes = len - ones;
long conjMax = MM.mul(MM.sub(MM.modPow(2, ones), 1), MM.modPow(2, zeroes));
println(conjMax);
}
}
/************************ SOLUTION ENDS HERE ************************/
/************************ TEMPLATE STARTS HERE **********************/
public static void main(String[] args) throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
st = null;
solve();
reader.close();
writer.close();
}
static BufferedReader reader;
static PrintWriter writer;
static StringTokenizer st;
static String next()
{while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;}
st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();}
static String nextLine() {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;}
static int nextInt() {return Integer.parseInt(next());}
static long nextLong() {return Long.parseLong(next());}
static double nextDouble(){return Double.parseDouble(next());}
static char nextChar() {return next().charAt(0);}
static int[] nextIntArray(int n) {int[] a= new int[n]; int i=0;while(i<n){a[i++]=nextInt();} return a;}
static long[] nextLongArray(int n) {long[]a= new long[n]; int i=0;while(i<n){a[i++]=nextLong();} return a;}
static int[] nextIntArrayOneBased(int n) {int[] a= new int[n+1]; int i=1;while(i<=n){a[i++]=nextInt();} return a;}
static long[] nextLongArrayOneBased(int n){long[]a= new long[n+1];int i=1;while(i<=n){a[i++]=nextLong();}return a;}
static void print(Object o) { writer.print(o); }
static void println(Object o){ writer.println(o);}
/************************ TEMPLATE ENDS HERE ************************/
} | JAVA |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, a[100005];
string s;
long long fastpow(int a, int b) {
if (b == 0) return 1LL;
if (b == 1) return 1LL * a;
int tmp = fastpow(a, b / 2) * fastpow(a, b / 2) % 1000000007;
if (b % 2 == 1)
return tmp * a % 1000000007;
else
return tmp;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin >> n >> q;
getline(cin, s);
getline(cin, s);
for (int i = 0; i < n; i++) a[i + 1] = a[i] + (s[i] == '1' ? 1 : 0);
while (q--) {
int l, r;
cin >> l >> r;
int k = a[r] - a[l - 1], K = r - l + 1 - k;
long long res = (fastpow(2, k) - 1LL) * (fastpow(2, K) - 1) % 1000000007;
res += fastpow(2, k) - 1LL;
res %= 1000000007;
cout << res << "\n";
}
return 0;
}
| CPP |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | from sys import *
m = 1000000007
n, q = map(int, stdin.readline().split())
a = stdin.readline()
ans = []
t = []
count = 0
for i in a:
if i == '1':
count+=1
t.append(count)
for _ in range(q):
x,y=map(int,input().split())
if(x==1):
p=t[y-1]
else:
p=t[y-1]-t[x-2]
q=(y-x+1-p)
s=pow(2,p+q,m) - (pow(2,q,m))
ans.append(s%m)
stdout.write('\n'.join(map(str, ans))) | PYTHON3 |
1062_C. Banh-mi | JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i β \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0.
For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7.
Input
The first line contains two integers n and q (1 β€ n, q β€ 100 000).
The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part.
Each of the following q lines contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the segment of the corresponding query.
Output
Print q lines, where i-th of them contains a single integer β the answer to the i-th query modulo 10^9 + 7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
* For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2.
* For query 2: Both 3, 4 and 4, 3 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer. | 2 | 9 | import sys
r = sys.stdin.readlines()
M = 10 ** 9 + 7
twopow = [1] * 100001
for j in range(1, 100001):
twopow[j] = (twopow[j - 1] * 2) % M
n, q = r[0].split(' ')
n = int(n)
q = int(q)
s = r[1]
p = [0]
for v in range(n):
p.append(p[v] + int(s[v]))
ans = []
for k in range(q):
a, b = r[k + 2].split(' ')
a = int(a)
b = int(b)
l = b - a + 1
one = p[b] - p[a - 1]
v = (twopow[l] - twopow[l - one] + M) % M
ans.append(str(v))
sys.stdout.write("\n".join(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;
long long n, a[1001], cnt = 0;
long long s;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
for (int i = 2; i <= n; i++) {
cnt += a[i] - a[1];
}
if (cnt >= s)
cout << a[1];
else if (a[1] >= (s - cnt) / n + ((s - cnt) % n == 0 ? 0 : 1))
cout << a[1] - (s - cnt) / n - ((s - cnt) % n == 0 ? 0 : 1);
else
cout << -1;
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.util.*;
public class Solution
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
long s,min=999999999,sum=0,t=0;
s=in.nextLong();
long v[]=new long[n];
for(int i=0;i<n;i++)
{
v[i]=in.nextLong();
if(min>v[i])
min=v[i];
t+=v[i];
}
if(t<s)
System.out.println(-1);
else
{
for(int i=0;i<n;i++)
{
sum+=v[i]-min;
v[i]=min;
}
if(sum>=s)
{
System.out.println(min);
}
else
{
s=s-sum;
System.out.println((n*min-s)/n);
}
}
}
}
| JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, a;
cin >> n >> a;
long long int arra[n + 1];
long long int sum = 0;
for (int i = 0; i < n; i++) {
cin >> arra[i];
sum = sum + arra[i];
}
if (sum >= a) {
long long int val = 0;
sort(arra, arra + n);
for (int i = 1; i < n; i++) {
val = val + (arra[i] - arra[0]);
}
if (val >= a) {
cout << arra[0] << endl;
} else {
long long int baki = a - val;
long long int tota = baki / n;
if (baki % n != 0) {
tota = tota + 1;
}
cout << arra[0] - (tota) << endl;
}
} else {
cout << "-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 | #include <bits/stdc++.h>
using namespace std;
void q1() {
long long int n;
cin >> n;
long long int arr[n];
long long int maxi = 0;
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] >= arr[maxi]) maxi = i;
}
maxi = 0;
long long int total = 0;
for (long long int i = 0; i < n; i++) {
long long int c1 = 2 * (abs(maxi - i) + abs(i - 0) + abs(maxi));
if (i != 0)
c1 *= arr[i];
else
c1 = 0;
total += c1;
}
cout << total << "\n";
}
void q2() {
long long int n, s;
cin >> n >> s;
long long int mi = INT_MAX;
long long int sum = 0;
for (long long int i = 0; i < n; i++) {
long long int temp;
cin >> temp;
sum += temp;
if (temp < mi) mi = temp;
}
if (sum < s)
cout << "-1";
else {
sum -= s;
cout << min(sum / n, mi);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
q2();
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 | x,y=map(int,input().split())
s=list(map(int,input().split()))
if sum(s)<y:
print(-1)
else:
j=sum(s)-y
if j<x:
print(0)
elif x==j:
print(1)
else:
print(min(min(s),j//x)) | PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n,m=map(int,input().split())
l=list(map(int,input().split()))
su=sum(l)
if su<m :
print(-1)
else :
c=(su-m)//n
print(min(min(l),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 | n, s = map(int, input().split())
a = list(map(int, input().split()))
def check(x):
global s
tot = 0
for i in a:
tot += i - x
return tot >= s
sum = 0
mi = 1e9
for i in a:
sum += i
mi = min(mi, i)
if sum < s:
print(-1)
else:
l = 0
r = mi
ans = r
while r >= l:
m = (l + r) // 2
if check(m):
l = m + 1
ans = m
else:
r = m - 1
print(int(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;
long long n, minn = 1e18;
long long a[1010];
int main() {
long long n, s, sum = 0;
cin >> n >> s;
for (long long i = 1; i <= n; ++i)
cin >> a[i], minn = min(minn, a[i]), sum += a[i];
if (s <= sum - n * minn) {
cout << minn << endl;
return 0;
}
if (minn - (s - (sum - n * minn) + n - 1) / n >= 0) {
cout << (minn - (s - sum + n * minn + n - 1) / n) << endl;
return 0;
}
cout << -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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
public class CodeForces {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader in = new FastReader();
int n = in.nextInt();
long s = in.nextLong();
long a[] = new long[n];
int i;
for(i=0;i<n;i++) {
a[i] = in.nextLong();
}
Arrays.sort(a);
i = n-1;
while(i>0) {
long min = Math.min(s,a[i]-a[0]);
s = s-min;
a[i] = a[i] - min;
if(s==0) {
break;
}
i--;
}
long x = s/(long)n;
long y = s%(long)n;
if(y>0) {
a[0] = a[0] - x -1;
} else {
a[0] = a[0] - x;
}
if(a[0] >= 0) {
System.out.println(a[0]);
} 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 | import math
n, s = map(int, input().split())
l = list(map(int, input().split()))
t1 = sum(l)
if t1<s:
print('-1')
else:
m = min(l)
t2 = 0
for i in range(len(l)):
x = l[i]-m
t2 = t2 + x
if t2 > s:
print(m)
else:
y = s - t2
ts = math.ceil(y/len(l))
min = m - ts
print(min)
| 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()))
y=min(a)*n
x=sum(a)
if x<s:
print(-1)
else:
if s<=x-y:
print(y//n)
else:
s=s-(x-y)
print((y-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 B_KvassAndTheFairNut {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader inp = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(inp, out);
out.close();
}
private static class Solver {
private void solve(InputReader inp, PrintWriter out) {
int n = inp.nextInt();
long s = inp.nextLong(), sum = 0, min = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
int value = inp.nextInt();
min = Math.min(min, value);
sum += value;
}
if (sum < s) {
out.print(-1);
return;
}
s -= sum - min * n;
if (s > 0) {
min -= (s / n);
if (s % n != 0) min--;
}
out.print(min);
}
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | from math import ceil
n,req=map(int,input().split())
ar=[int(x) for x in input().split()]
given=0
minn=min(ar)
for k in range(n):
given+=ar[k]-minn
if req-given>n*minn:
print(-1)
elif req-given<=0:
print(minn)
else:
print(minn-ceil((req-given)/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 | def doit():
xx = input().split()
n = int(xx[0])
s = int(xx[1])
v = [int(k) for k in input().split()]
S = sum(v)
newS = S - s
if newS < 0:
return -1
return min(newS//n, min(v))
print(doit())
| 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:
mi = min(v)
num = 0
for i in range(N):
num += v[i] - mi
v[i] -= v[i] - mi
if s - num <= 0:
print(mi)
else:
m = (s - num) // N + int((s - num) % N != 0)
print(mi - 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 int n, s;
cin >> n >> s;
vector<long long int> v(n);
long long int pp = 1e18;
long long int cnt = 0;
long long int kk = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
pp = min(pp, v[i]);
kk += v[i];
}
for (int i = 0; i < n; i++) {
cnt += (v[i] - pp);
}
if (kk < s) {
cout << "-1" << endl;
} else if (s <= cnt) {
cout << pp << endl;
} else {
s -= cnt;
cout << pp - (long long int)ceil((s + 0.0) / n) << endl;
}
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author real
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
long make = in.nextLong();
long ar[] = new long[n];
for (int i = 0; i < n; i++) {
ar[i] = in.nextLong();
}
long mn = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
mn = Math.min(mn, ar[i]);
}
long sum = 0;
for (int i = 0; i < n; i++) {
sum += ar[i] - mn;
}
// System.out.println(sum);
if (sum + mn * n < make) {
out.print(-1);
return;
}
if (sum >= make) {
out.print(mn);
return;
}
make = make - sum;
mn = mn - (make / n);
if (make % n != 0) {
mn--;
}
out.print(mn);
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
//*-*------clare------
//remeber while comparing 2 non primitive data type not to use ==
//remember Arrays.sort for primitive data has worst time case complexity of 0(n^2) bcoz it uses quick sort
//again silly mistakes ,yr kb tk krta rhega ye mistakes
//try to write simple codes ,break it into simple things
// for test cases make sure println(); ;)
//knowledge>rating
/*
public class Main
implements Runnable{
public static void main(String[] args) {
new Thread(null,new Main(),"Main",1<<26).start();
}
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();//chenge the name of task
solver.solve(1, in, out);
out.close();
}
*/
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 boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || 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 | import java.util.*;
public class A671{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long k=sc.nextLong();
long[] ar=new long[n];
long sum=0;
for(int i=0;i<n;i++){
ar[i]=sc.nextLong();
sum+=ar[i];
}
Arrays.sort(ar);
if(sum<k){
System.out.println("-1");
}
else{
long ans=0; boolean ok=false;
for(int i=0;i<n;i++){
ans+=ar[i]-ar[0];
if(ans>=k){
ok=true;
break;
}
}
if(ok==true){
System.out.println(ar[0]);
}
else{long key=k-ans; long mm=(long)n;
long an=ar[0];
an=(an*n-key)/n;
System.out.println(an);
}
}
}
}
| JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, s;
cin >> n >> s;
vector<long long> v(n);
long long base = 0x3f3f3f3f3f3f3f3f;
long long tot = 0;
for (auto &i : v) {
cin >> i;
base = min(base, i);
tot += i;
}
if (tot < s) return cout << -1 << '\n', 0;
long long sum = 0;
for (auto i : v) {
sum += i - base;
}
s -= sum;
if (s <= 0) return cout << base << '\n', 0;
cout << base - (((s - 1) / n) + 1) << '\n';
return 0;
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.io.*;
import java.util.*;
public class cf {
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader in) {
br = new BufferedReader(in);
}
public FastScanner() {
this(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextInt();
}
return a;
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
long vol = sc.nextLong();
Integer[] a = new Integer[n];
long sum = 0;
int min = Integer.MAX_VALUE;
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
sum += a[i];
min = Math.min(min, a[i]);
}
if(sum < vol) {
pw.println(-1);
pw.close();
return;
}
for(int i=0;i<n;i++) {
vol -= (a[i] - min);
}
if(vol <= 0) {
pw.println(min);
}
else {
pw.println(min - (vol + n - 1) / n);
}
pw.close();
}
}
| JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const double EPS = 0.0000000001;
const long long mod1 = 998244353;
const long long mod2 = 1000000007;
const long long mod3 = 1000000009;
const long long inf = 1000000000000000000;
signed main() {
long long n, a;
cin >> n >> a;
long long mass[n];
long long sum = 0;
for (long long i = 0; i < n; i++) cin >> mass[i];
sort(mass, mass + n);
for (long long i = 0; i < n; i++) sum += mass[i];
if (sum < a) {
cout << -1;
return 0;
}
long long c = 0;
for (long long i = 0; i < n; i++) c += mass[i] - mass[0];
if (c >= a) {
cout << mass[0];
return 0;
}
cout << mass[0] - (a - c + 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 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 12 19:13:40 2018
@author: mach
"""
n,m = map(int, input().strip().split())
l = list(map(int, input().strip().split()))
a_s = sum(l)
if a_s < m :
print(-1)
else:
l.sort(reverse=True)
mins = l[n-1]
for i in range(n):
m-= l[i] - mins
#print(m)
if m <= 0:
print(mins)
else:
#print((m + n -1)//2)
#print(mins)
#print(int((m+n - 1)/n))
print(mins - (m + 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;
long long abso(long long x) { return x > 0 ? x : (-x); }
signed main() {
long long n, s;
scanf("%lld%lld", &n, &s);
vector<long long> v;
v.resize(n);
long long sum = 0;
for (long long i = 0; i < n; i++) {
scanf("%lld", &v[i]);
sum += v[i];
}
if (sum < s) {
printf("-1\n");
return 0;
}
sort(v.begin(), v.end());
long long mn = v[0];
for (long long i = n - 1; i >= 0 && s > 0; i--) {
if (v[i] <= mn)
break;
else {
long long give = v[i] - mn;
if (s >= give) {
s -= give;
v[i] = mn;
} else {
v[i] = v[i] - s;
s = 0;
}
}
}
if (s > 0) {
long long final = s;
long long each = s / n;
long long leftOver = s - (each * n);
long long s = 0;
for (long long i = 0; i < n; i++) v[i] = v[i] - each;
long long j = 0;
while (leftOver--) v[j++]--;
}
printf("%lld\n", v[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.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Spoj {
public static void main(String[] arg) {
final Scanner scanner = new Scanner(System.in);
long n = scanner.nextLong();
long s = scanner.nextLong();
long[] arr = new long[(int)n];
long sum = 0;
for(int i=1;i<=n;i++){
arr[i-1] = scanner.nextLong();
sum += arr[i-1];
}
Arrays.sort(arr);
long cnt = 0;
for(int i=1;i<n; i++) {
cnt += arr[i] - arr[0];
}
if(cnt < s && sum >= s) {
long rem = s - cnt;
long dec = (rem + n -1)/n;
arr[0] -= dec;
cnt = s;
}
if(cnt>=s) {
System.out.println(arr[0]);
} 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 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
long s = in.nextLong();
long arr[] = new long[n];
long sum = 0;
long min = 999999999999L;
for (int i = 0; i < n; i++) {
arr[i] = in.nextLong();
sum = sum + arr[i];
min = Math.min(min, arr[i]);
}
if (sum < s) {
out.println("-1");
return;
}
long excess = 0;
for (int i = 0; i < n; i++) {
excess = excess + Math.abs(arr[i] - min);
}
if (excess >= s) {
out.println(min);
return;
}
long remaining = Math.abs(s - excess);
long division = (long) Math.ceil(remaining / (1.0 * n));
min = min - division;
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 | #include <bits/stdc++.h>
using namespace std;
int n, v[1005];
long long g, sum = 0;
int main() {
cin >> n >> g;
for (int i = 1; i <= n; i++) cin >> v[i];
sort(v + 1, v + n + 1, greater<int>());
for (int i = 1; i <= n; i++) sum += v[i];
if (sum < g) {
cout << -1;
return 0;
}
for (int i = 1; i < n; i++) g -= (v[i] - v[n]);
if (g <= 0) {
cout << v[n];
return 0;
}
cout << v[n] - (g + n - 1) / 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 math
n, s = list(map(int, input().split()))
v = list(map(int, input().split()))
sum_v = sum(v)
if sum_v < s:
print(-1)
else:
v.sort()
minimum = v[0]
remaining = s - (sum_v - n * minimum)
if remaining <= 0:
print(minimum)
else:
print(minimum - int(math.ceil(remaining/n))) | PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n,s = map(int,input().split())
v = [int(x) for x in input().split()]
v.sort()
l = v[0]
for i in range(1,n):
if v[i]>l:
s -= v[i]-l
if s <= 0:
print(v[0])
else:
r = (s+n-1)//n
if r > v[0]:
print(-1)
else:
print(v[0]-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;
long long int n, i, a, s, t, m = 2e9;
int main() {
for (cin >> n >> s; i < n; i++) cin >> a, t += a, m = min(m, a);
if (t < s)
cout << -1;
else if (t - m * n < s)
cout << (t - s) / n;
else
cout << m;
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.util.*;
public class div400{
public static void main(String sp[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long s = sc.nextLong();
ArrayList<Integer> al = new ArrayList<>();
for(int i=0;i<n;i++)
al.add(sc.nextInt());
long total=0;
for(int i=0;i<n;i++)
total+=al.get(i);
if(total<s){
System.out.println("-1");
return;
}
long exe =0;
long min = Collections.min(al);
for(int i=0;i<n;i++){
exe+=(al.get(i)-min);
}
if(exe>=s){
System.out.println(min);
}else{
s-=exe;
long mod = s%(n);
long div = s/(n);
if(mod==0){
System.out.println(min-div);
}else{
System.out.println(min-div-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;
const long long INF64 = 1e18 + 1337;
const int INF32 = 1e9 + 228;
const int MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, s;
cin >> n >> s;
vector<long long> a(n);
long long sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s) {
cout << -1;
return 0;
}
sort(a.begin(), a.end(), greater<int>());
for (int i = 0; i < n - 1; i++) s -= (a[i] - a[n - 1]);
while (s > 0 && a[n - 1] > 0) {
s -= n;
a[n - 1]--;
}
cout << a[n - 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 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, a[1000001], check(0), sum(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
sort(a + 1, a + n + 1);
for (int i = n; i >= 1; i--) {
check += a[i] - a[1];
}
if (m > sum)
cout << -1;
else {
if (m <= check)
cout << a[1];
else {
m -= check;
cout << a[1] - ((m - 1) / 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,stdout
nmbr=lambda:int(stdin.readline())
lst=lambda:list(map(int, stdin.readline().split()))
def fn(fin):
sm=0
for v in a:
if v>=fin:sm+=v-fin
else:return False
return sm>=req
for _ in range(1):#nmbr()):
n,req=lst()
a=lst()
if sum(a)<req:
print(-1)
continue
l=0;r=10**12+1
while l<=r:
mid=(l+r)>>1
if fn(mid):l=mid+1
else:r=mid-1
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 | n,s=map(int,input().split())
v=sorted(list(map(int,input().split())))
if sum(v)<s:
print(-1)
else:
s0=sum(v)-v[0]*n
if s<=s0:
print(v[0])
else:
s-=s0
if s%n==0:
print(v[0]-s//n)
else:
print(v[0]-s//n-1) | PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n,s = map(int,input().split())
a = list(map(int,input().split()))
if sum(a) < s: print(-1)
elif sum(a) == s: print(0)
else:
ans = 0
for i in range(n):
if a[i] != min(a): ans += (a[i]-min(a)); a[i] = min(a)
print(min(min(a),min(a)-((s-ans)//n)-((s-ans)%n > 0))) | PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n, s = [int(i) for i in input().split(" ")]
a = [int(i) for i in input().split(" ")]
if sum(a) < s:
print(-1)
else:
a.sort()
s -= sum(a) - a[0] * n
if (s < 1):
print(a[0])
elif s % n == 0:
print(a[0] - s//n)
else:
print(a[0] - (s//n) - 1) | PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | m,n = input().split(' ')
m = int(m)
n = int(n)
s = list(map(int,input().split(' ')))
t = 0
for i in s:
t += i
t = t-n
t = t // m
less = 10**9+1
for i in s:
if i < less:
less = i
if t < 0:
print(-1)
else:
if less < t:
print(less)
else:
print(t)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long s=sc.nextLong();
long min=Integer.MAX_VALUE;
long sum=0;
for(int i=0;i<n;i++) {
long x=sc.nextLong();
min=Math.min(x, min);
sum+=x;
}
// System.out.println(min);
// System.out.println(sum);
if(sum-min*n>=s) {
System.out.println(min);
}else if(sum<s) {
System.out.println(-1);
}else {
s-=(sum-min*n);
if(s%n==0) System.out.println(min-s/n);
else System.out.println(min-s/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 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll V[1001];
int main() {
ll n, s;
cin >> n >> s;
ll mmin = 0;
ll sum = 0;
for (ll i = 1; i <= n; i++) {
cin >> V[i];
sum += V[i];
mmin = mmin == 0 ? V[i] : (min(V[i], mmin));
}
ll t = 0;
for (ll i = 1; i <= n; i++) t += abs(V[i] - mmin);
if (t >= s) {
cout << mmin << '\n';
} else {
s -= t;
if (sum - t < s) {
cout << "-1\n";
} else {
mmin -= s / n;
if (s % n > 0) mmin--;
cout << mmin << '\n';
}
}
return 0;
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import sys
import math
import bisect
def solve(A, m):
n = len(A)
if sum(A) < m:
return -1
min_val = min(A)
tmp = sum(A) - min_val * n
if tmp >= m:
return min_val
m -= tmp
ans = (min_val * n - m) // n
return ans
def main():
n, m = map(int, input().split())
A = list(map(int, input().split()))
ans = solve(A, m)
print(ans)
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;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, s, arr[1010] = {0}, c = 0, m = 1000000001;
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> arr[i];
c += arr[i];
m = min(m, arr[i]);
}
long long int sur = 0;
for (int i = 0; i < n; i++) {
sur += abs(arr[i] - m);
}
if (s > c)
cout << -1 << endl;
else if (sur >= s)
cout << m << endl;
else {
s -= sur;
long long int a = s / n;
long long int b = s % n;
if (b)
cout << m - a - 1 << endl;
else
cout << m - a << endl;
}
return 0;
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long n, s;
long long v[1001];
bool f(long long x) {
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += v[i] - x;
}
if (sum >= s) return true;
return false;
}
int main() {
cin >> n >> s;
long long mn = 10e9 + 1, sum = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
mn = min(mn, v[i]);
sum += v[i];
}
if (sum < s) {
cout << -1 << endl;
return 0;
}
long long st = 0, en = mn, ans, mid;
ans = st;
while (st <= en) {
mid = st + (en - st) / 2;
if (f(mid)) {
ans = max(ans, mid);
st = mid + 1;
} else
en = mid - 1;
}
cout << ans << endl;
return 0;
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public final class Main{
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String line = "";
if (st.hasMoreTokens()) line = st.nextToken();
else try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (st.hasMoreTokens()) line += " " + st.nextToken();
return line;
}
}
public static int mod(long a,long b)
{
if(a>b)
return (int)(a-b);
else
return (int)(b-a);
}
public static void main(String args[])
{
FastScanner sc=new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
int n,i;
long var=0,s,sum=0,m,min;
n=sc.nextInt();
s=sc.nextLong();
long[] arr=new long[n];
for(i=0;i<n;i++)
{
arr[i]=sc.nextLong();
sum+=arr[i];
}
if(s>sum)
{
System.out.println("-1");
return ;
}
m=n;
var=(sum-s)/m;
min=var;
for(i=0;i<n;i++)
{
if(arr[i]<min)
min=arr[i];
}
pw.println(min);
pw.flush();
pw.close();
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 |
n,k=map(int,input().split())
l=list(map(int,input().split()))
m=min(l)
s=sum(l)
if sum(l)<k:
print(-1)
elif s-m*n>=k:
print(m)
else:
val=s-m*n
k-=val
print(m-(k//n+int(k%n!=0)))
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n, s;
cin >> n >> s;
ll ar[n];
ll su = 0;
for (int i = 0; i < n; i++) {
cin >> ar[i];
su += ar[i];
}
if (su < s) {
cout << -1 << "\n";
} else {
sort(ar, ar + n);
ll dif = 0;
for (int i = 1; i < n; i++) {
dif += abs(ar[i] - ar[0]);
}
if (s < dif) {
cout << ar[0] << "\n";
} else {
ll ff = (s - dif) / n;
ar[0] = ar[0] - (ff + ((s - dif) % n > 0));
cout << ar[0] << "\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 | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n, val;
cin >> n >> val;
vector<long long> v(n);
long long minV = LLONG_MAX;
long long ts = 0;
for (long long i = 0; i < n; i++) {
cin >> v[i];
minV = min(minV, v[i]);
ts += v[i];
}
long long bufSpace = 0;
for (long long i = 0; i < n; i++) bufSpace += v[i] - minV;
if (val <= bufSpace) {
cout << minV << endl;
return 0;
}
if (ts < val) {
cout << -1 << endl;
return 0;
}
val -= bufSpace;
while (minV) {
val -= n;
minV--;
if (val <= 0) {
cout << minV << 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=input().split()
n=int(n)
s=int(s)
l=input().split()
l=list(map(int,l))
x=sum(l)
m=min(l)
h=len(l)
if x<s:
print(-1)
elif m*h<=(x-s):
print(m)
else:
q=(m*h-(x-s))/h
if int(q)==q:
print(m-int(q))
else:
print(m-(int(q)+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.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;
public class l {
public static void main(String[]args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
long s=sc.nextLong();
long[]c= new long[n];
long min =Long.MAX_VALUE;
for (int i=0;i<n;i++){
c[i]= sc.nextLong();
min=Math.min(min,c[i]);
}
for (int i =0;i<n;i++){
s-=c[i]-min;
}
long all=(1l*n)*min;
if (s<=0){
out.println(min);
}
else{
if (all<s){
out.println(-1);
}
else {
all-=(s);
long ans = all/n;
out.println(ans);
}
}
out.flush();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
}
| JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, sum = 0;
cin >> n >> s;
long long a[n];
int i;
for (i = 0; i < n; ++i) {
cin >> a[i];
if (sum <= s + 1) {
sum += a[i];
}
}
if (sum == s) {
cout << 0 << endl;
} else if (sum < s) {
cout << -1 << endl;
} else {
sort(a, a + n);
long long m = 0;
for (i = 0; i < n; ++i) {
m += a[i] - a[0];
if (m >= s) {
break;
}
}
if (m >= s) {
cout << a[0] << endl;
} else {
long long k = s - m;
if (k % n == 0) {
a[0] -= k / n;
} else {
a[0] -= k / n + 1;
}
if (a[0] <= 0) {
cout << 0 << endl;
} else {
cout << a[0] << 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(w) for w in input().split()]
v = [int(w) for w in input().split()]
r = sum(v) - s
if r < 0:
print(-1)
else:
print(min(r // n, *v))
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | def _1084B():
n, s = map(int, input().split())
aa = list(map(int, input().split()))
mn = min(aa)
if sum(aa) < s:
return -1
totdif = 0
for i in range(n):
totdif += aa[i]-mn
if s <= totdif:
return mn
s -= totdif
q, r = s//n, s%n
return mn-q if r == 0 else mn-q-1
if __name__ == '__main__':
print(_1084B()) | 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())
l = list(map(int,input().split()))
l.sort()
l.reverse()
k = min(l)
x = 0
if s > sum(l):
print(-1)
exit()
for i in range(len(l)):
if l[i] > k:
s -= l[i] - k
l[i] = k
if s > 0:
if s % len(l) != 0:
tt = s // len(l)
tt += 1
else:
tt = s // len(l)
s -= tt * len(l)
l[0] -= tt
print(min(l))
| 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.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.StringTokenizer;
/**
* @author Sri Mouli
* Copyright Β© 2018 Sri Mouli. All rights reserved.
*/
@SuppressWarnings("ALL")
public class B1084 {
public static long MOD = 1000000007;
public static long[][][] dp;
public static String INPUT = "3 " +
"0 2 1";
@SuppressWarnings("unused")
public static void main(String[] args) {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// PrintWriter out = new PrintWriter(new FileOutputStream(new
// File("output.txt")));
PrintWriter out = new PrintWriter(System.out);
FastReader in = new FastReader(oj);
int n = in.nextInt();
long s = in.nextLong();
int[] a = in.nextIntArray(n);
long sum = in.sum;
int min = in.min;
if(s>sum){
System.out.println("-1");
return;
}
else if(s == sum){
System.out.println("0");
return;
}
long minChange = min * Long.valueOf(n);
long minGlassToChange = sum - minChange;
if(s<=minGlassToChange){
System.out.println(min);
return;
}
else {
long remaining = s-minGlassToChange;
min -= Math.toIntExact(remaining/n);
if(remaining%n==0){
System.out.println(min);
}else{
// if(min==1){
// System.out.println(min);
// return;
// } else {
// System.out.println(min-1);
// }
System.out.println(min-1);
}
}
return;
}
public static int upper_bound(int A[], int key) {
int first = 0;
int last = A.length;
int mid;
while (first < last) {
mid = first + (last - first) / 2;
if (A[mid] <= key)
first = mid + 1;
else
last = mid;
}
return first;
}
// For finding greater than equal to
public static int lower_bound(int A[], int key) {
int first = 0;
int last = A.length;
int mid;
while (first < last) {
mid = first + ((last - first) >> 1);
if (A[mid] < key)
first = mid + 1;
else
last = mid;
}
return first;
}
@SuppressWarnings("rawtypes")
public static void printPermutations(Comparable[] c) {
System.out.println(Arrays.toString(c));
while ((c = nextPermutation(c)) != null) {
System.out.println(Arrays.toString(c));
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
// modifies c to next permutation or returns null if such permutation does not
// exist
public static Comparable[] nextPermutation(final Comparable[] c) {
// 1. finds the largest k, that c[k] < c[k+1]
int first = getFirst(c);
if (first == -1)
return null; // no greater permutation
// 2. find last index toSwap, that c[k] < c[toSwap]
int toSwap = c.length - 1;
while (c[first].compareTo(c[toSwap]) >= 0)
--toSwap;
// 3. swap elements with indexes first and last
swap(c, first++, toSwap);
// 4. reverse sequence from k+1 to n (inclusive)
toSwap = c.length - 1;
while (first < toSwap)
swap(c, first++, toSwap--);
return c;
}
// finds the largest k, that c[k] < c[k+1]
// if no such k exists (there is not greater permutation), return -1
@SuppressWarnings({ "rawtypes", "unchecked" })
public static int getFirst(final Comparable[] c) {
for (int i = c.length - 2; i >= 0; --i)
if (c[i].compareTo(c[i + 1]) < 0)
return i;
return -1;
}
// swaps two elements (with indexes i and j) in array
@SuppressWarnings("rawtypes")
public static void swap(final Comparable[] c, final int i, final int j) {
final Comparable tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
public static long cal(int index, int last, int n, int left) {
if (index == n) {
return left == 0 ? 1 : 0;
}
if (left < 0) {
return 0;
}
if (dp[last][index][left] != -1) {
return dp[last][index][left];
}
long result = 0;
for (int i = 0; i < 4; i++) {
result += cal(index + 1, i, n, left - count(last, i));
result %= MOD;
}
return dp[last][index][left] = result;
}
public static int count(int last, int cur) {
if (last == cur) {
return 0;
}
if (last == 0) {
return 1;
}
if (last == 1) {
if (cur == 2) {
return 2;
}
return 0;
}
if (last == 2) {
if (cur == 1) {
return 2;
}
return 0;
}
return 1;
}
public static boolean div(int need, String v) {
int cur = 0;
for (int i = 0; i < v.length(); i++) {
cur += v.charAt(i) - '0';
if (cur == need) {
cur = 0;
} else if (cur > need) {
return false;
}
}
return true;
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static class Point implements Comparable<Point> {
int x, y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
@Override
public int hashCode() {
int hash = 5;
hash = 47 * hash + this.x;
hash = 47 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Point other = (Point) obj;
if (this.x != other.x) {
return false;
}
return this.y == other.y;
}
@Override
public int compareTo(Point o) {
return Integer.compare(x, o.x);
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, long b, long MOD) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2, MOD);
if (b % 2 == 0) {
return val * val % MOD;
} else {
return val * (val * a % MOD) % MOD;
}
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
private static class Pair {
long x;
long y;
@SuppressWarnings("unused")
public Pair(long first, long second) {
this.x = first;
this.y = second;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
@SuppressWarnings("rawtypes")
public static Comparator csort = (Comparator<Pair>) (a, b) -> {
if (a.x < b.x)
return -1;
else if (a.x > b.x)
return 1;
else
return Long.compare(a.y, b.y);
};
static class FastReader {
BufferedReader br;
StringTokenizer st;
InputStream is;
byte[] inbuf = new byte[1024];
int lenbuf = 0, ptrbuf = 0;
FastReader(boolean onlineJudge) {
is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
br = new BufferedReader(new InputStreamReader(System.in));
}
boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
FastReader(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
long nextLong() {
return Long.parseLong(next());
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
long sum = 0;
int min = Integer.MAX_VALUE;
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
int cur = nextInt();
a[i] = cur;
if(min>cur){
min = cur;
}
sum+=cur;
}
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[][] next2DInt(int n, int m) {
int[][] arr = new int[n][];
for (int i = 0; i < n; i++) {
arr[i] = nextIntArray(m);
}
return arr;
}
long[][] next2DLong(int n, int m) {
long[][] arr = new long[n][];
for (int i = 0; i < n; i++) {
arr[i] = nextLongArray(m);
}
return arr;
}
int[] shuffle(int[] arr) {
Random r = new Random();
for (int i = 1, j; i < arr.length; i++) {
j = r.nextInt(i);
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
return arr;
}
int[] uniq(int[] arr) {
Arrays.sort(arr);
int[] rv = new int[arr.length];
int pos = 0;
rv[pos++] = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1]) {
rv[pos++] = arr[i];
}
}
return Arrays.copyOf(rv, pos);
}
}
}
| 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 <typename T>
inline void read(T& x) {
int f = 0, c = getchar();
x = 0;
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
if (f) x = -x;
}
template <typename T, typename... Args>
inline void read(T& x, Args&... args) {
read(x);
read(args...);
}
template <typename T>
void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
template <typename T>
void writes(T x) {
write(x);
putchar(' ');
}
template <typename T>
void writeln(T x) {
write(x);
puts("");
}
template <typename T>
inline bool chkmin(T& x, const T& y) {
return y < x ? (x = y, true) : false;
}
template <typename T>
inline bool chkmax(T& x, const T& y) {
return x < y ? (x = y, true) : false;
}
const int maxn = (int)2e5 + 10;
const long long inf = (long long)1e18;
const int mod = (int)1e9 + 7;
long long n, s, v[1005], sum, mx, mn = 1e9 + 1;
bool check(long long m) {
long long res = 0;
for (int i = (0); i < (n); i++) {
res += v[i] - m;
}
return res >= s;
}
int main() {
read(n, s);
for (int i = (0); i < (n); i++)
read(v[i]), chkmin(mn, v[i]), chkmax(mx, v[i]), sum += v[i];
if (sum < s) return puts("-1"), 0;
long long l = 0, r = mn, m = l + r >> 1;
while (r - l > 1) {
m = l + r >> 1;
if (check(m)) {
l = m;
} else {
r = m;
}
}
if (check(r))
writes(r);
else if (check(m))
writes(m);
else
writes(l);
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.util.*;
import java.io.*;
import java.lang.*;
public class fair{
public static void main(String[] arg) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long s = Long.parseLong(st.nextToken());
int[] vol = new int[n];
int min = Integer.MAX_VALUE;
long sum = 0;
StringTokenizer rt = new StringTokenizer(br.readLine());
for(int k = 0; k < n; k++){
int temp = Integer.parseInt(rt.nextToken());
vol[k] = temp;
sum += temp;
if(temp < min){
min = temp;
}
}
if(sum < s){
out.println(-1);
out.close();
}
sum = 0;
for(int k = 0; k < n; k ++){
sum += vol[k] - min;
}
if(sum > s){
out.println(min);
out.close();
}
else{
long val = s - sum;
long mod = val % n;
long answer = min - (val/n);
if(mod!= 0){
answer--;
}
out.println(answer);
out.close();
}
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | (n, s), v = map(int, input().split()), tuple(map(int, input().split()))
minv, sumv = min(v), sum(v)
maxminv = (sumv - s) // n
# print(n, s, sumv, minv, maxminv)
print(-1 if s > sumv else (minv if minv < maxminv else maxminv))
| 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 codeforces.contests.round526;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* @author tainic on Nov 28, 2018
*/
public class B {
//private static final boolean LOCAL = "aurel".equalsIgnoreCase(System.getenv().get("USER"));
private static final boolean LOCAL = false;
private static final String TEST =
"3 6\n" +
"1 2 3";
void solve(Scanner in, PrintWriter out) {
int n = in.nextInt();
long s = in.nextLong();
long min = in.nextLong();
long sum = min;
long x = 0;
for (int i = 1; i < n; i++) {
long ai = in.nextLong();
sum += ai;
if (ai < min) {
x += (min - ai) * i;
min = ai;
} else {
x += ai - min;
}
}
if (s > sum) {
out.println(-1);
} else if (x >= s) {
out.println(min);
} else {
out.println(min - (s - x) / n - ((s - x) % n > 0 ? 1 : 0));
}
}
//region main
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(!LOCAL ? System.in : new ByteArrayInputStream(TEST.getBytes())));
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out, 2048), false);
new B().solve(in, out);
out.close();
}
//endregion
//region imports
//endregion
} | 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,raw_input().split())
l=map(int,raw_input().split())
l.sort()
su=sum(l)
if su<s:
print -1
else:
deff=su-s
mini=deff/n
print min(mini,l[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 = list(map(int,input().split()))
arr = list(map(int,input().split()))
if sum(arr)<s:
print(-1)
else:
arr.sort()
now = 0
minn = arr[0]
for i in range(len(arr)-1,0,-1):
if now>=s:
break
else:
now += arr[i]-minn
rem = s - now
if rem<=0:
print(minn)
else:
if rem<=len(arr):
print(minn-1)
else:
if rem%len(arr)==0:
print(minn - rem//n)
else:
diff = rem//n + 1
print(minn-diff) | PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s;
cin >> n >> s;
long long sum = 0;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s) {
cout << -1;
return 0;
}
sort(a, a + n);
for (int i = n - 1; i >= 1; i--) {
if (s <= 0) break;
s -= (a[i] - a[0]);
a[i] = a[0];
}
if (s <= 0) {
cout << a[0];
} else {
if (s % n == 0) {
cout << a[0] - s / n;
} else {
cout << a[0] - s / n - 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 | import java.util.Scanner;
public class Task1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long s=in.nextLong();
long minkol=1000000000;
long v=-s;
for (int i = 0; i <n ; i++) {
int a=in.nextInt();
if(a<minkol){
minkol=a;
}
v+=a;
}
if(0>v){
System.out.print(-1);
}else{
System.out.print(Math.min((v)/n,minkol));
}
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.io.*;
import java.util.*;
public class Main{
static int mod = (int)(Math.pow(10, 9) + 7);
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt();
long s = sc.nextLong();
long[] v = new long[n];
long total = 0;
for (int i = 0; i < n; i++){
v[i] = sc.nextInt();
total += v[i];
}
if (total < s) out.println(-1);
else{
Arrays.sort(v);
long cur = 0;
for (int i = 0; i < n; i++){
cur += v[i] - v[0];
v[i] = v[0];
}
if (cur >= s) out.println(v[0]);
else{
long rem = s - cur;
out.println((long)(v[0] - Math.ceil((double)rem/(double)n)));
}
}
out.close();
}
static long pow(long a, long N) {
if (N == 0) return 1;
else if (N == 1) return a;
else {
long R = pow(a,N/2);
if (N % 2 == 0) {
return R*R;
}
else {
return R*R*a;
}
}
}
static long powMod(long a, long N) {
if (N == 0) return 1;
else if (N == 1) return a % mod;
else {
long R = powMod(a,N/2) % mod;
R *= R % mod;
if (N % 2 == 1) {
R *= a % mod;
}
return R % mod;
}
}
static void mergeSort(int[] A){ // low to hi sort, single array only
int n = A.length;
if (n < 2) return;
int[] l = new int[n/2];
int[] r = new int[n - n/2];
for (int i = 0; i < n/2; i++){
l[i] = A[i];
}
for (int j = n/2; j < n; j++){
r[j-n/2] = A[j];
}
mergeSort(l);
mergeSort(r);
merge(l, r, A);
}
static void merge(int[] l, int[] r, int[] a){
int i = 0, j = 0, k = 0;
while (i < l.length && j < r.length && k < a.length){
if (l[i] < r[j]){
a[k] = l[i];
i++;
}
else{
a[k] = r[j];
j++;
}
k++;
}
while (i < l.length){
a[k] = l[i];
i++;
k++;
}
while (j < r.length){
a[k] = r[j];
j++;
k++;
}
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
}
| 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.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class z1 {
public static void main(String[] args) {
BigInteger k;
int n;
Scanner in = new Scanner(System.in);
n = in.nextInt();
k = in.nextBigInteger();
BigInteger[] m = new BigInteger[n];
BigInteger a = BigInteger.ZERO;
for (int i = 0; i < n; i++) {
m[i] = in.nextBigInteger();
a = a.add(m[i]);
}
if (a.compareTo(k) == 0) {
System.out.println(0);
return;
}
if (a.compareTo(k) < 0) {
System.out.println(-1);
return;
}
Arrays.sort(m);
BigInteger h = m[n - 1].subtract(m[0]);
if (h.compareTo(k) >= 0) {
System.out.println(m[0]);
return;
}
int i = n - 1;
while (k.compareTo(BigInteger.valueOf(0)) > 0 && h.compareTo(BigInteger.valueOf(0)) != 0) {
m[i] = m[i].subtract(h);
k = k.subtract(h);
i--;
h = m[i].subtract(m[0]);
}
if (k.compareTo(BigInteger.valueOf(0)) > 0) {
if ((k.mod(BigInteger.valueOf(n))).compareTo(BigInteger.valueOf(0)) == 0)
k = k.divide(BigInteger.valueOf(n));
else {
k = k.divide(BigInteger.valueOf(n));
k=k.add(BigInteger.valueOf(1));
}
m[0] = m[0].subtract(k);
}
System.out.println(m[0]);
}
}
| JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | arr =[]
cap = 0
def chck (k):
ans=0
for i in arr:
if (i<k):
return False
ans=ans+i-k
if (ans>=cap):
return True
return False
def bsrch (strt,ed):
ans=-1
while (strt<=ed):
mid=(strt+ed)//2
if (chck(mid)):
ans=mid
strt=mid+1
else:
ed=mid-1
return ans
n,cap=input().split()
n=int(n)
cap=int(cap)
arr = list(map(int,input().split()))
print(bsrch(0,1000000000)) | 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 mynfn():
x=input().split(" ")
n=int(x[0])
s=int(x[1])
y=input().split(" ")
min=1000000000000
sum=0
k=0
for i in range(len(y)):
y[i]=int(y[i])
if y[i]<min:
min=y[i]
k=i
sum=sum+y[i]
if sum<s:
print("-1")
else:
a=sum-min*n
if a>=s:
print(min)
else:
h1=int((s-a)/n)
h2=(s-a)%n
if h2==0:
print(min-h1)
else:
print(min-h1-1)
mynfn() | 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_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
t = 1;
while (t--) {
ll n, k;
cin >> n >> k;
ll a[n], m = INT_MAX, sum = 0, j = 0;
for (ll i = 0; i < n; i++) {
cin >> a[i];
m = min(m, a[i]);
j += a[i];
}
ll p = k;
ll rem = 0;
for (ll i = 0; i < n; i++) rem += (a[i] - m);
k -= rem;
if (j == p)
cout << 0;
else if (j < p)
cout << "-1";
else if (k < 0) {
cout << m;
} else {
ll g = m * n * 1ll;
g -= k;
cout << g / n;
}
}
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner ip=new Scanner(System.in);
long n=ip.nextLong();
long k=ip.nextLong();
long a[]=new long[1000+5];
long mn=1000000000+5;
long all=0;
for(int i=0;i<n;i++){
a[i]=ip.nextLong();
mn=Math.min(mn,a[i]);
all=all+a[i];
}
if(all<k) System.out.println(-1);
else{
long left=all-n*mn;
if(k<=left) System.out.println(mn);
else{
k=k-left;
long ans=(k+n-1)/n;
System.out.println(mn-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 | from os import path
import sys,time
# mod = int(1e9 + 7)
# import re
from math import ceil, floor,gcd,log,log2 ,factorial
from collections import defaultdict ,Counter , OrderedDict , deque
from itertools import combinations , groupby , zip_longest,permutations
# from string import ascii_lowercase ,ascii_uppercase
from bisect import *
from functools import reduce
from operator import mul
maxx = float('inf')
#----------------------------INPUT FUNCTIONS------------------------------------------#
I = lambda :int(sys.stdin.buffer.readline())
tup= lambda : map(int , sys.stdin.buffer.readline().split())
lint = lambda :[int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda: sys.stdin.readline().strip('\n')
grid = lambda r :[lint() for i in range(r)]
stpr = lambda x : sys.stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
localsys = 0
start_time = time.time()
if (path.exists('input.txt')):
sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
#left shift --- num*(2**k) --(k - shift)
# input = sys.stdin.readline
n ,s = tup()
ls = lint()
S = sum(ls)
if s < s:
print(-1)
elif s ==S :
print(0)
else:
ls.sort(reverse= True)
# print(ls)
x =0
for i in range(n-1):
if x <s:
p= min(s-x, ls[i]-ls[n-1])
x+=p
ls[i] = ls[i]-p
else:
break
s-=x
if s == 0:
print(min(ls))
exit()
if s <= n :
print(max(-1 ,min(ls)-1))
else:
print(max(-1,ls[n-1] - 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 | /* package whatever; // 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 Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long k=sc.nextLong();
ArrayList<Integer> a=new ArrayList<Integer> ();
int i;
long sum=0;
for(i=0;i<n;i++)
{
a.add(sc.nextInt());
sum+=a.get(i);
}
if(sum<k)
System.out.println("-1");
else
{
Collections.sort(a);
for(i=1;i<n;i++)
{
k-=(a.get(i)-a.get(0));
}
if(k<=0)
System.out.println(a.get(0));
else
{
if(k%a.size()==0)
{
long div=k/a.size();
if((a.get(0)-div)<0)
System.out.println(0);
else
System.out.println(a.get(0)-div);
}
else
{
long div1=k/a.size();
if((a.get(0)-div1-1)<0)
System.out.println("0");
else
System.out.println((a.get(0)-div1-1));
}
}
}
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.util.Scanner;
public class Main {
final static int INF = 0x3f3f3f3f;
static long n, s;
static int[] a = new int[1100];
public static boolean check(int m) {
long sum = 0;
for(int i = 0; i < n; i++) {
sum += a[i]-m;
}
return sum >= s;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextLong();
s = in.nextLong();
int L = 0, R = INF;
for(int i = 0; i < n; i++) {
a[i] = in.nextInt();
R = Math.min(R, a[i]);
}
while(L < R) {
int M = (L+R+1)>>1;
if(check(M)) L = M;
else R = M-1;
}
if(L==0 && check(0)==false) System.out.println(-1);
else System.out.println(L);
in.close();
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | n,s=map(int,input().split())
v=list(map(int,input().split()))
if sum(v)<s:
print(-1)
exit()
v.sort()
a=v[0]
for i in range(n):
s-=v[i]-a
if s<=0:
print(a)
else:
b=a-1-((s-1)//n)
if b>0:
print(b)
else:
print(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 = [int(i) for i in input().split()]
minVol = min(v)
sumVol = 0
for i in range(0,n):
sumVol = sumVol+v[i]
if sumVol < s :
print (-1)
elif sumVol == s:
print(0)
else:
kvas = 0
for i in range(n):
if v[i] > minVol:
kvas = kvas + v[i] - minVol
v[i] = v[i] - (v[i] - minVol)
if kvas >=s:
print(minVol)
else:
diff = s - kvas
if diff % n == 0:
print(v[0] - diff//n)
else:
print (v[0] - diff//n -1)
| PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long a[10001];
int main() {
long long n, s;
cin >> n >> s;
int Min = INT_MAX;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] < Min) Min = a[i];
}
long long sum = 0;
for (int i = 1; i <= n; i++) {
sum += (a[i] - Min);
}
if (sum >= s) cout << Min, exit(0);
for (int i = 1; true; i++) {
sum += n;
Min--;
if (sum >= s) cout << Min, exit(0);
if (Min == 0) cout << -1, exit(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.sort()
h=sum(a)
if h>s:
min=a[0]
f=h-min*n
if f>=s:
print(min)
else:
print((h-s)//n)
elif h==s:
print(0)
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 | I=lambda: map(int,input().split())
n, s=I()
l=list(I())
x=min(l)
total=0
for i in range(n):
total+=(l[i]-x)
y=s-total
if y<=0:
print(x)
else:
z=y//n
a=y%n
if a==0:
x=x-z
else:
z=z+1
x=x-z
if x<0:
print(-1)
else:
print(x) | PYTHON3 |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
long long S;
cin >> S;
vector<long long> a(n);
long long sum = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
sum += a[i];
}
if (sum < S) {
cout << -1;
return 0;
}
long long t = *min_element(a.begin(), a.end());
for (int i = 0; i < n; ++i) {
long long minus = a[i] - t;
if (S - minus <= 0) {
cout << t;
return 0;
}
S -= minus;
a[i] = t;
}
t -= S / n;
if (S % n) t--;
cout << t << "\n";
return 0;
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, s, i, an = 0, x;
cin >> n >> s;
int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (i = 1; i < n; i++) an += (a[i] - a[0]);
if (an >= s)
cout << a[0];
else {
s -= an;
x = a[0];
if (s > n * x)
cout << "-1";
else {
if (s % n == 0) {
s /= n;
cout << x - s;
} else {
s /= n;
cout << x - s - 1;
}
}
}
}
| CPP |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import java.util.Scanner;
public class kegs {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
double s=sc.nextDouble();
double[] k=new double[n];
for(int i=0;i<n;i++) {
k[i]=sc.nextDouble();
}
int pos=findLeast(k);
double level=findUsed(k,pos);
if(level>s) {
System.out.println((int)k[pos]);
}
else {
double rem=s-level;
int c=(int) (rem/n);
int r=(int) (rem%n);
if(r!=0) {
c=c+1;
}
double result= (k[pos]-c);
if(result>-1) {
System.out.printf("%.0f\n", result);
}
else System.out.println(-1);
}
//1000 980103855476
}
private static double findUsed(double[] k, int pos) {
double t=0;
for(int i=0;i<k.length;i++) {
t=t+Math.abs(k[i]-k[pos]);
}
return t;
}
private static int findLeast(double[] k) {
double t=k[0];int p=0;
for(int i=0;i<k.length-1;i++) {
if(t>k[i+1]){
t=k[i+1];
p=i+1;
}
}
return p;
}
}
| JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | import sys
a, b = input().split()
a, b = int(a), int(b)
k = input().split()
kk = k[:]
k = []
sum = 0
for i in kk:
a = int(i)
sum += a
k.append(a)
if sum < b:
print(-1)
sys.exit()
k.sort()
least = k[0]
summ = sum - least * len(k)
k = [least for i in k]
if(summ >= b):
print(least)
sys.exit()
else:
b -= summ
least -= (b // len(k)) + (1 if b % len(k) > 0 else 0)
print(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 |
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.InputMismatchException;
/**
* @author thesparkboy
*
*/
public class A2 {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int t = 1;
while (t-- > 0) {
int n = in.readInt();
long s = in.readLong();
Long arr[] = new Long[n];
long ans = 0;
long sum = 0;
for (int j = 0; j < n; j++) {
arr[j] = in.readLong();
sum += arr[j];
}
if (sum < s) {
System.out.println(-1);
out.close();
return;
}
Arrays.sort(arr);
long tmp = 0;
for (int i = 0; i < n; i++) {
tmp += arr[i] - arr[0];
}
if (tmp >= s) {
System.out.println(arr[0]);
} else {
s -= tmp;
long tot = arr[0] * n;
long rem = tot - s;
long diff = rem / n;
// System.out.println(arr[0]);
// System.out.println(rem);
System.out.println(diff);
}
}
out.close();
}
private static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
long sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
}
private static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
} | JAVA |
1084_B. Kvass and the Fair Nut | The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, s, sum = 0, count = 0, k, t = 0, p, i, j;
cin >> n >> s;
long long int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s)
cout << "-1";
else {
sort(a, a + n);
p = 0;
k = a[0];
for (i = 1; i < n; i++) {
if (a[i] > k) {
if (p <= s) {
j = a[i] - k;
a[i] = k;
p += j;
} else {
t = 1;
break;
}
} else
continue;
}
if (t == 1)
cout << a[0];
else if (s <= p) {
cout << a[0];
} else {
if (s - p <= n)
cout << a[0] - 1;
else {
i = 0;
while ((s - p - n * i) > 0) {
count++;
i++;
}
cout << a[0] - i;
}
}
}
}
| CPP |
Subsets and Splits