이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*input
7
2 0
2 0
2 0
0 5
2 0
2 0
2 0
*/
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
ll ans = 0, prefa = 0, prefb = 0;
vector<ll>diff(n);
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
prefa += a;
prefb += b;
if (i < n - 1) {
ans += abs(prefa - prefb);
diff[i] = prefa - prefb;
}
}
ll need = prefa - prefb;
vector<ll>dp(need + 1, -1e18);
dp[0] = 0;
for (int i = 0; i < n - 1; i++) {
ll mx = 0;
for (ll j = 0; j <= need; j++) {
mx = max(mx, dp[j]);
ll val = diff[i];
dp[j] = -1e18;
if (val > 0) {
ll add = min(val, j) - (j - min(val, j));
dp[j] = max(dp[j], mx + add);
} else {
dp[j] = max(dp[j], mx - j);
}
}
}
ll mx = 0;
for (int i = 0; i <= need; i++) {
mx = max(mx, dp[i]);
}
cout << ans - mx << "\n";
return 0;
}
# | 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... |