This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
int mx = -1;
for (auto c : X) mx = max(mx, c);
if (mx <= 1) {
vector <long long> col[2];
for (auto i : {0, 1})
col[i].resize(N);
for (int i = 0; i < M; i++)
col[X[i]][Y[i]] += W[i];
for (int i = 0; i < 2; i++)
for (int j = 1; j < N; j++)
col[i][j] += col[i][j-1];
if (N == 2)
return max(col[0][1], col[0][0]);
long long ans = col[1][N - 1];
for (int i = 0; i < N; i++)
ans = max(ans, col[0][i] + (col[1][N - 1] - col[1][i]));
return ans;
}
long long dp[N + 1][2];
long long val[N + 1];
for (int i = 0; i <= N; i++) {
for (int j : {0, 1})
dp[i][j] = 0;
val[i] = 0;
}
for (int i = 0; i < M; i++)
X[i]++;
for (int i = 0; i < M; i++)
val[X[i]] = W[i];
dp[0][1] = val[1];
for (int i = 2; i <= N; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(dp[i - 1][1] - val[i], max(dp[i - 2][0] + val[i - 1], dp[i - 2][1])) + val[i + 1];
}
return max(dp[N - 1][0], dp[N - 1][1]);
}
//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 (stderr)
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);
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |