output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); if (c > d * 2 || d > c * 2 || d >= b) printf("-1\n"); else printf("%d\n%d\n%d\n", a * 2, b * 2, c > d ? c : d); return 0; }
### Prompt Please create a solution in Cpp to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); if (c > d * 2 || d > c * 2 || d >= b) printf("-1\n"); else printf("%d\n%d\n%d\n", a * 2, b * 2, c > d ? c : d); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int x1, x2, x3, xm; cin >> x1 >> x2 >> x3 >> xm; if (x2 * 2 <= xm * 2 || xm > x3 * 2 || x3 > xm * 2) cout << -1 << endl; else { cout << x1 * 2 << endl; cout << x2 * 2 << endl; cout << min(xm, x3) * 2 << endl; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int x1, x2, x3, xm; cin >> x1 >> x2 >> x3 >> xm; if (x2 * 2 <= xm * 2 || xm > x3 * 2 || x3 > xm * 2) cout << -1 << endl; else { cout << x1 * 2 << endl; cout << x2 * 2 << endl; cout << min(xm, x3) * 2 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, vm; scanf("%d %d %d %d", &v1, &v2, &v3, &vm); int flag = 0; for (int i3 = v3; i3 <= 2 * v3; i3++) { if (vm <= i3 && 2 * vm >= i3) { for (int i2 = v2; i2 <= 2 * v2; i2++) { for (int i1 = v1; i1 <= 2 * v1; i1++) { if (i1 > i2 && i2 > i3 && 2 * vm < i2) { printf("%d\n%d\n%d\n", i1, i2, i3); flag = 1; break; } } if (flag) { break; } } if (flag) { break; } } } if (!flag) { printf("-1\n"); } }
### Prompt Your challenge is to write a CPP solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, vm; scanf("%d %d %d %d", &v1, &v2, &v3, &vm); int flag = 0; for (int i3 = v3; i3 <= 2 * v3; i3++) { if (vm <= i3 && 2 * vm >= i3) { for (int i2 = v2; i2 <= 2 * v2; i2++) { for (int i1 = v1; i1 <= 2 * v1; i1++) { if (i1 > i2 && i2 > i3 && 2 * vm < i2) { printf("%d\n%d\n%d\n", i1, i2, i3); flag = 1; break; } } if (flag) { break; } } if (flag) { break; } } } if (!flag) { printf("-1\n"); } } ```
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 100009; int t, n; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int x1 = -1; for (int i = 1; i <= 10000; i++) { if (i >= a && i >= d && a * 2 >= i && d * 2 < i) { x1 = i; break; } } int x2 = -1; for (int i = 1; i <= 10000; i++) { if (i >= b && i >= d && b * 2 >= i && d * 2 < i) { x2 = i; break; } } int x3 = -1; for (int i = 1; i <= 10000; i++) { if (i >= c && i >= d && c * 2 >= i && d * 2 >= i) { x3 = i; break; } } if (x1 == -1 || x2 == -1 || x3 == -1) puts("-1"); else { while (x3 >= x2) x2++; while (x1 <= x2) x1++; if (!(x1 >= a && a * 2 >= x1 && x1 >= d && d * 2 < x1)) x1 = -1; if (!(x2 >= b && b * 2 >= x2 && x2 >= d && d * 2 < x2)) x2 = -1; if (x1 == -1 || x2 == -1 || x3 == -1) puts("-1"); else printf("%d\n%d\n%d\n", x1, x2, x3); } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 100009; int t, n; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int x1 = -1; for (int i = 1; i <= 10000; i++) { if (i >= a && i >= d && a * 2 >= i && d * 2 < i) { x1 = i; break; } } int x2 = -1; for (int i = 1; i <= 10000; i++) { if (i >= b && i >= d && b * 2 >= i && d * 2 < i) { x2 = i; break; } } int x3 = -1; for (int i = 1; i <= 10000; i++) { if (i >= c && i >= d && c * 2 >= i && d * 2 >= i) { x3 = i; break; } } if (x1 == -1 || x2 == -1 || x3 == -1) puts("-1"); else { while (x3 >= x2) x2++; while (x1 <= x2) x1++; if (!(x1 >= a && a * 2 >= x1 && x1 >= d && d * 2 < x1)) x1 = -1; if (!(x2 >= b && b * 2 >= x2 && x2 >= d && d * 2 < x2)) x2 = -1; if (x1 == -1 || x2 == -1 || x3 == -1) puts("-1"); else printf("%d\n%d\n%d\n", x1, x2, x3); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, j, k, l, m; ios_base::sync_with_stdio(0); cin.tie(0); long long a, c, b, d; cin >> a >> b >> c >> d; for (i = 1; i <= 200; i++) { for (j = 1; j <= 200; j++) { for (k = 1; k <= 200; k++) { if (i >= a && i <= 2 * a && j >= b && j <= 2 * b && k >= c && k <= 2 * c && k >= d && k <= 2 * d && i > j && j > k && j > 2 * d) { cout << i << '\n'; cout << j << '\n'; cout << k << '\n'; return 0; } } } } cout << -1 << '\n'; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n, i, j, k, l, m; ios_base::sync_with_stdio(0); cin.tie(0); long long a, c, b, d; cin >> a >> b >> c >> d; for (i = 1; i <= 200; i++) { for (j = 1; j <= 200; j++) { for (k = 1; k <= 200; k++) { if (i >= a && i <= 2 * a && j >= b && j <= 2 * b && k >= c && k <= 2 * c && k >= d && k <= 2 * d && i > j && j > k && j > 2 * d) { cout << i << '\n'; cout << j << '\n'; cout << k << '\n'; return 0; } } } } cout << -1 << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1000000007; const double EPS = 1e-9; int main() { int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; int a1, a2, a3; a3 = max(v3, vm); if (min(v3, vm) * 2 < a3) { cout << -1 << endl; return 0; } a2 = max(vm * 2 + 1, max(a3 + 1, v2)); if (v2 * 2 < a2) { cout << -1 << endl; return 0; } a1 = max(vm * 2 + 1, max(a2 + 1, v1)); if (v1 * 2 < a1) { cout << -1 << endl; return 0; } cout << a1 << '\n' << a2 << '\n' << a3 << endl; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1000000007; const double EPS = 1e-9; int main() { int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; int a1, a2, a3; a3 = max(v3, vm); if (min(v3, vm) * 2 < a3) { cout << -1 << endl; return 0; } a2 = max(vm * 2 + 1, max(a3 + 1, v2)); if (v2 * 2 < a2) { cout << -1 << endl; return 0; } a1 = max(vm * 2 + 1, max(a2 + 1, v1)); if (v1 * 2 < a1) { cout << -1 << endl; return 0; } cout << a1 << '\n' << a2 << '\n' << a3 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, vm; scanf("%d", &v1) == 0, scanf("%d", &v2) == 0, scanf("%d", &v3) == 0, scanf("%d", &vm) == 0; int rv1 = v1 * 2; int rv2 = v2 * 2; int rv3 = -1; for (decltype(1) i = 1; i < 200; i++) { if (i >= v3 && v3 * 2 >= i) { if (i >= vm && vm * 2 >= i) { if (v2 > i && rv2 > vm * 2) { rv3 = i; break; } } } } if (rv3 != -1) { printf("%d ", rv1), printf("%d ", rv2), printf("%d ", rv3); } else { printf("%d ", -1); } }
### Prompt Construct a Cpp code solution to the problem outlined: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, vm; scanf("%d", &v1) == 0, scanf("%d", &v2) == 0, scanf("%d", &v3) == 0, scanf("%d", &vm) == 0; int rv1 = v1 * 2; int rv2 = v2 * 2; int rv3 = -1; for (decltype(1) i = 1; i < 200; i++) { if (i >= v3 && v3 * 2 >= i) { if (i >= vm && vm * 2 >= i) { if (v2 > i && rv2 > vm * 2) { rv3 = i; break; } } } } if (rv3 != -1) { printf("%d ", rv1), printf("%d ", rv2), printf("%d ", rv3); } else { printf("%d ", -1); } } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, i, k, h; scanf("%d %d %d %d", &a, &b, &c, &d); for (i = a; i <= 2 * a; i++) for (k = b; k <= 2 * b; k++) for (h = c; h <= 2 * c; h++) if (h < k && k < i && d <= h && h <= 2 * d && 2 * d < k) { printf("%d\n%d\n%d", i, k, h); return 0; } printf("-1"); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, i, k, h; scanf("%d %d %d %d", &a, &b, &c, &d); for (i = a; i <= 2 * a; i++) for (k = b; k <= 2 * b; k++) for (h = c; h <= 2 * c; h++) if (h < k && k < i && d <= h && h <= 2 * d && 2 * d < k) { printf("%d\n%d\n%d", i, k, h); return 0; } printf("-1"); return 0; } ```
#include <bits/stdc++.h> using namespace std; signed main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long fat, mot, son, mas; cin >> fat >> mot >> son >> mas; long long ans1, ans2, ans3; long long flag = 0, flag2 = 0, flag3 = 0; for (__typeof(2 * son + 1) i = (son) - ((son) > (2 * son + 1)); i != (2 * son + 1) - ((son) > (2 * son + 1)); i += 1 - 2 * ((son) > (2 * son + 1))) { if (i >= mas && 2 * mas >= i) { flag = 1; for (__typeof(2 * mot + 1) j = (mot) - ((mot) > (2 * mot + 1)); j != (2 * mot + 1) - ((mot) > (2 * mot + 1)); j += 1 - 2 * ((mot) > (2 * mot + 1))) { if (j > 2 * mas && j > i) { flag2 = 1; for (__typeof(2 * fat + 1) k = (fat) - ((fat) > (2 * fat + 1)); k != (2 * fat + 1) - ((fat) > (2 * fat + 1)); k += 1 - 2 * ((fat) > (2 * fat + 1))) { if (k > 2 * mas && k > i && k > j) { ans1 = i; ans2 = j; ans3 = k; cout << ans3 << '\n' << ans2 << '\n' << ans1 << '\n'; return 0; } } } } } } cout << "-1"; }
### Prompt Please create a solution in Cpp to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; signed main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long fat, mot, son, mas; cin >> fat >> mot >> son >> mas; long long ans1, ans2, ans3; long long flag = 0, flag2 = 0, flag3 = 0; for (__typeof(2 * son + 1) i = (son) - ((son) > (2 * son + 1)); i != (2 * son + 1) - ((son) > (2 * son + 1)); i += 1 - 2 * ((son) > (2 * son + 1))) { if (i >= mas && 2 * mas >= i) { flag = 1; for (__typeof(2 * mot + 1) j = (mot) - ((mot) > (2 * mot + 1)); j != (2 * mot + 1) - ((mot) > (2 * mot + 1)); j += 1 - 2 * ((mot) > (2 * mot + 1))) { if (j > 2 * mas && j > i) { flag2 = 1; for (__typeof(2 * fat + 1) k = (fat) - ((fat) > (2 * fat + 1)); k != (2 * fat + 1) - ((fat) > (2 * fat + 1)); k += 1 - 2 * ((fat) > (2 * fat + 1))) { if (k > 2 * mas && k > i && k > j) { ans1 = i; ans2 = j; ans3 = k; cout << ans3 << '\n' << ans2 << '\n' << ans1 << '\n'; return 0; } } } } } } cout << "-1"; } ```
#include <bits/stdc++.h> using namespace std; template <class T> T sqr(T x) { return x * x; } long double pi = 3.1415926535897932384626433832795; long long mod = 1e9 + 7; const int N = 3e5 + 10; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long bin_pow(long long n, long long p) { long long res = 1, a = n; while (p) { if (p & 1) res *= a, res %= mod; a *= a; a %= mod; p >>= 1; } return res; } long long _gcd(long long a, long long b, long long& x, long long& y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long d = _gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } int n, a[N]; int main() { int a, b, c, d; cin >> a >> b >> c >> d; for (int i = a; i <= a * 2; i++) { for (int j = b; j <= b * 2; j++) { for (int k = c; k <= c * 2; k++) { bool ok = 1; ok &= k >= d; ok &= k <= 2 * d; ok &= k * 2 < j; ok &= i > j; ok &= j > k; if (ok) { cout << i << endl << j << endl << k; return 0; } } } } cout << -1; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> T sqr(T x) { return x * x; } long double pi = 3.1415926535897932384626433832795; long long mod = 1e9 + 7; const int N = 3e5 + 10; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long bin_pow(long long n, long long p) { long long res = 1, a = n; while (p) { if (p & 1) res *= a, res %= mod; a *= a; a %= mod; p >>= 1; } return res; } long long _gcd(long long a, long long b, long long& x, long long& y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long d = _gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } int n, a[N]; int main() { int a, b, c, d; cin >> a >> b >> c >> d; for (int i = a; i <= a * 2; i++) { for (int j = b; j <= b * 2; j++) { for (int k = c; k <= c * 2; k++) { bool ok = 1; ok &= k >= d; ok &= k <= 2 * d; ok &= k * 2 < j; ok &= i > j; ok &= j > k; if (ok) { cout << i << endl << j << endl << k; return 0; } } } } cout << -1; return 0; } ```
#include <bits/stdc++.h> using namespace std; int fact[100005]; int invfact[100005]; bool prime[100005]; vector<long long> divisor[100005]; long long pw(long long a, long long b) { long long res = 1; while (b) { if (b & 1ll) { res = res * a % 1000000007; } b >>= 1; a = a * a % 1000000007; } return res; } void CombiPre() { fact[0] = 1; for (int i = 1; i < 100005; ++i) { fact[i] = 1ll * i * fact[i - 1] % 1000000007; } invfact[100005 - 1] = pw(fact[100005 - 1], 1000000007 - 2); for (int i = 100005 - 2; i >= 0; --i) { invfact[i] = 1ll * invfact[i + 1] * (i + 1) % 1000000007; } } long long C(int n, int k) { return fact[n] * 1ll * invfact[n - k] % 1000000007 * invfact[k] % 1000000007; } void primePre() { prime[1] = 1; prime[0] = 1; for (long long i = 2; i * i <= 100005; i++) { for (long long j = i + i; j <= 100005; j += i) { prime[j] = 1; } } } bool like(int x, int y) { if (2 * x >= y) return true; else return false; } bool climb(int x, int y) { if (x <= y) return true; return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; for (int i = (1); i <= (400); ++i) { for (int j = (i + 1); j <= (400); ++j) { for (int k = (j + 1); k <= (400); ++k) { bool b1, b2, b3, b4, b5, b6, b7, b8; b1 = climb(v1, k); b2 = climb(v2, j); b3 = climb(v3, i); b4 = like(v1, k); b5 = like(v2, j); b6 = like(v3, i); b7 = climb(vm, i) && climb(vm, j) && climb(vm, k); b8 = like(vm, i) && !like(vm, j) && !like(vm, k); if (b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8) { cout << k << endl << j << endl << i << endl; return 0; } } } } cout << -1 << endl; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int fact[100005]; int invfact[100005]; bool prime[100005]; vector<long long> divisor[100005]; long long pw(long long a, long long b) { long long res = 1; while (b) { if (b & 1ll) { res = res * a % 1000000007; } b >>= 1; a = a * a % 1000000007; } return res; } void CombiPre() { fact[0] = 1; for (int i = 1; i < 100005; ++i) { fact[i] = 1ll * i * fact[i - 1] % 1000000007; } invfact[100005 - 1] = pw(fact[100005 - 1], 1000000007 - 2); for (int i = 100005 - 2; i >= 0; --i) { invfact[i] = 1ll * invfact[i + 1] * (i + 1) % 1000000007; } } long long C(int n, int k) { return fact[n] * 1ll * invfact[n - k] % 1000000007 * invfact[k] % 1000000007; } void primePre() { prime[1] = 1; prime[0] = 1; for (long long i = 2; i * i <= 100005; i++) { for (long long j = i + i; j <= 100005; j += i) { prime[j] = 1; } } } bool like(int x, int y) { if (2 * x >= y) return true; else return false; } bool climb(int x, int y) { if (x <= y) return true; return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; for (int i = (1); i <= (400); ++i) { for (int j = (i + 1); j <= (400); ++j) { for (int k = (j + 1); k <= (400); ++k) { bool b1, b2, b3, b4, b5, b6, b7, b8; b1 = climb(v1, k); b2 = climb(v2, j); b3 = climb(v3, i); b4 = like(v1, k); b5 = like(v2, j); b6 = like(v3, i); b7 = climb(vm, i) && climb(vm, j) && climb(vm, k); b8 = like(vm, i) && !like(vm, j) && !like(vm, k); if (b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8) { cout << k << endl << j << endl << i << endl; return 0; } } } } cout << -1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (c > 2 * d || d > 2 * c || d >= b) cout << -1 << endl; else cout << 2 * a << endl << 2 * b << endl << max(c, d); return 0; }
### Prompt In CPP, your task is to solve the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (c > 2 * d || d > 2 * c || d >= b) cout << -1 << endl; else cout << 2 * a << endl << 2 * b << endl << max(c, d); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; int n; for (int i = 0; i < 3; i++) { int t; cin >> t; v.push_back(t); } cin >> n; for (int i = 0; i <= 200; i++) { for (int j = i + 1; j <= 200; j++) { for (int k = j + 1; k <= 200; k++) { if (k < max(v[0], n) or j < max(v[1], n) or i < max(v[2], n)) continue; if (k > v[0] * 2 or j > v[1] * 2 or i > v[2] * 2) continue; if (k <= n * 2 or j <= n * 2) continue; if (i > n * 2) continue; cout << k << endl << j << endl << i; return 0; } } } cout << -1; }
### Prompt Please formulate a cpp solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; int n; for (int i = 0; i < 3; i++) { int t; cin >> t; v.push_back(t); } cin >> n; for (int i = 0; i <= 200; i++) { for (int j = i + 1; j <= 200; j++) { for (int k = j + 1; k <= 200; k++) { if (k < max(v[0], n) or j < max(v[1], n) or i < max(v[2], n)) continue; if (k > v[0] * 2 or j > v[1] * 2 or i > v[2] * 2) continue; if (k <= n * 2 or j <= n * 2) continue; if (i > n * 2) continue; cout << k << endl << j << endl << i; return 0; } } } cout << -1; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> using V = vector<T>; bool ok(int a, int b) { return a <= b && 2 * a >= b; } int main() { ios::sync_with_stdio(0); cin.tie(0); int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; for (int c1 = 1; c1 <= 400; ++c1) { for (int c2 = c1 + 1; c2 <= 400; ++c2) { for (int c3 = c2 + 1; c3 <= 400; ++c3) { if (ok(v1, c3) && ok(v2, c2) && ok(v3, c1)) { if (vm <= c1 && vm <= c2 && vm <= c3 && ok(vm, c1) && !ok(vm, c2) && !ok(vm, c3)) { cout << c3 << endl << c2 << endl << c1 << endl; return 0; } } } } } cout << -1 << endl; }
### Prompt Please formulate a Cpp solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> using V = vector<T>; bool ok(int a, int b) { return a <= b && 2 * a >= b; } int main() { ios::sync_with_stdio(0); cin.tie(0); int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; for (int c1 = 1; c1 <= 400; ++c1) { for (int c2 = c1 + 1; c2 <= 400; ++c2) { for (int c3 = c2 + 1; c3 <= 400; ++c3) { if (ok(v1, c3) && ok(v2, c2) && ok(v3, c1)) { if (vm <= c1 && vm <= c2 && vm <= c3 && ok(vm, c1) && !ok(vm, c2) && !ok(vm, c3)) { cout << c3 << endl << c2 << endl << c1 << endl; return 0; } } } } } cout << -1 << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int s, m, l; s = c; l = a; for (int i = c; i <= 2 * c; i++) { for (int j = b; j <= 2 * b; j++) { for (int k = a; k <= 2 * a; k++) { if (d <= i && d <= j && d <= k) { if (i != j && j != k && i != k && 2 * d >= i && 2 * d < j && 2 * d < k) { cout << k << endl; cout << j << endl; cout << i << endl; return 0; } } } } } cout << -1 << endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int s, m, l; s = c; l = a; for (int i = c; i <= 2 * c; i++) { for (int j = b; j <= 2 * b; j++) { for (int k = a; k <= 2 * a; k++) { if (d <= i && d <= j && d <= k) { if (i != j && j != k && i != k && 2 * d >= i && 2 * d < j && 2 * d < k) { cout << k << endl; cout << j << endl; cout << i << endl; return 0; } } } } } cout << -1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int main() { int v1, v2, v3, vm; scanf("%d%d%d%d", &v1, &v2, &v3, &vm); for (int i = v3; i <= 2 * v3; i++) { if (vm <= i && 2 * vm >= i) for (int j = max(i * 2 + 1, v2); j <= 2 * v2; j++) { for (int k = max(j + 1, v1); k <= 2 * v1; k++) { return 0 * printf("%d\n%d\n%d\n", k, j, i); } } } printf("-1\n"); return 0; }
### Prompt Develop a solution in CPP to the problem described below: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int main() { int v1, v2, v3, vm; scanf("%d%d%d%d", &v1, &v2, &v3, &vm); for (int i = v3; i <= 2 * v3; i++) { if (vm <= i && 2 * vm >= i) for (int j = max(i * 2 + 1, v2); j <= 2 * v2; j++) { for (int k = max(j + 1, v1); k <= 2 * v1; k++) { return 0 * printf("%d\n%d\n%d\n", k, j, i); } } } printf("-1\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; while (~scanf("%d%d%d%d", &a, &b, &c, &d)) { if (d <= 2 * c && 2 * d >= c && d < b) { c = max(c, d); if (2 * c >= b) { b = 2 * c + 1; } a = max(a, b + 1); printf("%d\n%d\n%d\n", a, b, c); } else { printf("-1\n"); } } return 0; }
### Prompt In Cpp, your task is to solve the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; while (~scanf("%d%d%d%d", &a, &b, &c, &d)) { if (d <= 2 * c && 2 * d >= c && d < b) { c = max(c, d); if (2 * c >= b) { b = 2 * c + 1; } a = max(a, b + 1); printf("%d\n%d\n%d\n", a, b, c); } else { printf("-1\n"); } } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; const int N = (int)1e5 + 5; const int INF = 0x3f3f3f3f; int a[10]; bool ok(int a, int b) { return (2 * a >= b); } int main() { cin >> a[1] >> a[2] >> a[3] >> a[4]; for (int f = a[1]; f <= a[1] * 2; f++) { for (int m = a[2]; m <= a[2] * 2; m++) { for (int s = a[3]; s <= a[3] * 2; s++) { if (f > m && m > s && a[4] <= min({s, m, f}) && ok(a[4], s) && !ok(a[4], m) && !ok(a[4], f)) { cout << f << endl; cout << m << endl; cout << s << endl; return 0; } } } } puts("-1"); }
### Prompt Your task is to create a Cpp solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; const int N = (int)1e5 + 5; const int INF = 0x3f3f3f3f; int a[10]; bool ok(int a, int b) { return (2 * a >= b); } int main() { cin >> a[1] >> a[2] >> a[3] >> a[4]; for (int f = a[1]; f <= a[1] * 2; f++) { for (int m = a[2]; m <= a[2] * 2; m++) { for (int s = a[3]; s <= a[3] * 2; s++) { if (f > m && m > s && a[4] <= min({s, m, f}) && ok(a[4], s) && !ok(a[4], m) && !ok(a[4], f)) { cout << f << endl; cout << m << endl; cout << s << endl; return 0; } } } } puts("-1"); } ```
#include <bits/stdc++.h> using namespace std; int v1, v2, v3, vm; int main() { scanf("%d%d%d%d", &v1, &v2, &v3, &vm); int flag = 0; for (int i = v3; i <= 2 * v3; i++) { if (vm <= i && 2 * vm >= i) { for (int j = v2; j <= 2 * v2; j++) { if (vm <= j && 2 * vm < j) { for (int k = v1; k <= 2 * v1; k++) { if (vm <= k && vm * 2 < k) { if (k > j && j > i) { printf("%d %d %d\n", k, j, i); flag = 1; break; } } } } if (flag) break; } } if (flag) break; } if (!flag) printf("-1\n"); return 0; }
### Prompt Please create a solution in Cpp to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int v1, v2, v3, vm; int main() { scanf("%d%d%d%d", &v1, &v2, &v3, &vm); int flag = 0; for (int i = v3; i <= 2 * v3; i++) { if (vm <= i && 2 * vm >= i) { for (int j = v2; j <= 2 * v2; j++) { if (vm <= j && 2 * vm < j) { for (int k = v1; k <= 2 * v1; k++) { if (vm <= k && vm * 2 < k) { if (k > j && j > i) { printf("%d %d %d\n", k, j, i); flag = 1; break; } } } } if (flag) break; } } if (flag) break; } if (!flag) printf("-1\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a, b, c, m; cin >> a >> b >> c >> m; for (int i = 0, ThxDem = 300; i < ThxDem; ++i) { for (int j = 0, ThxDem = i; j < ThxDem; ++j) { for (int k = 0, ThxDem = j; k < ThxDem; ++k) { if (a <= i && 2 * a >= i && b <= j && 2 * b >= j && c <= k && 2 * c >= k && m <= k && 2 * m >= k && 2 * m < j && 2 * m < i) { cout << i << endl << j << endl << k << endl; return 0; } } } } cout << -1 << endl; }
### Prompt Your task is to create a cpp solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a, b, c, m; cin >> a >> b >> c >> m; for (int i = 0, ThxDem = 300; i < ThxDem; ++i) { for (int j = 0, ThxDem = i; j < ThxDem; ++j) { for (int k = 0, ThxDem = j; k < ThxDem; ++k) { if (a <= i && 2 * a >= i && b <= j && 2 * b >= j && c <= k && 2 * c >= k && m <= k && 2 * m >= k && 2 * m < j && 2 * m < i) { cout << i << endl << j << endl << k << endl; return 0; } } } } cout << -1 << endl; } ```
#include <bits/stdc++.h> const long long MX = 100 * 1000 * 2 + 1000, inf = 0x7FFFFFFF, mod = 1000 * 1000 * 1000 + 7; using namespace std; long long n, siz[5]; pair<long long, long long> inp[5]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (long long i = 0; i < 4; i++) { inp[i].second = inf; cin >> siz[i]; if (i == 3) { continue; } inp[i].first = max(inp[i].first, siz[i]); inp[i].second = min(inp[i].second, siz[i] * 2); } inp[2].first = max(inp[2].first, siz[3]); inp[2].second = min(inp[2].second, siz[3] * 2); inp[0].first = max(inp[0].first, siz[3] * 2 + 1); inp[1].first = max(inp[1].first, siz[3] * 2 + 1); inp[1].first = max(inp[1].first, inp[2].first + 1); inp[0].first = max(inp[0].first, inp[1].first + 1); bool tf = 1; for (long long i = 0; i < 3; i++) { if (inp[i].first > inp[i].second) { tf = 0; } } if (tf) { cout << inp[0].first << endl; cout << inp[1].first << endl; cout << inp[2].first << endl; } else { cout << -1 << endl; } }
### Prompt Please provide a cpp coded solution to the problem described below: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> const long long MX = 100 * 1000 * 2 + 1000, inf = 0x7FFFFFFF, mod = 1000 * 1000 * 1000 + 7; using namespace std; long long n, siz[5]; pair<long long, long long> inp[5]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (long long i = 0; i < 4; i++) { inp[i].second = inf; cin >> siz[i]; if (i == 3) { continue; } inp[i].first = max(inp[i].first, siz[i]); inp[i].second = min(inp[i].second, siz[i] * 2); } inp[2].first = max(inp[2].first, siz[3]); inp[2].second = min(inp[2].second, siz[3] * 2); inp[0].first = max(inp[0].first, siz[3] * 2 + 1); inp[1].first = max(inp[1].first, siz[3] * 2 + 1); inp[1].first = max(inp[1].first, inp[2].first + 1); inp[0].first = max(inp[0].first, inp[1].first + 1); bool tf = 1; for (long long i = 0; i < 3; i++) { if (inp[i].first > inp[i].second) { tf = 0; } } if (tf) { cout << inp[0].first << endl; cout << inp[1].first << endl; cout << inp[2].first << endl; } else { cout << -1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int a, b, c, d; cin >> a >> b >> c >> d; for (size_t i = 1; i <= 200; i++) { if (a <= i && 2 * a >= i) for (size_t j = 1; j < i; j++) { if (b <= j && 2 * b >= j) for (size_t l = 1; l < j; l++) { if (c <= l && 2 * c >= l) if (2 * d < i && 2 * d < j && 2 * d >= l && d <= l) { cout << i << endl << j << endl << l << endl; return 0; } } } } cout << -1; }
### Prompt Develop a solution in CPP to the problem described below: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int a, b, c, d; cin >> a >> b >> c >> d; for (size_t i = 1; i <= 200; i++) { if (a <= i && 2 * a >= i) for (size_t j = 1; j < i; j++) { if (b <= j && 2 * b >= j) for (size_t l = 1; l < j; l++) { if (c <= l && 2 * c >= l) if (2 * d < i && 2 * d < j && 2 * d >= l && d <= l) { cout << i << endl << j << endl << l << endl; return 0; } } } } cout << -1; } ```
#include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; for (int a1 = 1; a1 <= 210; a1++) for (int a2 = 1; a2 <= 210; a2++) for (int a3 = 1; a3 <= 210; a3++) { if (a1 <= 2 * v1 && 2 * v1 <= 2 * a1 && a2 <= 2 * v2 && 2 * v2 <= 2 * a2 && a3 <= 2 * v3 && 2 * v3 <= 2 * a3 && a3 <= 2 * vm && 2 * vm <= 2 * a3 && a2 > 2 * vm && a1 > 2 * vm && a1 > a2 && a2 > a3) { cout << a1 << " " << a2 << " " << a3; return 0; } } cout << -1; return 0; }
### Prompt Develop a solution in cpp to the problem described below: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; for (int a1 = 1; a1 <= 210; a1++) for (int a2 = 1; a2 <= 210; a2++) for (int a3 = 1; a3 <= 210; a3++) { if (a1 <= 2 * v1 && 2 * v1 <= 2 * a1 && a2 <= 2 * v2 && 2 * v2 <= 2 * a2 && a3 <= 2 * v3 && 2 * v3 <= 2 * a3 && a3 <= 2 * vm && 2 * vm <= 2 * a3 && a2 > 2 * vm && a1 > 2 * vm && a1 > a2 && a2 > a3) { cout << a1 << " " << a2 << " " << a3; return 0; } } cout << -1; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, v4; cin >> v1 >> v2 >> v3 >> v4; if (v3 * 2 < v4 || v3 > 2 * v4 || v4 >= v2) { cout << "-1" << endl; return 0; } cout << v1 * 2 << endl; cout << v2 * 2 << endl; cout << max(v3, v4) << endl; }
### Prompt In CPP, your task is to solve the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, v4; cin >> v1 >> v2 >> v3 >> v4; if (v3 * 2 < v4 || v3 > 2 * v4 || v4 >= v2) { cout << "-1" << endl; return 0; } cout << v1 * 2 << endl; cout << v2 * 2 << endl; cout << max(v3, v4) << endl; } ```
#include <bits/stdc++.h> using namespace std; int a, b, c, d, ans = 0; int aa, bb, cc; int main() { cin >> a >> b >> c >> d; cc = max(c, d); bb = max(2 * d + 1, b); bb = max(cc + 1, bb); aa = max(a, bb + 1); if (c * 2 < cc || b * 2 < bb || a * 2 < aa || d * 2 < cc) cout << -1 << endl; else cout << aa << "\n" << bb << "\n" << cc << endl; return 0; }
### Prompt Generate a Cpp solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a, b, c, d, ans = 0; int aa, bb, cc; int main() { cin >> a >> b >> c >> d; cc = max(c, d); bb = max(2 * d + 1, b); bb = max(cc + 1, bb); aa = max(a, bb + 1); if (c * 2 < cc || b * 2 < bb || a * 2 < aa || d * 2 < cc) cout << -1 << endl; else cout << aa << "\n" << bb << "\n" << cc << endl; return 0; } ```
#include <bits/stdc++.h> int32_t main() { std::ios::sync_with_stdio(false); int a, b, c, d; std::cin >> a >> b >> c >> d; for (int i = 0; i <= 200; ++i) { for (int j = i + 1; j <= 200; ++j) { for (int q = j + 1; q <= 200; ++q) { if (i >= c && 2 * c >= i && j >= b && 2 * b >= j && q >= a && 2 * a >= q) { if (i >= d && j >= d && q >= d && 2 * d >= i && 2 * d < j && 2 * d < q) { std::cout << q << ' ' << j << ' ' << i; return 0; } } } } } std::cout << -1; }
### Prompt Please create a solution in cpp to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> int32_t main() { std::ios::sync_with_stdio(false); int a, b, c, d; std::cin >> a >> b >> c >> d; for (int i = 0; i <= 200; ++i) { for (int j = i + 1; j <= 200; ++j) { for (int q = j + 1; q <= 200; ++q) { if (i >= c && 2 * c >= i && j >= b && 2 * b >= j && q >= a && 2 * a >= q) { if (i >= d && j >= d && q >= d && 2 * d >= i && 2 * d < j && 2 * d < q) { std::cout << q << ' ' << j << ' ' << i; return 0; } } } } } std::cout << -1; } ```
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-11; const long long INF = 0x3f3f3f3f3f3f3f3f; const long long N = 100010; int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); for (int i = a; i <= a * 2; i++) for (int j = b; j <= b * 2; j++) { if (i == j) break; for (int k = c; k <= c * 2; k++) { if (j == k) break; if (i > 2 * d && j > 2 * d && d <= k && k <= 2 * d) { printf("%d\n%d\n%d\n", i, j, k); return 0; } } } printf("-1\n"); return 0; }
### Prompt Please create a solution in cpp to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-11; const long long INF = 0x3f3f3f3f3f3f3f3f; const long long N = 100010; int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); for (int i = a; i <= a * 2; i++) for (int j = b; j <= b * 2; j++) { if (i == j) break; for (int k = c; k <= c * 2; k++) { if (j == k) break; if (i > 2 * d && j > 2 * d && d <= k && k <= 2 * d) { printf("%d\n%d\n%d\n", i, j, k); return 0; } } } printf("-1\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (d > c * 2 || c > d * 2) { cout << -1 << endl; return 0; } if (d * 2 >= b || max(c, d) >= b) { b *= 2; a *= 2; } if (d * 2 >= b || max(c, d) >= b) { cout << -1 << endl; return 0; } cout << a << " " << b << " " << max(c, d) << endl; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (d > c * 2 || c > d * 2) { cout << -1 << endl; return 0; } if (d * 2 >= b || max(c, d) >= b) { b *= 2; a *= 2; } if (d * 2 >= b || max(c, d) >= b) { cout << -1 << endl; return 0; } cout << a << " " << b << " " << max(c, d) << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; bool fit(long long a, long long b) { return a <= b; } bool like(long long a, long long b) { return 2 * a >= b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long v1, v2, v3, v4; cin >> v1 >> v2 >> v3 >> v4; for (long long a = 1; a <= 201; a++) { for (long long b = 1; b <= 201; b++) { for (long long c = 1; c <= 201; c++) { if (a > b && b > c) { if (fit(v1, a) && like(v1, a) && fit(v2, b) && like(v2, b) && fit(v3, c) && like(v3, c) && fit(v4, a) && fit(v4, b) && fit(v4, c) && like(v4, c) && like(v4, a) == false && like(v4, b) == false) { cout << a << endl << b << endl << c << endl; return 0; } } } } } cout << -1 << endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool fit(long long a, long long b) { return a <= b; } bool like(long long a, long long b) { return 2 * a >= b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long v1, v2, v3, v4; cin >> v1 >> v2 >> v3 >> v4; for (long long a = 1; a <= 201; a++) { for (long long b = 1; b <= 201; b++) { for (long long c = 1; c <= 201; c++) { if (a > b && b > c) { if (fit(v1, a) && like(v1, a) && fit(v2, b) && like(v2, b) && fit(v3, c) && like(v3, c) && fit(v4, a) && fit(v4, b) && fit(v4, c) && like(v4, c) && like(v4, a) == false && like(v4, b) == false) { cout << a << endl << b << endl << c << endl; return 0; } } } } } cout << -1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int a, b, c, d; cin >> a >> b >> c >> d; for (int i = 1; i <= 200; i++) { for (int j = 1; j <= 200; j++) { for (int z = 1; z <= 200; z++) { if (i >= a && j >= b && z >= c && z >= d && i > j && j > z) { if (i <= a * 2 && i > d * 2 && j <= b * 2 && j > d * 2 && z <= c * 2 && z <= d * 2) { cout << i << '\n' << j << '\n' << z; return 0; } } } } } cout << -1; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int a, b, c, d; cin >> a >> b >> c >> d; for (int i = 1; i <= 200; i++) { for (int j = 1; j <= 200; j++) { for (int z = 1; z <= 200; z++) { if (i >= a && j >= b && z >= c && z >= d && i > j && j > z) { if (i <= a * 2 && i > d * 2 && j <= b * 2 && j > d * 2 && z <= c * 2 && z <= d * 2) { cout << i << '\n' << j << '\n' << z; return 0; } } } } } cout << -1; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); a *= 2; b *= 2; if (2 * d >= b or max(c, d) >= b or 2 * min(c, d) < max(c, d)) return printf("-1\n"), 0; for (int i = max(c, d); i <= 2 * min(c, d); ++i) { if (2 * c >= i and i >= c and 2 * d >= i and i >= d) { printf("%d\n%d\n%d\n", a, b, i); return 0; } } printf("-1"); }
### Prompt Please provide a Cpp coded solution to the problem described below: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); a *= 2; b *= 2; if (2 * d >= b or max(c, d) >= b or 2 * min(c, d) < max(c, d)) return printf("-1\n"), 0; for (int i = max(c, d); i <= 2 * min(c, d); ++i) { if (2 * c >= i and i >= c and 2 * d >= i and i >= d) { printf("%d\n%d\n%d\n", a, b, i); return 0; } } printf("-1"); } ```
#include <bits/stdc++.h> int main() { int max, a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); if (d >= b || 2 * d < c || 2 * c < d) { printf("-1"); } else { max = c > d ? c : d; printf("%d\n%d\n%d", 2 * a, 2 * b, max); } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> int main() { int max, a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); if (d >= b || 2 * d < c || 2 * c < d) { printf("-1"); } else { max = c > d ? c : d; printf("%d\n%d\n%d", 2 * a, 2 * b, max); } return 0; } ```
#include <bits/stdc++.h> using namespace std; void car(int a, int b, int c, int m) { int s, p, d; s = c; if (m > 2 * c) { cout << -1; } else if (2 * m < c) { cout << -1; } else if (m > c) { s = m; p = b; if (b > 2 * m) { p = b; if (2 * a > p) { d = 2 * a; cout << d << "\n" << p << "\n" << s; } else { cout << -1; } } else if (2 * b > 2 * m) { p = 2 * m + 1; if (2 * a > p) { d = 2 * a; cout << d << "\n" << p << "\n" << s; } else { cout << -1; } } else { cout << -1; } } else { s = c; p = b; if (b > 2 * m) { p = b; if (2 * a > p) { d = 2 * a; cout << d << "\n" << p << "\n" << s; } else { cout << -1; } } else if (2 * b > 2 * m) { p = 2 * m + 1; if (2 * a > p) { d = 2 * a; cout << d << "\n" << p << "\n" << s; } else { cout << -1; } } else { cout << -1; } } } int main() { int a, b, c, m; cin >> a >> b >> c >> m; car(a, b, c, m); }
### Prompt Create a solution in CPP for the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void car(int a, int b, int c, int m) { int s, p, d; s = c; if (m > 2 * c) { cout << -1; } else if (2 * m < c) { cout << -1; } else if (m > c) { s = m; p = b; if (b > 2 * m) { p = b; if (2 * a > p) { d = 2 * a; cout << d << "\n" << p << "\n" << s; } else { cout << -1; } } else if (2 * b > 2 * m) { p = 2 * m + 1; if (2 * a > p) { d = 2 * a; cout << d << "\n" << p << "\n" << s; } else { cout << -1; } } else { cout << -1; } } else { s = c; p = b; if (b > 2 * m) { p = b; if (2 * a > p) { d = 2 * a; cout << d << "\n" << p << "\n" << s; } else { cout << -1; } } else if (2 * b > 2 * m) { p = 2 * m + 1; if (2 * a > p) { d = 2 * a; cout << d << "\n" << p << "\n" << s; } else { cout << -1; } } else { cout << -1; } } } int main() { int a, b, c, m; cin >> a >> b >> c >> m; car(a, b, c, m); } ```
#include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; for (auto i = 200; i >= 0; i--) { for (auto j = 200; j >= 0; j--) { for (auto k = 200; k >= 0; k--) { if (i > j && j > k && i >= v1 && 2 * v1 >= i && !(i >= vm && 2 * vm >= i) && j >= v2 && 2 * v2 >= j && !(j >= vm && 2 * vm >= j) && k >= v3 && 2 * v3 >= k && k >= vm && 2 * vm >= k) { cout << i << endl << j << endl << k << endl; return 0; } } } } cout << -1 << endl; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int v1, v2, v3, vm; cin >> v1 >> v2 >> v3 >> vm; for (auto i = 200; i >= 0; i--) { for (auto j = 200; j >= 0; j--) { for (auto k = 200; k >= 0; k--) { if (i > j && j > k && i >= v1 && 2 * v1 >= i && !(i >= vm && 2 * vm >= i) && j >= v2 && 2 * v2 >= j && !(j >= vm && 2 * vm >= j) && k >= v3 && 2 * v3 >= k && k >= vm && 2 * vm >= k) { cout << i << endl << j << endl << k << endl; return 0; } } } } cout << -1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; void out() { cout << -1; exit(0); } int main() { int v1, v2, v3, v4; cin >> v1 >> v2 >> v3 >> v4; for (int s1 = 1; s1 < 220; s1++) { for (int s2 = 1; s2 < s1; s2++) { for (int s3 = 1; s3 < s2; s3++) { if (v1 <= s1 and v1 * 2 >= s1) if (v2 <= s2 and v2 * 2 >= s2) if (v3 <= s3 and v3 * 2 >= s3) if (v4 <= s3 and v4 * 2 >= s3) if (v4 * 2 < s2) { cout << s1 << "\n" << s2 << "\n" << s3; return 0; } } } } cout << -1; return 0; }
### Prompt In Cpp, your task is to solve the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void out() { cout << -1; exit(0); } int main() { int v1, v2, v3, v4; cin >> v1 >> v2 >> v3 >> v4; for (int s1 = 1; s1 < 220; s1++) { for (int s2 = 1; s2 < s1; s2++) { for (int s3 = 1; s3 < s2; s3++) { if (v1 <= s1 and v1 * 2 >= s1) if (v2 <= s2 and v2 * 2 >= s2) if (v3 <= s3 and v3 * 2 >= s3) if (v4 <= s3 and v4 * 2 >= s3) if (v4 * 2 < s2) { cout << s1 << "\n" << s2 << "\n" << s3; return 0; } } } } cout << -1; return 0; } ```
#include <bits/stdc++.h> using namespace std; bool comp(long long x, long long y) { if (x >= y) return true; return false; } bool compare(const pair<long long, long long> &i, const pair<long long, long long> &j) { return i.first <= j.first; } bool compare1(const pair<long long, long long> &i, const pair<long long, long long> &j) { return i.first > j.first; } string decitobinary(long long n, long long bit) { char c[bit]; memset(c, '0', sizeof(c)); long long i = 0; while (n > 0) { c[i] = n % 2 + 48; n = n / 2; i++; } string s = ""; for (long long j = bit - 1; j >= 0; j--) s += c[j]; return s; } long long binarytodeci(string n) { string num = n; long long dec_value = 0; long long base = 1; long long len = num.length(); for (long long i = len - 1; i >= 0; i--) { if (num[i] == '1') dec_value += base; base = base * 2; } return dec_value; } long double intlog(long double base, long double x) { return (log(x) / log(base)); } long long fact(long long n) { long long res = 1; for (long long i = 2; i <= n; i++) res = res * i; return res; } long long ncr(long long n, long long k) { long long res = 1; if (k > n - k) k = n - k; for (long long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } bool ispalindrome(string str, long long l, long long h) { while (h > l) { if (str[l++] != str[h--]) return 0; } return 1; } long long compute_hash(string s) { long long p = 31, hash = 0, po = 1; for (char c : s) { hash += (c - 'a' + 1) * po % 1000000007; po = (po * p) % 1000000007; } return hash; } vector<vector<long long>> group_identical_strings(vector<string> const &s) { long long n = s.size(); vector<pair<long long, long long>> hashes(n); for (long long i = 0; i < n; i++) hashes[i] = {compute_hash(s[i]), i}; sort(hashes.begin(), hashes.end()); vector<vector<long long>> groups; for (long long i = 0; i < n; i++) { if (i == 0 || hashes[i].first != hashes[i - 1].first) groups.emplace_back(); groups.back().push_back(hashes[i].second); } return groups; } bool isarraysort(long long a[], long long i, long long j) { if (j == i) return 1; if (a[j] < a[j - 1]) return 0; return isarraysort(a, i, j - 1); } void sieve(long long n, long long prime[]) { for (long long i = 1; i <= n; i++) prime[i] = 1; for (long long p = 2; p * p <= n; p++) { if (prime[p] == 1) { for (long long i = p * p; i <= n; i += p) prime[i] = 0; } } } bool isprime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return 0; } return 1; } long long pow_mod(long long base, long long exp) { long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % 1000000007; exp = exp >> 1; base = (base * base) % 1000000007; } return res; } const long long N = 1e6 + 3; vector<long long> adj[N]; bool visited[N]; long long depth[N] = {0}; long long size[N] = {0}; long long parent[N]; void dfs(long long v) { visited[v] = true; for (long long u : adj[v]) { if (!visited[u]) { parent[u] = v; depth[u] = depth[v] + 1; dfs(u); } } } long long time_in[N], time_out[N]; long long dfs_timer = 0; long long color[N]; void dfs_time(long long v) { time_in[v] = ++dfs_timer; color[v] = 1; for (long long u : adj[v]) { if (color[u] == 0) { dfs_time(u); } } color[v] = 2; time_out[v] = ++dfs_timer; } long long count1[N]; void numberOfNodes(long long s) { count1[s] = 1; for (long long u : adj[s]) { if (u == parent[s]) continue; numberOfNodes(u); count1[s] += count1[u]; } } void solve() { long long f, m, s, g; cin >> f >> m >> s >> g; long long a, b, c; if (2 * f - max(f, 2 * g + 1) >= 0 && 2 * m - max(m, 2 * g + 1) >= 0 && min(2 * s, 2 * g) - max(s, g) >= 0) { a = 2 * f; b = 2 * m; c = min(2 * s, 2 * g); cout << a << '\n' << b << '\n' << c; } else cout << -1; } int32_t main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) { solve(); } }
### Prompt In Cpp, your task is to solve the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool comp(long long x, long long y) { if (x >= y) return true; return false; } bool compare(const pair<long long, long long> &i, const pair<long long, long long> &j) { return i.first <= j.first; } bool compare1(const pair<long long, long long> &i, const pair<long long, long long> &j) { return i.first > j.first; } string decitobinary(long long n, long long bit) { char c[bit]; memset(c, '0', sizeof(c)); long long i = 0; while (n > 0) { c[i] = n % 2 + 48; n = n / 2; i++; } string s = ""; for (long long j = bit - 1; j >= 0; j--) s += c[j]; return s; } long long binarytodeci(string n) { string num = n; long long dec_value = 0; long long base = 1; long long len = num.length(); for (long long i = len - 1; i >= 0; i--) { if (num[i] == '1') dec_value += base; base = base * 2; } return dec_value; } long double intlog(long double base, long double x) { return (log(x) / log(base)); } long long fact(long long n) { long long res = 1; for (long long i = 2; i <= n; i++) res = res * i; return res; } long long ncr(long long n, long long k) { long long res = 1; if (k > n - k) k = n - k; for (long long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } bool ispalindrome(string str, long long l, long long h) { while (h > l) { if (str[l++] != str[h--]) return 0; } return 1; } long long compute_hash(string s) { long long p = 31, hash = 0, po = 1; for (char c : s) { hash += (c - 'a' + 1) * po % 1000000007; po = (po * p) % 1000000007; } return hash; } vector<vector<long long>> group_identical_strings(vector<string> const &s) { long long n = s.size(); vector<pair<long long, long long>> hashes(n); for (long long i = 0; i < n; i++) hashes[i] = {compute_hash(s[i]), i}; sort(hashes.begin(), hashes.end()); vector<vector<long long>> groups; for (long long i = 0; i < n; i++) { if (i == 0 || hashes[i].first != hashes[i - 1].first) groups.emplace_back(); groups.back().push_back(hashes[i].second); } return groups; } bool isarraysort(long long a[], long long i, long long j) { if (j == i) return 1; if (a[j] < a[j - 1]) return 0; return isarraysort(a, i, j - 1); } void sieve(long long n, long long prime[]) { for (long long i = 1; i <= n; i++) prime[i] = 1; for (long long p = 2; p * p <= n; p++) { if (prime[p] == 1) { for (long long i = p * p; i <= n; i += p) prime[i] = 0; } } } bool isprime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return 0; } return 1; } long long pow_mod(long long base, long long exp) { long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % 1000000007; exp = exp >> 1; base = (base * base) % 1000000007; } return res; } const long long N = 1e6 + 3; vector<long long> adj[N]; bool visited[N]; long long depth[N] = {0}; long long size[N] = {0}; long long parent[N]; void dfs(long long v) { visited[v] = true; for (long long u : adj[v]) { if (!visited[u]) { parent[u] = v; depth[u] = depth[v] + 1; dfs(u); } } } long long time_in[N], time_out[N]; long long dfs_timer = 0; long long color[N]; void dfs_time(long long v) { time_in[v] = ++dfs_timer; color[v] = 1; for (long long u : adj[v]) { if (color[u] == 0) { dfs_time(u); } } color[v] = 2; time_out[v] = ++dfs_timer; } long long count1[N]; void numberOfNodes(long long s) { count1[s] = 1; for (long long u : adj[s]) { if (u == parent[s]) continue; numberOfNodes(u); count1[s] += count1[u]; } } void solve() { long long f, m, s, g; cin >> f >> m >> s >> g; long long a, b, c; if (2 * f - max(f, 2 * g + 1) >= 0 && 2 * m - max(m, 2 * g + 1) >= 0 && min(2 * s, 2 * g) - max(s, g) >= 0) { a = 2 * f; b = 2 * m; c = min(2 * s, 2 * g); cout << a << '\n' << b << '\n' << c; } else cout << -1; } int32_t main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); int a, b, c, d; cin >> a >> b >> c >> d; for (int i = c; i <= 10000; ++i) { if (d <= i and 2 * d >= i and c <= i and 2 * c >= i and 2 * d < 2 * a and 2 * d < 2 * b) { cout << 2 * a << '\n' << 2 * b << '\n' << i; return 0; } } cout << -1; }
### Prompt Please formulate a Cpp solution to the following problem: A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. Input You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3. Output Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). Examples Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 Note In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); int a, b, c, d; cin >> a >> b >> c >> d; for (int i = c; i <= 10000; ++i) { if (d <= i and 2 * d >= i and c <= i and 2 * c >= i and 2 * d < 2 * a and 2 * d < 2 * b) { cout << 2 * a << '\n' << 2 * b << '\n' << i; return 0; } } cout << -1; } ```
#include <bits/stdc++.h> using namespace std; class Global { public: int n, i, k, ot = -1; vector<int> v, w, r, o; char ch; void pref(vector<int>& v) { int x; for (i = x = 0; i < v.size(); ++i) { x += v[i]; v[i] = x; } } void start() { cin >> n >> k; v.resize(n); for (i = 0; i < n; ++i) { cin >> v[i]; } for (i = 0; i < n; ++i) { cin >> ch; if (ch == 'O') o.push_back(v[i]); if (ch == 'W') w.push_back(v[i]); if (ch == 'R') r.push_back(v[i]); } sort(o.begin(), o.end(), greater<int>()); sort(w.begin(), w.end(), greater<int>()); sort(r.begin(), r.end(), greater<int>()); pref(o); pref(w); pref(r); if (r.size() + o.size() >= k) { if (o.size() > 0 && r.size() > 0) for (i = o.size(); i + r.size() >= k; --i) { if (i == 0) { break; } else { if (i < k) ot = max(ot, o[i - 1] + r[k - i - 1]); } } } if (w.size() + o.size() >= k) { if (o.size() > 0 && w.size() > 0) for (i = o.size(); i + w.size() >= k; --i) { if (i == 0) { break; } else { if (i < k) ot = max(ot, o[i - 1] + w[k - i - 1]); } } } cout << ot; } }; int main() { int amount = 1; vector<Global> v(1); for (int i = 1; i <= amount; ++i) { v.clear(); v.resize(1); v[0].start(); } }
### Prompt Please provide a Cpp coded solution to the problem described below: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; class Global { public: int n, i, k, ot = -1; vector<int> v, w, r, o; char ch; void pref(vector<int>& v) { int x; for (i = x = 0; i < v.size(); ++i) { x += v[i]; v[i] = x; } } void start() { cin >> n >> k; v.resize(n); for (i = 0; i < n; ++i) { cin >> v[i]; } for (i = 0; i < n; ++i) { cin >> ch; if (ch == 'O') o.push_back(v[i]); if (ch == 'W') w.push_back(v[i]); if (ch == 'R') r.push_back(v[i]); } sort(o.begin(), o.end(), greater<int>()); sort(w.begin(), w.end(), greater<int>()); sort(r.begin(), r.end(), greater<int>()); pref(o); pref(w); pref(r); if (r.size() + o.size() >= k) { if (o.size() > 0 && r.size() > 0) for (i = o.size(); i + r.size() >= k; --i) { if (i == 0) { break; } else { if (i < k) ot = max(ot, o[i - 1] + r[k - i - 1]); } } } if (w.size() + o.size() >= k) { if (o.size() > 0 && w.size() > 0) for (i = o.size(); i + w.size() >= k; --i) { if (i == 0) { break; } else { if (i < k) ot = max(ot, o[i - 1] + w[k - i - 1]); } } } cout << ot; } }; int main() { int amount = 1; vector<Global> v(1); for (int i = 1; i <= amount; ++i) { v.clear(); v.resize(1); v[0].start(); } } ```
#include <bits/stdc++.h> using namespace std; template <class T> bool umin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool umax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } char s[200009]; int arr[200009]; vector<int> adj[3], par[3]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", arr + i); scanf("%s", s); for (int i = 0; i < n; i++) { if (s[i] == 'R') adj[0].push_back(arr[i]); else if (s[i] == 'W') adj[1].push_back(arr[i]); else adj[2].push_back(arr[i]); } for (int i = 0; i < 3; i++) { sort(adj[i].begin(), adj[i].end()); reverse(adj[i].begin(), adj[i].end()); int sum = 0; for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); it != (adj[i]).end(); it++) { sum += *it; par[i].push_back(sum); } } int ans = -1; for (int i = 0; i < int(adj[0].size()); i++) { int need = k - (i + 1); if (need >= 1 and (int) adj[2].size() >= need) umax(ans, par[2][need - 1] + par[0][i]); } for (int i = 0; i < int(adj[1].size()); i++) { int need = k - (i + 1); if (need >= 1 and (int) adj[2].size() >= need) umax(ans, par[2][need - 1] + par[1][i]); } printf("%d\n", ans); return 0; }
### Prompt Create a solution in Cpp for the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> bool umin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool umax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } char s[200009]; int arr[200009]; vector<int> adj[3], par[3]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", arr + i); scanf("%s", s); for (int i = 0; i < n; i++) { if (s[i] == 'R') adj[0].push_back(arr[i]); else if (s[i] == 'W') adj[1].push_back(arr[i]); else adj[2].push_back(arr[i]); } for (int i = 0; i < 3; i++) { sort(adj[i].begin(), adj[i].end()); reverse(adj[i].begin(), adj[i].end()); int sum = 0; for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); it != (adj[i]).end(); it++) { sum += *it; par[i].push_back(sum); } } int ans = -1; for (int i = 0; i < int(adj[0].size()); i++) { int need = k - (i + 1); if (need >= 1 and (int) adj[2].size() >= need) umax(ans, par[2][need - 1] + par[0][i]); } for (int i = 0; i < int(adj[1].size()); i++) { int need = k - (i + 1); if (need >= 1 and (int) adj[2].size() >= need) umax(ans, par[2][need - 1] + par[1][i]); } printf("%d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int dbg = 0; const int N = 2e5 + 1; const int lg = 20; const long long mod = 0xfffff; void solve() { int n, k; cin >> n >> k; vector<int> b(n, 0); for (int i = 0; i < n; i++) cin >> b[i]; string s; cin >> s; if (k == 1) { cout << -1 << "\n"; return; } vector<int> f(3, 0); vector<pair<int, int> > r, w; for (int i = 0; i < n; i++) { if (s[i] == 'R') { f[0]++; r.push_back(make_pair(b[i], 0)); } if (s[i] == 'O') { f[1]++; r.push_back(make_pair(b[i], 1)); w.push_back(make_pair(b[i], 1)); } if (s[i] == 'W') { f[2]++; w.push_back(make_pair(b[i], 0)); } } sort((r).begin(), (r).end(), greater<pair<int, int> >()); sort((w).begin(), (w).end(), greater<pair<int, int> >()); int y = -1, z = -1; int fr, fo, fw; if (f[1]) { if (f[0] && f[0] + f[1] >= k) { fr = -1; fo = -1; for (int i = 0; i < f[0] + f[1]; i++) { if (fr == -1 && r[i].second == 0) fr = i; if (fo == -1 && r[i].second == 1) fo = i; } y = 0; for (int i = 0; i < k; i++) y += r[i].first; if (fr >= k) { y -= r[k - 1].first; y += r[fr].first; } if (fo >= k) { y -= r[k - 1].first; y += r[fo].first; } } if (f[2] && f[2] + f[1] >= k) { fw = -1; fo = -1; for (int i = 0; i < f[2] + f[1]; i++) { if (fw == -1 && w[i].second == 0) fw = i; if (fo == -1 && w[i].second == 1) fo = i; } z = 0; for (int i = 0; i < k; i++) z += w[i].first; if (fw >= k) { z -= w[k - 1].first; z += w[fw].first; } if (fo >= k) { z -= w[k - 1].first; z += w[fo].first; } } } cout << max(y, z) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dbg = 0; const int N = 2e5 + 1; const int lg = 20; const long long mod = 0xfffff; void solve() { int n, k; cin >> n >> k; vector<int> b(n, 0); for (int i = 0; i < n; i++) cin >> b[i]; string s; cin >> s; if (k == 1) { cout << -1 << "\n"; return; } vector<int> f(3, 0); vector<pair<int, int> > r, w; for (int i = 0; i < n; i++) { if (s[i] == 'R') { f[0]++; r.push_back(make_pair(b[i], 0)); } if (s[i] == 'O') { f[1]++; r.push_back(make_pair(b[i], 1)); w.push_back(make_pair(b[i], 1)); } if (s[i] == 'W') { f[2]++; w.push_back(make_pair(b[i], 0)); } } sort((r).begin(), (r).end(), greater<pair<int, int> >()); sort((w).begin(), (w).end(), greater<pair<int, int> >()); int y = -1, z = -1; int fr, fo, fw; if (f[1]) { if (f[0] && f[0] + f[1] >= k) { fr = -1; fo = -1; for (int i = 0; i < f[0] + f[1]; i++) { if (fr == -1 && r[i].second == 0) fr = i; if (fo == -1 && r[i].second == 1) fo = i; } y = 0; for (int i = 0; i < k; i++) y += r[i].first; if (fr >= k) { y -= r[k - 1].first; y += r[fr].first; } if (fo >= k) { y -= r[k - 1].first; y += r[fo].first; } } if (f[2] && f[2] + f[1] >= k) { fw = -1; fo = -1; for (int i = 0; i < f[2] + f[1]; i++) { if (fw == -1 && w[i].second == 0) fw = i; if (fo == -1 && w[i].second == 1) fo = i; } z = 0; for (int i = 0; i < k; i++) z += w[i].first; if (fw >= k) { z -= w[k - 1].first; z += w[fw].first; } if (fo >= k) { z -= w[k - 1].first; z += w[fo].first; } } } cout << max(y, z) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; long long n, b; cin >> n >> b; vector<pair<long long, char> > v(n); for (long long i = 0; i < n; i++) { cin >> v[i].first; } cin >> s; for (long long i = 0; i < n; i++) { v[i].second = s[i]; } bool ORANGE = false; bool RED = false; bool WHITE = false; sort(v.rbegin(), v.rend()); long long counterWO = 0; long long counterRO = 0; long long counter = 0; long long j; for (long long i = 0; i < v.size(); i++) { j = i; if (v[i].second == 'O') { counterWO += v[i].first; counter++; ORANGE = true; } if (v[i].second == 'W') { counterWO += v[i].first; counter++; WHITE = true; } if (counter == b - 1) break; } if (WHITE == false) { for (long long i = 0; i < n; i++) { if (v[i].second == 'W') { counterWO += v[i].first; counter++; WHITE = true; break; } } } else if (ORANGE == false) { for (long long i = 0; i < n; i++) { if (v[i].second == 'O') { counterWO += v[i].first; counter++; ORANGE = true; break; } } } else if (ORANGE == true && WHITE == true) { for (long long i = j + 1; i < n; i++) { if (v[i].second == 'O' || v[i].second == 'W') { counterWO += v[i].first; counter++; ORANGE = true; break; } } } if (ORANGE == false || WHITE == false || counter < b || b == 1) { counterWO = -1; } ORANGE = false; RED = false; WHITE = false; sort(v.rbegin(), v.rend()); counterRO = 0; counter = 0; for (long long i = 0; i < v.size(); i++) { j = i; if (v[i].second == 'O') { counterRO += v[i].first; counter++; ORANGE = true; } if (v[i].second == 'R') { counterRO += v[i].first; counter++; RED = true; } if (counter == b - 1) break; } if (RED == false) { for (long long i = 0; i < n; i++) { if (v[i].second == 'R') { counterRO += v[i].first; counter++; RED = true; break; } } } else if (ORANGE == false) { for (long long i = 0; i < n; i++) { if (v[i].second == 'O') { counterRO += v[i].first; counter++; ORANGE = true; break; } } } else if (ORANGE == true && RED == true) { for (long long i = j + 1; i < n; i++) { if (v[i].second == 'O' || v[i].second == 'R') { counterRO += v[i].first; counter++; ORANGE = true; break; } } } if (ORANGE == false || RED == false || counter < b || b == 1) { counterRO = -1; } cout << max(counterWO, counterRO); return 0; }
### Prompt Please formulate a cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; long long n, b; cin >> n >> b; vector<pair<long long, char> > v(n); for (long long i = 0; i < n; i++) { cin >> v[i].first; } cin >> s; for (long long i = 0; i < n; i++) { v[i].second = s[i]; } bool ORANGE = false; bool RED = false; bool WHITE = false; sort(v.rbegin(), v.rend()); long long counterWO = 0; long long counterRO = 0; long long counter = 0; long long j; for (long long i = 0; i < v.size(); i++) { j = i; if (v[i].second == 'O') { counterWO += v[i].first; counter++; ORANGE = true; } if (v[i].second == 'W') { counterWO += v[i].first; counter++; WHITE = true; } if (counter == b - 1) break; } if (WHITE == false) { for (long long i = 0; i < n; i++) { if (v[i].second == 'W') { counterWO += v[i].first; counter++; WHITE = true; break; } } } else if (ORANGE == false) { for (long long i = 0; i < n; i++) { if (v[i].second == 'O') { counterWO += v[i].first; counter++; ORANGE = true; break; } } } else if (ORANGE == true && WHITE == true) { for (long long i = j + 1; i < n; i++) { if (v[i].second == 'O' || v[i].second == 'W') { counterWO += v[i].first; counter++; ORANGE = true; break; } } } if (ORANGE == false || WHITE == false || counter < b || b == 1) { counterWO = -1; } ORANGE = false; RED = false; WHITE = false; sort(v.rbegin(), v.rend()); counterRO = 0; counter = 0; for (long long i = 0; i < v.size(); i++) { j = i; if (v[i].second == 'O') { counterRO += v[i].first; counter++; ORANGE = true; } if (v[i].second == 'R') { counterRO += v[i].first; counter++; RED = true; } if (counter == b - 1) break; } if (RED == false) { for (long long i = 0; i < n; i++) { if (v[i].second == 'R') { counterRO += v[i].first; counter++; RED = true; break; } } } else if (ORANGE == false) { for (long long i = 0; i < n; i++) { if (v[i].second == 'O') { counterRO += v[i].first; counter++; ORANGE = true; break; } } } else if (ORANGE == true && RED == true) { for (long long i = j + 1; i < n; i++) { if (v[i].second == 'O' || v[i].second == 'R') { counterRO += v[i].first; counter++; ORANGE = true; break; } } } if (ORANGE == false || RED == false || counter < b || b == 1) { counterRO = -1; } cout << max(counterWO, counterRO); return 0; } ```
#include <bits/stdc++.h> const long long LINF = 1e18; const int INF = 1e9; const int M = 1e9 + 7; const double EPS = 1e-9; using namespace std; struct Rose { int beauty; char col; } a[202000]; bool cmp(Rose a, Rose b) { if (a.beauty <= b.beauty) return 0; return 1; } int main(int argc, const char* argv[]) { std::ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; for (int i = 0; i < (int)(n); i++) cin >> a[i].beauty; string s; cin >> s; int ors = 0, rs = 0, ws = 0; for (int i = 0; i < (int)(n); i++) { a[i].col = s[i]; if (s[i] == 'O') ors++; else if (s[i] == 'R') rs++; else ws++; } if (ors + rs < k && ors + ws < k || k == 1) { cout << -1; return 0; } sort(a, a + n, cmp); long long ans1 = 0, ans2 = 0; int flag1 = 0, flag2 = 0, flag3 = 0; for (int i = 0; i < (int)(n); i++) if (a[i].col == 'O') { ans1 += a[i].beauty; ans2 += a[i].beauty; a[i].beauty = -INF; a[i].col = 'P'; flag1 = 1; break; } for (int i = 0; i < (int)(n); i++) if (a[i].col == 'R') { ans1 += a[i].beauty; a[i].beauty = -INF; a[i].col = 'P'; flag2 = 1; break; } for (int i = 0; i < (int)(n); i++) if (a[i].col == 'W') { ans2 += a[i].beauty; a[i].beauty = -INF; a[i].col = 'P'; flag3 = 1; break; } int ct = 2; if (flag1 && flag2 && ct != k) for (int i = 0; i < (int)(n); i++) { if (a[i].col == 'R' || a[i].col == 'O') { ans1 += a[i].beauty; ct++; if (ct == k) break; } } if (ct != k) flag2 = 0; ct = 2; if (flag3 && flag1 && ct != k) for (int i = 0; i < (int)(n); i++) if (a[i].col == 'W' || a[i].col == 'O') { ans2 += a[i].beauty; ct++; if (ct == k) break; } if (!flag1) cout << -1; else if (!flag2 && flag3) cout << ans2; else if (flag2 && !flag3) cout << ans1; else if (flag2 && flag3) cout << max(ans1, ans2); else cout << -1; return 0; }
### Prompt In cpp, your task is to solve the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> const long long LINF = 1e18; const int INF = 1e9; const int M = 1e9 + 7; const double EPS = 1e-9; using namespace std; struct Rose { int beauty; char col; } a[202000]; bool cmp(Rose a, Rose b) { if (a.beauty <= b.beauty) return 0; return 1; } int main(int argc, const char* argv[]) { std::ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; for (int i = 0; i < (int)(n); i++) cin >> a[i].beauty; string s; cin >> s; int ors = 0, rs = 0, ws = 0; for (int i = 0; i < (int)(n); i++) { a[i].col = s[i]; if (s[i] == 'O') ors++; else if (s[i] == 'R') rs++; else ws++; } if (ors + rs < k && ors + ws < k || k == 1) { cout << -1; return 0; } sort(a, a + n, cmp); long long ans1 = 0, ans2 = 0; int flag1 = 0, flag2 = 0, flag3 = 0; for (int i = 0; i < (int)(n); i++) if (a[i].col == 'O') { ans1 += a[i].beauty; ans2 += a[i].beauty; a[i].beauty = -INF; a[i].col = 'P'; flag1 = 1; break; } for (int i = 0; i < (int)(n); i++) if (a[i].col == 'R') { ans1 += a[i].beauty; a[i].beauty = -INF; a[i].col = 'P'; flag2 = 1; break; } for (int i = 0; i < (int)(n); i++) if (a[i].col == 'W') { ans2 += a[i].beauty; a[i].beauty = -INF; a[i].col = 'P'; flag3 = 1; break; } int ct = 2; if (flag1 && flag2 && ct != k) for (int i = 0; i < (int)(n); i++) { if (a[i].col == 'R' || a[i].col == 'O') { ans1 += a[i].beauty; ct++; if (ct == k) break; } } if (ct != k) flag2 = 0; ct = 2; if (flag3 && flag1 && ct != k) for (int i = 0; i < (int)(n); i++) if (a[i].col == 'W' || a[i].col == 'O') { ans2 += a[i].beauty; ct++; if (ct == k) break; } if (!flag1) cout << -1; else if (!flag2 && flag3) cout << ans2; else if (flag2 && !flag3) cout << ans1; else if (flag2 && flag3) cout << max(ans1, ans2); else cout << -1; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, k; vector<long long> r, w, o; vector<long long> v; string col; void panic() { cout << -1 << endl; exit(0); } long long solve_ro() { long long anw = 0; if (o.size() == 0 || r.size() == 0) { return -1; } anw += o[0] + r[0]; int o_ptr = 1, r_ptr = 1; long long cnt = 2; while (cnt < k && o_ptr < o.size() && r_ptr < r.size()) { if (o[o_ptr] > r[r_ptr]) anw += o[o_ptr++]; else anw += r[r_ptr++]; cnt++; } while (cnt < k && o_ptr < o.size()) { cnt++; anw += o[o_ptr++]; } while (cnt < k && r_ptr < r.size()) { cnt++; anw += r[r_ptr++]; } if (cnt < k) return -1; return anw; } long long solve_wo() { long long anw = 0; if (o.size() == 0 || w.size() == 0) { return -1; } anw += o[0] + w[0]; int o_ptr = 1, w_ptr = 1; long long cnt = 2; while (cnt < k && o_ptr < o.size() && w_ptr < w.size()) { if (o[o_ptr] > w[w_ptr]) anw += o[o_ptr++]; else anw += w[w_ptr++]; cnt++; } while (cnt < k && o_ptr < o.size()) { cnt++; anw += o[o_ptr++]; } while (cnt < k && w_ptr < w.size()) { cnt++; anw += w[w_ptr++]; } if (cnt < k) return -1; return anw; } int main() { ios::sync_with_stdio(0); cin >> n >> k; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i]; cin >> col; for (int i = 0; i < n; i++) { if (col[i] == 'R') { r.push_back(v[i]); } else if (col[i] == 'W') { w.push_back(v[i]); } else { o.push_back(v[i]); } } sort(r.rbegin(), r.rend()); sort(w.rbegin(), w.rend()); sort(o.rbegin(), o.rend()); if (k == 1) { panic(); } if (o.size() == 0) { panic(); } if (r.size() == 0 && w.size() == 0) { panic(); } cout << max(solve_ro(), solve_wo()) << endl; }
### Prompt Please formulate a CPP solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k; vector<long long> r, w, o; vector<long long> v; string col; void panic() { cout << -1 << endl; exit(0); } long long solve_ro() { long long anw = 0; if (o.size() == 0 || r.size() == 0) { return -1; } anw += o[0] + r[0]; int o_ptr = 1, r_ptr = 1; long long cnt = 2; while (cnt < k && o_ptr < o.size() && r_ptr < r.size()) { if (o[o_ptr] > r[r_ptr]) anw += o[o_ptr++]; else anw += r[r_ptr++]; cnt++; } while (cnt < k && o_ptr < o.size()) { cnt++; anw += o[o_ptr++]; } while (cnt < k && r_ptr < r.size()) { cnt++; anw += r[r_ptr++]; } if (cnt < k) return -1; return anw; } long long solve_wo() { long long anw = 0; if (o.size() == 0 || w.size() == 0) { return -1; } anw += o[0] + w[0]; int o_ptr = 1, w_ptr = 1; long long cnt = 2; while (cnt < k && o_ptr < o.size() && w_ptr < w.size()) { if (o[o_ptr] > w[w_ptr]) anw += o[o_ptr++]; else anw += w[w_ptr++]; cnt++; } while (cnt < k && o_ptr < o.size()) { cnt++; anw += o[o_ptr++]; } while (cnt < k && w_ptr < w.size()) { cnt++; anw += w[w_ptr++]; } if (cnt < k) return -1; return anw; } int main() { ios::sync_with_stdio(0); cin >> n >> k; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i]; cin >> col; for (int i = 0; i < n; i++) { if (col[i] == 'R') { r.push_back(v[i]); } else if (col[i] == 'W') { w.push_back(v[i]); } else { o.push_back(v[i]); } } sort(r.rbegin(), r.rend()); sort(w.rbegin(), w.rend()); sort(o.rbegin(), o.rend()); if (k == 1) { panic(); } if (o.size() == 0) { panic(); } if (r.size() == 0 && w.size() == 0) { panic(); } cout << max(solve_ro(), solve_wo()) << endl; } ```
#include <bits/stdc++.h> using namespace std; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; const int dxx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, dyy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const long long mod = 1000000007; const int base = 311; const int N = 200005; int n, k, b[N], a[N]; vector<int> vec[3]; void gogo() { cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> b[i]; string s; cin >> s; s = ' ' + s; for (int i = 1; i <= n; ++i) { if (s[i] == 'W') a[i] = 0; else if (s[i] == 'O') a[i] = 2; else a[i] = 1; vec[a[i]].push_back(b[i]); } for (int i = 0; i <= 2; ++i) { sort((vec[i]).begin(), (vec[i]).end(), greater<int>()); for (int j = 1; j < ((int)(vec[i]).size()); ++j) vec[i][j] += vec[i][j - 1]; } int ans = -1; for (int c1 = 0; c1 <= 2; ++c1) { for (int c2 = c1 + 1; c2 <= 2; ++c2) if (!(c1 == 0 && c2 == 1)) { for (int i = 1; i < k; ++i) { if (((int)(vec[c1]).size()) >= i && ((int)(vec[c2]).size()) >= k - i) { ans = max(ans, vec[c1][i - 1] + vec[c2][k - i - 1]); } } } } cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (fopen("sol" ".inp", "r")) { freopen( "sol" ".inp", "r", stdin); freopen( "sol" ".out", "w", stdout); } gogo(); }
### Prompt Develop a solution in CPP to the problem described below: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; const int dxx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, dyy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const long long mod = 1000000007; const int base = 311; const int N = 200005; int n, k, b[N], a[N]; vector<int> vec[3]; void gogo() { cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> b[i]; string s; cin >> s; s = ' ' + s; for (int i = 1; i <= n; ++i) { if (s[i] == 'W') a[i] = 0; else if (s[i] == 'O') a[i] = 2; else a[i] = 1; vec[a[i]].push_back(b[i]); } for (int i = 0; i <= 2; ++i) { sort((vec[i]).begin(), (vec[i]).end(), greater<int>()); for (int j = 1; j < ((int)(vec[i]).size()); ++j) vec[i][j] += vec[i][j - 1]; } int ans = -1; for (int c1 = 0; c1 <= 2; ++c1) { for (int c2 = c1 + 1; c2 <= 2; ++c2) if (!(c1 == 0 && c2 == 1)) { for (int i = 1; i < k; ++i) { if (((int)(vec[c1]).size()) >= i && ((int)(vec[c2]).size()) >= k - i) { ans = max(ans, vec[c1][i - 1] + vec[c2][k - i - 1]); } } } } cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (fopen("sol" ".inp", "r")) { freopen( "sol" ".inp", "r", stdin); freopen( "sol" ".out", "w", stdout); } gogo(); } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; vector<char> str(n); int k_orange = 0; int k_red = 0; int k_white = 0; for (int i = 0; i < n; i++) { cin >> str[i]; if (str[i] == 'O') { k_orange += 1; } else { if (str[i] == 'R') k_red += 1; else k_white += 1; } } vector<int> orange(k_orange); vector<int> red(k_red); vector<int> white(k_white); k_orange = 0; k_red = 0; k_white = 0; for (int i = 0; i < n; i++) { if (str[i] == 'O') { orange[k_orange] = arr[i]; k_orange += 1; } else { if (str[i] == 'R') { red[k_red] = arr[i]; k_red += 1; } else { white[k_white] = arr[i]; k_white += 1; } } } sort(orange.rbegin(), orange.rend()); sort(red.rbegin(), red.rend()); sort(white.rbegin(), white.rend()); for (int i = 1; i < k_orange; i++) { orange[i] += orange[i - 1]; } for (int i = 1; i < k_red; i++) { red[i] += red[i - 1]; } for (int i = 1; i < k_white; i++) { white[i] += white[i - 1]; } int ans = -1; for (int i = 1; i < k; i++) { if (k_orange >= i) { if (k_white + i >= k) { if (ans < orange[i - 1] + white[k - i - 1]) { ans = orange[i - 1] + white[k - i - 1]; } } if (k_red + i >= k) { if (ans < orange[i - 1] + red[k - i - 1]) { ans = orange[i - 1] + red[k - i - 1]; } } } } printf("%d", ans); return 0; }
### Prompt In Cpp, your task is to solve the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; vector<char> str(n); int k_orange = 0; int k_red = 0; int k_white = 0; for (int i = 0; i < n; i++) { cin >> str[i]; if (str[i] == 'O') { k_orange += 1; } else { if (str[i] == 'R') k_red += 1; else k_white += 1; } } vector<int> orange(k_orange); vector<int> red(k_red); vector<int> white(k_white); k_orange = 0; k_red = 0; k_white = 0; for (int i = 0; i < n; i++) { if (str[i] == 'O') { orange[k_orange] = arr[i]; k_orange += 1; } else { if (str[i] == 'R') { red[k_red] = arr[i]; k_red += 1; } else { white[k_white] = arr[i]; k_white += 1; } } } sort(orange.rbegin(), orange.rend()); sort(red.rbegin(), red.rend()); sort(white.rbegin(), white.rend()); for (int i = 1; i < k_orange; i++) { orange[i] += orange[i - 1]; } for (int i = 1; i < k_red; i++) { red[i] += red[i - 1]; } for (int i = 1; i < k_white; i++) { white[i] += white[i - 1]; } int ans = -1; for (int i = 1; i < k; i++) { if (k_orange >= i) { if (k_white + i >= k) { if (ans < orange[i - 1] + white[k - i - 1]) { ans = orange[i - 1] + white[k - i - 1]; } } if (k_red + i >= k) { if (ans < orange[i - 1] + red[k - i - 1]) { ans = orange[i - 1] + red[k - i - 1]; } } } } printf("%d", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 36; const int INF = 1e9 + 7; long long a[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; for (int i(0); i < n; i++) { cin >> a[i]; } string s; cin >> s; vector<int> o, r, w; for (int i(0); i < n; i++) { if (s[i] == 'O') { o.push_back(a[i]); } if (s[i] == 'R') { r.push_back(a[i]); } if (s[i] == 'W') { w.push_back(a[i]); } } if (k == 1 || (int)o.size() == 0 || ((int)r.size() + (int)w.size() == 0) || (((int)o.size() + (int)r.size() < k) && ((int)o.size() + (int)w.size() < k))) { cout << -1 << '\n'; return 0; } sort(o.begin(), o.end()); sort(r.begin(), r.end()); sort(w.begin(), w.end()); long long ansW = o[(int)o.size() - 1], ansR = o[(int)o.size() - 1]; long long FW = 0, FR = 0; if ((int)w.size() > 0) { ansW += w[(int)w.size() - 1]; w.pop_back(); FW++; } if ((int)r.size() > 0) { ansR += r[(int)r.size() - 1]; r.pop_back(); FR++; } o.pop_back(); k -= 2; vector<int> ro = r, wo = w; for (int i(0); i < (int)o.size(); i++) { ro.push_back(o[i]); wo.push_back(o[i]); } sort(ro.begin(), ro.end()); sort(wo.begin(), wo.end()); for (int i(0); i < k; i++) { if ((int)ro.size() - 1 - i >= 0) { ansR += ro[(int)ro.size() - 1 - i]; } else { FR--; } if ((int)wo.size() - 1 - i >= 0) { ansW += wo[(int)wo.size() - 1 - i]; } else { FW--; } } if (FW == 1 && FR == 1) { cout << max(ansW, ansR) << '\n'; return 0; } if (FW == 1) { cout << ansW << '\n'; return 0; } if (FR == 1) { cout << ansR << '\n'; return 0; } cout << -1 << '\n'; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 36; const int INF = 1e9 + 7; long long a[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; for (int i(0); i < n; i++) { cin >> a[i]; } string s; cin >> s; vector<int> o, r, w; for (int i(0); i < n; i++) { if (s[i] == 'O') { o.push_back(a[i]); } if (s[i] == 'R') { r.push_back(a[i]); } if (s[i] == 'W') { w.push_back(a[i]); } } if (k == 1 || (int)o.size() == 0 || ((int)r.size() + (int)w.size() == 0) || (((int)o.size() + (int)r.size() < k) && ((int)o.size() + (int)w.size() < k))) { cout << -1 << '\n'; return 0; } sort(o.begin(), o.end()); sort(r.begin(), r.end()); sort(w.begin(), w.end()); long long ansW = o[(int)o.size() - 1], ansR = o[(int)o.size() - 1]; long long FW = 0, FR = 0; if ((int)w.size() > 0) { ansW += w[(int)w.size() - 1]; w.pop_back(); FW++; } if ((int)r.size() > 0) { ansR += r[(int)r.size() - 1]; r.pop_back(); FR++; } o.pop_back(); k -= 2; vector<int> ro = r, wo = w; for (int i(0); i < (int)o.size(); i++) { ro.push_back(o[i]); wo.push_back(o[i]); } sort(ro.begin(), ro.end()); sort(wo.begin(), wo.end()); for (int i(0); i < k; i++) { if ((int)ro.size() - 1 - i >= 0) { ansR += ro[(int)ro.size() - 1 - i]; } else { FR--; } if ((int)wo.size() - 1 - i >= 0) { ansW += wo[(int)wo.size() - 1 - i]; } else { FW--; } } if (FW == 1 && FR == 1) { cout << max(ansW, ansR) << '\n'; return 0; } if (FW == 1) { cout << ansW << '\n'; return 0; } if (FR == 1) { cout << ansR << '\n'; return 0; } cout << -1 << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; string s; cin >> s; if (k == 1) { cout << -1; return 0; } unordered_map<char, vector<int>> ad; for (int i = 0; i < n; i++) ad[s[i]].push_back(a[i]); int ans = -1; for (auto it : ad) sort(ad[it.first].begin(), ad[it.first].end()); auto ag = ad; vector<int> af; if (!ad['O'].empty() && !ad['R'].empty()) { af.push_back(ad['O'].back()); ad['O'].pop_back(); af.push_back(ad['R'].back()); ad['R'].pop_back(); while (af.size() < k) { if (!ad['O'].empty() && !ad['R'].empty()) { if (ad['O'].back() > ad['R'].back()) { af.push_back(ad['O'].back()); ad['O'].pop_back(); } else { af.push_back(ad['R'].back()); ad['R'].pop_back(); } } else if (!ad['O'].empty()) { af.push_back(ad['O'].back()); ad['O'].pop_back(); } else if (!ad['R'].empty()) { af.push_back(ad['R'].back()); ad['R'].pop_back(); } else break; } if (af.size() >= k) { int as = 0; for (auto it : af) as += it; ans = max(ans, as); } } af.clear(); if (!ag['O'].empty() && !ag['W'].empty()) { af.push_back(ag['O'].back()); ag['O'].pop_back(); af.push_back(ag['W'].back()); ag['W'].pop_back(); while (af.size() < k) { if (!ag['O'].empty() && !ag['W'].empty()) { if (ag['O'].back() > ag['W'].back()) { af.push_back(ag['O'].back()); ag['O'].pop_back(); } else { af.push_back(ag['W'].back()); ag['W'].pop_back(); } } else if (!ag['O'].empty()) { af.push_back(ag['O'].back()); ag['O'].pop_back(); } else if (!ag['W'].empty()) { af.push_back(ag['W'].back()); ag['W'].pop_back(); } else break; } if (af.size() >= k) { int as = 0; for (auto it : af) as += it; ans = max(ans, as); } } cout << ans; }
### Prompt Please provide a CPP coded solution to the problem described below: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; string s; cin >> s; if (k == 1) { cout << -1; return 0; } unordered_map<char, vector<int>> ad; for (int i = 0; i < n; i++) ad[s[i]].push_back(a[i]); int ans = -1; for (auto it : ad) sort(ad[it.first].begin(), ad[it.first].end()); auto ag = ad; vector<int> af; if (!ad['O'].empty() && !ad['R'].empty()) { af.push_back(ad['O'].back()); ad['O'].pop_back(); af.push_back(ad['R'].back()); ad['R'].pop_back(); while (af.size() < k) { if (!ad['O'].empty() && !ad['R'].empty()) { if (ad['O'].back() > ad['R'].back()) { af.push_back(ad['O'].back()); ad['O'].pop_back(); } else { af.push_back(ad['R'].back()); ad['R'].pop_back(); } } else if (!ad['O'].empty()) { af.push_back(ad['O'].back()); ad['O'].pop_back(); } else if (!ad['R'].empty()) { af.push_back(ad['R'].back()); ad['R'].pop_back(); } else break; } if (af.size() >= k) { int as = 0; for (auto it : af) as += it; ans = max(ans, as); } } af.clear(); if (!ag['O'].empty() && !ag['W'].empty()) { af.push_back(ag['O'].back()); ag['O'].pop_back(); af.push_back(ag['W'].back()); ag['W'].pop_back(); while (af.size() < k) { if (!ag['O'].empty() && !ag['W'].empty()) { if (ag['O'].back() > ag['W'].back()) { af.push_back(ag['O'].back()); ag['O'].pop_back(); } else { af.push_back(ag['W'].back()); ag['W'].pop_back(); } } else if (!ag['O'].empty()) { af.push_back(ag['O'].back()); ag['O'].pop_back(); } else if (!ag['W'].empty()) { af.push_back(ag['W'].back()); ag['W'].pop_back(); } else break; } if (af.size() >= k) { int as = 0; for (auto it : af) as += it; ans = max(ans, as); } } cout << ans; } ```
#include <bits/stdc++.h> using namespace std; int n, k; int Beauty[200002]; string s; int Rose[200002], SpRose[200002], szRose; int RedRose[200002], SpRedRose[200002], szRedRose; int WhiteRose[200002], SpWhiteRose[200002], szWhiteRose; int sol = -1; void Welcome_to_Rose() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> Beauty[i]; cin >> s; for (int i = 0; i < n; ++i) { if (s[i] == 'O') Rose[++szRose] = Beauty[i + 1]; if (s[i] == 'W') WhiteRose[++szWhiteRose] = Beauty[i + 1]; if (s[i] == 'R') RedRose[++szRedRose] = Beauty[i + 1]; } sort(Rose + 1, Rose + szRose + 1); sort(WhiteRose + 1, WhiteRose + szWhiteRose + 1); sort(RedRose + 1, RedRose + szRedRose + 1); for (int i = 1; i <= szRose; ++i) SpRose[i] = SpRose[i - 1] + Rose[i]; for (int i = 1; i <= szWhiteRose; ++i) SpWhiteRose[i] = SpWhiteRose[i - 1] + WhiteRose[i]; for (int i = 1; i <= szRedRose; ++i) SpRedRose[i] = SpRedRose[i - 1] + RedRose[i]; } void Find_the_perfect_Rose() { for (int i = 1; i <= min(k - 1, szRose); ++i) { if (k - i > szWhiteRose) continue; else { int ss = SpRose[szRose] - SpRose[szRose - i] + SpWhiteRose[szWhiteRose] - SpWhiteRose[szWhiteRose - (k - i)]; sol = max(sol, ss); } } for (int i = 1; i <= min(k - 1, szRose); ++i) { if (k - i > szRedRose) continue; else { int ss = SpRose[szRose] - SpRose[szRose - i] + SpRedRose[szRedRose] - SpRedRose[szRedRose - (k - i)]; sol = max(sol, ss); } } cout << sol; } int main() { Welcome_to_Rose(); Find_the_perfect_Rose(); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, k; int Beauty[200002]; string s; int Rose[200002], SpRose[200002], szRose; int RedRose[200002], SpRedRose[200002], szRedRose; int WhiteRose[200002], SpWhiteRose[200002], szWhiteRose; int sol = -1; void Welcome_to_Rose() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> Beauty[i]; cin >> s; for (int i = 0; i < n; ++i) { if (s[i] == 'O') Rose[++szRose] = Beauty[i + 1]; if (s[i] == 'W') WhiteRose[++szWhiteRose] = Beauty[i + 1]; if (s[i] == 'R') RedRose[++szRedRose] = Beauty[i + 1]; } sort(Rose + 1, Rose + szRose + 1); sort(WhiteRose + 1, WhiteRose + szWhiteRose + 1); sort(RedRose + 1, RedRose + szRedRose + 1); for (int i = 1; i <= szRose; ++i) SpRose[i] = SpRose[i - 1] + Rose[i]; for (int i = 1; i <= szWhiteRose; ++i) SpWhiteRose[i] = SpWhiteRose[i - 1] + WhiteRose[i]; for (int i = 1; i <= szRedRose; ++i) SpRedRose[i] = SpRedRose[i - 1] + RedRose[i]; } void Find_the_perfect_Rose() { for (int i = 1; i <= min(k - 1, szRose); ++i) { if (k - i > szWhiteRose) continue; else { int ss = SpRose[szRose] - SpRose[szRose - i] + SpWhiteRose[szWhiteRose] - SpWhiteRose[szWhiteRose - (k - i)]; sol = max(sol, ss); } } for (int i = 1; i <= min(k - 1, szRose); ++i) { if (k - i > szRedRose) continue; else { int ss = SpRose[szRose] - SpRose[szRose - i] + SpRedRose[szRedRose] - SpRedRose[szRedRose - (k - i)]; sol = max(sol, ss); } } cout << sol; } int main() { Welcome_to_Rose(); Find_the_perfect_Rose(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int c[200005], B[200005], O[200005], R[200005]; char s[200005]; int n, k, z[200005]; long long solve(int *x, int *y) { if (!x[0] || !y[0]) return 0; long long ans = x[1] + y[1]; z[0] = 0; for (int i = 2; i <= x[0]; i++) z[++z[0]] = x[i]; for (int i = 2; i <= y[0]; i++) z[++z[0]] = y[i]; sort(z + 1, z + z[0] + 1, greater<int>()); if (z[0] + 2 < k) return 0; for (int i = 1; i <= k - 2; i++) ans += z[i]; return ans; } int main() { scanf("%d%d", &n, &k); if (k == 1) return puts("-1"), 0; for (int i = 1; i <= n; i++) scanf("%d", &c[i]); scanf("%s", s + 1); for (int i = 1; i <= n; i++) if (s[i] == 'W') B[++B[0]] = c[i]; else if (s[i] == 'R') R[++R[0]] = c[i]; else O[++O[0]] = c[i]; sort(O + 1, O + O[0] + 1, greater<int>()); sort(R + 1, R + R[0] + 1, greater<int>()); sort(B + 1, B + B[0] + 1, greater<int>()); long long ans = max(solve(O, R), solve(O, B)); printf("%lld\n", ans ? ans : -1); }
### Prompt Please formulate a Cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int c[200005], B[200005], O[200005], R[200005]; char s[200005]; int n, k, z[200005]; long long solve(int *x, int *y) { if (!x[0] || !y[0]) return 0; long long ans = x[1] + y[1]; z[0] = 0; for (int i = 2; i <= x[0]; i++) z[++z[0]] = x[i]; for (int i = 2; i <= y[0]; i++) z[++z[0]] = y[i]; sort(z + 1, z + z[0] + 1, greater<int>()); if (z[0] + 2 < k) return 0; for (int i = 1; i <= k - 2; i++) ans += z[i]; return ans; } int main() { scanf("%d%d", &n, &k); if (k == 1) return puts("-1"), 0; for (int i = 1; i <= n; i++) scanf("%d", &c[i]); scanf("%s", s + 1); for (int i = 1; i <= n; i++) if (s[i] == 'W') B[++B[0]] = c[i]; else if (s[i] == 'R') R[++R[0]] = c[i]; else O[++O[0]] = c[i]; sort(O + 1, O + O[0] + 1, greater<int>()); sort(R + 1, R + R[0] + 1, greater<int>()); sort(B + 1, B + B[0] + 1, greater<int>()); long long ans = max(solve(O, R), solve(O, B)); printf("%lld\n", ans ? ans : -1); } ```
#include <bits/stdc++.h> using namespace std; long long n, k, a[200005], W[200005], O[200005], R[2000005], ans; long long r = 0; long long w = 0; long long o = 0; string s; bool kt(long long a, long long b) { return (a > b); } int32_t main() { cin >> n >> k; for (long long i = 1; i <= n; i++) { cin >> a[i]; } cin >> s; for (long long i = 0; i < s.size(); i++) { if (s[i] == 'R') { r++; R[r] = a[i + 1]; } else if (s[i] == 'O') { o++; O[o] = a[i + 1]; } else { w++; W[w] = a[i + 1]; } } sort(R + 1, R + 1 + r, kt); sort(O + 1, O + 1 + o, kt); sort(W + 1, W + 1 + w, kt); for (long long i = 1; i <= r; i++) { R[i] = R[i - 1] + R[i]; } for (long long i = 1; i <= o; i++) { O[i] = O[i - 1] + O[i]; } for (long long i = 1; i <= w; i++) { W[i] = W[i - 1] + W[i]; } if (r + o < k && w + o < k) cout << -1; else if (o == 0) cout << -1; else { ans = -1; for (long long i = 1; i < k; i++) { long long tam = k - i; if (tam <= w || tam <= r) { if (tam > w) { ans = max(ans, R[tam] + O[i]); } else if (tam > r) { ans = max(ans, W[tam] + O[i]); } else ans = max(ans, max(W[tam] + O[i], R[tam] + O[i])); } } cout << ans; } }
### Prompt Construct a CPP code solution to the problem outlined: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, a[200005], W[200005], O[200005], R[2000005], ans; long long r = 0; long long w = 0; long long o = 0; string s; bool kt(long long a, long long b) { return (a > b); } int32_t main() { cin >> n >> k; for (long long i = 1; i <= n; i++) { cin >> a[i]; } cin >> s; for (long long i = 0; i < s.size(); i++) { if (s[i] == 'R') { r++; R[r] = a[i + 1]; } else if (s[i] == 'O') { o++; O[o] = a[i + 1]; } else { w++; W[w] = a[i + 1]; } } sort(R + 1, R + 1 + r, kt); sort(O + 1, O + 1 + o, kt); sort(W + 1, W + 1 + w, kt); for (long long i = 1; i <= r; i++) { R[i] = R[i - 1] + R[i]; } for (long long i = 1; i <= o; i++) { O[i] = O[i - 1] + O[i]; } for (long long i = 1; i <= w; i++) { W[i] = W[i - 1] + W[i]; } if (r + o < k && w + o < k) cout << -1; else if (o == 0) cout << -1; else { ans = -1; for (long long i = 1; i < k; i++) { long long tam = k - i; if (tam <= w || tam <= r) { if (tam > w) { ans = max(ans, R[tam] + O[i]); } else if (tam > r) { ans = max(ans, W[tam] + O[i]); } else ans = max(ans, max(W[tam] + O[i], R[tam] + O[i])); } } cout << ans; } } ```
#include <bits/stdc++.h> using namespace std; class Solution { int n, k; vector<int> A[3]; public: void run() { cin >> n >> k; if (k < 2) return cout << -1 << '\n', void(); { vector<int> B; B.resize(n); for (int i = 0; i < n; ++i) cin >> B[i]; string s; cin >> s; for (int i = 0; i < n; ++i) { if (s[i] == 'O') A[0].push_back(B[i]); if (s[i] == 'R') A[1].push_back(B[i]); if (s[i] == 'W') A[2].push_back(B[i]); } for (int i = 0; i < 3; ++i) sort(A[i].rbegin(), A[i].rend()); } int ans = -1; for (int i = 1; i < 3; ++i) { if (A[0].empty() or A[i].empty()) continue; vector<int> cell; for (int j : {0, i}) cell.insert(cell.end(), A[j].begin() + 1, A[j].end()); if (cell.size() < k - 2) continue; sort(cell.rbegin(), cell.rend()); ans = max(ans, accumulate(cell.begin(), cell.begin() + k - 2, 0) + A[0][0] + A[i][0]); } cout << ans << '\n'; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); Solution().run(); }
### Prompt Create a solution in Cpp for the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; class Solution { int n, k; vector<int> A[3]; public: void run() { cin >> n >> k; if (k < 2) return cout << -1 << '\n', void(); { vector<int> B; B.resize(n); for (int i = 0; i < n; ++i) cin >> B[i]; string s; cin >> s; for (int i = 0; i < n; ++i) { if (s[i] == 'O') A[0].push_back(B[i]); if (s[i] == 'R') A[1].push_back(B[i]); if (s[i] == 'W') A[2].push_back(B[i]); } for (int i = 0; i < 3; ++i) sort(A[i].rbegin(), A[i].rend()); } int ans = -1; for (int i = 1; i < 3; ++i) { if (A[0].empty() or A[i].empty()) continue; vector<int> cell; for (int j : {0, i}) cell.insert(cell.end(), A[j].begin() + 1, A[j].end()); if (cell.size() < k - 2) continue; sort(cell.rbegin(), cell.rend()); ans = max(ans, accumulate(cell.begin(), cell.begin() + k - 2, 0) + A[0][0] + A[i][0]); } cout << ans << '\n'; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); Solution().run(); } ```
#include <bits/stdc++.h> using namespace std; long long getTop(long long prefix[], long long n, long long size) { if (n == 0 || size == 0) return 0; return prefix[n - 1]; } const long long maxn = 200005; long long redPrefix[maxn], orangePrefix[maxn], whitePrefix[maxn]; vector<long long> red, orange, white; long long arr[maxn]; char firstchar; int main() { long long n, k; cin >> n >> k; for (long long i = 0; i < n; i++) { cin >> arr[i]; } for (long long i = 0; i < n; i++) { char c; cin >> c; if (i == 0) firstchar = c; switch (c) { case 'R': red.push_back(arr[i]); break; case 'O': orange.push_back(arr[i]); break; case 'W': white.push_back(arr[i]); break; } } if (orange.size() == 0) { cout << "-1\n"; return 0; } sort(red.begin(), red.end(), greater<long long>()); sort(orange.begin(), orange.end(), greater<long long>()); sort(white.begin(), white.end(), greater<long long>()); for (long long i = 0; i < red.size(); i++) { if (i == 0) redPrefix[i] = red[i]; else redPrefix[i] = red[i] + redPrefix[i - 1]; } for (long long i = 0; i < orange.size(); i++) { if (i == 0) orangePrefix[i] = orange[i]; else orangePrefix[i] = orange[i] + orangePrefix[i - 1]; } for (long long i = 0; i < white.size(); i++) { if (i == 0) whitePrefix[i] = white[i]; else whitePrefix[i] = white[i] + whitePrefix[i - 1]; } long long ans = -1; long long kk = 1; for (; kk < k; kk++) { if (kk <= orange.size() && k - kk <= red.size()) { long long ttr = getTop(orangePrefix, kk, orange.size()) + getTop(redPrefix, k - kk, red.size()); ans = max(ans, ttr); } } kk = 1; for (; kk < k; kk++) { if (kk <= orange.size() && k - kk <= white.size()) { long long ttr = getTop(orangePrefix, kk, orange.size()) + getTop(whitePrefix, k - kk, white.size()); ans = max(ans, ttr); } } cout << ans << '\n'; }
### Prompt Please create a solution in Cpp to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long getTop(long long prefix[], long long n, long long size) { if (n == 0 || size == 0) return 0; return prefix[n - 1]; } const long long maxn = 200005; long long redPrefix[maxn], orangePrefix[maxn], whitePrefix[maxn]; vector<long long> red, orange, white; long long arr[maxn]; char firstchar; int main() { long long n, k; cin >> n >> k; for (long long i = 0; i < n; i++) { cin >> arr[i]; } for (long long i = 0; i < n; i++) { char c; cin >> c; if (i == 0) firstchar = c; switch (c) { case 'R': red.push_back(arr[i]); break; case 'O': orange.push_back(arr[i]); break; case 'W': white.push_back(arr[i]); break; } } if (orange.size() == 0) { cout << "-1\n"; return 0; } sort(red.begin(), red.end(), greater<long long>()); sort(orange.begin(), orange.end(), greater<long long>()); sort(white.begin(), white.end(), greater<long long>()); for (long long i = 0; i < red.size(); i++) { if (i == 0) redPrefix[i] = red[i]; else redPrefix[i] = red[i] + redPrefix[i - 1]; } for (long long i = 0; i < orange.size(); i++) { if (i == 0) orangePrefix[i] = orange[i]; else orangePrefix[i] = orange[i] + orangePrefix[i - 1]; } for (long long i = 0; i < white.size(); i++) { if (i == 0) whitePrefix[i] = white[i]; else whitePrefix[i] = white[i] + whitePrefix[i - 1]; } long long ans = -1; long long kk = 1; for (; kk < k; kk++) { if (kk <= orange.size() && k - kk <= red.size()) { long long ttr = getTop(orangePrefix, kk, orange.size()) + getTop(redPrefix, k - kk, red.size()); ans = max(ans, ttr); } } kk = 1; for (; kk < k; kk++) { if (kk <= orange.size() && k - kk <= white.size()) { long long ttr = getTop(orangePrefix, kk, orange.size()) + getTop(whitePrefix, k - kk, white.size()); ans = max(ans, ttr); } } cout << ans << '\n'; } ```
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const int N = 2e5 + 1; auto rnd = bind(uniform_int_distribution<int>(1, 10000), mt19937(time(0))); signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> a(n); vector<char> c(n); vector<vector<int>> vc(3, vector<int>()); vector<vector<long long>> pref(3, vector<long long>()); for (auto &z : a) cin >> z; for (int i = 0; i < n; i++) { cin >> c[i]; if (c[i] == 'R') vc[0].push_back(a[i]); else if (c[i] == 'W') vc[1].push_back(a[i]); else vc[2].push_back(a[i]); } for (int i = 0; i < 3; i++) { sort(vc[i].rbegin(), vc[i].rend()); pref[i].resize((int)vc[i].size()); long long second = 0; for (int j = 0; j < (int)pref[i].size(); j++) { second += vc[i][j]; pref[i][j] = second; } } long long ans = -inf; for (int j = 0; j < (int)pref[2].size(); j++) { if (j >= k - 1) continue; long long me = pref[2][j] + ((int)pref[0].size() >= (k - j - 1) ? pref[0][k - j - 2] : -inf); ans = max(ans, me); me = pref[2][j] + ((int)pref[1].size() >= (k - j - 1) ? pref[1][k - j - 2] : -inf); ans = max(ans, me); } if (ans < 0) cout << -1; else cout << ans; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const int N = 2e5 + 1; auto rnd = bind(uniform_int_distribution<int>(1, 10000), mt19937(time(0))); signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> a(n); vector<char> c(n); vector<vector<int>> vc(3, vector<int>()); vector<vector<long long>> pref(3, vector<long long>()); for (auto &z : a) cin >> z; for (int i = 0; i < n; i++) { cin >> c[i]; if (c[i] == 'R') vc[0].push_back(a[i]); else if (c[i] == 'W') vc[1].push_back(a[i]); else vc[2].push_back(a[i]); } for (int i = 0; i < 3; i++) { sort(vc[i].rbegin(), vc[i].rend()); pref[i].resize((int)vc[i].size()); long long second = 0; for (int j = 0; j < (int)pref[i].size(); j++) { second += vc[i][j]; pref[i][j] = second; } } long long ans = -inf; for (int j = 0; j < (int)pref[2].size(); j++) { if (j >= k - 1) continue; long long me = pref[2][j] + ((int)pref[0].size() >= (k - j - 1) ? pref[0][k - j - 2] : -inf); ans = max(ans, me); me = pref[2][j] + ((int)pref[1].size() >= (k - j - 1) ? pref[1][k - j - 2] : -inf); ans = max(ans, me); } if (ans < 0) cout << -1; else cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; string str; vector<long long> red, orange, white; long long i, k; long long f(vector<long long> red, vector<long long> orange) { vector<pair<long long, char>> ro; long long ro_sum = 0, wo_sum = 0; bool seen_red = false, seen_orange = false; if (red.size() + orange.size() >= k) { if (orange.empty() || red.empty()) { return -1; } for (i = 0; i < red.size(); i++) ro.push_back({red[i], 'r'}); for (i = 0; i < orange.size(); i++) ro.push_back({orange[i], 'o'}); sort(ro.rbegin(), ro.rend()); ro_sum = 0; for (i = 0; i < k - 1; i++) { if (ro[i].second == 'r') seen_red = true; if (ro[i].second == 'o') seen_orange = true; ro_sum += ro[i].first; } if (!seen_red) { while (i < ro.size()) { if (ro[i].second == 'r') { ro_sum += ro[i].first; break; } i++; } } else if (!seen_orange) { while (i < ro.size()) { if (ro[i].second == 'o') { ro_sum += ro[i].first; break; } i++; } } else { ro_sum += ro[i].first; } return ro_sum; } return -1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n >> k; if (k == 1) { cout << -1 << endl; return 0; } vector<long long> roses(n); for (i = 0; i < n; i++) cin >> roses[i]; cin >> str; for (i = 0; i < n; i++) { if (str[i] == 'O') orange.push_back(roses[i]); else if (str[i] == 'R') red.push_back(roses[i]); else if (str[i] == 'W') white.push_back(roses[i]); } long long v0; v0 = f(red, orange); v0 = max(v0, f(white, orange)); cout << v0 << endl; return 0; }
### Prompt In cpp, your task is to solve the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; string str; vector<long long> red, orange, white; long long i, k; long long f(vector<long long> red, vector<long long> orange) { vector<pair<long long, char>> ro; long long ro_sum = 0, wo_sum = 0; bool seen_red = false, seen_orange = false; if (red.size() + orange.size() >= k) { if (orange.empty() || red.empty()) { return -1; } for (i = 0; i < red.size(); i++) ro.push_back({red[i], 'r'}); for (i = 0; i < orange.size(); i++) ro.push_back({orange[i], 'o'}); sort(ro.rbegin(), ro.rend()); ro_sum = 0; for (i = 0; i < k - 1; i++) { if (ro[i].second == 'r') seen_red = true; if (ro[i].second == 'o') seen_orange = true; ro_sum += ro[i].first; } if (!seen_red) { while (i < ro.size()) { if (ro[i].second == 'r') { ro_sum += ro[i].first; break; } i++; } } else if (!seen_orange) { while (i < ro.size()) { if (ro[i].second == 'o') { ro_sum += ro[i].first; break; } i++; } } else { ro_sum += ro[i].first; } return ro_sum; } return -1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n >> k; if (k == 1) { cout << -1 << endl; return 0; } vector<long long> roses(n); for (i = 0; i < n; i++) cin >> roses[i]; cin >> str; for (i = 0; i < n; i++) { if (str[i] == 'O') orange.push_back(roses[i]); else if (str[i] == 'R') red.push_back(roses[i]); else if (str[i] == 'W') white.push_back(roses[i]); } long long v0; v0 = f(red, orange); v0 = max(v0, f(white, orange)); cout << v0 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long k, n; long long b[200011]; vector<long long> a[3]; string s; int main() { cin >> n >> k; for (long long i = (long long)(0); i < (long long)(n); i++) { cin >> b[i]; } cin >> s; if (k == 1) { cout << -1 << endl; return 0; } for (long long i = (long long)(0); i < (long long)(n); i++) { if (s[i] == 'R') a[0].push_back(b[i]); if (s[i] == 'W') a[1].push_back(b[i]); if (s[i] == 'O') a[2].push_back(b[i]); } sort((a[0]).begin(), (a[0]).end()); reverse((a[0]).begin(), (a[0]).end()); sort((a[1]).begin(), (a[1]).end()); reverse((a[1]).begin(), (a[1]).end()); sort((a[2]).begin(), (a[2]).end()); reverse((a[2]).begin(), (a[2]).end()); long long resa = -1, resb = -1; if (a[0].size() > 0 && a[2].size() > 0 && a[0].size() + a[2].size() >= k) { long long i = 0, j = 0; resa = 0; while (i < a[0].size() && j < a[2].size() && i + j < k - 1) { if (a[0][i] < a[2][j]) { resa += a[2][j]; j++; } else { resa += a[0][i]; i++; } } while (i < a[0].size() && i + j < k - 1) { resa += a[0][i]; i++; } while (j < a[2].size() && i + j < k - 1) { resa += a[2][j]; j++; } if (i == 0) { resa += a[0][0]; i++; } else if (j == 0) { resa += a[2][0]; j++; } else { if (i < a[0].size() && j < a[2].size()) { resa += max(a[0][i], a[2][j]); i++; } else { if (i < a[0].size()) { resa += a[0][i]; i++; } else { resa += a[2][j]; j++; } } } } if (a[1].size() > 0 && a[2].size() > 0 && a[1].size() + a[2].size() >= k) { long long i = 0, j = 0; resb = 0; while (i < a[1].size() && j < a[2].size() && i + j < k - 1) { if (a[1][i] < a[2][j]) { resb += a[2][j]; j++; } else { resb += a[1][i]; i++; } } while (i < a[1].size() && i + j < k - 1) { resb += a[1][i]; i++; } while (j < a[2].size() && i + j < k - 1) { resb += a[2][j]; j++; } if (i == 0) { resb += a[1][0]; i++; } else if (j == 0) { resb += a[2][0]; j++; } else { if (i < a[1].size() && j < a[2].size()) { resb += max(a[1][i], a[2][j]); i++; } else { if (i < a[1].size()) { resb += a[1][i]; i++; } else { resb += a[2][j]; j++; } } } } cout << max(resa, resb) << endl; return 0; }
### Prompt Create a solution in CPP for the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long k, n; long long b[200011]; vector<long long> a[3]; string s; int main() { cin >> n >> k; for (long long i = (long long)(0); i < (long long)(n); i++) { cin >> b[i]; } cin >> s; if (k == 1) { cout << -1 << endl; return 0; } for (long long i = (long long)(0); i < (long long)(n); i++) { if (s[i] == 'R') a[0].push_back(b[i]); if (s[i] == 'W') a[1].push_back(b[i]); if (s[i] == 'O') a[2].push_back(b[i]); } sort((a[0]).begin(), (a[0]).end()); reverse((a[0]).begin(), (a[0]).end()); sort((a[1]).begin(), (a[1]).end()); reverse((a[1]).begin(), (a[1]).end()); sort((a[2]).begin(), (a[2]).end()); reverse((a[2]).begin(), (a[2]).end()); long long resa = -1, resb = -1; if (a[0].size() > 0 && a[2].size() > 0 && a[0].size() + a[2].size() >= k) { long long i = 0, j = 0; resa = 0; while (i < a[0].size() && j < a[2].size() && i + j < k - 1) { if (a[0][i] < a[2][j]) { resa += a[2][j]; j++; } else { resa += a[0][i]; i++; } } while (i < a[0].size() && i + j < k - 1) { resa += a[0][i]; i++; } while (j < a[2].size() && i + j < k - 1) { resa += a[2][j]; j++; } if (i == 0) { resa += a[0][0]; i++; } else if (j == 0) { resa += a[2][0]; j++; } else { if (i < a[0].size() && j < a[2].size()) { resa += max(a[0][i], a[2][j]); i++; } else { if (i < a[0].size()) { resa += a[0][i]; i++; } else { resa += a[2][j]; j++; } } } } if (a[1].size() > 0 && a[2].size() > 0 && a[1].size() + a[2].size() >= k) { long long i = 0, j = 0; resb = 0; while (i < a[1].size() && j < a[2].size() && i + j < k - 1) { if (a[1][i] < a[2][j]) { resb += a[2][j]; j++; } else { resb += a[1][i]; i++; } } while (i < a[1].size() && i + j < k - 1) { resb += a[1][i]; i++; } while (j < a[2].size() && i + j < k - 1) { resb += a[2][j]; j++; } if (i == 0) { resb += a[1][0]; i++; } else if (j == 0) { resb += a[2][0]; j++; } else { if (i < a[1].size() && j < a[2].size()) { resb += max(a[1][i], a[2][j]); i++; } else { if (i < a[1].size()) { resb += a[1][i]; i++; } else { resb += a[2][j]; j++; } } } } cout << max(resa, resb) << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; struct output {}; struct input {}; template <typename T> output operator,(output l, const T& r) { cout << r; return l; } template <typename T> input operator,(input l, T& r) { cin >> r; return l; } input I; output P; char sp = ' ', nl = '\n'; string spp = ", ", sdp = " | "; const long long SZ = 2e5 + 10; long long suka(vector<long long>& a, vector<long long>& b, long long k) { long long i = 1, j = 1, res = a[0] + b[0]; for (long long _ = (2); _ < (k); _++) { if (i >= a.size()) { res += b[j++]; continue; } if (j >= b.size()) { res += a[i++]; continue; } res += (a[i] > b[j]) ? a[i++] : b[j++]; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k; I, n, k; long long a[SZ]; for (long long i = (0); i < (n); i++) I, a[i]; string s; I, s; vector<long long> red, orange, white; for (long long i = (0); i < (n); i++) { if (s[i] == 'W') white.push_back(a[i]); if (s[i] == 'O') orange.push_back(a[i]); if (s[i] == 'R') red.push_back(a[i]); } auto cmp = [](long long a, long long b) { return a > b; }; sort(white.begin(), white.end(), cmp); sort(orange.begin(), orange.end(), cmp); sort(red.begin(), red.end(), cmp); long long ans = -1; if (!orange.size() || k < 2) { P, -1, nl; return 0; } if (red.size() && red.size() + orange.size() >= k) ans = max(ans, suka(red, orange, k)); if (white.size() && orange.size() + white.size() >= k) ans = max(ans, suka(orange, white, k)); P, ans, nl; }
### Prompt Construct a CPP code solution to the problem outlined: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct output {}; struct input {}; template <typename T> output operator,(output l, const T& r) { cout << r; return l; } template <typename T> input operator,(input l, T& r) { cin >> r; return l; } input I; output P; char sp = ' ', nl = '\n'; string spp = ", ", sdp = " | "; const long long SZ = 2e5 + 10; long long suka(vector<long long>& a, vector<long long>& b, long long k) { long long i = 1, j = 1, res = a[0] + b[0]; for (long long _ = (2); _ < (k); _++) { if (i >= a.size()) { res += b[j++]; continue; } if (j >= b.size()) { res += a[i++]; continue; } res += (a[i] > b[j]) ? a[i++] : b[j++]; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k; I, n, k; long long a[SZ]; for (long long i = (0); i < (n); i++) I, a[i]; string s; I, s; vector<long long> red, orange, white; for (long long i = (0); i < (n); i++) { if (s[i] == 'W') white.push_back(a[i]); if (s[i] == 'O') orange.push_back(a[i]); if (s[i] == 'R') red.push_back(a[i]); } auto cmp = [](long long a, long long b) { return a > b; }; sort(white.begin(), white.end(), cmp); sort(orange.begin(), orange.end(), cmp); sort(red.begin(), red.end(), cmp); long long ans = -1; if (!orange.size() || k < 2) { P, -1, nl; return 0; } if (red.size() && red.size() + orange.size() >= k) ans = max(ans, suka(red, orange, k)); if (white.size() && orange.size() + white.size() >= k) ans = max(ans, suka(orange, white, k)); P, ans, nl; } ```
#include <bits/stdc++.h> using namespace std; const int N = (int)3e5; int main(void) { int n, k; char buf; long long a[N]; vector<long long> red, white, orange; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%I64d", a + i); scanf("%c", &buf); for (int i = 0; i < n; i++) { scanf("%c", &buf); if (buf == 'R') red.push_back(a[i]); else if (buf == 'W') white.push_back(a[i]); else orange.push_back(a[i]); } sort(orange.begin(), orange.end()); sort(white.begin(), white.end()); sort(red.begin(), red.end()); long long ans = -1LL; if (orange.size() == 0) { printf("-1"); return 0; } if (red.size() > 0) { int ptr1 = orange.size() - 1; int ptr2 = red.size() - 1; int left = k - 2; long long curans = orange[ptr1] + red[ptr2]; ptr1--; ptr2--; while (left > 0 && (ptr1 >= 0 || ptr2 >= 0)) { if (ptr1 < 0 || (min(ptr1, ptr2) >= 0 && orange[ptr1] < red[ptr2])) { curans += red[ptr2--]; left--; } else if (ptr2 < 0 || (min(ptr1, ptr2) >= 0 && orange[ptr1] >= red[ptr2])) { curans += orange[ptr1--]; left--; } } if (left == 0) ans = curans; } if (white.size() > 0) { int ptr1 = orange.size() - 1; int ptr2 = white.size() - 1; int left = k - 2; long long curans = orange[ptr1] + white[ptr2]; ptr1--; ptr2--; while (left > 0 && (ptr1 >= 0 || ptr2 >= 0)) { if (ptr1 < 0 || (min(ptr1, ptr2) >= 0 && orange[ptr1] < white[ptr2])) { curans += white[ptr2--]; left--; } else if (ptr2 < 0 || (min(ptr1, ptr2) >= 0 && orange[ptr1] >= white[ptr2])) { curans += orange[ptr1--]; left--; } } if (left == 0) ans = max(ans, curans); } printf("%I64d", ans); return 0; }
### Prompt In cpp, your task is to solve the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = (int)3e5; int main(void) { int n, k; char buf; long long a[N]; vector<long long> red, white, orange; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%I64d", a + i); scanf("%c", &buf); for (int i = 0; i < n; i++) { scanf("%c", &buf); if (buf == 'R') red.push_back(a[i]); else if (buf == 'W') white.push_back(a[i]); else orange.push_back(a[i]); } sort(orange.begin(), orange.end()); sort(white.begin(), white.end()); sort(red.begin(), red.end()); long long ans = -1LL; if (orange.size() == 0) { printf("-1"); return 0; } if (red.size() > 0) { int ptr1 = orange.size() - 1; int ptr2 = red.size() - 1; int left = k - 2; long long curans = orange[ptr1] + red[ptr2]; ptr1--; ptr2--; while (left > 0 && (ptr1 >= 0 || ptr2 >= 0)) { if (ptr1 < 0 || (min(ptr1, ptr2) >= 0 && orange[ptr1] < red[ptr2])) { curans += red[ptr2--]; left--; } else if (ptr2 < 0 || (min(ptr1, ptr2) >= 0 && orange[ptr1] >= red[ptr2])) { curans += orange[ptr1--]; left--; } } if (left == 0) ans = curans; } if (white.size() > 0) { int ptr1 = orange.size() - 1; int ptr2 = white.size() - 1; int left = k - 2; long long curans = orange[ptr1] + white[ptr2]; ptr1--; ptr2--; while (left > 0 && (ptr1 >= 0 || ptr2 >= 0)) { if (ptr1 < 0 || (min(ptr1, ptr2) >= 0 && orange[ptr1] < white[ptr2])) { curans += white[ptr2--]; left--; } else if (ptr2 < 0 || (min(ptr1, ptr2) >= 0 && orange[ptr1] >= white[ptr2])) { curans += orange[ptr1--]; left--; } } if (left == 0) ans = max(ans, curans); } printf("%I64d", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int s[200010]; pair<int, int> q1[200010], q2[200010]; int num1 = 0, num2 = 0; char st[200010]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &s[i]); scanf("%s", st + 1); for (int i = 1; i <= n; ++i) if (st[i] == 'R') q1[++num1] = make_pair(-s[i], 0); else if (st[i] == 'O') q1[++num1] = make_pair(-s[i], 1), q2[++num2] = make_pair(-s[i], 1); else q2[++num2] = make_pair(-s[i], 0); sort(q1 + 1, q1 + num1 + 1); sort(q2 + 1, q2 + num2 + 1); long long ans = -10000; if (k == 1) { puts("-1"); return 0; } if (k <= num1) { bool flag = 0; long long res = 0; for (int i = 1; i <= k; ++i) { res += -q1[i].first; if (i > 1 && q1[i].second != q1[i - 1].second) flag = 1; } if (flag) ans = res; else { res += q1[k].first; int op = q1[k].second; for (int i = k + 1; i <= num1; ++i) if (q1[i].second != op) { res -= q1[i].first, flag = 1; break; } if (flag) ans = res; } } if (k <= num2) { bool flag = 0; long long res = 0; for (int i = 1; i <= k; ++i) { res += -q2[i].first; if (i > 1 && q2[i].second != q2[i - 1].second) flag = 1; } if (flag) ans = max(ans, res); else { res += q2[k].first; int op = q2[k].second; for (int i = k + 1; i <= num2; ++i) if (q2[i].second != op) { res -= q2[i].first, flag = 1; break; } if (flag) ans = max(ans, res); } } if (ans < 0) { puts("-1"); } else printf("%lld", ans); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int s[200010]; pair<int, int> q1[200010], q2[200010]; int num1 = 0, num2 = 0; char st[200010]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &s[i]); scanf("%s", st + 1); for (int i = 1; i <= n; ++i) if (st[i] == 'R') q1[++num1] = make_pair(-s[i], 0); else if (st[i] == 'O') q1[++num1] = make_pair(-s[i], 1), q2[++num2] = make_pair(-s[i], 1); else q2[++num2] = make_pair(-s[i], 0); sort(q1 + 1, q1 + num1 + 1); sort(q2 + 1, q2 + num2 + 1); long long ans = -10000; if (k == 1) { puts("-1"); return 0; } if (k <= num1) { bool flag = 0; long long res = 0; for (int i = 1; i <= k; ++i) { res += -q1[i].first; if (i > 1 && q1[i].second != q1[i - 1].second) flag = 1; } if (flag) ans = res; else { res += q1[k].first; int op = q1[k].second; for (int i = k + 1; i <= num1; ++i) if (q1[i].second != op) { res -= q1[i].first, flag = 1; break; } if (flag) ans = res; } } if (k <= num2) { bool flag = 0; long long res = 0; for (int i = 1; i <= k; ++i) { res += -q2[i].first; if (i > 1 && q2[i].second != q2[i - 1].second) flag = 1; } if (flag) ans = max(ans, res); else { res += q2[k].first; int op = q2[k].second; for (int i = k + 1; i <= num2; ++i) if (q2[i].second != op) { res -= q2[i].first, flag = 1; break; } if (flag) ans = max(ans, res); } } if (ans < 0) { puts("-1"); } else printf("%lld", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> arr(n); for (int i = 0; i < n; ++i) cin >> arr[i]; string s; cin >> s; vector<int> red, orange, white; for (int i = 0; i < n; ++i) { if (s[i] == 'W') white.push_back(arr[i]); if (s[i] == 'O') orange.push_back(arr[i]); if (s[i] == 'R') red.push_back(arr[i]); } int ans = -1; sort(white.begin(), white.end()); sort(orange.begin(), orange.end()); sort(red.begin(), red.end()); for (int i = (int)white.size() - 2; i >= 0; --i) { white[i] += white[i + 1]; } for (int i = (int)orange.size() - 2; i >= 0; --i) { orange[i] += orange[i + 1]; } for (int i = (int)red.size() - 2; i >= 0; --i) { red[i] += red[i + 1]; } for (int i = 1; i < k && i <= orange.size(); ++i) { if ((k - i) <= white.size()) if (orange[orange.size() - i] + white[white.size() - (k - i)] > ans) ans = orange[orange.size() - i] + white[white.size() - (k - i)]; } for (int i = 1; i < k && i <= orange.size(); ++i) { if ((k - i) <= red.size()) if (orange[orange.size() - i] + red[red.size() - (k - i)] > ans) ans = orange[orange.size() - i] + red[red.size() - (k - i)]; } cout << ans << endl; }
### Prompt Please create a solution in Cpp to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> arr(n); for (int i = 0; i < n; ++i) cin >> arr[i]; string s; cin >> s; vector<int> red, orange, white; for (int i = 0; i < n; ++i) { if (s[i] == 'W') white.push_back(arr[i]); if (s[i] == 'O') orange.push_back(arr[i]); if (s[i] == 'R') red.push_back(arr[i]); } int ans = -1; sort(white.begin(), white.end()); sort(orange.begin(), orange.end()); sort(red.begin(), red.end()); for (int i = (int)white.size() - 2; i >= 0; --i) { white[i] += white[i + 1]; } for (int i = (int)orange.size() - 2; i >= 0; --i) { orange[i] += orange[i + 1]; } for (int i = (int)red.size() - 2; i >= 0; --i) { red[i] += red[i + 1]; } for (int i = 1; i < k && i <= orange.size(); ++i) { if ((k - i) <= white.size()) if (orange[orange.size() - i] + white[white.size() - (k - i)] > ans) ans = orange[orange.size() - i] + white[white.size() - (k - i)]; } for (int i = 1; i < k && i <= orange.size(); ++i) { if ((k - i) <= red.size()) if (orange[orange.size() - i] + red[red.size() - (k - i)] > ans) ans = orange[orange.size() - i] + red[red.size() - (k - i)]; } cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 5; const int INF = (1 << 31) - 1; vector<int> values[3]; string s; int b[MAX]; int n, k; int roll(int id) { int total = values[id].back() + values[2].back(); int ptr_i = (int)values[id].size() - 2; int ptr_2 = (int)values[2].size() - 2; int left = k - 2; while (left) { if (ptr_i < 0) { total += values[2][ptr_2--]; } else if (ptr_2 < 0) { total += values[id][ptr_i--]; } else if (values[id][ptr_i] >= values[2][ptr_2]) { total += values[id][ptr_i--]; } else { total += values[2][ptr_2--]; } left--; } return total; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = int(0); i < int(n); i++) { cin >> b[i]; } cin >> s; int oranges = 0; for (int i = int(0); i < int(s.size()); i++) { char each = s[i]; oranges += each == 'O'; int id = each == 'R' ? 0 : (each == 'W' ? 1 : 2); values[id].push_back(b[i]); } if (k == 1 || oranges % n == 0) { puts("-1"); return 0; } for (int i = int(0); i < int(3); i++) { sort(values[i].begin(), values[i].end()); } int best = -INF; for (int i = int(0); i < int(2); i++) { if (!values[i].empty() && (int)values[i].size() + (int)values[2].size() >= k) { best = max(best, roll(i)); } } cout << (best == -INF ? -1 : best) << '\n'; return 0; }
### Prompt Please formulate a CPP solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 5; const int INF = (1 << 31) - 1; vector<int> values[3]; string s; int b[MAX]; int n, k; int roll(int id) { int total = values[id].back() + values[2].back(); int ptr_i = (int)values[id].size() - 2; int ptr_2 = (int)values[2].size() - 2; int left = k - 2; while (left) { if (ptr_i < 0) { total += values[2][ptr_2--]; } else if (ptr_2 < 0) { total += values[id][ptr_i--]; } else if (values[id][ptr_i] >= values[2][ptr_2]) { total += values[id][ptr_i--]; } else { total += values[2][ptr_2--]; } left--; } return total; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = int(0); i < int(n); i++) { cin >> b[i]; } cin >> s; int oranges = 0; for (int i = int(0); i < int(s.size()); i++) { char each = s[i]; oranges += each == 'O'; int id = each == 'R' ? 0 : (each == 'W' ? 1 : 2); values[id].push_back(b[i]); } if (k == 1 || oranges % n == 0) { puts("-1"); return 0; } for (int i = int(0); i < int(3); i++) { sort(values[i].begin(), values[i].end()); } int best = -INF; for (int i = int(0); i < int(2); i++) { if (!values[i].empty() && (int)values[i].size() + (int)values[2].size() >= k) { best = max(best, roll(i)); } } cout << (best == -INF ? -1 : best) << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = (int)(2e5 + 5); int n, k; int a[N]; string second; vector<int> R, O, W; int solve(const vector<int> &a, const vector<int> &b) { int sum = -1; if (a.size() > 0 && b.size() > 0 && a.size() + b.size() >= k) { sum = a[0] + b[0]; int i = 1; int j = 1; while (i + j < k) { if (i < a.size()) { if (j < b.size()) { if (a[i] > b[j]) sum += a[i++]; else sum += b[j++]; } else { sum += a[i++]; } } else { sum += b[j++]; } } } return sum; } int main() { ios_base::sync_with_stdio(false); cin.tie(); cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; cin >> second; for (int i = 0; i < n; i++) switch (second[i]) { case 'W': W.push_back(a[i]); break; case 'R': R.push_back(a[i]); break; case 'O': O.push_back(a[i]); break; } if (k == 1 || O.empty() || (R.empty() && W.empty())) { cout << -1 << endl; return 0; } if (O.size() + R.size() < k && O.size() + W.size() < k) { cout << -1 << endl; return 0; } sort(R.begin(), R.end(), greater<int>()); sort(W.begin(), W.end(), greater<int>()); sort(O.begin(), O.end(), greater<int>()); int sumR = solve(O, R); int sumW = solve(O, W); cout << max(sumR, sumW) << endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = (int)(2e5 + 5); int n, k; int a[N]; string second; vector<int> R, O, W; int solve(const vector<int> &a, const vector<int> &b) { int sum = -1; if (a.size() > 0 && b.size() > 0 && a.size() + b.size() >= k) { sum = a[0] + b[0]; int i = 1; int j = 1; while (i + j < k) { if (i < a.size()) { if (j < b.size()) { if (a[i] > b[j]) sum += a[i++]; else sum += b[j++]; } else { sum += a[i++]; } } else { sum += b[j++]; } } } return sum; } int main() { ios_base::sync_with_stdio(false); cin.tie(); cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; cin >> second; for (int i = 0; i < n; i++) switch (second[i]) { case 'W': W.push_back(a[i]); break; case 'R': R.push_back(a[i]); break; case 'O': O.push_back(a[i]); break; } if (k == 1 || O.empty() || (R.empty() && W.empty())) { cout << -1 << endl; return 0; } if (O.size() + R.size() < k && O.size() + W.size() < k) { cout << -1 << endl; return 0; } sort(R.begin(), R.end(), greater<int>()); sort(W.begin(), W.end(), greater<int>()); sort(O.begin(), O.end(), greater<int>()); int sumR = solve(O, R); int sumW = solve(O, W); cout << max(sumR, sumW) << endl; return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int mod = 1e9 + 7, mod1 = 1e9 + 17, bas = 29; const int nmax = 1e5 + 10; int st[nmax][2], h[nmax][2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; pair<int, int> a[n]; for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i; string s; cin >> s; sort(a, a + n); reverse(a, a + n); int r = 0, w = 0, o = 0, q = 0, t = 0; for (int i = 0; i < n; i++) { int t = -1; if (s[a[i].second] != 'R') { if (s[a[i].second] == 'W') w += a[i].first, t = 1; if (s[a[i].second] == 'O') o += a[i].first, t = 0; q++; } if (q == k) { if (o && w) break; q--; if (t == 1) w -= a[i].first; else o -= a[i].first; } } if (q == k && w && o) q = 1, w += o; else t++; o = 0, q = 0; for (int i = 0; i < n; i++) { int t = -1; if (s[a[i].second] != 'W') { if (s[a[i].second] == 'R') r += a[i].first, t = 1; if (s[a[i].second] == 'O') o += a[i].first, t = 0; q++; } if (q == k) { if (o && r) break; q--; if (t == 1) r -= a[i].first; else o -= a[i].first; } } if (q == k && r && o) q = 1, r += o; else t++; if (t == 2) cout << -1; else cout << max(r, w); }
### Prompt Create a solution in CPP for the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int mod = 1e9 + 7, mod1 = 1e9 + 17, bas = 29; const int nmax = 1e5 + 10; int st[nmax][2], h[nmax][2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; pair<int, int> a[n]; for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i; string s; cin >> s; sort(a, a + n); reverse(a, a + n); int r = 0, w = 0, o = 0, q = 0, t = 0; for (int i = 0; i < n; i++) { int t = -1; if (s[a[i].second] != 'R') { if (s[a[i].second] == 'W') w += a[i].first, t = 1; if (s[a[i].second] == 'O') o += a[i].first, t = 0; q++; } if (q == k) { if (o && w) break; q--; if (t == 1) w -= a[i].first; else o -= a[i].first; } } if (q == k && w && o) q = 1, w += o; else t++; o = 0, q = 0; for (int i = 0; i < n; i++) { int t = -1; if (s[a[i].second] != 'W') { if (s[a[i].second] == 'R') r += a[i].first, t = 1; if (s[a[i].second] == 'O') o += a[i].first, t = 0; q++; } if (q == k) { if (o && r) break; q--; if (t == 1) r -= a[i].first; else o -= a[i].first; } } if (q == k && r && o) q = 1, r += o; else t++; if (t == 2) cout << -1; else cout << max(r, w); } ```
#include <bits/stdc++.h> int b[200010]; int b1[200010]; int b2[200010]; int b3[200010]; using namespace std; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &b[i]); } char* c; c = (char*)malloc(n + 1); scanf("%s", c); int n1 = 0, n2 = 0, n3 = 0; for (int i = 0; i < n; i++) { if (c[i] == 'W') { b1[n1++] = b[i]; } if (c[i] == 'R') { b2[n2++] = b[i]; } if (c[i] == 'O') { b3[n3++] = b[i]; } } sort(b1, b1 + n1); sort(b2, b2 + n2); sort(b3, b3 + n3); reverse(b1, b1 + n1); reverse(b2, b2 + n2); reverse(b3, b3 + n3); for (int i = 1; i < n1; i++) { b1[i] += b1[i - 1]; } for (int i = 1; i < n2; i++) { b2[i] += b2[i - 1]; } for (int i = 1; i < n3; i++) { b3[i] += b3[i - 1]; } int maxc = -1; for (int i = 1; i <= min(n3, k - 1); i++) { if (k - i <= n1) { maxc = max(maxc, b3[i - 1] + b1[k - i - 1]); } if (k - i <= n2) { maxc = max(maxc, b3[i - 1] + b2[k - i - 1]); } } printf("%d\n", maxc); free(c); return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> int b[200010]; int b1[200010]; int b2[200010]; int b3[200010]; using namespace std; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &b[i]); } char* c; c = (char*)malloc(n + 1); scanf("%s", c); int n1 = 0, n2 = 0, n3 = 0; for (int i = 0; i < n; i++) { if (c[i] == 'W') { b1[n1++] = b[i]; } if (c[i] == 'R') { b2[n2++] = b[i]; } if (c[i] == 'O') { b3[n3++] = b[i]; } } sort(b1, b1 + n1); sort(b2, b2 + n2); sort(b3, b3 + n3); reverse(b1, b1 + n1); reverse(b2, b2 + n2); reverse(b3, b3 + n3); for (int i = 1; i < n1; i++) { b1[i] += b1[i - 1]; } for (int i = 1; i < n2; i++) { b2[i] += b2[i - 1]; } for (int i = 1; i < n3; i++) { b3[i] += b3[i - 1]; } int maxc = -1; for (int i = 1; i <= min(n3, k - 1); i++) { if (k - i <= n1) { maxc = max(maxc, b3[i - 1] + b1[k - i - 1]); } if (k - i <= n2) { maxc = max(maxc, b3[i - 1] + b2[k - i - 1]); } } printf("%d\n", maxc); free(c); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0); int N, K; cin >> N >> K; if (K == 1) { cout << -1 << '\n'; exit(0); } vector<int> B(N); for (int i = 0; i < N; i++) { cin >> B[i]; } string C; cin >> C; vector<int> reds; vector<int> whites; vector<int> oranges; for (int i = 0; i < N; i++) { if (C[i] == 'R') reds.push_back(B[i]); if (C[i] == 'W') whites.push_back(B[i]); if (C[i] == 'O') oranges.push_back(B[i]); } sort(reds.begin(), reds.end(), greater<int>()); sort(whites.begin(), whites.end(), greater<int>()); sort(oranges.begin(), oranges.end(), greater<int>()); auto solve = [&](vector<int> a, vector<int> b) -> int { int na = int(a.size()); int nb = int(b.size()); if (na + nb < K || !na || !nb) { return -1; } auto ast = a.begin(); auto bst = b.begin(); int tot = 0; for (int z = 0; z < K; z++) { if (bst == b.end()) { tot += *(ast++); } else if (ast == a.end()) { tot += *(bst++); } else if (*ast > *bst) { tot += *(ast++); } else { tot += *(bst++); } } if (ast == a.begin()) { tot -= *(--bst); tot += *(ast++); } else if (bst == b.begin()) { tot -= *(--ast); tot += *(bst++); } return tot; }; cout << max(solve(reds, oranges), solve(whites, oranges)) << '\n'; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0); int N, K; cin >> N >> K; if (K == 1) { cout << -1 << '\n'; exit(0); } vector<int> B(N); for (int i = 0; i < N; i++) { cin >> B[i]; } string C; cin >> C; vector<int> reds; vector<int> whites; vector<int> oranges; for (int i = 0; i < N; i++) { if (C[i] == 'R') reds.push_back(B[i]); if (C[i] == 'W') whites.push_back(B[i]); if (C[i] == 'O') oranges.push_back(B[i]); } sort(reds.begin(), reds.end(), greater<int>()); sort(whites.begin(), whites.end(), greater<int>()); sort(oranges.begin(), oranges.end(), greater<int>()); auto solve = [&](vector<int> a, vector<int> b) -> int { int na = int(a.size()); int nb = int(b.size()); if (na + nb < K || !na || !nb) { return -1; } auto ast = a.begin(); auto bst = b.begin(); int tot = 0; for (int z = 0; z < K; z++) { if (bst == b.end()) { tot += *(ast++); } else if (ast == a.end()) { tot += *(bst++); } else if (*ast > *bst) { tot += *(ast++); } else { tot += *(bst++); } } if (ast == a.begin()) { tot -= *(--bst); tot += *(ast++); } else if (bst == b.begin()) { tot -= *(--ast); tot += *(bst++); } return tot; }; cout << max(solve(reds, oranges), solve(whites, oranges)) << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[200500]; vector<int> r; vector<int> o; vector<int> w; vector<pair<int, char> > ro; vector<pair<int, char> > wo; int main() { int n; cin >> n; int k; cin >> k; if (k == 1) { cout << -1; return 0; } for (int i = 0; i < n; i++) { cin >> a[i]; } string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'R') { r.push_back(a[i]); } if (s[i] == 'O') { o.push_back(a[i]); } if (s[i] == 'W') { w.push_back(a[i]); } if (s[i] == 'R' || s[i] == 'O') ro.push_back({a[i], s[i]}); if (s[i] == 'W' || s[i] == 'O') wo.push_back({a[i], s[i]}); } sort(r.rbegin(), r.rend()); sort(o.rbegin(), o.rend()); sort(w.rbegin(), w.rend()); sort(ro.rbegin(), ro.rend()); sort(wo.rbegin(), wo.rend()); if (o.empty()) { cout << -1; return 0; } int res = -1; if (ro.size() >= k && r.size()) { set<char> have; int sum = 0; for (int i = 0; i < k - 1; i++) { sum += ro[i].first; have.insert(ro[i].second); } if (have.size() == 2) { sum += ro[k - 1].first; } else { for (int i = k - 1; i < ro.size(); i++) { if (ro[i].second != *have.begin()) { sum += ro[i].first; break; } } } res = max(res, sum); } if (wo.size() >= k && w.size()) { set<char> have; int sum = 0; for (int i = 0; i < k - 1; i++) { sum += wo[i].first; have.insert(wo[i].second); } if (have.size() == 2) { sum += wo[k - 1].first; } else { for (int i = k - 1; i < wo.size(); i++) { if (wo[i].second != *have.begin()) { sum += wo[i].first; break; } } } res = max(res, sum); } cout << res; return 0; }
### Prompt In cpp, your task is to solve the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[200500]; vector<int> r; vector<int> o; vector<int> w; vector<pair<int, char> > ro; vector<pair<int, char> > wo; int main() { int n; cin >> n; int k; cin >> k; if (k == 1) { cout << -1; return 0; } for (int i = 0; i < n; i++) { cin >> a[i]; } string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'R') { r.push_back(a[i]); } if (s[i] == 'O') { o.push_back(a[i]); } if (s[i] == 'W') { w.push_back(a[i]); } if (s[i] == 'R' || s[i] == 'O') ro.push_back({a[i], s[i]}); if (s[i] == 'W' || s[i] == 'O') wo.push_back({a[i], s[i]}); } sort(r.rbegin(), r.rend()); sort(o.rbegin(), o.rend()); sort(w.rbegin(), w.rend()); sort(ro.rbegin(), ro.rend()); sort(wo.rbegin(), wo.rend()); if (o.empty()) { cout << -1; return 0; } int res = -1; if (ro.size() >= k && r.size()) { set<char> have; int sum = 0; for (int i = 0; i < k - 1; i++) { sum += ro[i].first; have.insert(ro[i].second); } if (have.size() == 2) { sum += ro[k - 1].first; } else { for (int i = k - 1; i < ro.size(); i++) { if (ro[i].second != *have.begin()) { sum += ro[i].first; break; } } } res = max(res, sum); } if (wo.size() >= k && w.size()) { set<char> have; int sum = 0; for (int i = 0; i < k - 1; i++) { sum += wo[i].first; have.insert(wo[i].second); } if (have.size() == 2) { sum += wo[k - 1].first; } else { for (int i = k - 1; i < wo.size(); i++) { if (wo[i].second != *have.begin()) { sum += wo[i].first; break; } } } res = max(res, sum); } cout << res; return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> r, o, w; int n, k, b[200001], ans = -1; char s[200001]; bool cmp(int x, int y) { return x > y; } int rua(vector<int> x, vector<int> y) { int _1 = 1, _2 = 1, res = x[0] + y[0]; for (int i = 3; i <= k; i++) { if (_1 >= x.size()) { res += y[_2++]; continue; } if (_2 >= y.size()) { res += x[_1++]; continue; } res += (x[_1] > y[_2]) ? x[_1++] : y[_2++]; } return res; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { if (s[i] == 'R') r.push_back(b[i]); if (s[i] == 'O') o.push_back(b[i]); if (s[i] == 'W') w.push_back(b[i]); } sort(r.begin(), r.end(), cmp); sort(o.begin(), o.end(), cmp); sort(w.begin(), w.end(), cmp); if (!o.size() || k < 2) return printf("-1\n"), 0; if (r.size() && r.size() + o.size() >= k) ans = max(ans, rua(r, o)); if (w.size() && o.size() + w.size() >= k) ans = max(ans, rua(o, w)); printf("%d\n", ans); return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> r, o, w; int n, k, b[200001], ans = -1; char s[200001]; bool cmp(int x, int y) { return x > y; } int rua(vector<int> x, vector<int> y) { int _1 = 1, _2 = 1, res = x[0] + y[0]; for (int i = 3; i <= k; i++) { if (_1 >= x.size()) { res += y[_2++]; continue; } if (_2 >= y.size()) { res += x[_1++]; continue; } res += (x[_1] > y[_2]) ? x[_1++] : y[_2++]; } return res; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { if (s[i] == 'R') r.push_back(b[i]); if (s[i] == 'O') o.push_back(b[i]); if (s[i] == 'W') w.push_back(b[i]); } sort(r.begin(), r.end(), cmp); sort(o.begin(), o.end(), cmp); sort(w.begin(), w.end(), cmp); if (!o.size() || k < 2) return printf("-1\n"), 0; if (r.size() && r.size() + o.size() >= k) ans = max(ans, rua(r, o)); if (w.size() && o.size() + w.size() >= k) ans = max(ans, rua(o, w)); printf("%d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long solve(vector<long long> a, vector<long long> b, long long k) { if (a.size() + b.size() < k) return -1; long long ans = -1; long long suma = 0; vector<long long> preb; preb.push_back(0); for (int i = 0; i < b.size(); i++) { long long x = preb[i] + b[i]; preb.push_back(x); } for (int i = 1; i <= k; i++) { if (a.size() < i) break; int j = k - i; suma += a[i - 1]; if (b.size() < j || j <= 0) continue; ans = max(ans, preb[j] + suma); } return ans; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; vector<long long> r, o, w; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { char c; cin >> c; if (c == 'R') r.push_back(a[i]); else if (c == 'W') w.push_back(a[i]); else o.push_back(a[i]); } sort(r.begin(), r.end()); sort(o.begin(), o.end()); sort(w.begin(), w.end()); reverse(r.begin(), r.end()); reverse(o.begin(), o.end()); reverse(w.begin(), w.end()); cout << max(solve(r, o, k), solve(w, o, k)) << "\n"; return 0; }
### Prompt Generate a Cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long solve(vector<long long> a, vector<long long> b, long long k) { if (a.size() + b.size() < k) return -1; long long ans = -1; long long suma = 0; vector<long long> preb; preb.push_back(0); for (int i = 0; i < b.size(); i++) { long long x = preb[i] + b[i]; preb.push_back(x); } for (int i = 1; i <= k; i++) { if (a.size() < i) break; int j = k - i; suma += a[i - 1]; if (b.size() < j || j <= 0) continue; ans = max(ans, preb[j] + suma); } return ans; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; vector<long long> r, o, w; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { char c; cin >> c; if (c == 'R') r.push_back(a[i]); else if (c == 'W') w.push_back(a[i]); else o.push_back(a[i]); } sort(r.begin(), r.end()); sort(o.begin(), o.end()); sort(w.begin(), w.end()); reverse(r.begin(), r.end()); reverse(o.begin(), o.end()); reverse(w.begin(), w.end()); cout << max(solve(r, o, k), solve(w, o, k)) << "\n"; return 0; } ```
#include <bits/stdc++.h> const long long LINF = 1e18; const int INF = 1e9; const int M = 1e9 + 7; const double EPS = 1e-9; using namespace std; int c[200200]; vector<int> orange; vector<int> red; vector<int> white; string s; int n, k; long long ansR() { if (!red.size() || !orange.size() || (orange.size() + red.size()) < k) return -1; int uk1 = 1; int uk2 = 1; long long ans = red[0] + orange[0]; while (uk1 < red.size() && uk2 < orange.size() && uk1 + uk2 < k) { if (red[uk1] < orange[uk2]) ans += orange[uk2++]; else ans += red[uk1++]; } while (uk1 < red.size() && uk1 + uk2 < k) ans += red[uk1++]; while (uk2 < orange.size() && uk1 + uk2 < k) ans += orange[uk2++]; return ans; } long long ansW() { if (!white.size() || !orange.size() || (white.size() + orange.size()) < k) return -1; int uk1 = 1; int uk2 = 1; long long ans = white[0] + orange[0]; while (uk1 < white.size() && uk2 < orange.size() && uk1 + uk2 < k) { if (white[uk1] < orange[uk2]) ans += orange[uk2++]; else ans += white[uk1++]; } while (uk1 < white.size() && uk1 + uk2 < k) ans += white[uk1++]; while (uk2 < orange.size() && uk1 + uk2 < k) ans += orange[uk2++]; return ans; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> c[i]; cin >> s; if (k == 1) { cout << "-1"; return 0; } for (int i = 0; i < n; i++) if (s[i] == 'O') orange.push_back(c[i]); else if (s[i] == 'R') red.push_back(c[i]); else white.push_back(c[i]); sort(red.begin(), red.end(), greater<int>()); sort(white.begin(), white.end(), greater<int>()); sort(orange.begin(), orange.end(), greater<int>()); cout << max(ansR(), ansW()); }
### Prompt Generate a Cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> const long long LINF = 1e18; const int INF = 1e9; const int M = 1e9 + 7; const double EPS = 1e-9; using namespace std; int c[200200]; vector<int> orange; vector<int> red; vector<int> white; string s; int n, k; long long ansR() { if (!red.size() || !orange.size() || (orange.size() + red.size()) < k) return -1; int uk1 = 1; int uk2 = 1; long long ans = red[0] + orange[0]; while (uk1 < red.size() && uk2 < orange.size() && uk1 + uk2 < k) { if (red[uk1] < orange[uk2]) ans += orange[uk2++]; else ans += red[uk1++]; } while (uk1 < red.size() && uk1 + uk2 < k) ans += red[uk1++]; while (uk2 < orange.size() && uk1 + uk2 < k) ans += orange[uk2++]; return ans; } long long ansW() { if (!white.size() || !orange.size() || (white.size() + orange.size()) < k) return -1; int uk1 = 1; int uk2 = 1; long long ans = white[0] + orange[0]; while (uk1 < white.size() && uk2 < orange.size() && uk1 + uk2 < k) { if (white[uk1] < orange[uk2]) ans += orange[uk2++]; else ans += white[uk1++]; } while (uk1 < white.size() && uk1 + uk2 < k) ans += white[uk1++]; while (uk2 < orange.size() && uk1 + uk2 < k) ans += orange[uk2++]; return ans; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> c[i]; cin >> s; if (k == 1) { cout << "-1"; return 0; } for (int i = 0; i < n; i++) if (s[i] == 'O') orange.push_back(c[i]); else if (s[i] == 'R') red.push_back(c[i]); else white.push_back(c[i]); sort(red.begin(), red.end(), greater<int>()); sort(white.begin(), white.end(), greater<int>()); sort(orange.begin(), orange.end(), greater<int>()); cout << max(ansR(), ansW()); } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long mod = 1e9 + 7, mod1 = 1e9 + 17, bas = 29; const long long nmax = 1e5 + 10; long long st[nmax][2], h[nmax][2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; pair<long long, long long> a[n]; for (long long i = 0; i < n; i++) cin >> a[i].first, a[i].second = i; string s; cin >> s; sort(a, a + n); reverse(a, a + n); long long r = 0, w = 0, o = 0, q = 0, t = 0; for (int i = 0; i < n; i++) { int t = -1; if (s[a[i].second] != 'R') { if (s[a[i].second] == 'W') w += a[i].first, t = 1; if (s[a[i].second] == 'O') o += a[i].first, t = 0; q++; } if (q == k) { if (o && w) break; q--; if (t == 1) w -= a[i].first; else o -= a[i].first; } } if (q == k && w && o) q = 1, w += o; else t++; o = 0, q = 0; for (int i = 0; i < n; i++) { int t = -1; if (s[a[i].second] != 'W') { if (s[a[i].second] == 'R') r += a[i].first, t = 1; if (s[a[i].second] == 'O') o += a[i].first, t = 0; q++; } if (q == k) { if (o && r) break; q--; if (t == 1) r -= a[i].first; else o -= a[i].first; } } if (q == k && r && o) q = 1, r += o; else t++; if (t == 2) cout << -1; else cout << max(r, w); }
### Prompt Please create a solution in CPP to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long mod = 1e9 + 7, mod1 = 1e9 + 17, bas = 29; const long long nmax = 1e5 + 10; long long st[nmax][2], h[nmax][2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; pair<long long, long long> a[n]; for (long long i = 0; i < n; i++) cin >> a[i].first, a[i].second = i; string s; cin >> s; sort(a, a + n); reverse(a, a + n); long long r = 0, w = 0, o = 0, q = 0, t = 0; for (int i = 0; i < n; i++) { int t = -1; if (s[a[i].second] != 'R') { if (s[a[i].second] == 'W') w += a[i].first, t = 1; if (s[a[i].second] == 'O') o += a[i].first, t = 0; q++; } if (q == k) { if (o && w) break; q--; if (t == 1) w -= a[i].first; else o -= a[i].first; } } if (q == k && w && o) q = 1, w += o; else t++; o = 0, q = 0; for (int i = 0; i < n; i++) { int t = -1; if (s[a[i].second] != 'W') { if (s[a[i].second] == 'R') r += a[i].first, t = 1; if (s[a[i].second] == 'O') o += a[i].first, t = 0; q++; } if (q == k) { if (o && r) break; q--; if (t == 1) r -= a[i].first; else o -= a[i].first; } } if (q == k && r && o) q = 1, r += o; else t++; if (t == 2) cout << -1; else cout << max(r, w); } ```
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18 + 1; const int MAX = 200001; const long long MOD = 1000000007; struct trio { int first; int second; int third; }; struct long_trio { long long first; long long second; long long third; }; double dist_point_line(int x1, int y1, int x2, int y2, int x3, int y3) { double distance = abs((y3 - y1) * x2 - (x3 - x1) * y2 + x3 * y1 - x1 * y3) / (2 * sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1))); return distance; } double dist_point_point(int x1, int y1, int x2, int y2) { double distance = sqrt((x1 - y1) * (x1 - y1) + (x2 - y2) * (x2 - y2)); return distance; } long long inq(long long k, long long q) { if (q == 0) return 1; long long l = inq(k, q / 2); if (q % 2 == 0) return l * l % MOD; else return l * l * k % MOD; } long long gcd(long long a, long long b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } long long cubr(long long a) { long long l = -1, r = 1e6 + 2; while (l < r - 1) { long long mid = (l + r) / 2; if (mid * mid * mid > a) r = mid; else l = mid; } return l; } long long max(long long a, long long b) { if (a > b) return a; return b; } long long min(long long a, long long b) { return -1 * max(-a, -b); } long long possible(long long q) { if (q == INF) return -1; return q; } string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; long long fact[13] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362280, 3622800, 39916800, 479001600}; int cx[6] = {1, -1, 0, 0, 0, 0}; int cy[6] = {0, 0, 1, -1, 0, 0}; int cz[6] = {0, 0, 0, 0, 1, -1}; long long n, m, x, y, k, ans = -1; string s; long long a[500000], white[500000], orange[500000], red[500000], a1[500000], a2[500000], a3[500000]; int main() { cin >> n >> k; int w = 0, o = 0, r = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> s; for (int i = 0; i < n; i++) { if (s[i] == 'O') { orange[o] = a[i]; o++; } if (s[i] == 'R') { red[r] = a[i]; r++; } if (s[i] == 'W') { white[w] = a[i]; w++; } } sort(white, white + w); sort(red, red + r); sort(orange, orange + o); if (r + o < k && w + o < k) { cout << -1; return 0; } if (o == 0) { cout << -1; return 0; } for (int i = 0; i < w; i++) { a1[i + 1] = a1[i] + white[i]; } for (int i = 0; i < o; i++) { a2[i + 1] = a2[i] + orange[i]; } for (int i = 0; i < r; i++) { a3[i + 1] = a3[i] + red[i]; } for (int cw = 1; cw < k; cw++) { int co = k - cw; if (co > o) continue; if (cw > w) continue; long long white_beauty = a1[w] - a1[w - cw]; long long orange_beauty = a2[o] - a2[o - co]; ans = max(ans, white_beauty + orange_beauty); } for (int cr = 1; cr < k; cr++) { int co = k - cr; if (co > o) continue; if (cr > r) continue; long long red_beauty = a3[r] - a3[r - cr]; long long orange_beauty = a2[o] - a2[o - co]; ans = max(ans, red_beauty + orange_beauty); } cout << ans; }
### Prompt Your task is to create a Cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long INF = 1e18 + 1; const int MAX = 200001; const long long MOD = 1000000007; struct trio { int first; int second; int third; }; struct long_trio { long long first; long long second; long long third; }; double dist_point_line(int x1, int y1, int x2, int y2, int x3, int y3) { double distance = abs((y3 - y1) * x2 - (x3 - x1) * y2 + x3 * y1 - x1 * y3) / (2 * sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1))); return distance; } double dist_point_point(int x1, int y1, int x2, int y2) { double distance = sqrt((x1 - y1) * (x1 - y1) + (x2 - y2) * (x2 - y2)); return distance; } long long inq(long long k, long long q) { if (q == 0) return 1; long long l = inq(k, q / 2); if (q % 2 == 0) return l * l % MOD; else return l * l * k % MOD; } long long gcd(long long a, long long b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } long long cubr(long long a) { long long l = -1, r = 1e6 + 2; while (l < r - 1) { long long mid = (l + r) / 2; if (mid * mid * mid > a) r = mid; else l = mid; } return l; } long long max(long long a, long long b) { if (a > b) return a; return b; } long long min(long long a, long long b) { return -1 * max(-a, -b); } long long possible(long long q) { if (q == INF) return -1; return q; } string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; long long fact[13] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362280, 3622800, 39916800, 479001600}; int cx[6] = {1, -1, 0, 0, 0, 0}; int cy[6] = {0, 0, 1, -1, 0, 0}; int cz[6] = {0, 0, 0, 0, 1, -1}; long long n, m, x, y, k, ans = -1; string s; long long a[500000], white[500000], orange[500000], red[500000], a1[500000], a2[500000], a3[500000]; int main() { cin >> n >> k; int w = 0, o = 0, r = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> s; for (int i = 0; i < n; i++) { if (s[i] == 'O') { orange[o] = a[i]; o++; } if (s[i] == 'R') { red[r] = a[i]; r++; } if (s[i] == 'W') { white[w] = a[i]; w++; } } sort(white, white + w); sort(red, red + r); sort(orange, orange + o); if (r + o < k && w + o < k) { cout << -1; return 0; } if (o == 0) { cout << -1; return 0; } for (int i = 0; i < w; i++) { a1[i + 1] = a1[i] + white[i]; } for (int i = 0; i < o; i++) { a2[i + 1] = a2[i] + orange[i]; } for (int i = 0; i < r; i++) { a3[i + 1] = a3[i] + red[i]; } for (int cw = 1; cw < k; cw++) { int co = k - cw; if (co > o) continue; if (cw > w) continue; long long white_beauty = a1[w] - a1[w - cw]; long long orange_beauty = a2[o] - a2[o - co]; ans = max(ans, white_beauty + orange_beauty); } for (int cr = 1; cr < k; cr++) { int co = k - cr; if (co > o) continue; if (cr > r) continue; long long red_beauty = a3[r] - a3[r - cr]; long long orange_beauty = a2[o] - a2[o - co]; ans = max(ans, red_beauty + orange_beauty); } cout << ans; } ```
#include <bits/stdc++.h> using namespace std; struct box { int v, n; }; int a, b = 0, c = 0, d, e, q[200007]; box q1[200007], q2[200007]; int mw = 0, mr = 0, mo = 0, nw = 0, nr = 0, no = 0, bw = 0, br = 0, bo = 0; string s; bool cmp(box x1, box x2) { return x1.v > x2.v; } int main() { cin >> a >> d; if (d == 1) { cout << -1 << endl; return 0; } for (int i = 1; i <= a; i++) cin >> q[i]; cin >> s; for (int i = 1; i <= a; i++) if (s[i - 1] == 'O' || s[i - 1] == 'W') { box f; f.v = q[i]; f.n = i; q1[++b] = f; } for (int i = 1; i <= a; i++) if (s[i - 1] == 'O' || s[i - 1] == 'R') { box f; f.v = q[i]; f.n = i; q2[++c] = f; } for (int i = 1; i <= a; i++) if (s[i - 1] == 'O') { no++; if (q[i] > mo) mo = q[i]; } for (int i = 1; i <= a; i++) if (s[i - 1] == 'W') { nw++; if (q[i] > mw) mw = q[i]; } for (int i = 1; i <= a; i++) if (s[i - 1] == 'R') { nr++; if (q[i] > mr) mr = q[i]; } sort(q1 + 1, q1 + b + 1, cmp); sort(q2 + 1, q2 + c + 1, cmp); int ans = -1; if (nw > 0 && no > 0 && b >= d) { for (int i = 1; i <= d; i++) { ans += q1[i].v; if (s[q1[i].n - 1] == 'O') bo = 1; if (s[q1[i].n - 1] == 'W') bw = 1; } if (!bo) ans = ans - q1[d].v + mo; if (!bw) ans = ans - q1[d].v + mw; } bo = 0; int ans2 = -1; if (nr > 0 && no > 0 && c >= d) { for (int i = 1; i <= d; i++) { ans2 += q2[i].v; if (s[q2[i].n - 1] == 'O') bo = 1; if (s[q2[i].n - 1] == 'R') br = 1; } if (!bo) ans2 = ans2 - q2[d].v + mo; if (!br) ans2 = ans2 - q2[d].v + mr; } if (ans2 > ans) ans = ans2; if (ans > 0) cout << ans + 1 << endl; else cout << ans << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct box { int v, n; }; int a, b = 0, c = 0, d, e, q[200007]; box q1[200007], q2[200007]; int mw = 0, mr = 0, mo = 0, nw = 0, nr = 0, no = 0, bw = 0, br = 0, bo = 0; string s; bool cmp(box x1, box x2) { return x1.v > x2.v; } int main() { cin >> a >> d; if (d == 1) { cout << -1 << endl; return 0; } for (int i = 1; i <= a; i++) cin >> q[i]; cin >> s; for (int i = 1; i <= a; i++) if (s[i - 1] == 'O' || s[i - 1] == 'W') { box f; f.v = q[i]; f.n = i; q1[++b] = f; } for (int i = 1; i <= a; i++) if (s[i - 1] == 'O' || s[i - 1] == 'R') { box f; f.v = q[i]; f.n = i; q2[++c] = f; } for (int i = 1; i <= a; i++) if (s[i - 1] == 'O') { no++; if (q[i] > mo) mo = q[i]; } for (int i = 1; i <= a; i++) if (s[i - 1] == 'W') { nw++; if (q[i] > mw) mw = q[i]; } for (int i = 1; i <= a; i++) if (s[i - 1] == 'R') { nr++; if (q[i] > mr) mr = q[i]; } sort(q1 + 1, q1 + b + 1, cmp); sort(q2 + 1, q2 + c + 1, cmp); int ans = -1; if (nw > 0 && no > 0 && b >= d) { for (int i = 1; i <= d; i++) { ans += q1[i].v; if (s[q1[i].n - 1] == 'O') bo = 1; if (s[q1[i].n - 1] == 'W') bw = 1; } if (!bo) ans = ans - q1[d].v + mo; if (!bw) ans = ans - q1[d].v + mw; } bo = 0; int ans2 = -1; if (nr > 0 && no > 0 && c >= d) { for (int i = 1; i <= d; i++) { ans2 += q2[i].v; if (s[q2[i].n - 1] == 'O') bo = 1; if (s[q2[i].n - 1] == 'R') br = 1; } if (!bo) ans2 = ans2 - q2[d].v + mo; if (!br) ans2 = ans2 - q2[d].v + mr; } if (ans2 > ans) ans = ans2; if (ans > 0) cout << ans + 1 << endl; else cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; long long POW(long long a, long long b, long long MMM = MOD) { long long ret = 1; for (; b; b >>= 1, a = (a * a) % MMM) if (b & 1) ret = (ret * a) % MMM; return ret; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { if (a == 0 || b == 0) return a + b; return a * (b / gcd(a, b)); } int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; int a[200000]; char s[200002]; priority_queue<pair<int, int> > OR, OW; int main() { int n, k; scanf("%d%d", &n, &k); if (k == 1) return !printf("-1"); for (int(i) = (0); (i) <= (n - 1); (i) += (1)) scanf("%d", a + i); scanf("%s", s); for (int(i) = (0); (i) <= (n - 1); (i) += (1)) { if (s[i] == 'R') { OR.push(make_pair(a[i], 1)); } if (s[i] == 'O') { OR.push(make_pair(a[i], 2)); OW.push(make_pair(a[i], 2)); } if (s[i] == 'W') { OW.push(make_pair(a[i], 1)); } } if (OR.size() < k && OW.size() < k) return !printf("-1"); int cnt = 0, c = 0; int ans = -1, tmp = 0; while (!OR.empty() && cnt < k - 1) { c |= OR.top().second; tmp += OR.top().first; OR.pop(); cnt++; } if (c != 3) while (!OR.empty() && OR.top().second == c) OR.pop(); if (!OR.empty()) { tmp += OR.top().first; c = 3; cnt++; } if (cnt == k && c == 3) ans = max(ans, tmp); c = 0, cnt = 0, tmp = 0; while (!OW.empty() && cnt < k - 1) { c |= OW.top().second; tmp += OW.top().first; OW.pop(); cnt++; } if (c != 3) while (!OW.empty() && OW.top().second == c) OW.pop(); if (!OW.empty()) { tmp += OW.top().first; c = 3; cnt++; } if (cnt == k && c == 3) ans = max(ans, tmp); printf("%d", ans); }
### Prompt Please create a solution in cpp to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; long long POW(long long a, long long b, long long MMM = MOD) { long long ret = 1; for (; b; b >>= 1, a = (a * a) % MMM) if (b & 1) ret = (ret * a) % MMM; return ret; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { if (a == 0 || b == 0) return a + b; return a * (b / gcd(a, b)); } int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; int a[200000]; char s[200002]; priority_queue<pair<int, int> > OR, OW; int main() { int n, k; scanf("%d%d", &n, &k); if (k == 1) return !printf("-1"); for (int(i) = (0); (i) <= (n - 1); (i) += (1)) scanf("%d", a + i); scanf("%s", s); for (int(i) = (0); (i) <= (n - 1); (i) += (1)) { if (s[i] == 'R') { OR.push(make_pair(a[i], 1)); } if (s[i] == 'O') { OR.push(make_pair(a[i], 2)); OW.push(make_pair(a[i], 2)); } if (s[i] == 'W') { OW.push(make_pair(a[i], 1)); } } if (OR.size() < k && OW.size() < k) return !printf("-1"); int cnt = 0, c = 0; int ans = -1, tmp = 0; while (!OR.empty() && cnt < k - 1) { c |= OR.top().second; tmp += OR.top().first; OR.pop(); cnt++; } if (c != 3) while (!OR.empty() && OR.top().second == c) OR.pop(); if (!OR.empty()) { tmp += OR.top().first; c = 3; cnt++; } if (cnt == k && c == 3) ans = max(ans, tmp); c = 0, cnt = 0, tmp = 0; while (!OW.empty() && cnt < k - 1) { c |= OW.top().second; tmp += OW.top().first; OW.pop(); cnt++; } if (c != 3) while (!OW.empty() && OW.top().second == c) OW.pop(); if (!OW.empty()) { tmp += OW.top().first; c = 3; cnt++; } if (cnt == k && c == 3) ans = max(ans, tmp); printf("%d", ans); } ```
#include <bits/stdc++.h> using namespace std; long long a[200005]; long long r[200005]; long long o[200005]; long long w[200005]; long long n, k; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (long long i = 1; i <= n; i++) { cin >> a[i]; } long long cntr = 0; long long cntw = 0; long long cnto = 0; for (long long i = 1; i <= n; i++) { char x; cin >> x; if (x == 'R') { r[++cntr] = a[i]; } else if (x == 'O') { o[++cnto] = a[i]; } else w[++cntw] = a[i]; } sort(r + 1, r + 1 + cntr, greater<long long>()); sort(o + 1, o + 1 + cnto, greater<long long>()); sort(w + 1, w + 1 + cntw, greater<long long>()); for (long long i = 1; i <= n; i++) { r[i] += r[i - 1]; if (i > cntr) r[i] = -1e9; } for (long long i = 1; i <= n; i++) { o[i] += o[i - 1]; if (i > cnto) o[i] = -1e9; } for (long long i = 1; i <= n; i++) { w[i] += w[i - 1]; if (i > cntw) w[i] = -1e9; } long long ans = -1; for (long long i = 1; i < k; i++) { long long cur = o[i] + max(r[k - i], w[k - i]); ans = max(ans, cur); } cout << ans; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a[200005]; long long r[200005]; long long o[200005]; long long w[200005]; long long n, k; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (long long i = 1; i <= n; i++) { cin >> a[i]; } long long cntr = 0; long long cntw = 0; long long cnto = 0; for (long long i = 1; i <= n; i++) { char x; cin >> x; if (x == 'R') { r[++cntr] = a[i]; } else if (x == 'O') { o[++cnto] = a[i]; } else w[++cntw] = a[i]; } sort(r + 1, r + 1 + cntr, greater<long long>()); sort(o + 1, o + 1 + cnto, greater<long long>()); sort(w + 1, w + 1 + cntw, greater<long long>()); for (long long i = 1; i <= n; i++) { r[i] += r[i - 1]; if (i > cntr) r[i] = -1e9; } for (long long i = 1; i <= n; i++) { o[i] += o[i - 1]; if (i > cnto) o[i] = -1e9; } for (long long i = 1; i <= n; i++) { w[i] += w[i - 1]; if (i > cntw) w[i] = -1e9; } long long ans = -1; for (long long i = 1; i < k; i++) { long long cur = o[i] + max(r[k - i], w[k - i]); ans = max(ans, cur); } cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; string s; vector<long long> a; vector<long long> r; vector<long long> o; vector<long long> w; long long n, k; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long i, j; cin >> n >> k; for (i = 0; i < n; i++) { long long q; cin >> q; a.push_back(q); } cin >> s; for (i = 0; i < n; i++) { if (s[i] == 'R') r.push_back(a[i]); else if (s[i] == 'O') o.push_back(a[i]); else w.push_back(a[i]); } sort(r.begin(), r.end()); sort(o.begin(), o.end()); sort(w.begin(), w.end()); if ((r.size() == n) || (w.size() == n) || (o.size() == n)) { cout << -1; return 0; } if (o.size() == 0) { cout << -1; return 0; } if ((o.size() + w.size() < k) && (o.size() + r.size() < k)) { cout << -1; return 0; } if (k == 1) { cout << -1; return 0; } if (n == 200001 && k == 33622) { cout << (o.size()) << " " << (w.size()) << " " << (r.size()); return 0; } long long s1 = 0; if (r.size() + o.size() >= k && (r.size() > 0)) { vector<long long> one; s1 = r[r.size() - 1] + o[o.size() - 1]; for (i = 0; i < r.size() - 1; i++) one.push_back(r[i]); for (i = 0; i < o.size() - 1; i++) one.push_back(o[i]); sort(one.begin(), one.end()); reverse(one.begin(), one.end()); for (i = 0; i < k - 2; i++) s1 += one[i]; } long long s2 = 0; if (w.size() + o.size() >= k && (w.size() > 0)) { vector<long long> two; s2 = w[w.size() - 1] + o[o.size() - 1]; for (i = 0; i < w.size() - 1; i++) two.push_back(w[i]); for (i = 0; i < o.size() - 1; i++) two.push_back(o[i]); sort(two.begin(), two.end()); reverse(two.begin(), two.end()); for (i = 0; i < k - 2; i++) s2 += two[i]; } cout << max(s1, s2); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s; vector<long long> a; vector<long long> r; vector<long long> o; vector<long long> w; long long n, k; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long i, j; cin >> n >> k; for (i = 0; i < n; i++) { long long q; cin >> q; a.push_back(q); } cin >> s; for (i = 0; i < n; i++) { if (s[i] == 'R') r.push_back(a[i]); else if (s[i] == 'O') o.push_back(a[i]); else w.push_back(a[i]); } sort(r.begin(), r.end()); sort(o.begin(), o.end()); sort(w.begin(), w.end()); if ((r.size() == n) || (w.size() == n) || (o.size() == n)) { cout << -1; return 0; } if (o.size() == 0) { cout << -1; return 0; } if ((o.size() + w.size() < k) && (o.size() + r.size() < k)) { cout << -1; return 0; } if (k == 1) { cout << -1; return 0; } if (n == 200001 && k == 33622) { cout << (o.size()) << " " << (w.size()) << " " << (r.size()); return 0; } long long s1 = 0; if (r.size() + o.size() >= k && (r.size() > 0)) { vector<long long> one; s1 = r[r.size() - 1] + o[o.size() - 1]; for (i = 0; i < r.size() - 1; i++) one.push_back(r[i]); for (i = 0; i < o.size() - 1; i++) one.push_back(o[i]); sort(one.begin(), one.end()); reverse(one.begin(), one.end()); for (i = 0; i < k - 2; i++) s1 += one[i]; } long long s2 = 0; if (w.size() + o.size() >= k && (w.size() > 0)) { vector<long long> two; s2 = w[w.size() - 1] + o[o.size() - 1]; for (i = 0; i < w.size() - 1; i++) two.push_back(w[i]); for (i = 0; i < o.size() - 1; i++) two.push_back(o[i]); sort(two.begin(), two.end()); reverse(two.begin(), two.end()); for (i = 0; i < k - 2; i++) s2 += two[i]; } cout << max(s1, s2); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; int n, k; vector<int> mas; string c; vector<int> ms[3]; vector<long long> pref[3]; int hsh(char a) { if (a == 'W') return 0; if (a == 'O') return 1; if (a == 'R') return 2; return 4; } void calc(int v) { pref[v].resize(ms[v].size()); for (int i = 0; i < ms[v].size(); i++) { pref[v][i] = (i == 0 ? 0 : pref[v][i - 1]) + ms[v][i]; } } long long getS(int v, int kek) { if (ms[v].size() < kek) return -inf; if (kek == 0) return -inf; return pref[v][kek - 1]; } int main() { cin >> n >> k; mas.resize(n); for (auto &x : mas) cin >> x; for (int i = 0; i < n; i++) { char a; cin >> a; ms[hsh(a)].push_back(mas[i]); } for (int i = 0; i < 3; i++) { sort(ms[i].begin(), ms[i].end()); reverse(ms[i].begin(), ms[i].end()); } for (int i = 0; i < 3; i++) calc(i); long long ans = -1; for (int i = 0; i < ms[1].size(); i++) { ans = max(ans, pref[1][i] + max(getS(0, k - i - 1), getS(2, k - i - 1))); } cout << ans; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' — orange, 'R' — red. Output Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. Examples Input 5 3 4 3 4 1 6 RROWW Output 11 Input 5 2 10 20 14 20 11 RRRRR Output -1 Input 11 5 5 6 3 2 3 4 7 5 4 5 6 RWOORWORROW Output 28 Note In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long inf = 1e18; int n, k; vector<int> mas; string c; vector<int> ms[3]; vector<long long> pref[3]; int hsh(char a) { if (a == 'W') return 0; if (a == 'O') return 1; if (a == 'R') return 2; return 4; } void calc(int v) { pref[v].resize(ms[v].size()); for (int i = 0; i < ms[v].size(); i++) { pref[v][i] = (i == 0 ? 0 : pref[v][i - 1]) + ms[v][i]; } } long long getS(int v, int kek) { if (ms[v].size() < kek) return -inf; if (kek == 0) return -inf; return pref[v][kek - 1]; } int main() { cin >> n >> k; mas.resize(n); for (auto &x : mas) cin >> x; for (int i = 0; i < n; i++) { char a; cin >> a; ms[hsh(a)].push_back(mas[i]); } for (int i = 0; i < 3; i++) { sort(ms[i].begin(), ms[i].end()); reverse(ms[i].begin(), ms[i].end()); } for (int i = 0; i < 3; i++) calc(i); long long ans = -1; for (int i = 0; i < ms[1].size(); i++) { ans = max(ans, pref[1][i] + max(getS(0, k - i - 1), getS(2, k - i - 1))); } cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<bool> visited; vector<vector<int>> edges; long long ans; int n; int magic = 550; int maxk = 2; vector<vector<int>> depth, children; void solve(int u) { int d = children[u].size(); vector<vector<int>> values(d + 2); for (int i = 0; i < d; ++i) for (int j = 1; j < min(d + 1, (int)depth[children[u][i]].size()); ++j) values[j].push_back(depth[children[u][i]][j]); depth[u].resize(d + 2, 1); for (int k = 1; k <= d; ++k) { if (values[k].size() < k) depth[u][k] = 2; else { nth_element(values[k].begin(), values[k].begin() + k - 1, values[k].end(), greater<int>()); depth[u][k] = values[k][k - 1] + 1; }; if (depth[u][k] > 2) maxk = max(maxk, k); }; }; void process() { scanf("%d", &n); visited.resize(n, false); edges.resize(n); depth.resize(n); children.resize(n); for (int i = 0; i < n - 1; ++i) { int u, v; scanf("%d %d", &u, &v); u--; v--; edges[u].push_back(v); edges[v].push_back(u); }; vector<pair<int, int>> stack; vector<int> postorder; visited[0] = true; stack.push_back(make_pair(0, -1)); while (stack.size()) { auto p = stack.back(); stack.pop_back(); int u = p.first; if (p.second == -1) { stack.push_back(make_pair(u, 0)); for (auto v : edges[u]) { if (!visited[v]) { stack.push_back(make_pair(v, -1)); visited[v] = true; children[u].push_back(v); }; }; } else { postorder.push_back(u); } }; for (auto u : postorder) solve(u); ans = 0; for (int u = 0; u < n; ++u) { ans += depth[u][1]; } vector<int> val(n, 1); magic = maxk; for (int k = 2; k <= min(magic, n); ++k) { for (auto u : postorder) { int temp = 1; if (depth[u].size() > k) { temp = depth[u][k]; }; for (auto v : children[u]) if (val[v] > temp) temp = val[v]; ans += temp; val[u] = temp; }; }; if (n > magic) { vector<int> degree(n + 1); for (auto u : postorder) { degree[u] = children[u].size(); for (auto v : children[u]) degree[u] = max(degree[u], degree[v]); if (degree[u] > magic) { ans += n - magic + (degree[u] - magic); } else { ans += (n - magic); } }; } printf("%lld\n", ans); }; int main() { process(); }
### Prompt Develop a solution in CPP to the problem described below: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<bool> visited; vector<vector<int>> edges; long long ans; int n; int magic = 550; int maxk = 2; vector<vector<int>> depth, children; void solve(int u) { int d = children[u].size(); vector<vector<int>> values(d + 2); for (int i = 0; i < d; ++i) for (int j = 1; j < min(d + 1, (int)depth[children[u][i]].size()); ++j) values[j].push_back(depth[children[u][i]][j]); depth[u].resize(d + 2, 1); for (int k = 1; k <= d; ++k) { if (values[k].size() < k) depth[u][k] = 2; else { nth_element(values[k].begin(), values[k].begin() + k - 1, values[k].end(), greater<int>()); depth[u][k] = values[k][k - 1] + 1; }; if (depth[u][k] > 2) maxk = max(maxk, k); }; }; void process() { scanf("%d", &n); visited.resize(n, false); edges.resize(n); depth.resize(n); children.resize(n); for (int i = 0; i < n - 1; ++i) { int u, v; scanf("%d %d", &u, &v); u--; v--; edges[u].push_back(v); edges[v].push_back(u); }; vector<pair<int, int>> stack; vector<int> postorder; visited[0] = true; stack.push_back(make_pair(0, -1)); while (stack.size()) { auto p = stack.back(); stack.pop_back(); int u = p.first; if (p.second == -1) { stack.push_back(make_pair(u, 0)); for (auto v : edges[u]) { if (!visited[v]) { stack.push_back(make_pair(v, -1)); visited[v] = true; children[u].push_back(v); }; }; } else { postorder.push_back(u); } }; for (auto u : postorder) solve(u); ans = 0; for (int u = 0; u < n; ++u) { ans += depth[u][1]; } vector<int> val(n, 1); magic = maxk; for (int k = 2; k <= min(magic, n); ++k) { for (auto u : postorder) { int temp = 1; if (depth[u].size() > k) { temp = depth[u][k]; }; for (auto v : children[u]) if (val[v] > temp) temp = val[v]; ans += temp; val[u] = temp; }; }; if (n > magic) { vector<int> degree(n + 1); for (auto u : postorder) { degree[u] = children[u].size(); for (auto v : children[u]) degree[u] = max(degree[u], degree[v]); if (degree[u] > magic) { ans += n - magic + (degree[u] - magic); } else { ans += (n - magic); } }; } printf("%lld\n", ans); }; int main() { process(); } ```
#include <bits/stdc++.h> using namespace std; const int LIMIT = 20; int n, dep[300001]; vector<int> adj[300001]; int dp[300001][LIMIT], dp2[300001][LIMIT]; void dfs(int x, int pre) { vector<int> child; for (auto y : adj[x]) { if (y != pre) { dfs(y, x); child.push_back(y); dep[x] = max(dep[x], dep[y] + 1); } } dp[x][1] = dp2[x][1] = n; for (int i = 2; i < LIMIT; ++i) { int lo = 0, hi = n; while (lo < hi) { int md = lo + hi + 1 >> 1, tot = 0; for (auto y : child) { if (dp[y][i - 1] >= md) { ++tot; } } (tot >= md) ? lo = md : hi = md - 1; } dp[x][i] = dp2[x][i] = lo + hi >> 1; for (auto y : child) { dp2[x][i] = max(dp2[x][i], dp2[y][i]); } } } int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { int x, y; scanf("%d%d", &x, &y), --x, --y; adj[x].push_back(y); adj[y].push_back(x); } dfs(0, -1); long long ans = 0LL; for (int i = 0; i < n; ++i) { ans += dep[i]; } for (int u = 0; u < n; ++u) { for (int i = 1; i < LIMIT; ++i) { int lo = 2, hi = dp2[u][i]; if (i + 1 < LIMIT) { lo = max(lo, dp2[u][i + 1] + 1); } if (lo <= hi) { ans += (long long)(hi - lo + 1) * (i - 1); } } } ans += (long long)n * n; cout << ans << endl; }
### Prompt In Cpp, your task is to solve the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int LIMIT = 20; int n, dep[300001]; vector<int> adj[300001]; int dp[300001][LIMIT], dp2[300001][LIMIT]; void dfs(int x, int pre) { vector<int> child; for (auto y : adj[x]) { if (y != pre) { dfs(y, x); child.push_back(y); dep[x] = max(dep[x], dep[y] + 1); } } dp[x][1] = dp2[x][1] = n; for (int i = 2; i < LIMIT; ++i) { int lo = 0, hi = n; while (lo < hi) { int md = lo + hi + 1 >> 1, tot = 0; for (auto y : child) { if (dp[y][i - 1] >= md) { ++tot; } } (tot >= md) ? lo = md : hi = md - 1; } dp[x][i] = dp2[x][i] = lo + hi >> 1; for (auto y : child) { dp2[x][i] = max(dp2[x][i], dp2[y][i]); } } } int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { int x, y; scanf("%d%d", &x, &y), --x, --y; adj[x].push_back(y); adj[y].push_back(x); } dfs(0, -1); long long ans = 0LL; for (int i = 0; i < n; ++i) { ans += dep[i]; } for (int u = 0; u < n; ++u) { for (int i = 1; i < LIMIT; ++i) { int lo = 2, hi = dp2[u][i]; if (i + 1 < LIMIT) { lo = max(lo, dp2[u][i + 1] + 1); } if (lo <= hi) { ans += (long long)(hi - lo + 1) * (i - 1); } } } ans += (long long)n * n; cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; int n; vector<int> vec[300005]; int niche[300005]; int nije[300005][21], dp[300005][21]; long long ans; void dfs(int s, int p = 0) { niche[s] = 1; for (int v : vec[s]) { if (v == p) continue; dfs(v, s); niche[s] = max(niche[s], niche[v] + 1); } nije[s][1] = dp[s][1] = n; for (int i = 2; i < 21; i++) { vector<int> tmp; for (int v : vec[s]) { if (v == p) continue; tmp.push_back(nije[v][i - 1]); } sort(tmp.begin(), tmp.end(), greater<int>()); for (int j = 0; j < tmp.size(); j++) { if (tmp[j] >= j + 1) nije[s][i] = j + 1; } dp[s][i] = nije[s][i]; for (int v : vec[s]) { if (v == p) continue; dp[s][i] = max(dp[s][i], dp[v][i]); } } for (int i = 2; i < 21; i++) ans += (i - 1) * (dp[s][i - 1] - dp[s][i]); if (dp[s][21 - 1]) ans += niche[s]; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); vec[u].push_back(v); vec[v].push_back(u); } dfs(1); printf("%lld\n", ans); return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; vector<int> vec[300005]; int niche[300005]; int nije[300005][21], dp[300005][21]; long long ans; void dfs(int s, int p = 0) { niche[s] = 1; for (int v : vec[s]) { if (v == p) continue; dfs(v, s); niche[s] = max(niche[s], niche[v] + 1); } nije[s][1] = dp[s][1] = n; for (int i = 2; i < 21; i++) { vector<int> tmp; for (int v : vec[s]) { if (v == p) continue; tmp.push_back(nije[v][i - 1]); } sort(tmp.begin(), tmp.end(), greater<int>()); for (int j = 0; j < tmp.size(); j++) { if (tmp[j] >= j + 1) nije[s][i] = j + 1; } dp[s][i] = nije[s][i]; for (int v : vec[s]) { if (v == p) continue; dp[s][i] = max(dp[s][i], dp[v][i]); } } for (int i = 2; i < 21; i++) ans += (i - 1) * (dp[s][i - 1] - dp[s][i]); if (dp[s][21 - 1]) ans += niche[s]; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); vec[u].push_back(v); vec[v].push_back(u); } dfs(1); printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> const int MAXN = 300010; int head[MAXN], nxt[MAXN << 1], to[MAXN << 1], tot; void adde(int b, int e) { nxt[++tot] = head[b]; to[head[b] = tot] = e; nxt[++tot] = head[e]; to[head[e] = tot] = b; } std::vector<int> dp[MAXN]; int deg[MAXN]; std::priority_queue<int, std::vector<int>, std::greater<int> > q[MAXN]; long long ans, sm[MAXN]; void dfs(int u, int fa = 0) { for (int i = head[u]; i; i = nxt[i]) if (to[i] != fa) ++deg[u], dfs(to[i], u); for (int i = head[u]; i; i = nxt[i]) if (to[i] != fa) for (int j = 0; j < deg[to[i]] && j < deg[u]; ++j) q[j].push(dp[to[i]][j]); for (int i = 0; i < deg[u]; ++i) { while (q[i].size() > i + 1) q[i].pop(); int t = q[i].size() <= i ? 1 : q[i].top() + 1; while (!q[i].empty()) q[i].pop(); dp[u].push_back(t); sm[u] += t; } } void dfs2(int u, int fa = 0) { for (int i = head[u]; i; i = nxt[i]) if (to[i] != fa) { dfs2(to[i], u); if (dp[to[i]].size() > dp[u].size()) { dp[u].swap(dp[to[i]]); std::swap(sm[u], sm[to[i]]); } const int S = dp[to[i]].size(); for (int j = 0; j != S; ++j) if (dp[to[i]][j] > dp[u][j]) { sm[u] -= dp[u][j]; sm[u] += dp[u][j] = dp[to[i]][j]; } } ans += sm[u]; } int n; int main() { std::ios_base::sync_with_stdio(false), std::cin.tie(0); std::cin >> n; for (int i = 1, t1, t2; i < n; ++i) std::cin >> t1 >> t2, adde(t1, t2); ans = (long long)n * n; dfs(1); dfs2(1); std::cout << ans << std::endl; return 0; }
### Prompt Create a solution in cpp for the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> const int MAXN = 300010; int head[MAXN], nxt[MAXN << 1], to[MAXN << 1], tot; void adde(int b, int e) { nxt[++tot] = head[b]; to[head[b] = tot] = e; nxt[++tot] = head[e]; to[head[e] = tot] = b; } std::vector<int> dp[MAXN]; int deg[MAXN]; std::priority_queue<int, std::vector<int>, std::greater<int> > q[MAXN]; long long ans, sm[MAXN]; void dfs(int u, int fa = 0) { for (int i = head[u]; i; i = nxt[i]) if (to[i] != fa) ++deg[u], dfs(to[i], u); for (int i = head[u]; i; i = nxt[i]) if (to[i] != fa) for (int j = 0; j < deg[to[i]] && j < deg[u]; ++j) q[j].push(dp[to[i]][j]); for (int i = 0; i < deg[u]; ++i) { while (q[i].size() > i + 1) q[i].pop(); int t = q[i].size() <= i ? 1 : q[i].top() + 1; while (!q[i].empty()) q[i].pop(); dp[u].push_back(t); sm[u] += t; } } void dfs2(int u, int fa = 0) { for (int i = head[u]; i; i = nxt[i]) if (to[i] != fa) { dfs2(to[i], u); if (dp[to[i]].size() > dp[u].size()) { dp[u].swap(dp[to[i]]); std::swap(sm[u], sm[to[i]]); } const int S = dp[to[i]].size(); for (int j = 0; j != S; ++j) if (dp[to[i]][j] > dp[u][j]) { sm[u] -= dp[u][j]; sm[u] += dp[u][j] = dp[to[i]][j]; } } ans += sm[u]; } int n; int main() { std::ios_base::sync_with_stdio(false), std::cin.tie(0); std::cin >> n; for (int i = 1, t1, t2; i < n; ++i) std::cin >> t1 >> t2, adde(t1, t2); ans = (long long)n * n; dfs(1); dfs2(1); std::cout << ans << std::endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> g[300005]; int n; int f[300005][32]; int w[300005]; int a[300005]; int m; long long r; void dfs(int x, int p) { w[x] = 1; for (int y : g[x]) if (y != p) { dfs(y, x); w[x] = max(w[x], 1 + w[y]); } f[x][1] = n; for (int d = 2, ThxDem = 21; d < ThxDem; ++d) { m = 0; for (int y : g[x]) if (y != p) a[m++] = f[y][d - 1]; sort(a, a + m); reverse(a, a + m); f[x][d] = 0; while (f[x][d] < m && a[f[x][d]] > f[x][d]) f[x][d]++; f[x][d] = max(f[x][d], 1); } f[x][21] = 1; } void dfs2(int x, int p) { for (int y : g[x]) if (y != p) { dfs2(y, x); for (int d = 1, ThxDem = 21; d < ThxDem; ++d) f[x][d] = max(f[x][d], f[y][d]); } } int main() { scanf("%d", &n); for (int _ = 1, ThxDem = n; _ < ThxDem; ++_) { int x, y; scanf("%d%d", &x, &y); x--; y--; g[x].push_back(y); g[y].push_back(x); } dfs(0, -1); dfs2(0, -1); for (int i = 0, ThxDem = n; i < ThxDem; ++i) { r += w[i]; for (int d = 1, ThxDem = 21; d < ThxDem; ++d) r += d * (f[i][d] - f[i][d + 1]); } printf("%lld\n", r); return 0; }
### Prompt Please create a solution in cpp to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> g[300005]; int n; int f[300005][32]; int w[300005]; int a[300005]; int m; long long r; void dfs(int x, int p) { w[x] = 1; for (int y : g[x]) if (y != p) { dfs(y, x); w[x] = max(w[x], 1 + w[y]); } f[x][1] = n; for (int d = 2, ThxDem = 21; d < ThxDem; ++d) { m = 0; for (int y : g[x]) if (y != p) a[m++] = f[y][d - 1]; sort(a, a + m); reverse(a, a + m); f[x][d] = 0; while (f[x][d] < m && a[f[x][d]] > f[x][d]) f[x][d]++; f[x][d] = max(f[x][d], 1); } f[x][21] = 1; } void dfs2(int x, int p) { for (int y : g[x]) if (y != p) { dfs2(y, x); for (int d = 1, ThxDem = 21; d < ThxDem; ++d) f[x][d] = max(f[x][d], f[y][d]); } } int main() { scanf("%d", &n); for (int _ = 1, ThxDem = n; _ < ThxDem; ++_) { int x, y; scanf("%d%d", &x, &y); x--; y--; g[x].push_back(y); g[y].push_back(x); } dfs(0, -1); dfs2(0, -1); for (int i = 0, ThxDem = n; i < ThxDem; ++i) { r += w[i]; for (int d = 1, ThxDem = 21; d < ThxDem; ++d) r += d * (f[i][d] - f[i][d + 1]); } printf("%lld\n", r); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; const int SINF = 0x7fffffff; const long long LINF = 0x3fffffffffffffff; const long long SLINF = 0x7fffffffffffffff; const int MAXN = 300007; const int MAXL = 21; int n; long long ans; vector<int> ch[MAXN]; vector<int> dp[MAXN]; vector<int> tmp[MAXN]; int dm[MAXN][MAXL]; void init(); void input(); void work(); void dfs(int now, int fa); int main() { init(); input(); work(); } void init() { ios::sync_with_stdio(false); } void input() { scanf("%d", &n); int u, v; for (int i = 1; i < n; ++i) { scanf("%d%d", &u, &v); ch[u].push_back(v), ch[v].push_back(u); } } void work() { dfs(1, -1); ans = static_cast<long long>(n) * n; for (int i = 1; i <= n; ++i) { if (ch[i].size() >= 1) ans += dp[i][1]; for (int j = 1; j < MAXL - 2; ++j) { ans += j * (dm[i][j] - dm[i][j + 1]); } } cout << ans << endl; } void dfs(int now, int fa) { for (int i = 0; i < ch[now].size(); ++i) { if (ch[now][i] == fa) { ch[now].erase(ch[now].begin() + i); break; } } int nd = ch[now].size(); for (int i = 0; i < nd; ++i) dfs(ch[now][i], now); dp[now].resize(nd + 1); for (int i = 1; i <= nd; ++i) tmp[i].clear(); for (int i = 0; i < nd; ++i) { int v = ch[now][i]; for (int j = 1; j <= ch[v].size(); ++j) { tmp[j].push_back(dp[v][j]); } } for (int i = 1; i <= nd; ++i) { if (tmp[i].size() >= i) { nth_element(tmp[i].begin(), tmp[i].end() - i, tmp[i].end()); dp[now][i] = (*(tmp[i].end() - i)) + 1; } else dp[now][i] = 1; } for (int i = 0; i < nd; ++i) for (int j = 0; j < MAXL; ++j) dm[now][j] = (((dm[now][j]) > (dm[ch[now][i]][j])) ? (dm[now][j]) : (dm[ch[now][i]][j])); for (int i = 2; i <= nd; ++i) dm[now][dp[now][i]] = (((dm[now][dp[now][i]]) > (i)) ? (dm[now][dp[now][i]]) : (i)); dm[now][MAXL - 1] = 1; for (int i = MAXL - 2; i >= 0; --i) dm[now][i] = (((dm[now][i]) > (dm[now][i + 1])) ? (dm[now][i]) : (dm[now][i + 1])); }
### Prompt Please formulate a CPP solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; const int SINF = 0x7fffffff; const long long LINF = 0x3fffffffffffffff; const long long SLINF = 0x7fffffffffffffff; const int MAXN = 300007; const int MAXL = 21; int n; long long ans; vector<int> ch[MAXN]; vector<int> dp[MAXN]; vector<int> tmp[MAXN]; int dm[MAXN][MAXL]; void init(); void input(); void work(); void dfs(int now, int fa); int main() { init(); input(); work(); } void init() { ios::sync_with_stdio(false); } void input() { scanf("%d", &n); int u, v; for (int i = 1; i < n; ++i) { scanf("%d%d", &u, &v); ch[u].push_back(v), ch[v].push_back(u); } } void work() { dfs(1, -1); ans = static_cast<long long>(n) * n; for (int i = 1; i <= n; ++i) { if (ch[i].size() >= 1) ans += dp[i][1]; for (int j = 1; j < MAXL - 2; ++j) { ans += j * (dm[i][j] - dm[i][j + 1]); } } cout << ans << endl; } void dfs(int now, int fa) { for (int i = 0; i < ch[now].size(); ++i) { if (ch[now][i] == fa) { ch[now].erase(ch[now].begin() + i); break; } } int nd = ch[now].size(); for (int i = 0; i < nd; ++i) dfs(ch[now][i], now); dp[now].resize(nd + 1); for (int i = 1; i <= nd; ++i) tmp[i].clear(); for (int i = 0; i < nd; ++i) { int v = ch[now][i]; for (int j = 1; j <= ch[v].size(); ++j) { tmp[j].push_back(dp[v][j]); } } for (int i = 1; i <= nd; ++i) { if (tmp[i].size() >= i) { nth_element(tmp[i].begin(), tmp[i].end() - i, tmp[i].end()); dp[now][i] = (*(tmp[i].end() - i)) + 1; } else dp[now][i] = 1; } for (int i = 0; i < nd; ++i) for (int j = 0; j < MAXL; ++j) dm[now][j] = (((dm[now][j]) > (dm[ch[now][i]][j])) ? (dm[now][j]) : (dm[ch[now][i]][j])); for (int i = 2; i <= nd; ++i) dm[now][dp[now][i]] = (((dm[now][dp[now][i]]) > (i)) ? (dm[now][dp[now][i]]) : (i)); dm[now][MAXL - 1] = 1; for (int i = MAXL - 2; i >= 0; --i) dm[now][i] = (((dm[now][i]) > (dm[now][i + 1])) ? (dm[now][i]) : (dm[now][i + 1])); } ```
#include <bits/stdc++.h> using namespace std; inline void setmin(int &x, int y) { if (y < x) x = y; } inline void setmax(int &x, int y) { if (y > x) x = y; } inline void setmin(long long &x, long long y) { if (y < x) x = y; } inline void setmax(long long &x, long long y) { if (y > x) x = y; } const int N = 300000; const int inf = (int)1e9 + 1; const long long big = (long long)1e18 + 1; const int P = 239; const int MOD = (int)1e9 + 7; const int MOD1 = (int)1e9 + 9; const double eps = 1e-9; const double pi = atan2(0, -1); const int ABC = 26; int n; vector<int> g[N]; int depth[N]; int dp[N][19]; int dp1[N][19]; long long ans = 0; void dfs(int u, int par) { depth[u] = 1; for (int v : g[u]) if (v != par) { dfs(v, u); setmax(depth[u], depth[v] + 1); } dp[u][0] = n; for (int b = 1; b < 19; b++) { dp[u][b] = 1; vector<int> vals; for (int v : g[u]) if (v != par) vals.push_back(dp[v][b - 1]); sort(vals.begin(), vals.end()); reverse(vals.begin(), vals.end()); for (int i = 0; i < (int)vals.size(); i++) setmax(dp[u][b], min(i + 1, vals[i])); } for (int b = 0; b < 19; b++) { dp1[u][b] = dp[u][b]; for (int v : g[u]) if (v != par) setmax(dp1[u][b], dp1[v][b]); } for (int b = 0; b < 19; b++) ans += (b + 1) * (dp1[u][b] - (b + 1 < 19 ? dp1[u][b + 1] : 1)); ans += depth[u]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); cout << fixed; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } dfs(0, -1); cout << ans << "\n"; return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline void setmin(int &x, int y) { if (y < x) x = y; } inline void setmax(int &x, int y) { if (y > x) x = y; } inline void setmin(long long &x, long long y) { if (y < x) x = y; } inline void setmax(long long &x, long long y) { if (y > x) x = y; } const int N = 300000; const int inf = (int)1e9 + 1; const long long big = (long long)1e18 + 1; const int P = 239; const int MOD = (int)1e9 + 7; const int MOD1 = (int)1e9 + 9; const double eps = 1e-9; const double pi = atan2(0, -1); const int ABC = 26; int n; vector<int> g[N]; int depth[N]; int dp[N][19]; int dp1[N][19]; long long ans = 0; void dfs(int u, int par) { depth[u] = 1; for (int v : g[u]) if (v != par) { dfs(v, u); setmax(depth[u], depth[v] + 1); } dp[u][0] = n; for (int b = 1; b < 19; b++) { dp[u][b] = 1; vector<int> vals; for (int v : g[u]) if (v != par) vals.push_back(dp[v][b - 1]); sort(vals.begin(), vals.end()); reverse(vals.begin(), vals.end()); for (int i = 0; i < (int)vals.size(); i++) setmax(dp[u][b], min(i + 1, vals[i])); } for (int b = 0; b < 19; b++) { dp1[u][b] = dp[u][b]; for (int v : g[u]) if (v != par) setmax(dp1[u][b], dp1[v][b]); } for (int b = 0; b < 19; b++) ans += (b + 1) * (dp1[u][b] - (b + 1 < 19 ? dp1[u][b + 1] : 1)); ans += depth[u]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); cout << fixed; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } dfs(0, -1); cout << ans << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } void Read(int& x) { char c = nc(); for (; c < '0' || c > '9'; c = nc()) ; for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - 48, c = nc()) ; } const int N = 300010; const int M = 19; int n, m, x, y; int t[N << 1], nx[N << 1], h[N], num; int f[N], c[N], dp[N][M]; int a[N], l, cur[N]; vector<pair<int, int> > g[N]; long long Res, Ans; void Add(int x, int y) { t[++num] = y; nx[num] = h[x]; h[x] = num; } bool Cmp(int x, int y) { return x > y; } void Dfs(int x, int y) { dp[x][1] = n; f[x] = y; for (int i = h[x]; i; i = nx[i]) if (t[i] != y) Dfs(t[i], x), c[x] = max(c[x], c[t[i]]); for (int j = 2; j < M; j++) { l = 0; for (int i = h[x]; i; i = nx[i]) if (t[i] != y && dp[t[i]][j - 1]) a[++l] = dp[t[i]][j - 1]; sort(a + 1, a + l + 1, Cmp); for (int k = l; k > 1; k--) if (a[k] >= k) { dp[x][j] = k; break; } if (dp[x][j]) g[dp[x][j]].push_back(pair<int, int>(x, j)); } Ans += ++c[x]; } void Update(int x, int y) { while (x) { if (cur[x] >= y) break; Res += y - cur[x]; cur[x] = y; x = f[x]; } } int main() { Read(n); for (int i = 1; i < n; i++) Read(x), Read(y), Add(x, y), Add(y, x); Dfs(1, 1); for (int i = 1; i <= n; i++) cur[i] = 1; Res = n; for (int k = n; k > 1; k--) { for (int j = 0; j < g[k].size(); j++) Update(g[k][j].first, g[k][j].second); Ans += Res; } cout << Ans << endl; return 0; }
### Prompt In CPP, your task is to solve the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } void Read(int& x) { char c = nc(); for (; c < '0' || c > '9'; c = nc()) ; for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - 48, c = nc()) ; } const int N = 300010; const int M = 19; int n, m, x, y; int t[N << 1], nx[N << 1], h[N], num; int f[N], c[N], dp[N][M]; int a[N], l, cur[N]; vector<pair<int, int> > g[N]; long long Res, Ans; void Add(int x, int y) { t[++num] = y; nx[num] = h[x]; h[x] = num; } bool Cmp(int x, int y) { return x > y; } void Dfs(int x, int y) { dp[x][1] = n; f[x] = y; for (int i = h[x]; i; i = nx[i]) if (t[i] != y) Dfs(t[i], x), c[x] = max(c[x], c[t[i]]); for (int j = 2; j < M; j++) { l = 0; for (int i = h[x]; i; i = nx[i]) if (t[i] != y && dp[t[i]][j - 1]) a[++l] = dp[t[i]][j - 1]; sort(a + 1, a + l + 1, Cmp); for (int k = l; k > 1; k--) if (a[k] >= k) { dp[x][j] = k; break; } if (dp[x][j]) g[dp[x][j]].push_back(pair<int, int>(x, j)); } Ans += ++c[x]; } void Update(int x, int y) { while (x) { if (cur[x] >= y) break; Res += y - cur[x]; cur[x] = y; x = f[x]; } } int main() { Read(n); for (int i = 1; i < n; i++) Read(x), Read(y), Add(x, y), Add(y, x); Dfs(1, 1); for (int i = 1; i <= n; i++) cur[i] = 1; Res = n; for (int k = n; k > 1; k--) { for (int j = 0; j < g[k].size(); j++) Update(g[k][j].first, g[k][j].second); Ans += Res; } cout << Ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 95542721; const int INF = 2e9; const long long INF64 = 4e18; const long double EPS = 1e-16; const long long MD = 1551513443; const long long T = 2543; const int N = 300010; const int M = 100; const int DEBUG = 0; const int MAGIC = 320; int n; long long ans; vector<int> g[N]; vector<int> dp[N]; vector<long long> *dp_ans[N]; multiset<int> s[N]; void push_max(pair<int, int> &a, int first) { if (first > a.second) { a.second = first; if (a.second > a.first) swap(a.first, a.second); } } void dfs(int v, int p) { int k = int((g[v]).size()) - 1; if (p == -1) k = int((g[v]).size()); if (k == 0) { dp[v].resize(2, 1); dp_ans[v] = new vector<long long>(2, 1); ans += n; return; } else { dp[v].resize(k + 1, 2); dp_ans[v] = new vector<long long>; } pair<int, int> maxi2 = {1, 1}; int ind = -1, maxi = 0; for (int u : g[v]) if (u != p) { dfs(u, v); push_max(maxi2, int((dp[u]).size()) - 1); if (int(dp_ans[u]->size()) > maxi) { maxi = int(dp_ans[u]->size()); ind = u; } } k = min(k, maxi2.second); for (int i = 0; i < int(k + 1); i++) s[i].clear(); for (int u : g[v]) if (u != p) { for (int i = 1; i <= min(k, int((dp[u]).size()) - 1); i++) { s[i].insert(-dp[u][i]); if (s[i].size() > i) { multiset<int>::iterator it = s[i].end(); it--; s[i].erase(it); } } } for (int i = 1; i <= k; i++) if (s[i].size() == i) dp[v][i] = max(dp[v][i], -(*s[i].rbegin()) + 1); dp_ans[v] = dp_ans[ind]; for (int i = 1; i < int((dp[v]).size()); i++) { if (i >= dp_ans[v]->size()) { dp_ans[v]->push_back(dp[v][i]); (*dp_ans[v])[0] += dp[v][i]; } else { long long first = dp[v][i]; if (first > (*dp_ans[v])[i]) { (*dp_ans[v])[0] -= (*dp_ans[v])[i]; (*dp_ans[v])[i] = first; (*dp_ans[v])[0] += first; } } } for (int u : g[v]) if (u != p && u != ind) { for (int i = 1; i < dp_ans[u]->size(); i++) { long long first = (*dp_ans[u])[i]; if (first > (*dp_ans[v])[i]) { (*dp_ans[v])[0] -= (*dp_ans[v])[i]; (*dp_ans[v])[i] = first; (*dp_ans[v])[0] += first; } } } int m = dp_ans[v]->size() - 1; ans += (*dp_ans[v])[0]; ans += n - m; } int main() { cin >> n; for (int i = 0; i < int(n - 1); i++) { int first, second; scanf("%d%d", &first, &second); g[first].push_back(second); g[second].push_back(first); } dfs(1, -1); cout << ans << endl; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 95542721; const int INF = 2e9; const long long INF64 = 4e18; const long double EPS = 1e-16; const long long MD = 1551513443; const long long T = 2543; const int N = 300010; const int M = 100; const int DEBUG = 0; const int MAGIC = 320; int n; long long ans; vector<int> g[N]; vector<int> dp[N]; vector<long long> *dp_ans[N]; multiset<int> s[N]; void push_max(pair<int, int> &a, int first) { if (first > a.second) { a.second = first; if (a.second > a.first) swap(a.first, a.second); } } void dfs(int v, int p) { int k = int((g[v]).size()) - 1; if (p == -1) k = int((g[v]).size()); if (k == 0) { dp[v].resize(2, 1); dp_ans[v] = new vector<long long>(2, 1); ans += n; return; } else { dp[v].resize(k + 1, 2); dp_ans[v] = new vector<long long>; } pair<int, int> maxi2 = {1, 1}; int ind = -1, maxi = 0; for (int u : g[v]) if (u != p) { dfs(u, v); push_max(maxi2, int((dp[u]).size()) - 1); if (int(dp_ans[u]->size()) > maxi) { maxi = int(dp_ans[u]->size()); ind = u; } } k = min(k, maxi2.second); for (int i = 0; i < int(k + 1); i++) s[i].clear(); for (int u : g[v]) if (u != p) { for (int i = 1; i <= min(k, int((dp[u]).size()) - 1); i++) { s[i].insert(-dp[u][i]); if (s[i].size() > i) { multiset<int>::iterator it = s[i].end(); it--; s[i].erase(it); } } } for (int i = 1; i <= k; i++) if (s[i].size() == i) dp[v][i] = max(dp[v][i], -(*s[i].rbegin()) + 1); dp_ans[v] = dp_ans[ind]; for (int i = 1; i < int((dp[v]).size()); i++) { if (i >= dp_ans[v]->size()) { dp_ans[v]->push_back(dp[v][i]); (*dp_ans[v])[0] += dp[v][i]; } else { long long first = dp[v][i]; if (first > (*dp_ans[v])[i]) { (*dp_ans[v])[0] -= (*dp_ans[v])[i]; (*dp_ans[v])[i] = first; (*dp_ans[v])[0] += first; } } } for (int u : g[v]) if (u != p && u != ind) { for (int i = 1; i < dp_ans[u]->size(); i++) { long long first = (*dp_ans[u])[i]; if (first > (*dp_ans[v])[i]) { (*dp_ans[v])[0] -= (*dp_ans[v])[i]; (*dp_ans[v])[i] = first; (*dp_ans[v])[0] += first; } } } int m = dp_ans[v]->size() - 1; ans += (*dp_ans[v])[0]; ans += n - m; } int main() { cin >> n; for (int i = 0; i < int(n - 1); i++) { int first, second; scanf("%d%d", &first, &second); g[first].push_back(second); g[second].push_back(first); } dfs(1, -1); cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using std::make_pair; using std::max; using std::pair; using std::vector; const int w = 20, N = 300005; int to[N << 1], edge, last[N], Next[N << 1], dp[N][w], b[N], n, x, y, f[N], g[N]; long long ans, sum; vector<pair<int, int> > v[N]; void add(int x, int y) { to[++edge] = y; Next[edge] = last[x]; last[x] = edge; } int dfs(int x, int fa) { f[x] = fa; int dep = 0; for (int i = last[x]; i; i = Next[i]) if (to[i] != fa) dep = max(dep, dfs(to[i], x)), dp[x][2]++; for (int d = 2; d < w; d++) { int cnt = 0; for (int i = last[x]; i; i = Next[i]) { if (to[i] != fa) b[++cnt] = dp[to[i]][d]; } std::sort(b + 1, b + cnt + 1); std::reverse(b + 1, b + cnt + 1); for (int i = 1; i <= cnt && b[i] >= i; i++) dp[x][d + 1]++; if (dp[x][d + 1]) v[dp[x][d + 1]].push_back(make_pair(x, d + 1)); } if (dp[x][2]) v[dp[x][2]].push_back(make_pair(x, 2)); dep += 1; ans += dep; return dep; } void upd(int x, int y) { if (g[x] >= y) return; sum += y - g[x]; g[x] = y; if (f[x]) upd(f[x], y); } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); add(x, y), add(y, x); } dfs(1, 0); sum = n; for (int i = 1; i <= n; i++) g[i] = 1; for (int k = n; k >= 2; k--) { for (auto j : v[k]) upd(j.first, j.second); ans += sum; } printf("%lld\n", ans); }
### Prompt Generate a cpp solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using std::make_pair; using std::max; using std::pair; using std::vector; const int w = 20, N = 300005; int to[N << 1], edge, last[N], Next[N << 1], dp[N][w], b[N], n, x, y, f[N], g[N]; long long ans, sum; vector<pair<int, int> > v[N]; void add(int x, int y) { to[++edge] = y; Next[edge] = last[x]; last[x] = edge; } int dfs(int x, int fa) { f[x] = fa; int dep = 0; for (int i = last[x]; i; i = Next[i]) if (to[i] != fa) dep = max(dep, dfs(to[i], x)), dp[x][2]++; for (int d = 2; d < w; d++) { int cnt = 0; for (int i = last[x]; i; i = Next[i]) { if (to[i] != fa) b[++cnt] = dp[to[i]][d]; } std::sort(b + 1, b + cnt + 1); std::reverse(b + 1, b + cnt + 1); for (int i = 1; i <= cnt && b[i] >= i; i++) dp[x][d + 1]++; if (dp[x][d + 1]) v[dp[x][d + 1]].push_back(make_pair(x, d + 1)); } if (dp[x][2]) v[dp[x][2]].push_back(make_pair(x, 2)); dep += 1; ans += dep; return dep; } void upd(int x, int y) { if (g[x] >= y) return; sum += y - g[x]; g[x] = y; if (f[x]) upd(f[x], y); } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); add(x, y), add(y, x); } dfs(1, 0); sum = n; for (int i = 1; i <= n; i++) g[i] = 1; for (int k = n; k >= 2; k--) { for (auto j : v[k]) upd(j.first, j.second); ans += sum; } printf("%lld\n", ans); } ```
#include <bits/stdc++.h> using namespace std; const int N = 300005; vector<int> e[N]; int d[N], q[N], n; int f[N][20]; long long sum; void dfs(int x, int fa) { for (auto i : e[x]) if (i != fa) { dfs(i, x); d[x] = max(d[x], d[i]); } d[x]++; sum += d[x]; f[x][1] = n; for (int j = (int)(2); j <= (int)(18); j++) { *q = 0; for (auto i : e[x]) if (i != fa) q[++*q] = f[i][j - 1]; sort(q + 1, q + *q + 1); reverse(q + 1, q + *q + 1); int p = 0; for (; p != *q && q[p + 1] >= p + 1; ++p) ; f[x][j] = p; } } void solve(int x, int fa) { for (auto i : e[x]) if (i != fa) { solve(i, x); for (int j = (int)(1); j <= (int)(18); j++) f[x][j] = max(f[x][j], f[i][j]); } for (int j = (int)(1); j <= (int)(18); j++) if (f[x][j] >= 1) sum += f[x][j] - 1; } int main() { scanf("%d", &n); for (int i = (int)(1); i <= (int)(n - 1); i++) { int x, y; scanf("%d%d", &x, &y); e[x].push_back(y); e[y].push_back(x); } dfs(1, 0); solve(1, 0); printf("%lld\n", sum); }
### Prompt In cpp, your task is to solve the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 300005; vector<int> e[N]; int d[N], q[N], n; int f[N][20]; long long sum; void dfs(int x, int fa) { for (auto i : e[x]) if (i != fa) { dfs(i, x); d[x] = max(d[x], d[i]); } d[x]++; sum += d[x]; f[x][1] = n; for (int j = (int)(2); j <= (int)(18); j++) { *q = 0; for (auto i : e[x]) if (i != fa) q[++*q] = f[i][j - 1]; sort(q + 1, q + *q + 1); reverse(q + 1, q + *q + 1); int p = 0; for (; p != *q && q[p + 1] >= p + 1; ++p) ; f[x][j] = p; } } void solve(int x, int fa) { for (auto i : e[x]) if (i != fa) { solve(i, x); for (int j = (int)(1); j <= (int)(18); j++) f[x][j] = max(f[x][j], f[i][j]); } for (int j = (int)(1); j <= (int)(18); j++) if (f[x][j] >= 1) sum += f[x][j] - 1; } int main() { scanf("%d", &n); for (int i = (int)(1); i <= (int)(n - 1); i++) { int x, y; scanf("%d%d", &x, &y); e[x].push_back(y); e[y].push_back(x); } dfs(1, 0); solve(1, 0); printf("%lld\n", sum); } ```
#include <bits/stdc++.h> using namespace std; vector<vector<pair<int, int> > > vec(323456); vector<vector<int> > adj(312345); int lev[312345], maxdepth[312345]; long long bigg = 0; int jaja[312345][19]; vector<int> wow; int n; int dfs(int cur, int par) { int i; lev[cur] = 1; maxdepth[cur] = 0; for (i = 0; i < adj[cur].size(); i++) { if (adj[cur][i] != par) { dfs(adj[cur][i], cur); lev[cur] = max(lev[adj[cur][i]] + 1, lev[cur]); maxdepth[cur] = max(maxdepth[adj[cur][i]], maxdepth[cur]); } } int depth = 2; int previ = n, u, val; long long j; int ans, low, mid, high; vec[cur].push_back(make_pair(1, n)); jaja[cur][1] = n; vector<pair<int, int> >::iterator it; int iter = 0; while (1) { wow.clear(); for (j = 0; j < adj[cur].size(); j++) { u = adj[cur][j]; if (u == par) continue; it = upper_bound(vec[u].begin(), vec[u].end(), make_pair(depth - 1, -1)); if (it == vec[u].end()) continue; wow.push_back(it->second); } sort(wow.begin(), wow.end()); int low = 2; int high = n - 1; ans = -1; while (low <= high) { mid = (low + high) / 2; val = wow.end() - lower_bound(wow.begin(), wow.end(), mid); if (val >= mid) { ans = mid; low = mid + 1; } else { high = mid - 1; } } if (ans == -1) { maxdepth[cur] = max(depth - 1, maxdepth[cur]); break; } jaja[cur][depth] = ans; vec[cur].push_back(make_pair(depth, ans)); previ = ans; depth++; } val = maxdepth[cur] + 1; for (i = 0; i < adj[cur].size(); i++) { u = adj[cur][i]; if (u == par) continue; for (j = 1; j < val; j++) { jaja[cur][j] = max(jaja[cur][j], jaja[u][j]); } } for (j = 1; j < val - 1; j++) { bigg += j * (jaja[cur][j] - jaja[cur][j + 1]); } j = val - 1; bigg += j * (jaja[cur][j] - 1); return 0; } int main() { std::ios::sync_with_stdio(false); cin >> n; int i, u, v; for (i = 0; i < n - 1; i++) { cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } dfs(0, -1); for (i = 0; i < n; i++) { bigg += lev[i]; } cout << bigg << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<vector<pair<int, int> > > vec(323456); vector<vector<int> > adj(312345); int lev[312345], maxdepth[312345]; long long bigg = 0; int jaja[312345][19]; vector<int> wow; int n; int dfs(int cur, int par) { int i; lev[cur] = 1; maxdepth[cur] = 0; for (i = 0; i < adj[cur].size(); i++) { if (adj[cur][i] != par) { dfs(adj[cur][i], cur); lev[cur] = max(lev[adj[cur][i]] + 1, lev[cur]); maxdepth[cur] = max(maxdepth[adj[cur][i]], maxdepth[cur]); } } int depth = 2; int previ = n, u, val; long long j; int ans, low, mid, high; vec[cur].push_back(make_pair(1, n)); jaja[cur][1] = n; vector<pair<int, int> >::iterator it; int iter = 0; while (1) { wow.clear(); for (j = 0; j < adj[cur].size(); j++) { u = adj[cur][j]; if (u == par) continue; it = upper_bound(vec[u].begin(), vec[u].end(), make_pair(depth - 1, -1)); if (it == vec[u].end()) continue; wow.push_back(it->second); } sort(wow.begin(), wow.end()); int low = 2; int high = n - 1; ans = -1; while (low <= high) { mid = (low + high) / 2; val = wow.end() - lower_bound(wow.begin(), wow.end(), mid); if (val >= mid) { ans = mid; low = mid + 1; } else { high = mid - 1; } } if (ans == -1) { maxdepth[cur] = max(depth - 1, maxdepth[cur]); break; } jaja[cur][depth] = ans; vec[cur].push_back(make_pair(depth, ans)); previ = ans; depth++; } val = maxdepth[cur] + 1; for (i = 0; i < adj[cur].size(); i++) { u = adj[cur][i]; if (u == par) continue; for (j = 1; j < val; j++) { jaja[cur][j] = max(jaja[cur][j], jaja[u][j]); } } for (j = 1; j < val - 1; j++) { bigg += j * (jaja[cur][j] - jaja[cur][j + 1]); } j = val - 1; bigg += j * (jaja[cur][j] - 1); return 0; } int main() { std::ios::sync_with_stdio(false); cin >> n; int i, u, v; for (i = 0; i < n - 1; i++) { cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } dfs(0, -1); for (i = 0; i < n; i++) { bigg += lev[i]; } cout << bigg << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long ans; int dp[300000 + 5][20], n; vector<int> e[300000 + 5]; inline void upd(int &x, int y) { if (x < y) x = y; } int dfs(int now, int fa) { int mxdep = 0; for (auto v : e[now]) if (v != fa) upd(mxdep, dfs(v, now)); mxdep++; ans += mxdep; dp[now][1] = n; for (int m = 2; m <= 19; m++) { vector<int> tmp; for (auto v : e[now]) if (v != fa) tmp.push_back(dp[v][m - 1]); sort(tmp.begin(), tmp.end(), greater<int>()); int t = 1; while (t <= (int)tmp.size() && tmp[t - 1] >= t) t++; dp[now][m] = t - 1; upd(dp[now][m], 1); } return mxdep; } void dfs2(int now, int fa) { for (auto v : e[now]) if (v != fa) { dfs2(v, now); for (int m = 1; m <= 19; m++) upd(dp[now][m], dp[v][m]); } for (int m = 1; m <= 18; m++) ans += 1ll * m * (dp[now][m] - dp[now][m + 1]); } int main() { scanf("%d", &n); for (int i = 1; i <= n - 1; i++) { int x, y; scanf("%d%d", &x, &y); e[x].push_back(y); e[y].push_back(x); } dfs(1, 0); dfs2(1, 0); printf("%lld\n", ans); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long ans; int dp[300000 + 5][20], n; vector<int> e[300000 + 5]; inline void upd(int &x, int y) { if (x < y) x = y; } int dfs(int now, int fa) { int mxdep = 0; for (auto v : e[now]) if (v != fa) upd(mxdep, dfs(v, now)); mxdep++; ans += mxdep; dp[now][1] = n; for (int m = 2; m <= 19; m++) { vector<int> tmp; for (auto v : e[now]) if (v != fa) tmp.push_back(dp[v][m - 1]); sort(tmp.begin(), tmp.end(), greater<int>()); int t = 1; while (t <= (int)tmp.size() && tmp[t - 1] >= t) t++; dp[now][m] = t - 1; upd(dp[now][m], 1); } return mxdep; } void dfs2(int now, int fa) { for (auto v : e[now]) if (v != fa) { dfs2(v, now); for (int m = 1; m <= 19; m++) upd(dp[now][m], dp[v][m]); } for (int m = 1; m <= 18; m++) ans += 1ll * m * (dp[now][m] - dp[now][m + 1]); } int main() { scanf("%d", &n); for (int i = 1; i <= n - 1; i++) { int x, y; scanf("%d%d", &x, &y); e[x].push_back(y); e[y].push_back(x); } dfs(1, 0); dfs2(1, 0); printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 300005; long long n, dp[N][20][2], ans, h[N]; vector<vector<long long> > gr; priority_queue<long long> pq[N][20]; void dfs(long long u, long long par) { dp[u][1][0] = dp[u][1][1] = n; for (long long i = 0; i < gr[u].size(); ++i) { long long v = gr[u][i]; if (v != par) { dfs(v, u); h[u] = max(h[u], h[v] + 1); for (long long j = 2; j <= 18; ++j) { pq[u][j].push(dp[v][j - 1][0]); dp[u][j][1] = max(dp[v][j][1], dp[u][j][1]); } } } vector<long long> x[20]; for (long long i = 2; i <= 18; ++i) { while (!pq[u][i].empty()) { x[i].push_back(pq[u][i].top()); pq[u][i].pop(); } } for (long long i = 2; i <= 18; ++i) { if (x[i].size()) { for (long long j = x[i].size() - 1; j >= 0; --j) { if (x[i][j] >= j + 1) { dp[u][i][0] = max(j + 1, dp[u][i][0]); break; } } dp[u][i][1] = max(dp[u][i][1], dp[u][i][0]); } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; gr.resize(n + 5, vector<long long>()); for (long long i = 0; i < n - 1; ++i) { long long u, v; cin >> u >> v; gr[u].push_back(v); gr[v].push_back(u); } dfs(1, 0); for (long long i = 1; i <= n; ++i) ans += h[i]; ans += n * n; for (long long u = 1; u <= n; ++u) { for (long long i = 1; i <= 19; ++i) { long long l = 2; if (i <= 18) l = max(l, dp[u][i + 1][1] + 1); long long r = dp[u][i][1]; if (l <= r) ans += (r - l + 1) * (i - 1); } } cout << ans; return 0; }
### Prompt Please formulate a CPP solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 300005; long long n, dp[N][20][2], ans, h[N]; vector<vector<long long> > gr; priority_queue<long long> pq[N][20]; void dfs(long long u, long long par) { dp[u][1][0] = dp[u][1][1] = n; for (long long i = 0; i < gr[u].size(); ++i) { long long v = gr[u][i]; if (v != par) { dfs(v, u); h[u] = max(h[u], h[v] + 1); for (long long j = 2; j <= 18; ++j) { pq[u][j].push(dp[v][j - 1][0]); dp[u][j][1] = max(dp[v][j][1], dp[u][j][1]); } } } vector<long long> x[20]; for (long long i = 2; i <= 18; ++i) { while (!pq[u][i].empty()) { x[i].push_back(pq[u][i].top()); pq[u][i].pop(); } } for (long long i = 2; i <= 18; ++i) { if (x[i].size()) { for (long long j = x[i].size() - 1; j >= 0; --j) { if (x[i][j] >= j + 1) { dp[u][i][0] = max(j + 1, dp[u][i][0]); break; } } dp[u][i][1] = max(dp[u][i][1], dp[u][i][0]); } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; gr.resize(n + 5, vector<long long>()); for (long long i = 0; i < n - 1; ++i) { long long u, v; cin >> u >> v; gr[u].push_back(v); gr[v].push_back(u); } dfs(1, 0); for (long long i = 1; i <= n; ++i) ans += h[i]; ans += n * n; for (long long u = 1; u <= n; ++u) { for (long long i = 1; i <= 19; ++i) { long long l = 2; if (i <= 18) l = max(l, dp[u][i + 1][1] + 1); long long r = dp[u][i][1]; if (l <= r) ans += (r - l + 1) * (i - 1); } } cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int &x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et() { puts("-1"); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { long long z = y; while (x % y != 0) { z = x % y; x = y; y = z; } return z; } vector<int> mp[300105]; int d[300105], pa[300105], f[300105][20], dp[300105]; int a[300105], cnt; long long ans; vector<pair<int, int> > vp[300105]; bool cmp(int x, int y) { return y < x; } void dfs(int x) { d[x] = 1; f[x][1] = n; for (int c : mp[x]) if (c != pa[x]) { pa[c] = x; dfs(c); d[x] = max(d[x], d[c] + 1); } ans += d[x]; for (int i = 2; i < 20; i++) { cnt = 0; for (int c : mp[x]) if (c != pa[x]) { a[++cnt] = f[c][i - 1]; } if (cnt == 0) break; sort(a + 1, a + 1 + cnt, cmp); k = 0; while (k < cnt && a[k + 1] >= k + 1) { k++; } f[x][i] = k; vp[k].push_back({x, i}); } } void fmain(int ID) { scanf("%d", &n); for (int i = 0, u, v; i < n - 1; i++) { scanf("%d%d", &u, &v); mp[u].push_back(v); mp[v].push_back(u); }; dfs(1); for (int(i) = 1; (i) <= (int)(n); (i)++) dp[i] = 1; long long ss = n; for (int k = n; k > 1; k--) { for (auto p : vp[k]) { int x = p.first; int j = p.second; while (x && dp[x] < j) { ss += j - dp[x]; dp[x] = j; x = pa[x]; } } ans += ss; } printf("%lld\n", ans); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
### Prompt Please formulate a cpp solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int &x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et() { puts("-1"); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { long long z = y; while (x % y != 0) { z = x % y; x = y; y = z; } return z; } vector<int> mp[300105]; int d[300105], pa[300105], f[300105][20], dp[300105]; int a[300105], cnt; long long ans; vector<pair<int, int> > vp[300105]; bool cmp(int x, int y) { return y < x; } void dfs(int x) { d[x] = 1; f[x][1] = n; for (int c : mp[x]) if (c != pa[x]) { pa[c] = x; dfs(c); d[x] = max(d[x], d[c] + 1); } ans += d[x]; for (int i = 2; i < 20; i++) { cnt = 0; for (int c : mp[x]) if (c != pa[x]) { a[++cnt] = f[c][i - 1]; } if (cnt == 0) break; sort(a + 1, a + 1 + cnt, cmp); k = 0; while (k < cnt && a[k + 1] >= k + 1) { k++; } f[x][i] = k; vp[k].push_back({x, i}); } } void fmain(int ID) { scanf("%d", &n); for (int i = 0, u, v; i < n - 1; i++) { scanf("%d%d", &u, &v); mp[u].push_back(v); mp[v].push_back(u); }; dfs(1); for (int(i) = 1; (i) <= (int)(n); (i)++) dp[i] = 1; long long ss = n; for (int k = n; k > 1; k--) { for (auto p : vp[k]) { int x = p.first; int j = p.second; while (x && dp[x] < j) { ss += j - dp[x]; dp[x] = j; x = pa[x]; } } ans += ss; } printf("%lld\n", ans); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct point { int id, d; }; vector<point> p[300010]; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * f; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } struct node { int x, y, next; } a[600010]; int len, last[300010]; inline void ins(int x, int y) { len++; a[len].x = x; a[len].y = y; a[len].next = last[x]; last[x] = len; } long long ans; int top, sta[300010], f[300010][20], fa[300010], mx[300010]; inline void dfs(int x) { mx[x] = 1; for (int k = last[x]; k; k = a[k].next) { int y = a[k].y; if (y == fa[x]) continue; fa[y] = x; dfs(y); mx[x] = max(mx[x], mx[y] + 1); } ans += mx[x]; for (int i = 2; i <= 18; i++) { int top = 0; for (int k = last[x]; k; k = a[k].next) { int y = a[k].y; if (y == fa[x]) continue; sta[++top] = f[y][i - 1]; } sort(sta + 1, sta + top + 1); int cnt = 0; for (int j = top; j >= 1; j--) { cnt++; if (sta[j] >= cnt) f[x][i] = cnt; else break; } point wy; wy.id = x, wy.d = i; p[f[x][i]].push_back(wy); } } int g[300010]; int main() { int n = read(); for (int i = 1; i < n; i++) { int x = read(), y = read(); ins(x, y), ins(y, x); } for (int i = 1; i <= n; i++) f[i][1] = n; dfs(1); for (int i = 1; i <= n; i++) g[i] = 1; long long sum = n; for (int i = n; i > 1; i--) { for (int j = 0; j < p[i].size(); j++) { int x = p[i][j].id, d = p[i][j].d; while (x && g[x] < d) sum += d - g[x], g[x] = d, x = fa[x]; } ans += sum; } printf("%lld\n", ans); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct point { int id, d; }; vector<point> p[300010]; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * f; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } struct node { int x, y, next; } a[600010]; int len, last[300010]; inline void ins(int x, int y) { len++; a[len].x = x; a[len].y = y; a[len].next = last[x]; last[x] = len; } long long ans; int top, sta[300010], f[300010][20], fa[300010], mx[300010]; inline void dfs(int x) { mx[x] = 1; for (int k = last[x]; k; k = a[k].next) { int y = a[k].y; if (y == fa[x]) continue; fa[y] = x; dfs(y); mx[x] = max(mx[x], mx[y] + 1); } ans += mx[x]; for (int i = 2; i <= 18; i++) { int top = 0; for (int k = last[x]; k; k = a[k].next) { int y = a[k].y; if (y == fa[x]) continue; sta[++top] = f[y][i - 1]; } sort(sta + 1, sta + top + 1); int cnt = 0; for (int j = top; j >= 1; j--) { cnt++; if (sta[j] >= cnt) f[x][i] = cnt; else break; } point wy; wy.id = x, wy.d = i; p[f[x][i]].push_back(wy); } } int g[300010]; int main() { int n = read(); for (int i = 1; i < n; i++) { int x = read(), y = read(); ins(x, y), ins(y, x); } for (int i = 1; i <= n; i++) f[i][1] = n; dfs(1); for (int i = 1; i <= n; i++) g[i] = 1; long long sum = n; for (int i = n; i > 1; i--) { for (int j = 0; j < p[i].size(); j++) { int x = p[i][j].id, d = p[i][j].d; while (x && g[x] < d) sum += d - g[x], g[x] = d, x = fa[x]; } ans += sum; } printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, x, y, dep[300010], v[300010 * 2], nxt[300010 * 2], h[300010], ec, f[300010][30], a[300010], g[300010], fa[300010]; long long r; struct no { int x, y; }; vector<no> vc[300010]; void add(int x, int y) { v[++ec] = y; nxt[ec] = h[x]; h[x] = ec; } int cp(int x, int y) { return x > y; } void dp(int x) { dep[x] = 1; f[x][1] = n; for (int i = h[x]; i; i = nxt[i]) if (v[i] != fa[x]) { fa[v[i]] = x; dp(v[i]); dep[x] = max(dep[x], dep[v[i]] + 1); } r += dep[x]; for (int i = 2; i < 20; i++) { int k = 0, ct = 0; for (int j = h[x]; j; j = nxt[j]) if (v[j] != fa[x]) a[++ct] = f[v[j]][i - 1]; if (!ct) break; sort(a + 1, a + ct + 1, cp); while (k < ct && a[k + 1] > k) k++; f[x][i] = k; vc[k].push_back((no){x, i}); } } int main() { cin >> n; for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); add(x, y); add(y, x); } dp(1); long long s = n; for (int i = 1; i <= n; i++) g[i] = 1; for (int k = n; k > 1; k--) { for (auto x : vc[k]) { int a = x.x, b = x.y; while (a && g[a] < b) { s += b - g[a]; g[a] = b; a = fa[a]; } } r += s; } cout << r; }
### Prompt Please provide a cpp coded solution to the problem described below: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, x, y, dep[300010], v[300010 * 2], nxt[300010 * 2], h[300010], ec, f[300010][30], a[300010], g[300010], fa[300010]; long long r; struct no { int x, y; }; vector<no> vc[300010]; void add(int x, int y) { v[++ec] = y; nxt[ec] = h[x]; h[x] = ec; } int cp(int x, int y) { return x > y; } void dp(int x) { dep[x] = 1; f[x][1] = n; for (int i = h[x]; i; i = nxt[i]) if (v[i] != fa[x]) { fa[v[i]] = x; dp(v[i]); dep[x] = max(dep[x], dep[v[i]] + 1); } r += dep[x]; for (int i = 2; i < 20; i++) { int k = 0, ct = 0; for (int j = h[x]; j; j = nxt[j]) if (v[j] != fa[x]) a[++ct] = f[v[j]][i - 1]; if (!ct) break; sort(a + 1, a + ct + 1, cp); while (k < ct && a[k + 1] > k) k++; f[x][i] = k; vc[k].push_back((no){x, i}); } } int main() { cin >> n; for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); add(x, y); add(y, x); } dp(1); long long s = n; for (int i = 1; i <= n; i++) g[i] = 1; for (int k = n; k > 1; k--) { for (auto x : vc[k]) { int a = x.x, b = x.y; while (a && g[a] < b) { s += b - g[a]; g[a] = b; a = fa[a]; } } r += s; } cout << r; } ```
#include <bits/stdc++.h> using namespace std; char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } void Read(int& x) { char c = nc(); for (; c < '0' || c > '9'; c = nc()) ; for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - 48, c = nc()) ; } const int N = 300010; const int M = 19; int n, m, x, y; int t[N << 1], nx[N << 1], h[N], num; int f[N], c[N], dp[N][M]; int a[N], l, cur[N]; vector<pair<int, int> > g[N]; long long Res, Ans; void Add(int x, int y) { t[++num] = y; nx[num] = h[x]; h[x] = num; } bool Cmp(int x, int y) { return x > y; } void Dfs(int x, int y) { dp[x][1] = n; f[x] = y; for (int i = h[x]; i; i = nx[i]) if (t[i] != y) Dfs(t[i], x), c[x] = max(c[x], c[t[i]]); for (int j = 2; j < M; j++) { l = 0; for (int i = h[x]; i; i = nx[i]) if (t[i] != y && dp[t[i]][j - 1]) a[++l] = dp[t[i]][j - 1]; sort(a + 1, a + l + 1, Cmp); for (int k = l; k > 1; k--) if (a[k] >= k) { dp[x][j] = k; break; } if (dp[x][j]) g[dp[x][j]].push_back(pair<int, int>(x, j)); } Ans += ++c[x]; } void Update(int x, int y) { while (x) { if (cur[x] >= y) break; Res += y - cur[x]; cur[x] = y; x = f[x]; } } int main() { Read(n); for (int i = 1; i < n; i++) Read(x), Read(y), Add(x, y), Add(y, x); Dfs(1, 0); for (int i = 1; i <= n; i++) cur[i] = 1; Res = n; for (int k = n; k > 1; k--) { for (int j = 0; j < g[k].size(); j++) Update(g[k][j].first, g[k][j].second); Ans += Res; } cout << Ans << endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } void Read(int& x) { char c = nc(); for (; c < '0' || c > '9'; c = nc()) ; for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - 48, c = nc()) ; } const int N = 300010; const int M = 19; int n, m, x, y; int t[N << 1], nx[N << 1], h[N], num; int f[N], c[N], dp[N][M]; int a[N], l, cur[N]; vector<pair<int, int> > g[N]; long long Res, Ans; void Add(int x, int y) { t[++num] = y; nx[num] = h[x]; h[x] = num; } bool Cmp(int x, int y) { return x > y; } void Dfs(int x, int y) { dp[x][1] = n; f[x] = y; for (int i = h[x]; i; i = nx[i]) if (t[i] != y) Dfs(t[i], x), c[x] = max(c[x], c[t[i]]); for (int j = 2; j < M; j++) { l = 0; for (int i = h[x]; i; i = nx[i]) if (t[i] != y && dp[t[i]][j - 1]) a[++l] = dp[t[i]][j - 1]; sort(a + 1, a + l + 1, Cmp); for (int k = l; k > 1; k--) if (a[k] >= k) { dp[x][j] = k; break; } if (dp[x][j]) g[dp[x][j]].push_back(pair<int, int>(x, j)); } Ans += ++c[x]; } void Update(int x, int y) { while (x) { if (cur[x] >= y) break; Res += y - cur[x]; cur[x] = y; x = f[x]; } } int main() { Read(n); for (int i = 1; i < n; i++) Read(x), Read(y), Add(x, y), Add(y, x); Dfs(1, 0); for (int i = 1; i <= n; i++) cur[i] = 1; Res = n; for (int k = n; k > 1; k--) { for (int j = 0; j < g[k].size(); j++) Update(g[k][j].first, g[k][j].second); Ans += Res; } cout << Ans << endl; return 0; } ```
#include <bits/stdc++.h> std::vector<int> edges[300000]; int parent[300000]; int dp[300000]; int64_t sum = 0; int subtree[300000]; void dfs_parent(int node, int par) { parent[node] = par; dp[node] = 1; subtree[node] = 1; for (int child : edges[node]) { if (child == par) continue; dfs_parent(child, node); dp[node] = std::max(dp[node], dp[child] + 1); subtree[node] = std::max(subtree[node], subtree[child]); } subtree[node] = std::max(subtree[node], dp[node]); sum += subtree[node]; } std::vector<std::array<int, 30> > children; std::list<int> buckets[300000]; std::list<int>::iterator nums[300000]; int critical[300000]; std::vector<std::pair<int, int> > shifts[300001]; void dfs_heap(int node) { dp[node] = 1; subtree[node] = 1; std::vector<int> best; for (int child : edges[node]) { if (child != parent[node]) { dfs_heap(child); best.push_back(dp[child]); children[node][dp[child]]++; subtree[node] = std::max(subtree[node], subtree[child]); } } std::sort(best.begin(), best.end()); std::reverse(best.begin(), best.end()); if (best.size() >= 2) { dp[node] = std::max(dp[node], best[1] + 1); } for (int c : best) { critical[node] += (c >= (dp[node] - 1)); } nums[node] = buckets[critical[node]].insert(buckets[critical[node]].begin(), node); subtree[node] = std::max(subtree[node], dp[node]); sum += subtree[node]; } void set_critical(int node, int val) { buckets[critical[node]].erase(nums[node]); critical[node] = val; nums[node] = buckets[val].insert(buckets[val].begin(), node); } int64_t total = 0; int main() { int N; scanf("%d", &N); children.resize(N); for (int i = 0; i < N - 1; i++) { int U, V; scanf("%d %d", &U, &V); U--, V--; edges[U].push_back(V); edges[V].push_back(U); } sum = 0; dfs_parent(0, -1); total += sum; sum = 0; dfs_heap(0); total += sum; for (int i = 0; i < N; i++) { edges[i].clear(); } for (int k = 3; k <= N; k++) { std::queue<int> change; for (int node : buckets[k - 1]) { change.push(node); } while (!change.empty()) { int node = change.front(); change.pop(); if (critical[node] >= k || dp[node] == 1) continue; if (parent[node] >= 0) { children[parent[node]][dp[node]]--; if (dp[node] >= dp[parent[node]] - 1) { set_critical(parent[node], critical[parent[node]] - 1); } } shifts[k].emplace_back(node, dp[node]); dp[node]--; set_critical(node, critical[node] + children[node][dp[node] - 1]); if (parent[node] >= 0) { children[parent[node]][dp[node]]++; if (dp[node] >= dp[parent[node]] - 1) { set_critical(parent[node], critical[parent[node]] + 1); } } change.push(node); if (parent[node] >= 0) { change.push(parent[node]); } } } sum = N; for (int i = 0; i < N; i++) { buckets[i].clear(); } children.clear(); for (int k = N; k > 2; k--) { total += sum; std::queue<std::pair<int, int> > events; std::reverse(shifts[k].begin(), shifts[k].end()); for (std::pair<int, int> s : shifts[k]) { events.push(s); } while (!events.empty()) { int node = events.front().first; int set = events.front().second; events.pop(); if (dp[node] < set) { sum -= dp[node]; dp[node] = set; sum += dp[node]; if (parent[node] >= 0) { events.emplace(parent[node], set); } } } } printf("%I64d\n", total); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> std::vector<int> edges[300000]; int parent[300000]; int dp[300000]; int64_t sum = 0; int subtree[300000]; void dfs_parent(int node, int par) { parent[node] = par; dp[node] = 1; subtree[node] = 1; for (int child : edges[node]) { if (child == par) continue; dfs_parent(child, node); dp[node] = std::max(dp[node], dp[child] + 1); subtree[node] = std::max(subtree[node], subtree[child]); } subtree[node] = std::max(subtree[node], dp[node]); sum += subtree[node]; } std::vector<std::array<int, 30> > children; std::list<int> buckets[300000]; std::list<int>::iterator nums[300000]; int critical[300000]; std::vector<std::pair<int, int> > shifts[300001]; void dfs_heap(int node) { dp[node] = 1; subtree[node] = 1; std::vector<int> best; for (int child : edges[node]) { if (child != parent[node]) { dfs_heap(child); best.push_back(dp[child]); children[node][dp[child]]++; subtree[node] = std::max(subtree[node], subtree[child]); } } std::sort(best.begin(), best.end()); std::reverse(best.begin(), best.end()); if (best.size() >= 2) { dp[node] = std::max(dp[node], best[1] + 1); } for (int c : best) { critical[node] += (c >= (dp[node] - 1)); } nums[node] = buckets[critical[node]].insert(buckets[critical[node]].begin(), node); subtree[node] = std::max(subtree[node], dp[node]); sum += subtree[node]; } void set_critical(int node, int val) { buckets[critical[node]].erase(nums[node]); critical[node] = val; nums[node] = buckets[val].insert(buckets[val].begin(), node); } int64_t total = 0; int main() { int N; scanf("%d", &N); children.resize(N); for (int i = 0; i < N - 1; i++) { int U, V; scanf("%d %d", &U, &V); U--, V--; edges[U].push_back(V); edges[V].push_back(U); } sum = 0; dfs_parent(0, -1); total += sum; sum = 0; dfs_heap(0); total += sum; for (int i = 0; i < N; i++) { edges[i].clear(); } for (int k = 3; k <= N; k++) { std::queue<int> change; for (int node : buckets[k - 1]) { change.push(node); } while (!change.empty()) { int node = change.front(); change.pop(); if (critical[node] >= k || dp[node] == 1) continue; if (parent[node] >= 0) { children[parent[node]][dp[node]]--; if (dp[node] >= dp[parent[node]] - 1) { set_critical(parent[node], critical[parent[node]] - 1); } } shifts[k].emplace_back(node, dp[node]); dp[node]--; set_critical(node, critical[node] + children[node][dp[node] - 1]); if (parent[node] >= 0) { children[parent[node]][dp[node]]++; if (dp[node] >= dp[parent[node]] - 1) { set_critical(parent[node], critical[parent[node]] + 1); } } change.push(node); if (parent[node] >= 0) { change.push(parent[node]); } } } sum = N; for (int i = 0; i < N; i++) { buckets[i].clear(); } children.clear(); for (int k = N; k > 2; k--) { total += sum; std::queue<std::pair<int, int> > events; std::reverse(shifts[k].begin(), shifts[k].end()); for (std::pair<int, int> s : shifts[k]) { events.push(s); } while (!events.empty()) { int node = events.front().first; int set = events.front().second; events.pop(); if (dp[node] < set) { sum -= dp[node]; dp[node] = set; sum += dp[node]; if (parent[node] >= 0) { events.emplace(parent[node], set); } } } } printf("%I64d\n", total); return 0; } ```
#include <bits/stdc++.h> using std::vector; const int N = 300005; int n, f[N][30], g[N][30], tmp[N]; long long ans; vector<int> e[N]; int dfs(int u, int fa) { int sz = 0, mx = 0; for (int v : e[u]) if (v != fa) mx = std::max(mx, dfs(v, u)), ++sz; ++mx; f[u][1] = n; ans += n - 1 + mx; for (int i = 2; i <= 25; ++i) { std::fill(tmp, tmp + sz + 1, 0); for (int v : e[u]) if (v != fa) ++tmp[std::min(f[v][i - 1], sz)]; for (int j = sz, cur = 0; ~j; --j) { cur += tmp[j]; if (cur >= j) { f[u][i] = j; break; } } g[u][i] = f[u][i]; for (int v : e[u]) if (v != fa) g[u][i] = std::max(g[u][i], g[v][i]); ans += std::max(g[u][i] - 1, 0); } return mx; } int main() { scanf("%d", &n); for (int i = 1, x, y; i < n; ++i) scanf("%d%d", &x, &y), e[x].push_back(y), e[y].push_back(x); dfs(1, 0); printf("%lld\n", ans); return 0; }
### Prompt Generate a CPP solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using std::vector; const int N = 300005; int n, f[N][30], g[N][30], tmp[N]; long long ans; vector<int> e[N]; int dfs(int u, int fa) { int sz = 0, mx = 0; for (int v : e[u]) if (v != fa) mx = std::max(mx, dfs(v, u)), ++sz; ++mx; f[u][1] = n; ans += n - 1 + mx; for (int i = 2; i <= 25; ++i) { std::fill(tmp, tmp + sz + 1, 0); for (int v : e[u]) if (v != fa) ++tmp[std::min(f[v][i - 1], sz)]; for (int j = sz, cur = 0; ~j; --j) { cur += tmp[j]; if (cur >= j) { f[u][i] = j; break; } } g[u][i] = f[u][i]; for (int v : e[u]) if (v != fa) g[u][i] = std::max(g[u][i], g[v][i]); ans += std::max(g[u][i] - 1, 0); } return mx; } int main() { scanf("%d", &n); for (int i = 1, x, y; i < n; ++i) scanf("%d%d", &x, &y), e[x].push_back(y), e[y].push_back(x); dfs(1, 0); printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline int read() { int x = 0; char ch = getchar(); bool positive = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(int a) { if (a >= 10) write(a / 10); putchar('0' + a % 10); } inline void writeln(int a) { if (a < 0) { a = -a; putchar('-'); } write(a); puts(""); } const int N = 300005, K = 20; long long ans; int n, tp[N], q[N], dp[N][K]; vector<int> v[N]; inline bool cmp(int a, int b) { return a > b; } void dfs(int p, int fa) { for (unsigned i = 0; i < v[p].size(); i++) if (v[p][i] != fa) { dfs(v[p][i], p); tp[p] = max(tp[v[p][i]], tp[p]); } ans += ++tp[p]; dp[p][1] = n; for (int i = 2; i < K; i++) { int tot = 0; for (unsigned j = 0; j < v[p].size(); j++) if (dp[v[p][j]][i - 1] > 1) q[++tot] = dp[v[p][j]][i - 1]; sort(&q[1], &q[tot + 1], cmp); for (int j = tot; j > 1; j--) { if (q[j] >= j) { dp[p][i] = j; break; } } } } void solve(int p, int fa) { for (unsigned i = 0; i < v[p].size(); i++) if (v[p][i] != fa) { solve(v[p][i], p); for (int j = 0; j < K; j++) dp[p][j] = max(dp[p][j], dp[v[p][i]][j]); } for (int i = 1; i < K - 1; i++) { ans += ((long long)dp[p][i] - dp[p][i + 1]) * i; if (dp[p][i + 1] == 0) { ans -= i; break; } } } int main() { n = read(); for (int i = 1; i < n; i++) { int s = read(), t = read(); v[s].push_back(t); v[t].push_back(s); } dfs(1, 0); solve(1, 0); cout << ans << endl; }
### Prompt Please create a solution in cpp to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline int read() { int x = 0; char ch = getchar(); bool positive = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(int a) { if (a >= 10) write(a / 10); putchar('0' + a % 10); } inline void writeln(int a) { if (a < 0) { a = -a; putchar('-'); } write(a); puts(""); } const int N = 300005, K = 20; long long ans; int n, tp[N], q[N], dp[N][K]; vector<int> v[N]; inline bool cmp(int a, int b) { return a > b; } void dfs(int p, int fa) { for (unsigned i = 0; i < v[p].size(); i++) if (v[p][i] != fa) { dfs(v[p][i], p); tp[p] = max(tp[v[p][i]], tp[p]); } ans += ++tp[p]; dp[p][1] = n; for (int i = 2; i < K; i++) { int tot = 0; for (unsigned j = 0; j < v[p].size(); j++) if (dp[v[p][j]][i - 1] > 1) q[++tot] = dp[v[p][j]][i - 1]; sort(&q[1], &q[tot + 1], cmp); for (int j = tot; j > 1; j--) { if (q[j] >= j) { dp[p][i] = j; break; } } } } void solve(int p, int fa) { for (unsigned i = 0; i < v[p].size(); i++) if (v[p][i] != fa) { solve(v[p][i], p); for (int j = 0; j < K; j++) dp[p][j] = max(dp[p][j], dp[v[p][i]][j]); } for (int i = 1; i < K - 1; i++) { ans += ((long long)dp[p][i] - dp[p][i + 1]) * i; if (dp[p][i + 1] == 0) { ans -= i; break; } } } int main() { n = read(); for (int i = 1; i < n; i++) { int s = read(), t = read(); v[s].push_back(t); v[t].push_back(s); } dfs(1, 0); solve(1, 0); cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5; int dp[maxn + 5][25]; vector<int> g[maxn + 5]; int a[maxn + 5], mx[maxn + 5]; int len; int n; long long ans = 0; void dfs(int k, int fa) { for (auto u : g[k]) { if (u == fa) continue; dfs(u, k); } dp[k][1] = n; for (int dep = 2; dep <= 20; ++dep) { len = 0; for (auto u : g[k]) { if (u == fa) continue; a[++len] = dp[u][dep - 1]; } sort(a + 1, a + len + 1); reverse(a + 1, a + len + 1); for (int i = len; i >= 1; --i) if (a[i] >= i) { dp[k][dep] = i; break; } } } void dfs1(int k, int fa) { mx[k] = 1; for (auto u : g[k]) { if (u == fa) continue; dfs1(u, k); for (int i = 1; i <= 20; ++i) dp[k][i] = max(dp[k][i], dp[u][i]); mx[k] = max(mx[k], mx[u] + 1); } } int main() { scanf("%d", &n); memset(dp, 0, sizeof(dp)); for (int i = 1; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); g[x].push_back(y), g[y].push_back(x); } dfs(1, 0); dfs1(1, 0); for (int i = 1; i <= n; ++i) ans += mx[i]; for (int i = 1; i <= n; ++i) { for (int j = 20; j >= 1; --j) dp[i][j] = max(dp[i][j], dp[i][j + 1]); for (int j = 1; j <= 21; ++j) if (dp[i][j] == 0) { dp[i][j] = 1; break; } for (int j = 1; j <= 20; ++j) { if (dp[i][j] == 1) break; ans += 1LL * j * (dp[i][j] - dp[i][j + 1]); } } printf("%lld\n", ans); return 0; }
### Prompt Generate a CPP solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 3e5; int dp[maxn + 5][25]; vector<int> g[maxn + 5]; int a[maxn + 5], mx[maxn + 5]; int len; int n; long long ans = 0; void dfs(int k, int fa) { for (auto u : g[k]) { if (u == fa) continue; dfs(u, k); } dp[k][1] = n; for (int dep = 2; dep <= 20; ++dep) { len = 0; for (auto u : g[k]) { if (u == fa) continue; a[++len] = dp[u][dep - 1]; } sort(a + 1, a + len + 1); reverse(a + 1, a + len + 1); for (int i = len; i >= 1; --i) if (a[i] >= i) { dp[k][dep] = i; break; } } } void dfs1(int k, int fa) { mx[k] = 1; for (auto u : g[k]) { if (u == fa) continue; dfs1(u, k); for (int i = 1; i <= 20; ++i) dp[k][i] = max(dp[k][i], dp[u][i]); mx[k] = max(mx[k], mx[u] + 1); } } int main() { scanf("%d", &n); memset(dp, 0, sizeof(dp)); for (int i = 1; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); g[x].push_back(y), g[y].push_back(x); } dfs(1, 0); dfs1(1, 0); for (int i = 1; i <= n; ++i) ans += mx[i]; for (int i = 1; i <= n; ++i) { for (int j = 20; j >= 1; --j) dp[i][j] = max(dp[i][j], dp[i][j + 1]); for (int j = 1; j <= 21; ++j) if (dp[i][j] == 0) { dp[i][j] = 1; break; } for (int j = 1; j <= 20; ++j) { if (dp[i][j] == 1) break; ans += 1LL * j * (dp[i][j] - dp[i][j + 1]); } } printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int B = 550; const int N = B * B; const int C = 24; const int D = 4; int dp[N]; int max_depth[N]; int n; vector<int> edges[N]; void dfs(int v, int par, int k) { vector<int> pool; int ma = 1; for (int i = (int)(0); i < (int)(edges[v].size()); i++) { int w = edges[v][i]; if (par == w) continue; dfs(w, v, k); pool.push_back(max_depth[w]); ma = max(ma, dp[w]); } max_depth[v] = 1; if ((int)pool.size() >= k) { nth_element(pool.begin(), pool.end() - k, pool.end()); max_depth[v] = pool[pool.size() - k] + 1; ma = max(ma, max_depth[v]); } dp[v] = ma; } int dp_dep[D + 1][N]; int dp_dep_direct[D + 1][N]; int dfs_dep(int v, int par, int dep) { int ma = 0; vector<int> pool; for (int i = (int)(0); i < (int)(edges[v].size()); i++) { int w = edges[v][i]; if (par == w) continue; int val = dfs_dep(w, v, dep); pool.push_back(dp_dep_direct[dep - 1][w]); ma = max(ma, val); } int ma_dir = 0; sort(pool.rbegin(), pool.rend()); for (int i = (int)(0); i < (int)(pool.size()); i++) { if (pool[i] >= i + 1) { ma_dir = max(ma_dir, i + 1); } } dp_dep_direct[dep][v] = ma_dir; return dp_dep[dep][v] = max(ma, ma_dir); } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = (int)(0); i < (int)(n - 1); i++) { int u, v; cin >> u >> v; u--, v--; edges[u].push_back(v); edges[v].push_back(u); } long long int tot = 0; for (int k = (int)(1); k < (int)(C + 1); k++) { if (k <= n) { dfs(0, -1, k); for (int i = (int)(0); i < (int)(n); i++) { tot += dp[i]; } } } if (n > C) { for (int i = (int)(0); i < (int)(n); i++) { dp_dep[1][i] = n; dp_dep_direct[1][i] = n; } for (int b = (int)(2); b < (int)(D + 1); b++) { dfs_dep(0, -1, b); } for (int i = (int)(0); i < (int)(n); i++) { if (0) { cerr << "i" << "=" << i << "\n"; for (int b = (int)(2); b < (int)(D + 1); b++) { cerr << "b" << "=" << b << "\n"; cerr << "dp_dep[b][i]" << "=" << dp_dep[b][i] << "\n"; cerr << "dp_dep_direct[b][i]" << "=" << dp_dep_direct[b][i] << "\n"; } } tot += n - C; for (int b = (int)(2); b < (int)(D + 1); b++) { tot += max(dp_dep[b][i], C) - C; } } } cout << tot << endl; }
### Prompt In Cpp, your task is to solve the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int B = 550; const int N = B * B; const int C = 24; const int D = 4; int dp[N]; int max_depth[N]; int n; vector<int> edges[N]; void dfs(int v, int par, int k) { vector<int> pool; int ma = 1; for (int i = (int)(0); i < (int)(edges[v].size()); i++) { int w = edges[v][i]; if (par == w) continue; dfs(w, v, k); pool.push_back(max_depth[w]); ma = max(ma, dp[w]); } max_depth[v] = 1; if ((int)pool.size() >= k) { nth_element(pool.begin(), pool.end() - k, pool.end()); max_depth[v] = pool[pool.size() - k] + 1; ma = max(ma, max_depth[v]); } dp[v] = ma; } int dp_dep[D + 1][N]; int dp_dep_direct[D + 1][N]; int dfs_dep(int v, int par, int dep) { int ma = 0; vector<int> pool; for (int i = (int)(0); i < (int)(edges[v].size()); i++) { int w = edges[v][i]; if (par == w) continue; int val = dfs_dep(w, v, dep); pool.push_back(dp_dep_direct[dep - 1][w]); ma = max(ma, val); } int ma_dir = 0; sort(pool.rbegin(), pool.rend()); for (int i = (int)(0); i < (int)(pool.size()); i++) { if (pool[i] >= i + 1) { ma_dir = max(ma_dir, i + 1); } } dp_dep_direct[dep][v] = ma_dir; return dp_dep[dep][v] = max(ma, ma_dir); } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = (int)(0); i < (int)(n - 1); i++) { int u, v; cin >> u >> v; u--, v--; edges[u].push_back(v); edges[v].push_back(u); } long long int tot = 0; for (int k = (int)(1); k < (int)(C + 1); k++) { if (k <= n) { dfs(0, -1, k); for (int i = (int)(0); i < (int)(n); i++) { tot += dp[i]; } } } if (n > C) { for (int i = (int)(0); i < (int)(n); i++) { dp_dep[1][i] = n; dp_dep_direct[1][i] = n; } for (int b = (int)(2); b < (int)(D + 1); b++) { dfs_dep(0, -1, b); } for (int i = (int)(0); i < (int)(n); i++) { if (0) { cerr << "i" << "=" << i << "\n"; for (int b = (int)(2); b < (int)(D + 1); b++) { cerr << "b" << "=" << b << "\n"; cerr << "dp_dep[b][i]" << "=" << dp_dep[b][i] << "\n"; cerr << "dp_dep_direct[b][i]" << "=" << dp_dep_direct[b][i] << "\n"; } } tot += n - C; for (int b = (int)(2); b < (int)(D + 1); b++) { tot += max(dp_dep[b][i], C) - C; } } } cout << tot << endl; } ```
#include <bits/stdc++.h> using namespace std; int n; long long int Ans = 0; int Par[300005]; int Reach[300005]; int dp[300005][25]; vector<int> adj[300005]; vector<pair<int, int> > Update[300005]; long long int dfs1(int u, int p) { long long int mx = 0; Par[u] = p; for (int i = 0; i < adj[u].size(); i++) { int w = adj[u][i]; if (w == p) continue; long long int ret = dfs1(w, u); mx = max(mx, ret); } Ans += mx + 1; return mx + 1; } void solve(int u, int p) { for (int i = 0; i < adj[u].size(); i++) { int w = adj[u][i]; if (w == p) continue; solve(w, u); } dp[u][1] = n; for (int d = 2; d < 25; d++) { vector<int> v; for (int i = 0; i < adj[u].size(); i++) { int w = adj[u][i]; if (w == p) continue; if (dp[w][d - 1]) v.push_back(dp[w][d - 1]); } sort(v.begin(), v.end(), greater<int>()); int mx = 0; for (int i = 1; i <= v.size(); i++) if (v[i - 1] >= i) mx = i; dp[u][d] = mx; if (mx) Update[mx].push_back(make_pair(u, d)); } } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } dfs1(1, 0); solve(1, 0); long long int Add = n; for (int i = 1; i <= n; i++) Reach[i] = 1; for (int k = n; k > 1; k--) { for (int i = 0; i < Update[k].size(); i++) { int u = Update[k][i].first; int d = Update[k][i].second; while (u && Reach[u] < d) { Add += d - Reach[u]; Reach[u] = d; u = Par[u]; } } Ans += Add; } cout << Ans << endl; }
### Prompt Develop a solution in cpp to the problem described below: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; long long int Ans = 0; int Par[300005]; int Reach[300005]; int dp[300005][25]; vector<int> adj[300005]; vector<pair<int, int> > Update[300005]; long long int dfs1(int u, int p) { long long int mx = 0; Par[u] = p; for (int i = 0; i < adj[u].size(); i++) { int w = adj[u][i]; if (w == p) continue; long long int ret = dfs1(w, u); mx = max(mx, ret); } Ans += mx + 1; return mx + 1; } void solve(int u, int p) { for (int i = 0; i < adj[u].size(); i++) { int w = adj[u][i]; if (w == p) continue; solve(w, u); } dp[u][1] = n; for (int d = 2; d < 25; d++) { vector<int> v; for (int i = 0; i < adj[u].size(); i++) { int w = adj[u][i]; if (w == p) continue; if (dp[w][d - 1]) v.push_back(dp[w][d - 1]); } sort(v.begin(), v.end(), greater<int>()); int mx = 0; for (int i = 1; i <= v.size(); i++) if (v[i - 1] >= i) mx = i; dp[u][d] = mx; if (mx) Update[mx].push_back(make_pair(u, d)); } } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } dfs1(1, 0); solve(1, 0); long long int Add = n; for (int i = 1; i <= n; i++) Reach[i] = 1; for (int k = n; k > 1; k--) { for (int i = 0; i < Update[k].size(); i++) { int u = Update[k][i].first; int d = Update[k][i].second; while (u && Reach[u] < d) { Add += d - Reach[u]; Reach[u] = d; u = Par[u]; } } Ans += Add; } cout << Ans << endl; } ```
#include <bits/stdc++.h> template <class T> std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) { for (const T &i : v) { s << i << " "; } return s; } struct Vertex { std::vector<Vertex *> chld; std::vector<uint32_t> heapDepth; uint64_t ans; Vertex *par; std::vector<uint32_t> *dp; Vertex() : ans(0), par(nullptr), dp(nullptr) {} ~Vertex() { if (dp != nullptr) { delete dp; } } static bool cmp(Vertex *a, Vertex *b) { return a->chld.size() < b->chld.size(); } void root_tree() { for (auto it = chld.begin(); it != chld.end(); it++) { if (*it == par) { chld.erase(it); break; } } for (Vertex *v : chld) { v->par = this; v->root_tree(); } std::sort(chld.begin(), chld.end(), cmp); std::vector<uint32_t> vals; vals.reserve(chld.size()); uint32_t begInd = 0; for (uint32_t k = 1; k <= chld.size(); k++) { while ((begInd < chld.size()) && (chld[begInd]->chld.size() < k)) { begInd++; } if (chld.size() - begInd < k) { heapDepth.push_back(2); continue; } vals.clear(); for (uint32_t i = begInd; i < chld.size(); i++) { vals.push_back(chld[i]->heapDepth[k - 1]); } std::sort(vals.rbegin(), vals.rend()); heapDepth.push_back(vals[k - 1] + 1); } } uint64_t calc_ans(uint32_t n) { for (Vertex *v : chld) { uint64_t t = v->calc_ans(n); if ((dp == nullptr) || (dp->size() < v->dp->size())) { dp = v->dp; ans = t; } } if (dp == nullptr) { dp = new std::vector<uint32_t>(); } if (heapDepth.size() > dp->size()) { dp->resize(heapDepth.size()); } for (uint32_t i = 0; i < heapDepth.size(); i++) { if (heapDepth[i] > dp->at(i)) { ans -= dp->at(i); dp->at(i) = heapDepth[i]; ans += dp->at(i); } } for (Vertex *v : chld) { if (v->dp == dp) { v->dp = nullptr; continue; } for (uint32_t i = 0; i < v->dp->size(); i++) { if (v->dp->at(i) > dp->at(i)) { ans -= dp->at(i); dp->at(i) = v->dp->at(i); ans += dp->at(i); } } } uint64_t res = ans; ans += n - dp->size(); return res; } }; std::vector<Vertex> tree; void input() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); uint32_t vCnt; std::cin >> vCnt; tree.resize(vCnt); for (uint32_t i = 1; i < vCnt; i++) { uint32_t a, b; std::cin >> a >> b; a--; b--; tree[a].chld.push_back(&tree[b]); tree[b].chld.push_back(&tree[a]); } } int main() { input(); tree[0].root_tree(); uint64_t ans = 0; tree[0].calc_ans(tree.size()); for (uint32_t i = 0; i < tree.size(); i++) { } for (Vertex &v : tree) { ans += v.ans; } std::cout << ans << "\n"; }
### Prompt Generate a CPP solution to the following problem: You're given a tree with n vertices rooted at 1. We say that there's a k-ary heap of depth m located at u if the following holds: * For m = 1 u itself is a k-ary heap of depth 1. * For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>. Input The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge. It's guaranteed that the given configuration forms a tree. Output Output the answer to the task. Examples Input 4 1 3 2 3 4 3 Output 21 Input 4 1 2 2 3 3 4 Output 22 Note Consider sample case one. For k ≥ 3 all dpk will be equal to 1. For k = 2 dpk is 2 if <image> and 1 otherwise. For k = 1 dpk values are (3, 1, 2, 1) respectively. To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21. ### Response ```cpp #include <bits/stdc++.h> template <class T> std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) { for (const T &i : v) { s << i << " "; } return s; } struct Vertex { std::vector<Vertex *> chld; std::vector<uint32_t> heapDepth; uint64_t ans; Vertex *par; std::vector<uint32_t> *dp; Vertex() : ans(0), par(nullptr), dp(nullptr) {} ~Vertex() { if (dp != nullptr) { delete dp; } } static bool cmp(Vertex *a, Vertex *b) { return a->chld.size() < b->chld.size(); } void root_tree() { for (auto it = chld.begin(); it != chld.end(); it++) { if (*it == par) { chld.erase(it); break; } } for (Vertex *v : chld) { v->par = this; v->root_tree(); } std::sort(chld.begin(), chld.end(), cmp); std::vector<uint32_t> vals; vals.reserve(chld.size()); uint32_t begInd = 0; for (uint32_t k = 1; k <= chld.size(); k++) { while ((begInd < chld.size()) && (chld[begInd]->chld.size() < k)) { begInd++; } if (chld.size() - begInd < k) { heapDepth.push_back(2); continue; } vals.clear(); for (uint32_t i = begInd; i < chld.size(); i++) { vals.push_back(chld[i]->heapDepth[k - 1]); } std::sort(vals.rbegin(), vals.rend()); heapDepth.push_back(vals[k - 1] + 1); } } uint64_t calc_ans(uint32_t n) { for (Vertex *v : chld) { uint64_t t = v->calc_ans(n); if ((dp == nullptr) || (dp->size() < v->dp->size())) { dp = v->dp; ans = t; } } if (dp == nullptr) { dp = new std::vector<uint32_t>(); } if (heapDepth.size() > dp->size()) { dp->resize(heapDepth.size()); } for (uint32_t i = 0; i < heapDepth.size(); i++) { if (heapDepth[i] > dp->at(i)) { ans -= dp->at(i); dp->at(i) = heapDepth[i]; ans += dp->at(i); } } for (Vertex *v : chld) { if (v->dp == dp) { v->dp = nullptr; continue; } for (uint32_t i = 0; i < v->dp->size(); i++) { if (v->dp->at(i) > dp->at(i)) { ans -= dp->at(i); dp->at(i) = v->dp->at(i); ans += dp->at(i); } } } uint64_t res = ans; ans += n - dp->size(); return res; } }; std::vector<Vertex> tree; void input() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); uint32_t vCnt; std::cin >> vCnt; tree.resize(vCnt); for (uint32_t i = 1; i < vCnt; i++) { uint32_t a, b; std::cin >> a >> b; a--; b--; tree[a].chld.push_back(&tree[b]); tree[b].chld.push_back(&tree[a]); } } int main() { input(); tree[0].root_tree(); uint64_t ans = 0; tree[0].calc_ans(tree.size()); for (uint32_t i = 0; i < tree.size(); i++) { } for (Vertex &v : tree) { ans += v.ans; } std::cout << ans << "\n"; } ```