#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
int block1 = 0;
for (auto c : X) block1 += (c & 1 ^ 1);
if (block1 == M)
return accumulate(all(W), 0ll);
long long val[N + 1];
for (int i = 0; i <= N; i++)
val[i] = 0;
for (int i = 0; i < M; i++)
val[X[i]] = W[i];
long long evenPref[N + 1], evenSuf[N + 1];
long long oddPref[N + 1], oddSuf[N + 1];
for (int i = 0; i <= N; i++)
evenPref[i] = evenSuf[i] = oddPref[i] = oddSuf[i] = 0;
evenPref[0] = val[0];
evenSuf[N - 1] = ((N - 1) & 1 ^ 1) * val[N - 1];
for (int i = 1; i < N; i++) {
evenPref[i] = evenPref[i - 1];
if (i & 1 ^ 1) evenPref[i] += val[i];
}
for (int i = N - 2; i >= 0; i--) {
evenSuf[i] = evenSuf[i + 1];
if (i & 1 ^ 1) evenSuf[i] += val[i];
}
for (int i = 1; i < N; i++) {
oddPref[i] = oddPref[i - 1];
if (i & 1) oddPref[i] += val[i];
}
oddSuf[N - 1] = ((N - 1) & 1) * val[N - 1];
for (int i = N - 2; i >= 0; i--) {
oddSuf[i] = oddSuf[i + 1];
if (i & 1) oddSuf[i] += val[i];
}
long long ans = max(evenPref[N - 1], oddPref[N - 1]);
for (int i = 0; i < N; i++) {
long long lEven = (i ? evenPref[i - 1] : 0);
long long rOdd = oddSuf[i + 1];
ans = max(ans, lEven + rOdd);
long long lOdd = (i ? oddPref[i - 1] : 0);
long long rEven = evenSuf[i + 1];
ans = max(ans, lOdd + rEven);
}
return ans;
}
//main() {
// int N = 10, M = 4;
// vector <int> X = {0, 5, 7, 4};
// vector <int> Y = {0, 0, 0, 0};
// vector <int> W = {10, 5, 3, 6};
// cout << max_weights(N, M, X, Y, W) << '\n';
//}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:9:35: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
9 | for (auto c : X) block1 += (c & 1 ^ 1);
| ~~^~~
fish.cpp:26:31: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
26 | evenSuf[N - 1] = ((N - 1) & 1 ^ 1) * val[N - 1];
| ~~~~~~~~^~~
fish.cpp:30:15: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
30 | if (i & 1 ^ 1) evenPref[i] += val[i];
| ~~^~~
fish.cpp:35:15: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
35 | if (i & 1 ^ 1) evenSuf[i] += val[i];
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
2144 KB |
Output is correct |
2 |
Correct |
28 ms |
2628 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
81 ms |
7268 KB |
Output is correct |
6 |
Correct |
82 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Incorrect |
18 ms |
5036 KB |
1st lines differ - on the 1st token, expected: '21261825233649', found: '13439137918613' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Incorrect |
18 ms |
5036 KB |
1st lines differ - on the 1st token, expected: '21261825233649', found: '13439137918613' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
2144 KB |
Output is correct |
2 |
Correct |
28 ms |
2628 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
81 ms |
7268 KB |
Output is correct |
6 |
Correct |
82 ms |
7260 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
8 |
Halted |
0 ms |
0 KB |
- |