#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
int max_score(int n, int x, int y, ll k, vi u, vi v, vi w) {
vector<vector<pair<int, ll>>> adj(n);
for (int i = 0; i < n - 1; i++) {
adj[u[i]].push_back({v[i], w[i]});
adj[v[i]].push_back({u[i], w[i]});
}
int ans = 2;
vector<vector<ll>> dst(n, vector<ll>(n));
for (int i = 0; i < n; i++) {
ll sum = 0;
for (int j = i + 1; j < n; j++) {
dst[i][j] = dst[i][j - 1] + w[j - 1];
dst[j][i] = dst[i][j];
}
}
for (int i = 0; i < n; i++) {
ll sum = 0;
for (int j = i; j < n; j++) {
sum += max(dst[j][x], dst[j][y]);
if (sum > k) break;
int l = i - 1, r = j + 1;
ll add = 0;
while (l >= x) {
add += dst[l][x]; l--;
}
while (r <= y) {
add += dst[r][y]; r++;
}
while (true) {
if (l == -1 && r == n) {
break;
}
if (l == -1) {
if (add + sum + dst[r][y] > k) break;
add += dst[r][y]; r++;
} else if (r == n) {
if (add + sum + dst[l][x] > k) break;
add += dst[l][x]; l++;
} else {
if (dst[l][x] < dst[r][y]) {
if (add + sum + dst[l][x] > k) break;
add += dst[l][x]; l++;
} else {
if (add + sum + dst[r][y] > k) break;
add += dst[r][y]; r++;
}
}
}
ans = max(ans, r - l - 1);
}
}
auto proc = [&w, &n, &dst](int x) -> vector<ll> {
vector<ll> dp(n + 1, 1e13);
dp[0] = 0;
for (int i = 0; i <= x; i++) {
ll sum = 0;
for (int j = i; j < n; j++) {
sum += dst[j][x];
if (j < x) continue;
dp[j - i + 1] = min(dp[j - i + 1], sum);
}
}
return dp;
};
vector<ll> dpX = proc(x);
vector<ll> dpY = proc(y);
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (dpX[i] + dpY[j] <= k) {
ans = max(ans, i + j);
}
}
}
return ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |