This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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, int& q)
{
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;
else
++q;
}
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;
int u;
while (l <= r)
{
int m = (l + r) / 2;
int q = 0;
if (stg(m, q) > -INF)
{
u = m;
r = m - 1;
}
else
l = m + 1;
}
l = u, r = s;
while (l <= r)
{
int m = (l + r) / 2;
int q = 0;
ans = max(ans, stg(m, q));
if (q > 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;
}
Compilation message (stderr)
Main.cpp: In function 'void solv()':
Main.cpp:185:20: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
185 | int m = (l + r) / 2;
| ~~~^~~~
# | 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... |