output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
long long i, j, k, n, m, x, z, q, a[100009], pw[100009];
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
string s;
cin >> x >> q >> s;
for (i = 0; i < x; i++) {
if (s[i] == '0')
a[i + 1] = a[i] + 1;
else
a[i + 1] = a[i];
}
pw[0] = 1;
for (i = 1; i < 100001; i++) pw[i] = (pw[i - 1] * 2) % 1000000007;
while (q--) {
cin >> n >> m;
x = a[m] - a[n - 1];
cout << (pw[m - n + 1] - pw[x] + 1000000007) % 1000000007 << endl;
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
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.
### Response
```cpp
#include <bits/stdc++.h>
long long i, j, k, n, m, x, z, q, a[100009], pw[100009];
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
string s;
cin >> x >> q >> s;
for (i = 0; i < x; i++) {
if (s[i] == '0')
a[i + 1] = a[i] + 1;
else
a[i + 1] = a[i];
}
pw[0] = 1;
for (i = 1; i < 100001; i++) pw[i] = (pw[i - 1] * 2) % 1000000007;
while (q--) {
cin >> n >> m;
x = a[m] - a[n - 1];
cout << (pw[m - n + 1] - pw[x] + 1000000007) % 1000000007 << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int MAX = 1e5 + 10;
const long long BIG = 1e11 + 10;
const double eps = 1e-6;
const double PI = 3.14159;
const long long mod = 1000000007;
int n, q;
string s;
int one[MAX], zero[MAX];
long long mypow(long long a, long long n, long long MOD) {
long long res = 1;
while (n) {
if (n % 2 == 1) {
res *= a;
res = res % MOD;
n--;
}
n = n / 2;
a = (a * a) % MOD;
}
return res;
}
int main() {
while (cin >> n >> q) {
cin >> s;
int len = s.size();
memset(one, 0, sizeof(one));
memset(zero, 0, sizeof(zero));
for (int i = 0; i < len; i++) {
one[i + 1] = one[i];
zero[i + 1] = zero[i];
if (s[i] == '0')
zero[i + 1]++;
else
one[i + 1]++;
;
}
int l, r;
long long a, b;
while (q--) {
scanf("%d%d", &l, &r);
a = one[r];
b = zero[r];
a -= one[l - 1];
b -= zero[l - 1];
long long ans = mypow(2, a, mod) - 1;
ans = (ans * mypow(2, b, mod)) % mod;
cout << ans << endl;
}
}
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int MAX = 1e5 + 10;
const long long BIG = 1e11 + 10;
const double eps = 1e-6;
const double PI = 3.14159;
const long long mod = 1000000007;
int n, q;
string s;
int one[MAX], zero[MAX];
long long mypow(long long a, long long n, long long MOD) {
long long res = 1;
while (n) {
if (n % 2 == 1) {
res *= a;
res = res % MOD;
n--;
}
n = n / 2;
a = (a * a) % MOD;
}
return res;
}
int main() {
while (cin >> n >> q) {
cin >> s;
int len = s.size();
memset(one, 0, sizeof(one));
memset(zero, 0, sizeof(zero));
for (int i = 0; i < len; i++) {
one[i + 1] = one[i];
zero[i + 1] = zero[i];
if (s[i] == '0')
zero[i + 1]++;
else
one[i + 1]++;
;
}
int l, r;
long long a, b;
while (q--) {
scanf("%d%d", &l, &r);
a = one[r];
b = zero[r];
a -= one[l - 1];
b -= zero[l - 1];
long long ans = mypow(2, a, mod) - 1;
ans = (ans * mypow(2, b, mod)) % mod;
cout << ans << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
const long long mod = 1e9 + 7;
int cnt[maxn][2];
long long powMod(long long a, long long b) {
long long sum = 1;
a %= mod;
while (b > 0) {
if (b % 2 == 1) sum = (sum * a) % mod;
b /= 2;
a = (a * a) % mod;
}
return sum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int c0 = 0, c1 = 0;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] == '0')
c0++;
else
c1++;
cnt[i][0] = c0;
cnt[i][1] = c1;
}
int l, r;
while (m--) {
cin >> l >> r;
l--, r--;
c0 = cnt[r][0] - cnt[l][0] + (s[l] == '0');
c1 = cnt[r][1] - cnt[l][1] + (s[l] == '1');
long long ans = powMod(2, c1) - 1;
if (ans < 0) ans += mod;
cout << powMod(2, c0) * ans % mod << endl;
}
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
const long long mod = 1e9 + 7;
int cnt[maxn][2];
long long powMod(long long a, long long b) {
long long sum = 1;
a %= mod;
while (b > 0) {
if (b % 2 == 1) sum = (sum * a) % mod;
b /= 2;
a = (a * a) % mod;
}
return sum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int c0 = 0, c1 = 0;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] == '0')
c0++;
else
c1++;
cnt[i][0] = c0;
cnt[i][1] = c1;
}
int l, r;
while (m--) {
cin >> l >> r;
l--, r--;
c0 = cnt[r][0] - cnt[l][0] + (s[l] == '0');
c1 = cnt[r][1] - cnt[l][1] + (s[l] == '1');
long long ans = powMod(2, c1) - 1;
if (ans < 0) ans += mod;
cout << powMod(2, c0) * ans % mod << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e5 + 7;
const long long mod = 1e9 + 7;
string a;
long long a1[maxn];
long long counting(long long a, long long b) {
long long sum = 1;
while (a) {
if (a & 1) sum = (b * sum) % mod;
b = (b * b) % mod;
a >>= 1;
}
return sum % mod;
}
int main() {
long long n, q;
while (~scanf("%lld%lld", &n, &q)) {
cin >> a;
for (long long b = 1; b <= a.size(); b++)
a1[b] += a1[b - 1] + a[b - 1] - '0';
while (q--) {
long long l, r;
cin >> l >> r;
long long num = 0;
long long num1 = a1[r] - a1[l - 1];
num = (num + counting(num1, 2) - 1) % mod;
num = (num * counting((r - l + 1) - num1, 2)) % mod;
printf("%lld\n", num % mod);
}
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e5 + 7;
const long long mod = 1e9 + 7;
string a;
long long a1[maxn];
long long counting(long long a, long long b) {
long long sum = 1;
while (a) {
if (a & 1) sum = (b * sum) % mod;
b = (b * b) % mod;
a >>= 1;
}
return sum % mod;
}
int main() {
long long n, q;
while (~scanf("%lld%lld", &n, &q)) {
cin >> a;
for (long long b = 1; b <= a.size(); b++)
a1[b] += a1[b - 1] + a[b - 1] - '0';
while (q--) {
long long l, r;
cin >> l >> r;
long long num = 0;
long long num1 = a1[r] - a1[l - 1];
num = (num + counting(num1, 2) - 1) % mod;
num = (num * counting((r - l + 1) - num1, 2)) % mod;
printf("%lld\n", num % mod);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void dout() { cerr << endl; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
const int mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
string s;
cin >> s;
vector<int> pw(n + 1);
vector<int> pws(n + 1);
pw[0] = 1;
pws[0] = 1;
for (int i = 1; i <= n; i++) {
pw[i] = (pw[i - 1] * 2) % mod;
pws[i] = (pw[i] + pws[i - 1]) % mod;
}
vector<int> psum(n + 1);
for (int i = 0; i < n; i++) {
psum[i + 1] = psum[i] + (s[i] - '0');
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
int len = r - l + 1;
int c = psum[r] - psum[l - 1];
int ans = pw[c] - 1;
if (c > 0) {
ans += pws[len - 1];
ans %= mod;
ans -= pws[c - 1];
ans += mod;
ans %= mod;
if (c < len) {
ans -= pws[len - c - 1];
ans += mod;
ans %= mod;
}
}
cout << ans << '\n';
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void dout() { cerr << endl; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
const int mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
string s;
cin >> s;
vector<int> pw(n + 1);
vector<int> pws(n + 1);
pw[0] = 1;
pws[0] = 1;
for (int i = 1; i <= n; i++) {
pw[i] = (pw[i - 1] * 2) % mod;
pws[i] = (pw[i] + pws[i - 1]) % mod;
}
vector<int> psum(n + 1);
for (int i = 0; i < n; i++) {
psum[i + 1] = psum[i] + (s[i] - '0');
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
int len = r - l + 1;
int c = psum[r] - psum[l - 1];
int ans = pw[c] - 1;
if (c > 0) {
ans += pws[len - 1];
ans %= mod;
ans -= pws[c - 1];
ans += mod;
ans %= mod;
if (c < len) {
ans -= pws[len - c - 1];
ans += mod;
ans %= mod;
}
}
cout << ans << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007ll;
int n, q;
char s[100100];
long long count0[100100];
long long count1[100100];
long long QPow(long long x, long long n) {
long long ret = 1;
long long tmp = x % MOD;
while (n) {
if (n & 1) {
ret = (ret * tmp) % MOD;
}
tmp = (tmp * tmp) % MOD;
n >>= 1;
}
return ret;
}
int main() {
scanf("%d%d", &n, &q);
scanf("%s", s);
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
count0[i + 1] = 1 + count0[i];
count1[i + 1] = count1[i];
} else {
count1[i + 1] = 1 + count1[i];
count0[i + 1] = count0[i];
}
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
long long c0 = count0[r] - count0[l - 1];
long long c1 = count1[r] - count1[l - 1];
long long ans = QPow(2, c1) - 1;
long long ans2 = ((QPow(2, c0) - 1) * ans) % MOD;
ans = (ans + ans2) % MOD;
printf("%lld\n", ans);
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007ll;
int n, q;
char s[100100];
long long count0[100100];
long long count1[100100];
long long QPow(long long x, long long n) {
long long ret = 1;
long long tmp = x % MOD;
while (n) {
if (n & 1) {
ret = (ret * tmp) % MOD;
}
tmp = (tmp * tmp) % MOD;
n >>= 1;
}
return ret;
}
int main() {
scanf("%d%d", &n, &q);
scanf("%s", s);
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
count0[i + 1] = 1 + count0[i];
count1[i + 1] = count1[i];
} else {
count1[i + 1] = 1 + count1[i];
count0[i + 1] = count0[i];
}
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
long long c0 = count0[r] - count0[l - 1];
long long c1 = count1[r] - count1[l - 1];
long long ans = QPow(2, c1) - 1;
long long ans2 = ((QPow(2, c0) - 1) * ans) % MOD;
ans = (ans + ans2) % MOD;
printf("%lld\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int LIMIT = 1e5 + 7;
const int MOD = 1e9 + 7;
const int MAX = 1 << 30;
int n, q, l, r, dp[LIMIT], f[LIMIT];
char c;
int main() {
scanf("%d %d\n", &n, &q);
f[0] = 1;
for (int i = 0; i < n; i++) {
scanf("%c", &c);
dp[i + 1] = dp[i] + (c - '0');
f[i + 1] = f[i] * 2ll % MOD;
}
while (q--) {
scanf("%d %d", &l, &r);
unsigned long long ones = dp[r] - dp[l - 1];
unsigned long long zeros = r - l + 1 - ones;
unsigned long long ans = (f[ones] + MOD - 1ll) % MOD;
ans = (ans * (unsigned long long)f[zeros]) % MOD;
printf("%lld\n", ans);
}
return EXIT_SUCCESS;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int LIMIT = 1e5 + 7;
const int MOD = 1e9 + 7;
const int MAX = 1 << 30;
int n, q, l, r, dp[LIMIT], f[LIMIT];
char c;
int main() {
scanf("%d %d\n", &n, &q);
f[0] = 1;
for (int i = 0; i < n; i++) {
scanf("%c", &c);
dp[i + 1] = dp[i] + (c - '0');
f[i + 1] = f[i] * 2ll % MOD;
}
while (q--) {
scanf("%d %d", &l, &r);
unsigned long long ones = dp[r] - dp[l - 1];
unsigned long long zeros = r - l + 1 - ones;
unsigned long long ans = (f[ones] + MOD - 1ll) % MOD;
ans = (ans * (unsigned long long)f[zeros]) % MOD;
printf("%lld\n", ans);
}
return EXIT_SUCCESS;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void smain();
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(12);
smain();
}
vector<long long> pf;
const long long N = 1 << 17;
const long long M = 1e9 + 7;
long long cnt[N];
inline long long MOD(long long v) {
if (v < 0) {
v %= M;
return v < 0 ? v + M : v;
}
return v % M;
}
long long prog(long long l, long long r) { return (l + r) * (r - l + 1) / 2; }
long long hlp[N] = {0, 1};
long long quer(long long l, long long r) {
l--, r--;
long long first = pf[r];
if (l) first -= pf[l - 1];
if (!first) return 0;
long long ans = cnt[r - l + 1] - 1;
long long v = (r - l + 1) - first;
return MOD(ans - hlp[v]);
}
void smain() {
cnt[0] = 1;
for (long long i = 1; i < N; ++i) cnt[i] = (cnt[i - 1] * 2) % M;
for (long long i = 2; i < N; ++i) hlp[i] = MOD(hlp[i - 1] + cnt[i - 1]);
long long n, q;
cin >> n >> q;
string s;
cin >> s;
pf.resize(n);
for (long long i = 0; i < n; ++i) pf[i] = s[i] - '0';
for (long long i = 1; i < n; ++i) pf[i] += pf[i - 1];
for (long long i = 0; i < q; ++i) {
long long l, r;
cin >> l >> r;
cout << quer(l, r) << '\n';
}
}
| ### Prompt
In CPP, your task is to solve the following problem:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void smain();
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(12);
smain();
}
vector<long long> pf;
const long long N = 1 << 17;
const long long M = 1e9 + 7;
long long cnt[N];
inline long long MOD(long long v) {
if (v < 0) {
v %= M;
return v < 0 ? v + M : v;
}
return v % M;
}
long long prog(long long l, long long r) { return (l + r) * (r - l + 1) / 2; }
long long hlp[N] = {0, 1};
long long quer(long long l, long long r) {
l--, r--;
long long first = pf[r];
if (l) first -= pf[l - 1];
if (!first) return 0;
long long ans = cnt[r - l + 1] - 1;
long long v = (r - l + 1) - first;
return MOD(ans - hlp[v]);
}
void smain() {
cnt[0] = 1;
for (long long i = 1; i < N; ++i) cnt[i] = (cnt[i - 1] * 2) % M;
for (long long i = 2; i < N; ++i) hlp[i] = MOD(hlp[i - 1] + cnt[i - 1]);
long long n, q;
cin >> n >> q;
string s;
cin >> s;
pf.resize(n);
for (long long i = 0; i < n; ++i) pf[i] = s[i] - '0';
for (long long i = 1; i < n; ++i) pf[i] += pf[i - 1];
for (long long i = 0; i < q; ++i) {
long long l, r;
cin >> l >> r;
cout << quer(l, r) << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, q, p[100005];
string s;
long long expo(long long base, long long exponent, long long mod) {
long long ans = 1;
while (exponent != 0) {
if (exponent & 1) ans = (1LL * ans * base) % mod;
base = (1LL * base * base) % mod;
exponent >>= 1;
}
return ans % mod;
}
void solve() {
cin >> n >> q;
cin >> s;
p[0] = s[0] == '1';
for (long long i = 1; i < n; i++) {
p[i] = (s[i] == '1') + p[i - 1];
}
while (q--) {
long long l, r;
cin >> l >> r;
l--, r--;
long long first = p[r] - (l ? p[l - 1] : 0);
long long second = r - l + 1 - first;
long long ans = (expo(2, first, 1000000007) - 1 + 1000000007) % 1000000007;
ans = (ans * expo(2, second, 1000000007)) % 1000000007;
cout << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, q, p[100005];
string s;
long long expo(long long base, long long exponent, long long mod) {
long long ans = 1;
while (exponent != 0) {
if (exponent & 1) ans = (1LL * ans * base) % mod;
base = (1LL * base * base) % mod;
exponent >>= 1;
}
return ans % mod;
}
void solve() {
cin >> n >> q;
cin >> s;
p[0] = s[0] == '1';
for (long long i = 1; i < n; i++) {
p[i] = (s[i] == '1') + p[i - 1];
}
while (q--) {
long long l, r;
cin >> l >> r;
l--, r--;
long long first = p[r] - (l ? p[l - 1] : 0);
long long second = r - l + 1 - first;
long long ans = (expo(2, first, 1000000007) - 1 + 1000000007) % 1000000007;
ans = (ans * expo(2, second, 1000000007)) % 1000000007;
cout << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int dcmp(long double n, long double y) {
return fabs(n - y) <= 1e-9 ? 0 : n < y ? -1 : 1;
}
const int MAX = 1e5 + 10;
const long long MOD = 1e9 + 7;
long long prefix[MAX][2], sum_of_pow[MAX], n, q;
string in;
long long POW_M(long long a, long long p, long long m = MOD) {
if (p == 0) return 1;
if (p == 1) return a % m;
long long x = POW_M(a, p / 2, m);
if (p % 2 == 0) return ((x % m) * x) % m;
return (((x % m) * x % m) * a) % m;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> n >> q;
sum_of_pow[0] = 1;
cin >> in;
for (long long i = 1; i < MAX; i++) {
sum_of_pow[i] = (2LL * sum_of_pow[i - 1]) % MOD;
}
for (int i = 0; i < (int)(MAX - 2); ++i) {
sum_of_pow[i + 1] = (sum_of_pow[i + 1] % MOD + sum_of_pow[i] % MOD) % MOD;
}
for (int i = 0; i < ((int)((in).size())); ++i) {
prefix[i + 1][0] += (in[i] == '0') + prefix[i][0];
prefix[i + 1][1] += (in[i] == '1') + prefix[i][1];
}
for (int i = 0; i < (int)(q); ++i) {
int l, r;
cin >> l >> r;
long long z = prefix[r][0] - prefix[l - 1][0];
long long o = prefix[r][1] - prefix[l - 1][1];
long long ans = 0;
if (o > 0) {
ans += sum_of_pow[o - 1];
}
if (z > 0) {
ans +=
(o > 0 ? (sum_of_pow[z - 1] * (sum_of_pow[o - 1] % MOD)) % MOD : 0LL);
ans %= MOD;
}
cout << ans << '\n';
}
}
| ### Prompt
Please formulate a cpp solution to the following problem:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int dcmp(long double n, long double y) {
return fabs(n - y) <= 1e-9 ? 0 : n < y ? -1 : 1;
}
const int MAX = 1e5 + 10;
const long long MOD = 1e9 + 7;
long long prefix[MAX][2], sum_of_pow[MAX], n, q;
string in;
long long POW_M(long long a, long long p, long long m = MOD) {
if (p == 0) return 1;
if (p == 1) return a % m;
long long x = POW_M(a, p / 2, m);
if (p % 2 == 0) return ((x % m) * x) % m;
return (((x % m) * x % m) * a) % m;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> n >> q;
sum_of_pow[0] = 1;
cin >> in;
for (long long i = 1; i < MAX; i++) {
sum_of_pow[i] = (2LL * sum_of_pow[i - 1]) % MOD;
}
for (int i = 0; i < (int)(MAX - 2); ++i) {
sum_of_pow[i + 1] = (sum_of_pow[i + 1] % MOD + sum_of_pow[i] % MOD) % MOD;
}
for (int i = 0; i < ((int)((in).size())); ++i) {
prefix[i + 1][0] += (in[i] == '0') + prefix[i][0];
prefix[i + 1][1] += (in[i] == '1') + prefix[i][1];
}
for (int i = 0; i < (int)(q); ++i) {
int l, r;
cin >> l >> r;
long long z = prefix[r][0] - prefix[l - 1][0];
long long o = prefix[r][1] - prefix[l - 1][1];
long long ans = 0;
if (o > 0) {
ans += sum_of_pow[o - 1];
}
if (z > 0) {
ans +=
(o > 0 ? (sum_of_pow[z - 1] * (sum_of_pow[o - 1] % MOD)) % MOD : 0LL);
ans %= MOD;
}
cout << ans << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
long long a[maxn];
long long quick(long long a, long long n) {
long long ans = 1;
while (n != 0) {
if (n % 2 == 0)
a = a % mod * a % mod, n = n / 2;
else
ans = a % mod * ans % mod, n--;
}
return ans;
}
int32_t main() {
long long n, q;
cin >> n;
cin >> q;
string ss;
cin >> ss;
for (long long i = 1; i <= n; i++) {
if (ss[i - 1] == '1')
a[i] = a[i - 1] + 1;
else
a[i] = a[i - 1];
}
while (q--) {
long long l, r;
cin >> l >> r;
long long d = r - l + 1;
long long x = a[r] - a[l - 1];
if (x == 0) {
cout << 0 << endl;
continue;
}
long long num = quick(2, x) - 1;
num += (quick(2, x) - 1) % mod * (quick(2, d - x) - 1) % mod;
cout << num % mod << endl;
}
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
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.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
long long a[maxn];
long long quick(long long a, long long n) {
long long ans = 1;
while (n != 0) {
if (n % 2 == 0)
a = a % mod * a % mod, n = n / 2;
else
ans = a % mod * ans % mod, n--;
}
return ans;
}
int32_t main() {
long long n, q;
cin >> n;
cin >> q;
string ss;
cin >> ss;
for (long long i = 1; i <= n; i++) {
if (ss[i - 1] == '1')
a[i] = a[i - 1] + 1;
else
a[i] = a[i - 1];
}
while (q--) {
long long l, r;
cin >> l >> r;
long long d = r - l + 1;
long long x = a[r] - a[l - 1];
if (x == 0) {
cout << 0 << endl;
continue;
}
long long num = quick(2, x) - 1;
num += (quick(2, x) - 1) % mod * (quick(2, d - x) - 1) % mod;
cout << num % mod << endl;
}
}
``` |
#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;
}
}
| ### Prompt
Please create a solution in CPP to the following problem:
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.
### Response
```cpp
#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;
}
}
``` |
#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;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
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.
### Response
```cpp
#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;
}
``` |
#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;
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
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.
### Response
```cpp
#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;
}
}
``` |
#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;
}
| ### Prompt
Generate a Cpp solution to the following problem:
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.
### Response
```cpp
#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;
}
``` |
#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;
}
}
| ### Prompt
Create a solution in CPP for the following problem:
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.
### Response
```cpp
#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;
}
}
``` |
#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;
}
| ### Prompt
Create a solution in CPP for the following problem:
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.
### Response
```cpp
#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;
}
``` |
#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";
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
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.
### Response
```cpp
#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";
}
}
``` |
#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;
}
}
| ### Prompt
Please formulate a CPP solution to the following problem:
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.
### Response
```cpp
#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;
}
}
``` |
#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;
}
| ### Prompt
Create a solution in Cpp for the following problem:
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.
### Response
```cpp
#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;
}
``` |
#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;
}
| ### Prompt
Generate a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
}
``` |
#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;
}
| ### Prompt
Please create a solution in cpp to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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]);
}
| ### Prompt
Create a solution in CPP for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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]);
}
``` |
#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;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
| ### Prompt
Generate a cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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);
}
}
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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);
}
}
}
``` |
#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;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
| ### Prompt
Please create a solution in CPP to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
| ### Prompt
Create a solution in Cpp for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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";
}
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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";
}
}
}
``` |
#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;
}
}
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
}
}
``` |
#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;
}
}
}
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
}
}
}
``` |
#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);
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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);
}
``` |
#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;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
}
}
``` |
#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);
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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);
}
}
``` |
#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;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
``` |
#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;
}
}
}
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
}
}
}
``` |
#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;
}
}
}
}
| ### Prompt
Create a solution in cpp for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#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;
}
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 100000000000000000;
const long long MOD = 1000000007;
const long long MAXN = 1005;
long long dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
long long dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
vector<long long> v(MAXN);
long long n, s;
bool f(long long x) {
long long sum = 0;
for (int i = 0; i < n; ++i) {
sum += (v[i] - x);
}
return (sum >= s);
}
void solve() {
cin >> n >> s;
long long lo = 0, hi = INF;
for (int i = 0; i < n; ++i) {
cin >> v[i];
hi = min(hi, v[i]);
}
long long ans = -1;
while (lo <= hi) {
long long mid = lo + hi >> 1;
if (f(mid)) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long t = 1;
while (t--) solve();
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 100000000000000000;
const long long MOD = 1000000007;
const long long MAXN = 1005;
long long dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
long long dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
vector<long long> v(MAXN);
long long n, s;
bool f(long long x) {
long long sum = 0;
for (int i = 0; i < n; ++i) {
sum += (v[i] - x);
}
return (sum >= s);
}
void solve() {
cin >> n >> s;
long long lo = 0, hi = INF;
for (int i = 0; i < n; ++i) {
cin >> v[i];
hi = min(hi, v[i]);
}
long long ans = -1;
while (lo <= hi) {
long long mid = lo + hi >> 1;
if (f(mid)) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long t = 1;
while (t--) solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long v[2000];
int main() {
long long n, s, tot = 0;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
cin >> v[i];
tot += v[i];
}
if (tot <= s) {
if (tot < s)
cout << "-1\n";
else
cout << "0\n";
return 0;
}
sort(v, v + n);
long long carry = 0;
for (int i = 1; i < n; ++i) carry += v[i] - v[0];
if (carry >= s) {
cout << v[0] << endl;
return 0;
}
long long extra = s - carry;
long long mm = extra % n, div = extra / n;
cout << (v[0] - div - (mm > 0)) << endl;
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long v[2000];
int main() {
long long n, s, tot = 0;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
cin >> v[i];
tot += v[i];
}
if (tot <= s) {
if (tot < s)
cout << "-1\n";
else
cout << "0\n";
return 0;
}
sort(v, v + n);
long long carry = 0;
for (int i = 1; i < n; ++i) carry += v[i] - v[0];
if (carry >= s) {
cout << v[0] << endl;
return 0;
}
long long extra = s - carry;
long long mm = extra % n, div = extra / n;
cout << (v[0] - div - (mm > 0)) << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, i, a, s, t, m = 2e9;
int main() {
for (cin >> n >> s; i < n; i++) cin >> a, t += a, m = min(m, a);
if (t < s) return cout << -1, 0;
if (t - m * n < s) return cout << (t - s) / n, 0;
cout << m;
}
| ### Prompt
Create a solution in cpp for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, i, a, s, t, m = 2e9;
int main() {
for (cin >> n >> s; i < n; i++) cin >> a, t += a, m = min(m, a);
if (t < s) return cout << -1, 0;
if (t - m * n < s) return cout << (t - s) / n, 0;
cout << m;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
if (a.first == b.first) return (a.second < b.second);
return (a.first < b.first);
}
vector<long long int> prime(1000005, 1);
void p() {
long long int i, j;
for (i = 2; i <= 1000000; i++) {
if (prime[i] == 1) {
for (j = 2 * i; j <= 1000000; j += i) prime[j] = 0;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, s, i;
cin >> n >> s;
vector<long long int> a(n);
for (i = 0; i < (n); i++) cin >> a[i];
sort(a.begin(), a.end());
long long int sum = 0;
for (i = 0; i < (n); i++) sum += a[i];
if (sum < s)
cout << -1 << "\n";
else {
long long int ans = 0;
long long int left = 0, mid;
long long int right = a[0];
while (left <= right) {
mid = left + (right - left) / 2;
if (sum - mid * n >= s) {
ans = mid;
left = mid + 1;
} else
right = mid - 1;
}
cout << ans << "\n";
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
if (a.first == b.first) return (a.second < b.second);
return (a.first < b.first);
}
vector<long long int> prime(1000005, 1);
void p() {
long long int i, j;
for (i = 2; i <= 1000000; i++) {
if (prime[i] == 1) {
for (j = 2 * i; j <= 1000000; j += i) prime[j] = 0;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, s, i;
cin >> n >> s;
vector<long long int> a(n);
for (i = 0; i < (n); i++) cin >> a[i];
sort(a.begin(), a.end());
long long int sum = 0;
for (i = 0; i < (n); i++) sum += a[i];
if (sum < s)
cout << -1 << "\n";
else {
long long int ans = 0;
long long int left = 0, mid;
long long int right = a[0];
while (left <= right) {
mid = left + (right - left) / 2;
if (sum - mid * n >= s) {
ans = mid;
left = mid + 1;
} else
right = mid - 1;
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long a[10001], minn = 0x7fffffff, sum = 0, n, s;
long long fmin(long long k1, long long k2) {
if (k1 < k2)
return k1;
else
return k2;
}
int main() {
scanf("%lld%lld", &n, &s);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
minn = fmin(minn, a[i]);
sum += a[i];
}
sum -= s;
if (sum < 0) {
printf("-1\n");
return 0;
}
if (minn * n >= sum)
printf("%lld\n", sum / n);
else
printf("%lld\n", minn);
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long a[10001], minn = 0x7fffffff, sum = 0, n, s;
long long fmin(long long k1, long long k2) {
if (k1 < k2)
return k1;
else
return k2;
}
int main() {
scanf("%lld%lld", &n, &s);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
minn = fmin(minn, a[i]);
sum += a[i];
}
sum -= s;
if (sum < 0) {
printf("-1\n");
return 0;
}
if (minn * n >= sum)
printf("%lld\n", sum / n);
else
printf("%lld\n", minn);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long a[1050];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
long long s = 0;
for (int i = 0; i < (n); i++) {
cin >> a[i];
s += a[i];
}
if (s < m) {
cout << -1;
return 0;
}
sort(a, a + n);
reverse(a, a + n);
long long ans = a[n - 1], cur = 0;
for (int i = 0; i < (n); i++) {
cur += a[i] - ans;
if (cur >= m) break;
}
if (cur < m) {
long long z = m - cur;
if (z % n != 0)
z = z / n + 1;
else
z /= n;
ans -= z;
}
cout << ans;
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long a[1050];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
long long s = 0;
for (int i = 0; i < (n); i++) {
cin >> a[i];
s += a[i];
}
if (s < m) {
cout << -1;
return 0;
}
sort(a, a + n);
reverse(a, a + n);
long long ans = a[n - 1], cur = 0;
for (int i = 0; i < (n); i++) {
cur += a[i] - ans;
if (cur >= m) break;
}
if (cur < m) {
long long z = m - cur;
if (z % n != 0)
z = z / n + 1;
else
z /= n;
ans -= z;
}
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1100;
long long arr[maxn];
int n;
long long s;
int check(long long ck) {
long long cur_sum = 0;
for (int i = 1; i <= n; i++) {
if (arr[i] < ck) return 0;
cur_sum += (arr[i] - ck);
}
return cur_sum >= s;
}
int main() {
long long sum = 0;
scanf("%d%I64d", &n, &s);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &arr[i]);
sum += arr[i];
}
long long l = 0, r = sum, m;
long long ret = -1;
while (l <= r) {
m = (l + r) >> 1;
if (check(m)) {
l = m + 1;
ret = m;
} else {
r = m - 1;
}
}
printf("%I64d\n", r);
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1100;
long long arr[maxn];
int n;
long long s;
int check(long long ck) {
long long cur_sum = 0;
for (int i = 1; i <= n; i++) {
if (arr[i] < ck) return 0;
cur_sum += (arr[i] - ck);
}
return cur_sum >= s;
}
int main() {
long long sum = 0;
scanf("%d%I64d", &n, &s);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &arr[i]);
sum += arr[i];
}
long long l = 0, r = sum, m;
long long ret = -1;
while (l <= r) {
m = (l + r) >> 1;
if (check(m)) {
l = m + 1;
ret = m;
} else {
r = m - 1;
}
}
printf("%I64d\n", r);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void in() { return; }
template <typename T, typename... Types>
void in(T &a, Types &...b) {
cin >> (a);
in(b...);
}
void o() { return; }
template <typename T, typename... Types>
void o(T a, Types... b) {
cout << (a);
cout << ' ';
o(b...);
}
bool sortin(const pair<long long int, long long int> &e,
const pair<long long int, long long int> &f) {
return (e.first < f.first);
}
bool POT(long long int x) { return x && (!(x & (x - 1))); }
long long int i, j, k, l, m, n, p, q, r, a, b, c, x, y, z, ts, mn = 1e18,
mod = 1e9 + 7;
long long int ar[250005], br[250005], xr[250005], tem[250005];
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
in(n, m);
for (long long int i = 0; i <= n - 1; i++) {
in(ar[i]);
c += ar[i];
}
if (c < m) {
o(-1);
return 0;
}
sort(ar, ar + n);
x = c - ar[0] * n;
if (x >= m) {
o(ar[0]);
return 0;
}
p = (m - x + n - 1) / n;
o(ar[0] - p);
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void in() { return; }
template <typename T, typename... Types>
void in(T &a, Types &...b) {
cin >> (a);
in(b...);
}
void o() { return; }
template <typename T, typename... Types>
void o(T a, Types... b) {
cout << (a);
cout << ' ';
o(b...);
}
bool sortin(const pair<long long int, long long int> &e,
const pair<long long int, long long int> &f) {
return (e.first < f.first);
}
bool POT(long long int x) { return x && (!(x & (x - 1))); }
long long int i, j, k, l, m, n, p, q, r, a, b, c, x, y, z, ts, mn = 1e18,
mod = 1e9 + 7;
long long int ar[250005], br[250005], xr[250005], tem[250005];
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
in(n, m);
for (long long int i = 0; i <= n - 1; i++) {
in(ar[i]);
c += ar[i];
}
if (c < m) {
o(-1);
return 0;
}
sort(ar, ar + n);
x = c - ar[0] * n;
if (x >= m) {
o(ar[0]);
return 0;
}
p = (m - x + n - 1) / n;
o(ar[0] - p);
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long cont[1005];
int main() {
ios::sync_with_stdio(0);
long long n, m;
cin >> n >> m;
long long sum = 0;
long long Min = 1e18;
for (int i = 0; i < n; i++) {
cin >> cont[i];
sum += cont[i];
Min = min(Min, cont[i]);
}
if (sum < m) {
cout << -1 << endl;
exit(0);
}
int ans = 0;
for (int i = 1 << 30; i; i >>= 1) {
int test = ans + i;
long long s = 0;
for (int j = 0; j < n; j++) {
s += cont[j] - test;
}
if (s >= m && test <= Min) {
ans = test;
}
}
cout << ans << endl;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long cont[1005];
int main() {
ios::sync_with_stdio(0);
long long n, m;
cin >> n >> m;
long long sum = 0;
long long Min = 1e18;
for (int i = 0; i < n; i++) {
cin >> cont[i];
sum += cont[i];
Min = min(Min, cont[i]);
}
if (sum < m) {
cout << -1 << endl;
exit(0);
}
int ans = 0;
for (int i = 1 << 30; i; i >>= 1) {
int test = ans + i;
long long s = 0;
for (int j = 0; j < n; j++) {
s += cont[j] - test;
}
if (s >= m && test <= Min) {
ans = test;
}
}
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
long long i, j, k, t, mn = 1e13, x, sum = 0, temp, ans, s, n, baki;
cin >> n >> s;
vector<long long> vec;
for (int i = 0; i < n; i++) {
cin >> x;
sum += x;
vec.push_back(x);
mn = min(mn, x);
}
if (sum < s) {
cout << -1;
return 0;
} else {
k = 0;
for (int i = 0; i < n; i++) {
temp = vec[i] - mn;
k += temp;
}
if (k >= s) {
cout << mn;
return 0;
} else {
baki = s - k;
j = (baki + n - 1) / n;
ans = mn - j;
cout << ans;
}
}
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
long long i, j, k, t, mn = 1e13, x, sum = 0, temp, ans, s, n, baki;
cin >> n >> s;
vector<long long> vec;
for (int i = 0; i < n; i++) {
cin >> x;
sum += x;
vec.push_back(x);
mn = min(mn, x);
}
if (sum < s) {
cout << -1;
return 0;
} else {
k = 0;
for (int i = 0; i < n; i++) {
temp = vec[i] - mn;
k += temp;
}
if (k >= s) {
cout << mn;
return 0;
} else {
baki = s - k;
j = (baki + n - 1) / n;
ans = mn - j;
cout << ans;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
long long int s, sum = 0, mn = 1e9 + 9;
vector<long long int> arr(1e3 + 3);
int main(void) {
ios ::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
sum += arr[i];
mn = min(mn, arr[i]);
}
if (sum < s) {
cout << -1 << "\n";
return 0;
}
sum = 0;
for (int i = 1; i <= n; ++i) {
sum += arr[i] - mn;
}
if (sum >= s) {
cout << mn << "\n";
return 0;
}
s -= sum;
mn -= s / n;
if (s % n != 0) {
--mn;
}
cout << mn << "\n";
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
long long int s, sum = 0, mn = 1e9 + 9;
vector<long long int> arr(1e3 + 3);
int main(void) {
ios ::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
sum += arr[i];
mn = min(mn, arr[i]);
}
if (sum < s) {
cout << -1 << "\n";
return 0;
}
sum = 0;
for (int i = 1; i <= n; ++i) {
sum += arr[i] - mn;
}
if (sum >= s) {
cout << mn << "\n";
return 0;
}
s -= sum;
mn -= s / n;
if (s % n != 0) {
--mn;
}
cout << mn << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, k;
cin >> n >> k;
int a[n];
long long s = 0;
for (int i = 0; i < (n); i++) {
cin >> a[i];
s += a[i];
}
if (k > s) {
cout << "-1";
} else {
long long min_ = *min_element(a, a + n);
long long exc = 0;
for (auto &i : a) exc += (i - min_);
if (exc >= k) {
cout << min_;
} else {
k -= exc;
while (k > 0) {
min_--;
k -= n;
}
cout << min_;
}
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, k;
cin >> n >> k;
int a[n];
long long s = 0;
for (int i = 0; i < (n); i++) {
cin >> a[i];
s += a[i];
}
if (k > s) {
cout << "-1";
} else {
long long min_ = *min_element(a, a + n);
long long exc = 0;
for (auto &i : a) exc += (i - min_);
if (exc >= k) {
cout << min_;
} else {
k -= exc;
while (k > 0) {
min_--;
k -= n;
}
cout << min_;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
map<pair<pair<long long int, long long int>,
pair<long long int, long long int>>,
long long int>
pqr;
map<pair<long long int, long long int>, long long int> xyz;
map<long long int, long long int, greater<long long int>> yz;
vector<pair<long long int, string>> a1;
vector<pair<long long int, long long int>> a2;
vector<pair<long long int, pair<long long int, long long int>>> a3;
bool isSubSequence(string str1, string str2, long long int m, long long int n) {
if (m == 0) return true;
if (n == 0) return false;
if (str1[m - 1] == str2[n - 1])
return isSubSequence(str1, str2, m - 1, n - 1);
return isSubSequence(str1, str2, m, n - 1);
}
long long int fac[5005];
void output2(long long int t) {
if (t > 2) {
cout << "3"
<< " "
<< "1"
<< "\n";
cout << "3"
<< " "
<< "2"
<< "\n";
for (long long int i = 2; i < t - 1; i++) {
cout << "3"
<< " " << i + 2 << "\n";
}
} else {
for (long long int i = 0; i < t - 1; i++) {
cout << "1"
<< " " << i + 2 << "\n";
}
}
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int modInverse(long long int n, long long int p) {
return power(n, p - 2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p,
long long int fac[]) {
if (r == 0) return 1;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
p;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, s;
cin >> n >> s;
long long int arr[n + 2];
long long int sum = 0;
map<long long int, long long int> freq;
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
freq[arr[i]]++;
sum = sum + arr[i];
}
if (sum < s) {
cout << "-1\n";
return 0;
}
sort(arr, arr + n);
long long int r = arr[0];
long long int d = n - freq[r];
long long int h = sum - freq[r] * r;
long long int g = h - d * r;
if (g >= s) {
cout << r << "\n";
return 0;
}
long long int w = n * r;
long long int q = sum - w;
s = s - q;
long long int ans = w - s;
cout << ans / n;
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
map<pair<pair<long long int, long long int>,
pair<long long int, long long int>>,
long long int>
pqr;
map<pair<long long int, long long int>, long long int> xyz;
map<long long int, long long int, greater<long long int>> yz;
vector<pair<long long int, string>> a1;
vector<pair<long long int, long long int>> a2;
vector<pair<long long int, pair<long long int, long long int>>> a3;
bool isSubSequence(string str1, string str2, long long int m, long long int n) {
if (m == 0) return true;
if (n == 0) return false;
if (str1[m - 1] == str2[n - 1])
return isSubSequence(str1, str2, m - 1, n - 1);
return isSubSequence(str1, str2, m, n - 1);
}
long long int fac[5005];
void output2(long long int t) {
if (t > 2) {
cout << "3"
<< " "
<< "1"
<< "\n";
cout << "3"
<< " "
<< "2"
<< "\n";
for (long long int i = 2; i < t - 1; i++) {
cout << "3"
<< " " << i + 2 << "\n";
}
} else {
for (long long int i = 0; i < t - 1; i++) {
cout << "1"
<< " " << i + 2 << "\n";
}
}
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int modInverse(long long int n, long long int p) {
return power(n, p - 2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p,
long long int fac[]) {
if (r == 0) return 1;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
p;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, s;
cin >> n >> s;
long long int arr[n + 2];
long long int sum = 0;
map<long long int, long long int> freq;
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
freq[arr[i]]++;
sum = sum + arr[i];
}
if (sum < s) {
cout << "-1\n";
return 0;
}
sort(arr, arr + n);
long long int r = arr[0];
long long int d = n - freq[r];
long long int h = sum - freq[r] * r;
long long int g = h - d * r;
if (g >= s) {
cout << r << "\n";
return 0;
}
long long int w = n * r;
long long int q = sum - w;
s = s - q;
long long int ans = w - s;
cout << ans / n;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, mn = 2e9, sum = 0;
cin >> n >> s;
long long a[1100];
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
mn = min(mn, a[i]);
}
if (sum < s) return cout << -1, 0;
if (sum == s) return cout << 0, 0;
if (sum - mn * n < s) return cout << (sum - s) / n, 0;
cout << mn;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, mn = 2e9, sum = 0;
cin >> n >> s;
long long a[1100];
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
mn = min(mn, a[i]);
}
if (sum < s) return cout << -1, 0;
if (sum == s) return cout << 0, 0;
if (sum - mn * n < s) return cout << (sum - s) / n, 0;
cout << mn;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n, s;
cin >> n >> s;
vector<long long> v(n);
long long sum = 0;
long long mn = 1e9 + 7;
for (int i = 0; i < n; ++i) {
cin >> v[i];
sum += v[i];
mn = min(mn, v[i]);
}
if (sum < s) {
cout << -1 << '\n';
return 0;
}
sort(v.rbegin(), v.rend());
for (int i = 0; i < n; ++i) {
long long p = min(s, v[i] - mn);
v[i] -= min(s, v[i] - mn);
s -= p;
}
cout << mn - s / n - !!(s % n) << '\n';
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n, s;
cin >> n >> s;
vector<long long> v(n);
long long sum = 0;
long long mn = 1e9 + 7;
for (int i = 0; i < n; ++i) {
cin >> v[i];
sum += v[i];
mn = min(mn, v[i]);
}
if (sum < s) {
cout << -1 << '\n';
return 0;
}
sort(v.rbegin(), v.rend());
for (int i = 0; i < n; ++i) {
long long p = min(s, v[i] - mn);
v[i] -= min(s, v[i] - mn);
s -= p;
}
cout << mn - s / n - !!(s % n) << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int minimum(int a, int b) {
if (a > b)
return b;
else
return a;
}
int main() {
int n;
long long int s;
cin >> n >> s;
int v[n];
int min = 1000000000;
long long int total = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
total = total + v[i];
if (v[i] < min) min = v[i];
}
if (s > total) {
cout << -1 << endl;
return 0;
}
sort(v, v + n);
for (int i = n - 1; i >= 0; i--) {
if (s > v[i] - min) {
s = s - (v[i] - min);
v[i] = min;
} else {
cout << min << endl;
return 0;
}
}
if (s > 0) {
int x = min - s / n;
int rem = s % n;
if (rem == 0)
cout << x;
else
cout << x - 1 << endl;
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int minimum(int a, int b) {
if (a > b)
return b;
else
return a;
}
int main() {
int n;
long long int s;
cin >> n >> s;
int v[n];
int min = 1000000000;
long long int total = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
total = total + v[i];
if (v[i] < min) min = v[i];
}
if (s > total) {
cout << -1 << endl;
return 0;
}
sort(v, v + n);
for (int i = n - 1; i >= 0; i--) {
if (s > v[i] - min) {
s = s - (v[i] - min);
v[i] = min;
} else {
cout << min << endl;
return 0;
}
}
if (s > 0) {
int x = min - s / n;
int rem = s % n;
if (rem == 0)
cout << x;
else
cout << x - 1 << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long binarySearch(long long arr[], long long l, long long r, long long x) {
if (r >= l) {
long long mid = l + (r - l) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
if (arr[mid] < x) return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
long long fact(long long n) {
if (n == 0 || n == 1)
return 1;
else
return n * fact(n - 1);
}
long long ncr(long long n, long long r) {
if (r < 0 || r > n) return 0;
if (r == 0)
return 1;
else
return (n * ncr(n - 1, r - 1)) / r;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
bool isPowerOfTwo(long long n) {
if (n == 0) return false;
return (ceil(log2(n)) == floor(log2(n)));
}
long long mostFrequent(long long arr[], long long n) {
unordered_map<long long, long long> hash;
for (long long i = 0; i < n; i++) hash[arr[i]]++;
long long max_count = 0, res = -1;
for (auto i : hash) {
if (max_count < i.second) {
res = i.first;
max_count = i.second;
}
}
return max_count;
}
long long countSetBits(long long n) {
long long count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}
long long factors(long long n) {
long long a = 0, z = 1;
while (n % 2 == 0) {
a++;
n >>= 1;
}
z *= a + 1;
for (long long i = 3; i <= sqrt(n); i += 2) {
a = 0;
while (n % i == 0) {
a++;
n = n / i;
}
z *= a + 1;
}
if (n > 2) z *= 2;
return z - 2;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
std::cout << std::fixed;
std::cout << std::setprecision(9);
long long testcases = 1;
while (testcases--) {
long long n, s, k, a = 0, p = 1000000007;
cin >> n >> s;
for (long long i = 0; i < n; i++) {
cin >> k;
p = ((k > p) ? p : k);
a += k;
}
if (a < s)
cout << -1;
else
cout << ((p > (a - s) / n) ? (a - s) / n : p);
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long binarySearch(long long arr[], long long l, long long r, long long x) {
if (r >= l) {
long long mid = l + (r - l) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
if (arr[mid] < x) return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
long long fact(long long n) {
if (n == 0 || n == 1)
return 1;
else
return n * fact(n - 1);
}
long long ncr(long long n, long long r) {
if (r < 0 || r > n) return 0;
if (r == 0)
return 1;
else
return (n * ncr(n - 1, r - 1)) / r;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
bool isPowerOfTwo(long long n) {
if (n == 0) return false;
return (ceil(log2(n)) == floor(log2(n)));
}
long long mostFrequent(long long arr[], long long n) {
unordered_map<long long, long long> hash;
for (long long i = 0; i < n; i++) hash[arr[i]]++;
long long max_count = 0, res = -1;
for (auto i : hash) {
if (max_count < i.second) {
res = i.first;
max_count = i.second;
}
}
return max_count;
}
long long countSetBits(long long n) {
long long count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}
long long factors(long long n) {
long long a = 0, z = 1;
while (n % 2 == 0) {
a++;
n >>= 1;
}
z *= a + 1;
for (long long i = 3; i <= sqrt(n); i += 2) {
a = 0;
while (n % i == 0) {
a++;
n = n / i;
}
z *= a + 1;
}
if (n > 2) z *= 2;
return z - 2;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
std::cout << std::fixed;
std::cout << std::setprecision(9);
long long testcases = 1;
while (testcases--) {
long long n, s, k, a = 0, p = 1000000007;
cin >> n >> s;
for (long long i = 0; i < n; i++) {
cin >> k;
p = ((k > p) ? p : k);
a += k;
}
if (a < s)
cout << -1;
else
cout << ((p > (a - s) / n) ? (a - s) / n : p);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e18;
const long long N = 2e5 + 5;
long long a[N];
int main() {
long long i, j, k, l;
long long n, m, t;
cin >> n >> k;
long long minx = MOD, ans = 0;
for (i = 0; i < n; i++) scanf("%lld", &a[i]), minx = min(minx, a[i]);
for (i = 0; i < n; i++) ans += a[i] - minx;
if (k <= ans) return cout << minx << endl, 0;
k -= ans;
t = 0;
m = k / n;
t = k % n;
if (k > n * minx) return cout << -1 << endl, 0;
minx -= m;
if (t) minx--;
cout << minx << endl;
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e18;
const long long N = 2e5 + 5;
long long a[N];
int main() {
long long i, j, k, l;
long long n, m, t;
cin >> n >> k;
long long minx = MOD, ans = 0;
for (i = 0; i < n; i++) scanf("%lld", &a[i]), minx = min(minx, a[i]);
for (i = 0; i < n; i++) ans += a[i] - minx;
if (k <= ans) return cout << minx << endl, 0;
k -= ans;
t = 0;
m = k / n;
t = k % n;
if (k > n * minx) return cout << -1 << endl, 0;
minx -= m;
if (t) minx--;
cout << minx << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long s, sum = 0;
cin >> n >> s;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s) {
cout << -1;
return 0;
}
long long c = 0;
sort(a, a + n);
if (n == 1) {
cout << a[0] - s;
return 0;
}
for (int i = 1; i < n; i++) {
c += a[i] - a[0];
if (c >= s) {
cout << a[0];
return 0;
}
a[i] = a[0];
}
int i = 1, b;
if ((s - c) % n == 0)
b = (s - c) / n;
else
b = ((s - c) / n) + 1;
if (a[0] - b < 0)
cout << -1;
else
cout << a[0] - b;
}
| ### Prompt
In cpp, your task is to solve the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long s, sum = 0;
cin >> n >> s;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s) {
cout << -1;
return 0;
}
long long c = 0;
sort(a, a + n);
if (n == 1) {
cout << a[0] - s;
return 0;
}
for (int i = 1; i < n; i++) {
c += a[i] - a[0];
if (c >= s) {
cout << a[0];
return 0;
}
a[i] = a[0];
}
int i = 1, b;
if ((s - c) % n == 0)
b = (s - c) / n;
else
b = ((s - c) / n) + 1;
if (a[0] - b < 0)
cout << -1;
else
cout << a[0] - b;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, vector<T>&& object) {
for (auto& i : object) {
os << i;
}
return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<vector<T>>&& object) {
for (auto& i : object) {
os << i << endl;
}
return os;
}
const size_t MAX_N = (long long)2e5 + 10;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, s, sum = 0;
cin >> n >> s;
vector<long long> a(n);
for (auto& i : a) {
cin >> i;
sum += i;
}
if (sum < s) {
cout << "-1\n";
return 0;
}
sort(a.begin(), a.end(), less<long long>());
long long cmin = a[0];
long long accv = 0;
for (auto& i : a) {
accv += i - cmin;
i = cmin;
}
if (accv >= s) {
cout << cmin << "\n";
return 0;
} else {
cout << max(0LL, cmin - (long long)ceil(((long double)s - accv) / n))
<< endl;
}
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, vector<T>&& object) {
for (auto& i : object) {
os << i;
}
return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<vector<T>>&& object) {
for (auto& i : object) {
os << i << endl;
}
return os;
}
const size_t MAX_N = (long long)2e5 + 10;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, s, sum = 0;
cin >> n >> s;
vector<long long> a(n);
for (auto& i : a) {
cin >> i;
sum += i;
}
if (sum < s) {
cout << "-1\n";
return 0;
}
sort(a.begin(), a.end(), less<long long>());
long long cmin = a[0];
long long accv = 0;
for (auto& i : a) {
accv += i - cmin;
i = cmin;
}
if (accv >= s) {
cout << cmin << "\n";
return 0;
} else {
cout << max(0LL, cmin - (long long)ceil(((long double)s - accv) / n))
<< endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, s, a[1010], sum, mim = 0x3f3f3f3f, l, r, mid, ans = 0;
int can(long long x) { return (sum - n * x >= s ? 1 : 0); }
int main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
mim = min(mim, a[i]);
}
if (sum < s) {
cout << -1;
return 0;
}
l = 0;
r = mim;
while (l <= r) {
mid = (l + r) / 2;
if (can(mid)) {
l = mid + 1;
ans = max(ans, mid);
} else
r = mid - 1;
}
cout << ans << endl;
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, s, a[1010], sum, mim = 0x3f3f3f3f, l, r, mid, ans = 0;
int can(long long x) { return (sum - n * x >= s ? 1 : 0); }
int main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
mim = min(mim, a[i]);
}
if (sum < s) {
cout << -1;
return 0;
}
l = 0;
r = mim;
while (l <= r) {
mid = (l + r) / 2;
if (can(mid)) {
l = mid + 1;
ans = max(ans, mid);
} else
r = mid - 1;
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, a[1005], s, mini, tot;
int main() {
cin >> n >> s;
tot = 0;
mini = 1e15;
for (int i = 1; i <= n; i++) {
cin >> a[i];
tot += a[i];
mini = min(a[i], mini);
}
if (tot < s) {
cout << -1;
return 0;
}
tot = tot - s;
mini = min(tot / n, mini);
cout << mini;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, a[1005], s, mini, tot;
int main() {
cin >> n >> s;
tot = 0;
mini = 1e15;
for (int i = 1; i <= n; i++) {
cin >> a[i];
tot += a[i];
mini = min(a[i], mini);
}
if (tot < s) {
cout << -1;
return 0;
}
tot = tot - s;
mini = min(tot / n, mini);
cout << mini;
}
``` |
#include <bits/stdc++.h>
const int N = 1e7 + 2 + 5, M = 1e3 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dxx[] = {-1, 1, -1, 1, -2, -2, 2, 2};
int dyy[] = {-2, -2, 2, 2, 1, -1, 1, -1};
using namespace std;
long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); }
long long lcm(long long x, long long y) { return ((x * y) / gcd(x, y)); }
void file() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
vector<long long> v;
int n;
bool ok(int md, long long s) {
long long ans = 0;
for (int i = 0; i < n; i++) {
s -= (v[i]);
s += md;
if (s <= 0) return 1;
}
return 0;
}
int main() {
file();
long long s;
cin >> n >> s;
v.resize(n);
long long sum = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
}
if (sum < s) return cout << -1, 0;
int st = 0, ed = 1e9, md, cur;
while (st <= ed) {
md = (st + ed) / 2;
if (ok(md, s)) {
cur = md;
st = md + 1;
} else
ed = md - 1;
}
int nw = *min_element(((v).begin()), ((v).end()));
cout << min(cur, nw) << '\n';
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 1e7 + 2 + 5, M = 1e3 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dxx[] = {-1, 1, -1, 1, -2, -2, 2, 2};
int dyy[] = {-2, -2, 2, 2, 1, -1, 1, -1};
using namespace std;
long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); }
long long lcm(long long x, long long y) { return ((x * y) / gcd(x, y)); }
void file() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
vector<long long> v;
int n;
bool ok(int md, long long s) {
long long ans = 0;
for (int i = 0; i < n; i++) {
s -= (v[i]);
s += md;
if (s <= 0) return 1;
}
return 0;
}
int main() {
file();
long long s;
cin >> n >> s;
v.resize(n);
long long sum = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
}
if (sum < s) return cout << -1, 0;
int st = 0, ed = 1e9, md, cur;
while (st <= ed) {
md = (st + ed) / 2;
if (ok(md, s)) {
cur = md;
st = md + 1;
} else
ed = md - 1;
}
int nw = *min_element(((v).begin()), ((v).end()));
cout << min(cur, nw) << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long SIZE = 100000;
const int INF = 0x3f3f3f3f;
const long long ll_INF = 0x3f3f3f3f3f3f3f3f;
const long double PI = acos(-1);
const long long MAXN = numeric_limits<long long>::max();
const long long MAX = 2000000;
void disp(vector<long long> v) {
for (auto u : v) cout << u << " ";
cout << '\n';
}
void solve() {
long long n, s, tot = 0, x;
cin >> n >> s;
vector<long long> v(n);
for (long long i = 0; i < n; i++) cin >> v[i];
sort((v).begin(), (v).end());
for (long long i = 1; i < n; i++) {
tot += v[i] - v[0];
}
if (tot >= s)
cout << v[0];
else {
s -= tot;
if (s % n == 0)
x = s / n;
else
x = s / n + 1;
if (x > v[0])
cout << -1;
else
cout << v[0] - x;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long SIZE = 100000;
const int INF = 0x3f3f3f3f;
const long long ll_INF = 0x3f3f3f3f3f3f3f3f;
const long double PI = acos(-1);
const long long MAXN = numeric_limits<long long>::max();
const long long MAX = 2000000;
void disp(vector<long long> v) {
for (auto u : v) cout << u << " ";
cout << '\n';
}
void solve() {
long long n, s, tot = 0, x;
cin >> n >> s;
vector<long long> v(n);
for (long long i = 0; i < n; i++) cin >> v[i];
sort((v).begin(), (v).end());
for (long long i = 1; i < n; i++) {
tot += v[i] - v[0];
}
if (tot >= s)
cout << v[0];
else {
s -= tot;
if (s % n == 0)
x = s / n;
else
x = s / n + 1;
if (x > v[0])
cout << -1;
else
cout << v[0] - x;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long n, v, k;
int main() {
cin >> n >> k;
long long mi = 0x3f3f3f3f, sum = 0;
for (int i = 1; i <= n; i++) {
cin >> v;
mi = min(mi, v);
sum += v;
}
if (sum < k)
printf("-1\n");
else {
sum -= k;
printf("%I64d\n", min(mi, sum / n));
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long n, v, k;
int main() {
cin >> n >> k;
long long mi = 0x3f3f3f3f, sum = 0;
for (int i = 1; i <= n; i++) {
cin >> v;
mi = min(mi, v);
sum += v;
}
if (sum < k)
printf("-1\n");
else {
sum -= k;
printf("%I64d\n", min(mi, sum / n));
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
vector<long long int> ar[1000001];
long long int vis[1000001], dis[1000001], level[1000001];
const int MAX_SIZE = 1000001;
const int N = 2000010;
const int mod = 1e9 + 7;
vector<int> isprime(MAX_SIZE, true);
vector<int> prime;
vector<int> SPF(MAX_SIZE);
long long int fact[N];
void manipulated_seive(int N) {
isprime[0] = isprime[1] = false;
for (int i = 2; i < N; ++i) {
if (isprime[i]) {
prime.push_back(i);
SPF[i] = i;
}
for (int j = 0;
j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; ++j) {
isprime[i * prime[j]] = false;
SPF[i * prime[j]] = prime[j];
}
}
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
return (a.second < b.second);
}
unordered_map<long long int, long long int> myp;
void primeFactors(long long int n) {
while (n % 2 == 0) {
myp[2]++;
n = n / 2;
}
for (long long int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
myp[i]++;
n = n / i;
}
}
if (n > 2) myp[n]++;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int findlcm(long long int a, long long int b) {
return a * b / gcd(a, b);
}
long long int power(long long int a, long long int b) {
long long int res = 1;
while (b) {
if (b % 2) res *= a;
a *= a;
b /= 2;
}
return res;
}
long long int power_mod(long long int a, long long int b, long long int p) {
long long int res = 1;
while (b) {
if (b % 2) {
res *= a;
res %= p;
}
a *= a;
a %= p;
b /= 2;
}
return res;
}
long long int mod_inverse(long long int x) {
return power_mod(x, mod - 2, mod);
}
long long int nCr(long long int n, long long int r) {
if (r == 0) return 1;
long long int a = fact[n];
long long int b = mod_inverse(fact[n - r]);
long long int c = mod_inverse(fact[r]);
return (((a * b) % mod) * c) % mod;
}
void fun() {
long long int n, s, a;
cin >> n >> s;
vector<long long int> v;
long long int s1 = 0;
for (long long int i = 1; i <= n; i++) {
cin >> a;
v.push_back(a);
s1 += a;
}
if (s1 < s) {
cout << -1 << '\n';
return;
}
sort(v.begin(), v.end());
long long int sum = 0;
for (long long int i = 1; i < n; i++) {
long long int dif = v[i] - v[0];
sum += dif;
}
if (sum >= s) {
cout << v[0] << '\n';
return;
}
s -= sum;
long long int ans = floor(v[0] - (s + n - 1) / n);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
unsigned long long int t;
t = 1;
while (t--) {
cout << fixed;
cout << setprecision(10);
fun();
}
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
vector<long long int> ar[1000001];
long long int vis[1000001], dis[1000001], level[1000001];
const int MAX_SIZE = 1000001;
const int N = 2000010;
const int mod = 1e9 + 7;
vector<int> isprime(MAX_SIZE, true);
vector<int> prime;
vector<int> SPF(MAX_SIZE);
long long int fact[N];
void manipulated_seive(int N) {
isprime[0] = isprime[1] = false;
for (int i = 2; i < N; ++i) {
if (isprime[i]) {
prime.push_back(i);
SPF[i] = i;
}
for (int j = 0;
j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; ++j) {
isprime[i * prime[j]] = false;
SPF[i * prime[j]] = prime[j];
}
}
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
return (a.second < b.second);
}
unordered_map<long long int, long long int> myp;
void primeFactors(long long int n) {
while (n % 2 == 0) {
myp[2]++;
n = n / 2;
}
for (long long int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
myp[i]++;
n = n / i;
}
}
if (n > 2) myp[n]++;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int findlcm(long long int a, long long int b) {
return a * b / gcd(a, b);
}
long long int power(long long int a, long long int b) {
long long int res = 1;
while (b) {
if (b % 2) res *= a;
a *= a;
b /= 2;
}
return res;
}
long long int power_mod(long long int a, long long int b, long long int p) {
long long int res = 1;
while (b) {
if (b % 2) {
res *= a;
res %= p;
}
a *= a;
a %= p;
b /= 2;
}
return res;
}
long long int mod_inverse(long long int x) {
return power_mod(x, mod - 2, mod);
}
long long int nCr(long long int n, long long int r) {
if (r == 0) return 1;
long long int a = fact[n];
long long int b = mod_inverse(fact[n - r]);
long long int c = mod_inverse(fact[r]);
return (((a * b) % mod) * c) % mod;
}
void fun() {
long long int n, s, a;
cin >> n >> s;
vector<long long int> v;
long long int s1 = 0;
for (long long int i = 1; i <= n; i++) {
cin >> a;
v.push_back(a);
s1 += a;
}
if (s1 < s) {
cout << -1 << '\n';
return;
}
sort(v.begin(), v.end());
long long int sum = 0;
for (long long int i = 1; i < n; i++) {
long long int dif = v[i] - v[0];
sum += dif;
}
if (sum >= s) {
cout << v[0] << '\n';
return;
}
s -= sum;
long long int ans = floor(v[0] - (s + n - 1) / n);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
unsigned long long int t;
t = 1;
while (t--) {
cout << fixed;
cout << setprecision(10);
fun();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, val;
cin >> n >> s;
vector<long long> vec;
long long sumtot = 0;
for (long long i = 0; i < n; i++) {
cin >> val;
sumtot += val;
vec.push_back(val);
}
if (sumtot < s) {
cout << -1;
return 0;
}
sort(vec.begin(), vec.end());
long long value = (sumtot - s) / n;
if (value < vec[0])
cout << value;
else
cout << vec[0];
}
| ### Prompt
Create a solution in CPP for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, val;
cin >> n >> s;
vector<long long> vec;
long long sumtot = 0;
for (long long i = 0; i < n; i++) {
cin >> val;
sumtot += val;
vec.push_back(val);
}
if (sumtot < s) {
cout << -1;
return 0;
}
sort(vec.begin(), vec.end());
long long value = (sumtot - s) / n;
if (value < vec[0])
cout << value;
else
cout << vec[0];
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio;
cin.tie(NULL);
cout.tie(NULL);
long long int i, j, sum = 0, n, m, min = 1000000001, ans, s;
cin >> n >> s;
long long int a[n];
for (i = 0; i < n; i++) {
cin >> j;
sum = sum + j;
if (j <= min) min = j;
}
ans = (sum - s) / n;
if (sum < s) {
cout << -1;
return 0;
}
if (ans >= min)
cout << min;
else
cout << ans;
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio;
cin.tie(NULL);
cout.tie(NULL);
long long int i, j, sum = 0, n, m, min = 1000000001, ans, s;
cin >> n >> s;
long long int a[n];
for (i = 0; i < n; i++) {
cin >> j;
sum = sum + j;
if (j <= min) min = j;
}
ans = (sum - s) / n;
if (sum < s) {
cout << -1;
return 0;
}
if (ans >= min)
cout << min;
else
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long int n = 0, k = 0, sum = 0;
cin >> n >> k;
long long int arr[n];
for (int a = 0; a < n; a++) cin >> arr[a];
sort(arr, arr + n);
for (int a = 0; a < n; a++) sum += arr[a] - arr[0];
cout << max(arr[0] - (max(k - sum, 0ll) / n +
(max(k - sum, 0ll) % n != 0 ? 1ll : 0ll)),
-1ll);
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long int n = 0, k = 0, sum = 0;
cin >> n >> k;
long long int arr[n];
for (int a = 0; a < n; a++) cin >> arr[a];
sort(arr, arr + n);
for (int a = 0; a < n; a++) sum += arr[a] - arr[0];
cout << max(arr[0] - (max(k - sum, 0ll) / n +
(max(k - sum, 0ll) % n != 0 ? 1ll : 0ll)),
-1ll);
}
``` |
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
size_t n;
int64_t s;
std::cin >> n >> s;
std::vector<int64_t> v(n);
for (size_t i = 0; i < n; ++i) {
std::cin >> v[i];
}
const int64_t min = *min_element(v.begin(), v.end());
int64_t cur = 0;
for (size_t i = 0; i < n; ++i) {
cur += v[i] - min;
}
if (cur >= s) {
std::cout << min << '\n';
return 0;
}
const int64_t need = (s - cur - 1) / int64_t(n) + 1;
if (min < need) {
std::cout << "-1\n";
} else {
std::cout << min - need << '\n';
}
}
| ### Prompt
In CPP, your task is to solve the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
size_t n;
int64_t s;
std::cin >> n >> s;
std::vector<int64_t> v(n);
for (size_t i = 0; i < n; ++i) {
std::cin >> v[i];
}
const int64_t min = *min_element(v.begin(), v.end());
int64_t cur = 0;
for (size_t i = 0; i < n; ++i) {
cur += v[i] - min;
}
if (cur >= s) {
std::cout << min << '\n';
return 0;
}
const int64_t need = (s - cur - 1) / int64_t(n) + 1;
if (min < need) {
std::cout << "-1\n";
} else {
std::cout << min - need << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
ll s;
cin >> n >> s;
vector<int> a(n);
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s) {
cout << -1 << '\n';
return 0;
}
sort(a.begin(), a.end());
int lo = 0, hi = a[0] + 1;
while (lo + 1 < hi) {
int mid = (lo + hi) / 2;
ll t = 0;
for (int i = 0; i < n; i++) {
t += max(0, a[i] - mid);
}
if (t >= s) {
lo = mid;
} else {
hi = mid;
}
}
cout << lo << '\n';
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
ll s;
cin >> n >> s;
vector<int> a(n);
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < s) {
cout << -1 << '\n';
return 0;
}
sort(a.begin(), a.end());
int lo = 0, hi = a[0] + 1;
while (lo + 1 < hi) {
int mid = (lo + hi) / 2;
ll t = 0;
for (int i = 0; i < n; i++) {
t += max(0, a[i] - mid);
}
if (t >= s) {
lo = mid;
} else {
hi = mid;
}
}
cout << lo << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long int s, sum = 0;
cin >> n >> s;
long long int* v = new long long int[n];
for (int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
}
if (s > sum)
cout << -1 << endl;
else {
sort(v, v + n);
if (s < (sum - v[0] * n))
cout << v[0] << endl;
else {
s -= sum - v[0] * n;
if (s % n == 0)
cout << v[0] - s / n << endl;
else
cout << v[0] - (s / n) - 1 << endl;
}
}
}
| ### Prompt
Generate a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long int s, sum = 0;
cin >> n >> s;
long long int* v = new long long int[n];
for (int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
}
if (s > sum)
cout << -1 << endl;
else {
sort(v, v + n);
if (s < (sum - v[0] * n))
cout << v[0] << endl;
else {
s -= sum - v[0] * n;
if (s % n == 0)
cout << v[0] - s / n << endl;
else
cout << v[0] - (s / n) - 1 << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const long long INF = 1e18;
const int M = 1e9 + 7;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << '\n';
err(++it, args...);
}
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int FX[] = {-2, -2, -1, -1, 1, 1, 2, 2};
int FY[] = {-1, 1, -2, 2, -2, 2, -1, 1};
long long n, m, l, r, d, a, b, k, u, p = -1, q, x, y, z, mn, mx, rem, ans,
res = 0, c = 0;
long long aa[2000];
long long ch(long long NN) {
long long sm = 0;
for (long long i = 0; i < n; i++) {
if (aa[i] < NN)
return 0;
else
sm += aa[i] - NN;
}
if (sm >= b)
return sm;
else
return 0;
}
void _CODE() {
cin >> n >> b;
for (long long i = 0; i < n; i++) cin >> aa[i];
p = -1;
l = 0;
r = 1e9;
while (l <= r) {
m = (l + r) >> 1ll;
if (ch(m))
l = m + 1, p = m;
else
r = m - 1;
}
cout << p;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
_CODE();
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const long long INF = 1e18;
const int M = 1e9 + 7;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << '\n';
err(++it, args...);
}
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int FX[] = {-2, -2, -1, -1, 1, 1, 2, 2};
int FY[] = {-1, 1, -2, 2, -2, 2, -1, 1};
long long n, m, l, r, d, a, b, k, u, p = -1, q, x, y, z, mn, mx, rem, ans,
res = 0, c = 0;
long long aa[2000];
long long ch(long long NN) {
long long sm = 0;
for (long long i = 0; i < n; i++) {
if (aa[i] < NN)
return 0;
else
sm += aa[i] - NN;
}
if (sm >= b)
return sm;
else
return 0;
}
void _CODE() {
cin >> n >> b;
for (long long i = 0; i < n; i++) cin >> aa[i];
p = -1;
l = 0;
r = 1e9;
while (l <= r) {
m = (l + r) >> 1ll;
if (ch(m))
l = m + 1, p = m;
else
r = m - 1;
}
cout << p;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
_CODE();
}
``` |
#include <bits/stdc++.h>
long long int power(long long int x, long long int b) {
long long int p = 1;
while (b > 0) {
if (b & 1) {
p = p * x;
p %= 1000000007;
}
b >>= 1;
x *= x;
x %= 1000000007;
}
return p % 1000000007;
}
using namespace std;
struct lex_compare {
bool operator()(
const pair<long long int, pair<long long int, long long int> > p1,
const pair<long long int, pair<long long int, long long int> > p2) {
return (p1.first == p2.first) ? p1.second.first < p2.second.first
: p1.first > p2.first;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long int n, s;
cin >> n >> s;
long long int arr[n];
long long int sum = 0;
long long int mint = INT_MAX;
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
mint = min(arr[i], mint);
}
if (sum < s) {
cout << -1 << "\n";
return 0;
} else if (sum == s) {
cout << 0 << "\n";
return 0;
}
long long int sum2 = sum - n * mint;
if (sum2 >= s) {
cout << mint << "\n";
} else {
s -= sum2;
cout << (n * mint - s) / n << "\n";
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
long long int power(long long int x, long long int b) {
long long int p = 1;
while (b > 0) {
if (b & 1) {
p = p * x;
p %= 1000000007;
}
b >>= 1;
x *= x;
x %= 1000000007;
}
return p % 1000000007;
}
using namespace std;
struct lex_compare {
bool operator()(
const pair<long long int, pair<long long int, long long int> > p1,
const pair<long long int, pair<long long int, long long int> > p2) {
return (p1.first == p2.first) ? p1.second.first < p2.second.first
: p1.first > p2.first;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long int n, s;
cin >> n >> s;
long long int arr[n];
long long int sum = 0;
long long int mint = INT_MAX;
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
mint = min(arr[i], mint);
}
if (sum < s) {
cout << -1 << "\n";
return 0;
} else if (sum == s) {
cout << 0 << "\n";
return 0;
}
long long int sum2 = sum - n * mint;
if (sum2 >= s) {
cout << mint << "\n";
} else {
s -= sum2;
cout << (n * mint - s) / n << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, v[1010], t = 0;
cin >> n >> s;
for (long long i = 0; i < n; ++i) {
cin >> v[i];
t += v[i];
}
if (t < s) {
puts("-1");
return 0;
}
sort(v, v + n);
t = 0;
for (int i = 1; i < n; ++i) {
t += v[i] - v[0];
}
if (t >= s) {
cout << v[0] << endl;
return 0;
}
long long k = (s - t) / n;
t += k * n;
if (t < s) ++k;
cout << v[0] - k << endl;
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, v[1010], t = 0;
cin >> n >> s;
for (long long i = 0; i < n; ++i) {
cin >> v[i];
t += v[i];
}
if (t < s) {
puts("-1");
return 0;
}
sort(v, v + n);
t = 0;
for (int i = 1; i < n; ++i) {
t += v[i] - v[0];
}
if (t >= s) {
cout << v[0] << endl;
return 0;
}
long long k = (s - t) / n;
t += k * n;
if (t < s) ++k;
cout << v[0] - k << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long a[5000];
int main() {
int n;
long long m;
cin >> n >> m;
long long sum = 0ll;
for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i];
sort(a + 1, a + 1 + n);
long long fuck = 0ll;
for (int i = 2; i <= n; i++) fuck += a[i] - a[1];
m -= fuck;
if (m <= 0) return cout << a[1], 0;
int flag = 0;
if (m % n != 0) flag = 1;
m /= n;
m += flag;
cout << max(-1ll, a[1] - m);
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long a[5000];
int main() {
int n;
long long m;
cin >> n >> m;
long long sum = 0ll;
for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i];
sort(a + 1, a + 1 + n);
long long fuck = 0ll;
for (int i = 2; i <= n; i++) fuck += a[i] - a[1];
m -= fuck;
if (m <= 0) return cout << a[1], 0;
int flag = 0;
if (m % n != 0) flag = 1;
m /= n;
m += flag;
cout << max(-1ll, a[1] - m);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, s;
cin >> n >> s;
vector<long long> v(n);
long long sum = 0, minimum = 1e9;
for (int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
if (v[i] < minimum) minimum = v[i];
}
sum -= s;
if (sum < 0)
cout << -1 << endl;
else
cout << min(minimum, sum / n) << endl;
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, s;
cin >> n >> s;
vector<long long> v(n);
long long sum = 0, minimum = 1e9;
for (int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
if (v[i] < minimum) minimum = v[i];
}
sum -= s;
if (sum < 0)
cout << -1 << endl;
else
cout << min(minimum, sum / n) << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
const char dl = '\n';
const long double eps = 0.00000001;
const long long MOD = 1e9 + 7;
const double PI = 3.141592653589793238463;
using namespace std;
void debug() { cout << endl; }
template <typename H, typename... T>
void debug(H p, T... t) {
std::cout << p << " ";
debug(t...);
}
long long ans;
int n, m, k;
long long second, sum;
int main() {
fflush(stdin);
cout << fixed, cout.precision(18);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, j;
cin >> n >> second;
long long a[n];
for (i = 0; i < n; ++i) cin >> a[i], sum += a[i];
if (sum < second) return cout << -1, 0;
long long up = 0;
sort(a, a + n);
long long mi = a[0];
for (i = 0; i < n; ++i) up += (a[i] - mi);
second -= up;
if (second <= 0) return cout << mi, 0;
long long need = second / n;
second %= n;
mi -= need;
if (second) mi--;
cout << mi;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
const char dl = '\n';
const long double eps = 0.00000001;
const long long MOD = 1e9 + 7;
const double PI = 3.141592653589793238463;
using namespace std;
void debug() { cout << endl; }
template <typename H, typename... T>
void debug(H p, T... t) {
std::cout << p << " ";
debug(t...);
}
long long ans;
int n, m, k;
long long second, sum;
int main() {
fflush(stdin);
cout << fixed, cout.precision(18);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, j;
cin >> n >> second;
long long a[n];
for (i = 0; i < n; ++i) cin >> a[i], sum += a[i];
if (sum < second) return cout << -1, 0;
long long up = 0;
sort(a, a + n);
long long mi = a[0];
for (i = 0; i < n; ++i) up += (a[i] - mi);
second -= up;
if (second <= 0) return cout << mi, 0;
long long need = second / n;
second %= n;
mi -= need;
if (second) mi--;
cout << mi;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int s;
cin >> s;
long long int arr[n];
long long int sum = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
sort(arr, arr + n);
if (s > sum) {
cout << "-1" << endl;
} else {
long long int m = 0;
int id = n;
for (int i = 1; i < n; i++) {
if (arr[i] != arr[0]) {
id = i;
break;
}
}
for (int i = id; i < n; i++) {
m = m + arr[i] - arr[0];
}
s = s - m;
if (s <= 0) {
cout << arr[0] << endl;
} else {
long long int ans;
ans = arr[0] - ceil(s / (n + 0.0));
cout << ans << endl;
}
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int s;
cin >> s;
long long int arr[n];
long long int sum = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
sort(arr, arr + n);
if (s > sum) {
cout << "-1" << endl;
} else {
long long int m = 0;
int id = n;
for (int i = 1; i < n; i++) {
if (arr[i] != arr[0]) {
id = i;
break;
}
}
for (int i = id; i < n; i++) {
m = m + arr[i] - arr[0];
}
s = s - m;
if (s <= 0) {
cout << arr[0] << endl;
} else {
long long int ans;
ans = arr[0] - ceil(s / (n + 0.0));
cout << ans << endl;
}
}
}
``` |
#include <bits/stdc++.h>
long long int v[1001];
int main() {
int i, j, k, n, t, l;
long long int min = 1000000001;
long long int s, r, d, total = 0;
scanf("%d %lld", &n, &s);
for (int a = 0; a < n; a++) {
scanf("%lld", &v[a]);
if (min > v[a]) min = v[a];
total += v[a];
}
if (s > total)
printf("-1\n");
else if (s == total)
printf("0\n");
else {
if (((total - s) / n) >= min)
printf("%lld\n", min);
else
printf("%lld\n", ((total - s) / n));
}
}
| ### Prompt
Please formulate a cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
long long int v[1001];
int main() {
int i, j, k, n, t, l;
long long int min = 1000000001;
long long int s, r, d, total = 0;
scanf("%d %lld", &n, &s);
for (int a = 0; a < n; a++) {
scanf("%lld", &v[a]);
if (min > v[a]) min = v[a];
total += v[a];
}
if (s > total)
printf("-1\n");
else if (s == total)
printf("0\n");
else {
if (((total - s) / n) >= min)
printf("%lld\n", min);
else
printf("%lld\n", ((total - s) / n));
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, s;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
t = 1;
while (t--) {
cin >> n >> s;
vector<long long> v(n);
long long sum = 0;
long long mini = INT_MAX;
for (long long i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
mini = min(mini, v[i]);
}
if (sum < s) {
cout << -1;
continue;
}
sum = 0;
for (long long i = 0; i < n; i++) {
sum += (v[i] - mini);
}
if (sum >= s) {
cout << mini;
continue;
}
s -= sum;
long long ans = mini;
while (s > 0) {
s -= n;
ans--;
}
cout << ans;
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, s;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
t = 1;
while (t--) {
cin >> n >> s;
vector<long long> v(n);
long long sum = 0;
long long mini = INT_MAX;
for (long long i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
mini = min(mini, v[i]);
}
if (sum < s) {
cout << -1;
continue;
}
sum = 0;
for (long long i = 0; i < n; i++) {
sum += (v[i] - mini);
}
if (sum >= s) {
cout << mini;
continue;
}
s -= sum;
long long ans = mini;
while (s > 0) {
s -= n;
ans--;
}
cout << ans;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m, mn, res, sum;
cin >> n >> m;
long long arr[n];
sum = 0;
mn = INT_MAX;
for (int i = 0; i < n; i++) {
cin >> arr[i];
mn = min(arr[i], mn);
sum += arr[i];
}
res = mn;
if (sum < m) {
cout << "-1";
return 0;
}
if (sum == 0) {
cout << "0";
return 0;
}
for (int i = 0; i < n; i++) {
m = m - (arr[i] - mn);
if (m <= 0) break;
}
if (m > 0) {
{ mn = mn - ((m + n - 1) / n); }
}
cout << mn << "\n";
}
| ### Prompt
In CPP, your task is to solve the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m, mn, res, sum;
cin >> n >> m;
long long arr[n];
sum = 0;
mn = INT_MAX;
for (int i = 0; i < n; i++) {
cin >> arr[i];
mn = min(arr[i], mn);
sum += arr[i];
}
res = mn;
if (sum < m) {
cout << "-1";
return 0;
}
if (sum == 0) {
cout << "0";
return 0;
}
for (int i = 0; i < n; i++) {
m = m - (arr[i] - mn);
if (m <= 0) break;
}
if (m > 0) {
{ mn = mn - ((m + n - 1) / n); }
}
cout << mn << "\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<int, int> &a, pair<int, int> &b) {
if (a.second == b.second)
return a.first < b.first;
else
return a.second < b.second;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
long long S;
cin >> N >> S;
vector<long long> a(N);
long long sum1 = 0ll, sum2 = 0ll;
for (int i = 0; i < N; ++i) {
cin >> a[i];
sum1 += a[i];
}
if (sum1 < S) {
cout << -1;
return 0;
}
sort(a.begin(), a.end());
for (int i = 0; i < N; ++i) {
sum2 += a[i] - a[0];
}
if (sum2 >= S) {
cout << a[0];
return 0;
}
S -= sum2;
cout << a[0] - ((S - 1ll) / (long long)N + 1ll);
return 0;
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<int, int> &a, pair<int, int> &b) {
if (a.second == b.second)
return a.first < b.first;
else
return a.second < b.second;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
long long S;
cin >> N >> S;
vector<long long> a(N);
long long sum1 = 0ll, sum2 = 0ll;
for (int i = 0; i < N; ++i) {
cin >> a[i];
sum1 += a[i];
}
if (sum1 < S) {
cout << -1;
return 0;
}
sort(a.begin(), a.end());
for (int i = 0; i < N; ++i) {
sum2 += a[i] - a[0];
}
if (sum2 >= S) {
cout << a[0];
return 0;
}
S -= sum2;
cout << a[0] - ((S - 1ll) / (long long)N + 1ll);
return 0;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int llINF = 0x3f3f3f3f3f3f3f;
long long int n, s;
vector<long long int> v;
bool check(long long int val) {
long long int bebe = 0;
for (auto x : v) {
if (x < val) return false;
bebe += abs(x - val);
}
return bebe >= s;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> s;
long long int tot = 0;
for (int i = 1; i <= n; i++) {
long long int x;
cin >> x;
v.push_back(x);
tot += x;
}
if (tot < s) {
cout << -1 << endl;
return 0;
}
sort(v.begin(), v.end());
long long int ini = 0;
long long int end = 1e9;
long long int best = -INF;
while (ini <= end) {
long long int mid = (ini + end) >> 1;
if (check(mid)) {
best = max(best, mid);
ini = mid + 1;
} else
end = mid - 1;
}
cout << best << endl;
}
| ### Prompt
Create a solution in Cpp for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int llINF = 0x3f3f3f3f3f3f3f;
long long int n, s;
vector<long long int> v;
bool check(long long int val) {
long long int bebe = 0;
for (auto x : v) {
if (x < val) return false;
bebe += abs(x - val);
}
return bebe >= s;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> s;
long long int tot = 0;
for (int i = 1; i <= n; i++) {
long long int x;
cin >> x;
v.push_back(x);
tot += x;
}
if (tot < s) {
cout << -1 << endl;
return 0;
}
sort(v.begin(), v.end());
long long int ini = 0;
long long int end = 1e9;
long long int best = -INF;
while (ini <= end) {
long long int mid = (ini + end) >> 1;
if (check(mid)) {
best = max(best, mid);
ini = mid + 1;
} else
end = mid - 1;
}
cout << best << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, ans = 0, acum = 0, s, tonel;
cin >> n >> s >> ans;
acum = ans;
for (int i = 1; i < n; ++i) {
cin >> tonel;
acum += tonel;
ans = min(ans, tonel);
}
if (acum < s)
ans = -1;
else if (acum == s)
ans = 0;
else {
s -= (acum - ans * n);
while (s > 0) {
ans--;
s -= n;
}
}
cout << ans << "\n";
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, ans = 0, acum = 0, s, tonel;
cin >> n >> s >> ans;
acum = ans;
for (int i = 1; i < n; ++i) {
cin >> tonel;
acum += tonel;
ans = min(ans, tonel);
}
if (acum < s)
ans = -1;
else if (acum == s)
ans = 0;
else {
s -= (acum - ans * n);
while (s > 0) {
ans--;
s -= n;
}
}
cout << ans << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
int v[1005];
long long s, sum;
bool cmp(int x, int y) { return x > y; }
int main() {
scanf("%d %lld", &n, &s);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]), sum += v[i];
if (sum < s) {
printf("-1\n");
return 0;
}
sort(v + 1, v + n + 1, cmp);
for (int i = 1; i <= n; i++) {
long long vi = (long long)i * (v[i] - v[i + 1]);
if (s > vi)
s -= vi;
else {
if (i == n)
printf("%d\n", v[i] - (s + i - 1) / i);
else
printf("%d\n", v[n]);
return 0;
}
}
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int v[1005];
long long s, sum;
bool cmp(int x, int y) { return x > y; }
int main() {
scanf("%d %lld", &n, &s);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]), sum += v[i];
if (sum < s) {
printf("-1\n");
return 0;
}
sort(v + 1, v + n + 1, cmp);
for (int i = 1; i <= n; i++) {
long long vi = (long long)i * (v[i] - v[i + 1]);
if (s > vi)
s -= vi;
else {
if (i == n)
printf("%d\n", v[i] - (s + i - 1) / i);
else
printf("%d\n", v[n]);
return 0;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long ar[1001];
int main() {
long long int n, s;
cin >> n >> s;
for (int i = 1; i <= n; i++) cin >> ar[i];
long long sum = 0;
for (int i = 1; i <= n; i++) sum += ar[i];
if (sum < s) {
cout << -1 << endl;
exit(0);
}
long long m = *min_element(ar + 1, ar + n + 1);
long long diff = sum - m * n;
if (diff > s)
cout << m << endl;
else {
s -= diff;
int h;
if (s % n == 0)
h = s / n;
else
h = s / n + 1;
cout << m - h << endl;
}
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long ar[1001];
int main() {
long long int n, s;
cin >> n >> s;
for (int i = 1; i <= n; i++) cin >> ar[i];
long long sum = 0;
for (int i = 1; i <= n; i++) sum += ar[i];
if (sum < s) {
cout << -1 << endl;
exit(0);
}
long long m = *min_element(ar + 1, ar + n + 1);
long long diff = sum - m * n;
if (diff > s)
cout << m << endl;
else {
s -= diff;
int h;
if (s % n == 0)
h = s / n;
else
h = s / n + 1;
cout << m - h << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, s;
vector<long long> v(1001);
long long check(long long x) {
long long sum = 0;
for (long long i = 0; i < n; i++) {
if (v[i] < x) return 0;
sum += max((long long)0, v[i] - x);
}
return (sum >= s);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> s;
for (long long i = 0; i < n; i++) cin >> v[i];
long long l = 0;
long long r = 1e15;
while (l < r) {
long long mid = (l + r + 1) / 2;
if (check(mid))
l = mid;
else
r = mid - 1;
}
if (check(l))
cout << l;
else
cout << -1;
}
| ### Prompt
Create a solution in cpp for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, s;
vector<long long> v(1001);
long long check(long long x) {
long long sum = 0;
for (long long i = 0; i < n; i++) {
if (v[i] < x) return 0;
sum += max((long long)0, v[i] - x);
}
return (sum >= s);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> s;
for (long long i = 0; i < n; i++) cin >> v[i];
long long l = 0;
long long r = 1e15;
while (l < r) {
long long mid = (l + r + 1) / 2;
if (check(mid))
l = mid;
else
r = mid - 1;
}
if (check(l))
cout << l;
else
cout << -1;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, s, a[1001];
int main() {
while (cin >> n >> s) {
long long maxa = 0, sum = 0, mina = 0x3f3f3f3f;
for (int i = 0; i < n; i++) {
cin >> a[i];
maxa = max(a[i], maxa);
mina = min(mina, a[i]);
sum += a[i];
}
long long l = 0, r = maxa;
while (l + 1 < r) {
long long mid = (l + r) / 2;
if (sum - mid * n >= s)
l = mid;
else
r = mid;
}
while (r >= 0 && sum - r * n < s) {
r--;
}
cout << min(r, mina) << endl;
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, s, a[1001];
int main() {
while (cin >> n >> s) {
long long maxa = 0, sum = 0, mina = 0x3f3f3f3f;
for (int i = 0; i < n; i++) {
cin >> a[i];
maxa = max(a[i], maxa);
mina = min(mina, a[i]);
sum += a[i];
}
long long l = 0, r = maxa;
while (l + 1 < r) {
long long mid = (l + r) / 2;
if (sum - mid * n >= s)
l = mid;
else
r = mid;
}
while (r >= 0 && sum - r * n < s) {
r--;
}
cout << min(r, mina) << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool P(long long r, vector<long long> &v, long long &s) {
int i;
long long sum = 0;
for (i = 0; i < v.size(); i++)
if (v[i] - r >= 0) sum += v[i] - r;
if (sum >= s) return true;
return false;
}
int main() {
long long n, s, i, sum = 0;
cin >> n >> s;
vector<long long> v(n);
for (i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
}
if (sum < s) {
cout << -1;
return 0;
}
long long l = 0, h = *min_element(v.begin(), v.end()), ans, mid;
while (l <= h) {
mid = (l + h) / 2;
if (P(mid, v, s)) {
ans = mid;
l = mid + 1;
} else
h = mid - 1;
}
cout << ans;
}
| ### Prompt
Generate a cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool P(long long r, vector<long long> &v, long long &s) {
int i;
long long sum = 0;
for (i = 0; i < v.size(); i++)
if (v[i] - r >= 0) sum += v[i] - r;
if (sum >= s) return true;
return false;
}
int main() {
long long n, s, i, sum = 0;
cin >> n >> s;
vector<long long> v(n);
for (i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
}
if (sum < s) {
cout << -1;
return 0;
}
long long l = 0, h = *min_element(v.begin(), v.end()), ans, mid;
while (l <= h) {
mid = (l + h) / 2;
if (P(mid, v, s)) {
ans = mid;
l = mid + 1;
} else
h = mid - 1;
}
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline char gch() {
static char buf[100010], *h = buf, *t = buf;
return h == t && (t = (h = buf) + fread(buf, 1, 100000, stdin), h == t)
? EOF
: *h++;
}
inline void re(long long &x) {
x = 0;
char a;
bool b = 0;
while (!isdigit(a = getchar())) b = a == '-';
while (isdigit(a)) x = x * 10 + a - '0', a = getchar();
if (b == 1) x = -x;
}
long long n, s, ans = -1, a[1010];
inline bool check(long long x) {
long long rt = 0;
for (register int i = 1; i <= n; i++) {
if (a[i] < x) return 0;
rt += (a[i] - x);
}
return rt >= s;
}
int main() {
re(n), re(s);
long long l = 0, r = 0;
for (register int i = 1; i <= n; i++) re(a[i]), r += a[i];
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
cout << ans;
}
| ### Prompt
In CPP, your task is to solve the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline char gch() {
static char buf[100010], *h = buf, *t = buf;
return h == t && (t = (h = buf) + fread(buf, 1, 100000, stdin), h == t)
? EOF
: *h++;
}
inline void re(long long &x) {
x = 0;
char a;
bool b = 0;
while (!isdigit(a = getchar())) b = a == '-';
while (isdigit(a)) x = x * 10 + a - '0', a = getchar();
if (b == 1) x = -x;
}
long long n, s, ans = -1, a[1010];
inline bool check(long long x) {
long long rt = 0;
for (register int i = 1; i <= n; i++) {
if (a[i] < x) return 0;
rt += (a[i] - x);
}
return rt >= s;
}
int main() {
re(n), re(s);
long long l = 0, r = 0;
for (register int i = 1; i <= n; i++) re(a[i]), r += a[i];
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
long long int t, n, m, j, ans, k, a, b, c, d, e, f, sum, i, sz;
string s, s2, s3, s4;
vector<long long int> v;
int ar[(int)(1e6 + 10)], ar2[(int)(1e6 + 10)];
void brainfuck();
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
brainfuck();
return 0;
}
void brainfuck() {
cin >> n >> m;
b = INT_MAX;
for (i = 1; i <= n; i++) {
cin >> a;
v.push_back(a);
b = min(a, b);
sum += a;
}
if (sum < m) {
cout << "-1";
return;
}
a = 0;
for (i = 1; i <= n; i++) {
a += abs(v[i - 1] - b);
v[i - 1] = b;
}
if (m <= a) {
cout << b;
return;
}
c = m - a;
d = c / n;
if (!d) d = 1;
if (d * n >= c) {
cout << v[0] - d;
return;
} else
cout << v[0] - d - 1;
}
| ### Prompt
Create a solution in Cpp for the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
long long int t, n, m, j, ans, k, a, b, c, d, e, f, sum, i, sz;
string s, s2, s3, s4;
vector<long long int> v;
int ar[(int)(1e6 + 10)], ar2[(int)(1e6 + 10)];
void brainfuck();
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
brainfuck();
return 0;
}
void brainfuck() {
cin >> n >> m;
b = INT_MAX;
for (i = 1; i <= n; i++) {
cin >> a;
v.push_back(a);
b = min(a, b);
sum += a;
}
if (sum < m) {
cout << "-1";
return;
}
a = 0;
for (i = 1; i <= n; i++) {
a += abs(v[i - 1] - b);
v[i - 1] = b;
}
if (m <= a) {
cout << b;
return;
}
c = m - a;
d = c / n;
if (!d) d = 1;
if (d * n >= c) {
cout << v[0] - d;
return;
} else
cout << v[0] - d - 1;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long inf = (long long)1e18;
long long mod = 1e9 + 7;
long long max1 = (long long)1e9;
int main() {
long long i, j, k, n, m, ct = 0, t, ans = 0;
cin >> n >> k;
long long sum1 = 0;
long long a[n], min1 = 1e18;
for (i = 0; i < n; i++) {
cin >> a[i];
sum1 += a[i];
if (min1 > a[i]) {
min1 = a[i];
j = i;
}
}
if (sum1 < k)
cout << "-1";
else {
long long sum = 0;
for (i = 0; i < n; i++) {
if (a[i] != min1) {
sum += (a[i] - min1);
a[i] = min1;
}
}
long long low = 0, high = min1;
while (low <= high) {
long long mid = (low + high) / 2;
if (sum + mid * n >= k) {
ans = mid;
high = mid - 1;
} else
low = mid + 1;
}
cout << a[0] - ans << endl;
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long inf = (long long)1e18;
long long mod = 1e9 + 7;
long long max1 = (long long)1e9;
int main() {
long long i, j, k, n, m, ct = 0, t, ans = 0;
cin >> n >> k;
long long sum1 = 0;
long long a[n], min1 = 1e18;
for (i = 0; i < n; i++) {
cin >> a[i];
sum1 += a[i];
if (min1 > a[i]) {
min1 = a[i];
j = i;
}
}
if (sum1 < k)
cout << "-1";
else {
long long sum = 0;
for (i = 0; i < n; i++) {
if (a[i] != min1) {
sum += (a[i] - min1);
a[i] = min1;
}
}
long long low = 0, high = min1;
while (low <= high) {
long long mid = (low + high) / 2;
if (sum + mid * n >= k) {
ans = mid;
high = mid - 1;
} else
low = mid + 1;
}
cout << a[0] - ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
t = 1;
while (t--) solve();
return 0;
}
void solve() {
long long i, j, n, s;
cin >> n >> s;
vector<long long> a(n);
long long small, tot = 0;
for (i = 0; i < n; ++i) {
cin >> a[i];
tot += a[i];
}
small = *min_element(a.begin(), a.end());
if (s > tot) {
cout << -1;
return;
}
for (i = 0; i < n; ++i) s -= a[i] - small;
if (s <= 0) {
cout << small;
return;
}
if (s % n)
cout << small - (s / n) - 1;
else
cout << small - s / n;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
t = 1;
while (t--) solve();
return 0;
}
void solve() {
long long i, j, n, s;
cin >> n >> s;
vector<long long> a(n);
long long small, tot = 0;
for (i = 0; i < n; ++i) {
cin >> a[i];
tot += a[i];
}
small = *min_element(a.begin(), a.end());
if (s > tot) {
cout << -1;
return;
}
for (i = 0; i < n; ++i) s -= a[i] - small;
if (s <= 0) {
cout << small;
return;
}
if (s % n)
cout << small - (s / n) - 1;
else
cout << small - s / n;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s;
cin >> n >> s;
long long a[n];
long long sumofa = 0;
long long minofa = INT_MAX;
for (long long i = 0; i < n; i++) {
cin >> a[i];
sumofa += a[i];
if (minofa > a[i]) {
minofa = a[i];
}
}
if (sumofa < s) {
cout << -1 << endl;
} else if (sumofa == s) {
cout << 0 << endl;
} else {
long long temp = sumofa - (n * minofa);
if (temp >= s) {
cout << minofa << endl;
} else {
long long cnt = s - temp;
long long res = ((n * minofa) - cnt) / n;
cout << res << endl;
}
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass.
Input
The first line contains two integers n and s (1 β€ n β€ 10^3, 1 β€ s β€ 10^{12}) β the number of kegs and glass volume.
The second line contains n integers v_1, v_2, β¦, v_n (1 β€ v_i β€ 10^9) β the volume of i-th keg.
Output
If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β how much kvass in the least keg can be.
Examples
Input
3 3
4 3 5
Output
3
Input
3 4
5 3 4
Output
2
Input
3 7
1 2 3
Output
-1
Note
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s;
cin >> n >> s;
long long a[n];
long long sumofa = 0;
long long minofa = INT_MAX;
for (long long i = 0; i < n; i++) {
cin >> a[i];
sumofa += a[i];
if (minofa > a[i]) {
minofa = a[i];
}
}
if (sumofa < s) {
cout << -1 << endl;
} else if (sumofa == s) {
cout << 0 << endl;
} else {
long long temp = sumofa - (n * minofa);
if (temp >= s) {
cout << minofa << endl;
} else {
long long cnt = s - temp;
long long res = ((n * minofa) - cnt) / n;
cout << res << endl;
}
}
return 0;
}
``` |