#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()
{
n = rnd() % 10 + 1;
for (int i = 1; i <= n; ++i)
x[i] = rnd() % 20;
for (int i = 1; i <= n; ++i)
y[i] = rnd() % 20;
int ss = 0;
for (int i = 1; i <= n; ++i)
ss += x[i];
for (int i = 1; i <= n; ++i)
ss -= y[i];
if (ss < 0)
{
for (int i = 1; i <= n; ++i)
swap(x[i], y[i]);
}
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 = -s;
/*for (int p = 0; p <= s; ++p)
{
ans = max(ans, stg(p));
cout << stg(p) << ' ';
}
cout << "\n";*/
int l = 0, r = s - 1;
while (l <= r)
{
int m = (l + r) / 2;
int yans = stg(m);
ans = max(ans, yans);
int l2 = m + 1, r2 = s;
int u = m;
while (l2 <= r2)
{
int m2 = (l2 + r2) / 2;
if (yans == stg(m2))
{
u = m2;
l = m2 + 1;
}
else
r = m2 - 1;
}
if (yans < stg(u + 1))
l = m + 1;
else
r = m - 1;
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Execution timed out |
1062 ms |
4444 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Execution timed out |
1093 ms |
4444 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
15 ms |
2936 KB |
Output is correct |
3 |
Execution timed out |
1092 ms |
5088 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Execution timed out |
1062 ms |
4444 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Execution timed out |
1062 ms |
4444 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |