/*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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
13 ms |
1024 KB |
Output is correct |
5 |
Correct |
20 ms |
1812 KB |
Output is correct |
6 |
Correct |
66 ms |
5752 KB |
Output is correct |
7 |
Correct |
125 ms |
11000 KB |
Output is correct |
8 |
Correct |
108 ms |
9208 KB |
Output is correct |
9 |
Correct |
107 ms |
8440 KB |
Output is correct |
10 |
Correct |
77 ms |
6264 KB |
Output is correct |
11 |
Correct |
77 ms |
6264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
13 ms |
1024 KB |
Output is correct |
5 |
Correct |
20 ms |
1812 KB |
Output is correct |
6 |
Correct |
66 ms |
5752 KB |
Output is correct |
7 |
Correct |
125 ms |
11000 KB |
Output is correct |
8 |
Correct |
108 ms |
9208 KB |
Output is correct |
9 |
Correct |
107 ms |
8440 KB |
Output is correct |
10 |
Correct |
77 ms |
6264 KB |
Output is correct |
11 |
Correct |
77 ms |
6264 KB |
Output is correct |
12 |
Correct |
44 ms |
3064 KB |
Output is correct |
13 |
Correct |
74 ms |
6776 KB |
Output is correct |
14 |
Correct |
121 ms |
11000 KB |
Output is correct |
15 |
Correct |
100 ms |
9208 KB |
Output is correct |
16 |
Correct |
97 ms |
8584 KB |
Output is correct |
17 |
Correct |
66 ms |
6264 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
13 ms |
384 KB |
Output is correct |
5 |
Correct |
34 ms |
512 KB |
Output is correct |
6 |
Correct |
154 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
17 ms |
384 KB |
Output is correct |
9 |
Correct |
17 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
13 ms |
384 KB |
Output is correct |
6 |
Correct |
34 ms |
512 KB |
Output is correct |
7 |
Correct |
154 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
17 ms |
384 KB |
Output is correct |
10 |
Correct |
17 ms |
384 KB |
Output is correct |
11 |
Execution timed out |
1088 ms |
163960 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
13 ms |
384 KB |
Output is correct |
6 |
Correct |
34 ms |
512 KB |
Output is correct |
7 |
Correct |
154 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
17 ms |
384 KB |
Output is correct |
10 |
Correct |
17 ms |
384 KB |
Output is correct |
11 |
Correct |
13 ms |
1024 KB |
Output is correct |
12 |
Correct |
20 ms |
1812 KB |
Output is correct |
13 |
Correct |
66 ms |
5752 KB |
Output is correct |
14 |
Correct |
125 ms |
11000 KB |
Output is correct |
15 |
Correct |
108 ms |
9208 KB |
Output is correct |
16 |
Correct |
107 ms |
8440 KB |
Output is correct |
17 |
Correct |
77 ms |
6264 KB |
Output is correct |
18 |
Correct |
77 ms |
6264 KB |
Output is correct |
19 |
Correct |
44 ms |
3064 KB |
Output is correct |
20 |
Correct |
74 ms |
6776 KB |
Output is correct |
21 |
Correct |
121 ms |
11000 KB |
Output is correct |
22 |
Correct |
100 ms |
9208 KB |
Output is correct |
23 |
Correct |
97 ms |
8584 KB |
Output is correct |
24 |
Correct |
66 ms |
6264 KB |
Output is correct |
25 |
Execution timed out |
1088 ms |
163960 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |