#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 300005;
const int INF = 1000000009;
int n;
int x[N], y[N];
int cx[N], cy[N];
int stg(int p)
{
for (int i = 1; i <= n; ++i)
{
cx[i] = x[i];
cy[i] = y[i];
}
int ans = -p;
int s = p;
for (int i = n; i >= 1; --i)
{
if (x[i] >= s)
{
x[i] -= s;
s = 0;
}
else
{
s -= x[i];
x[i] = 0;
}
}
assert(s == 0);
s = p;
for (int i = 1; i <= n; ++i)
{
if (y[i] >= s)
{
y[i] -= s;
s = 0;
}
else
{
s -= y[i];
y[i] = 0;
}
}
assert(s == 0);
int i = 1;
int j = 1;
while (i <= n && j <= n)
{
if (x[i] == 0)
{
++i;
continue;
}
if (y[j] == 0)
{
++j;
continue;
}
if (i > j)
{
for (int i = 1; i <= n; ++i)
{
x[i] = cx[i];
y[i] = cy[i];
}
return -INF;
}
int minu = min(x[i], y[j]);
if (x[i] >= y[j])
{
x[i] -= y[j];
y[j] = 0;
}
else
{
y[j] -= x[i];
x[i] = 0;
}
if (i < j)
ans += minu;
}
for (int i = 1; i <= n; ++i)
{
x[i] = cx[i];
y[i] = cy[i];
}
return ans;
}
void solv()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> x[i];
for (int i = 1; i <= n; ++i)
cin >> y[i];
int s = 0;
for (int i = 1; i <= n; ++i)
s += x[i];
for (int i = 1; i <= n; ++i)
s -= y[i];
assert(s >= 0);
for (int i = n; i >= 1; --i)
{
if (x[i] >= s)
{
x[i] -= s;
s = 0;
}
else
{
s -= x[i];
x[i] = 0;
}
}
assert(s == 0);
for (int i = 1; i <= n; ++i)
s += x[i];
int ans = -INF;
for (int p = 0; p <= s; ++p)
{
ans = max(ans, stg(p));
}
cout << ans << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4580 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
4444 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
0 ms |
4572 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
0 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
6 ms |
4444 KB |
Output is correct |
5 |
Correct |
34 ms |
6160 KB |
Output is correct |
6 |
Execution timed out |
1070 ms |
6032 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
17 ms |
6236 KB |
Output is correct |
3 |
Execution timed out |
1065 ms |
5980 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4580 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
4444 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
0 ms |
4572 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
0 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4572 KB |
Output is correct |
24 |
Correct |
0 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
0 ms |
4444 KB |
Output is correct |
27 |
Correct |
0 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
0 ms |
4572 KB |
Output is correct |
30 |
Correct |
0 ms |
4444 KB |
Output is correct |
31 |
Correct |
0 ms |
4444 KB |
Output is correct |
32 |
Correct |
0 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
0 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
0 ms |
4576 KB |
Output is correct |
38 |
Correct |
1 ms |
4440 KB |
Output is correct |
39 |
Correct |
0 ms |
4444 KB |
Output is correct |
40 |
Correct |
0 ms |
4444 KB |
Output is correct |
41 |
Correct |
0 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
4 ms |
4580 KB |
Output is correct |
44 |
Correct |
0 ms |
4444 KB |
Output is correct |
45 |
Correct |
1 ms |
4444 KB |
Output is correct |
46 |
Correct |
0 ms |
4444 KB |
Output is correct |
47 |
Correct |
0 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
4440 KB |
Output is correct |
49 |
Correct |
0 ms |
4444 KB |
Output is correct |
50 |
Correct |
3 ms |
4444 KB |
Output is correct |
51 |
Correct |
3 ms |
4444 KB |
Output is correct |
52 |
Correct |
3 ms |
4444 KB |
Output is correct |
53 |
Correct |
5 ms |
4444 KB |
Output is correct |
54 |
Correct |
3 ms |
4444 KB |
Output is correct |
55 |
Correct |
3 ms |
4444 KB |
Output is correct |
56 |
Correct |
3 ms |
4580 KB |
Output is correct |
57 |
Correct |
2 ms |
4440 KB |
Output is correct |
58 |
Correct |
3 ms |
4444 KB |
Output is correct |
59 |
Correct |
3 ms |
4444 KB |
Output is correct |
60 |
Correct |
5 ms |
4440 KB |
Output is correct |
61 |
Correct |
5 ms |
4444 KB |
Output is correct |
62 |
Correct |
4 ms |
4444 KB |
Output is correct |
63 |
Correct |
8 ms |
4444 KB |
Output is correct |
64 |
Correct |
5 ms |
4444 KB |
Output is correct |
65 |
Correct |
4 ms |
4444 KB |
Output is correct |
66 |
Correct |
9 ms |
4576 KB |
Output is correct |
67 |
Correct |
0 ms |
4444 KB |
Output is correct |
68 |
Correct |
0 ms |
4444 KB |
Output is correct |
69 |
Correct |
1 ms |
4444 KB |
Output is correct |
70 |
Correct |
1 ms |
4444 KB |
Output is correct |
71 |
Correct |
1 ms |
4444 KB |
Output is correct |
72 |
Correct |
0 ms |
4444 KB |
Output is correct |
73 |
Correct |
1 ms |
4444 KB |
Output is correct |
74 |
Correct |
1 ms |
4540 KB |
Output is correct |
75 |
Correct |
0 ms |
4444 KB |
Output is correct |
76 |
Correct |
1 ms |
4444 KB |
Output is correct |
77 |
Correct |
1 ms |
4444 KB |
Output is correct |
78 |
Correct |
0 ms |
4444 KB |
Output is correct |
79 |
Correct |
1 ms |
4444 KB |
Output is correct |
80 |
Correct |
1 ms |
4444 KB |
Output is correct |
81 |
Correct |
1 ms |
4444 KB |
Output is correct |
82 |
Correct |
1 ms |
4444 KB |
Output is correct |
83 |
Correct |
1 ms |
4700 KB |
Output is correct |
84 |
Correct |
1 ms |
4444 KB |
Output is correct |
85 |
Correct |
0 ms |
4572 KB |
Output is correct |
86 |
Correct |
0 ms |
4568 KB |
Output is correct |
87 |
Correct |
1 ms |
4444 KB |
Output is correct |
88 |
Correct |
0 ms |
4444 KB |
Output is correct |
89 |
Correct |
0 ms |
4444 KB |
Output is correct |
90 |
Correct |
1 ms |
4444 KB |
Output is correct |
91 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4580 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
4444 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
0 ms |
4572 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
0 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
0 ms |
4444 KB |
Output is correct |
25 |
Correct |
6 ms |
4444 KB |
Output is correct |
26 |
Correct |
34 ms |
6160 KB |
Output is correct |
27 |
Execution timed out |
1070 ms |
6032 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |