#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define st first
#define nd second
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) a.begin(), a.end()
const ll INF = 1e18;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
ll A[n];
ll B[m];
rep(i, n) cin >> A[i];
rep(i, m) cin >> B[i];
vector<pll> xda;
vector<pll> xdb;
rep(i, n - 1) {
pll pom = {A[i + 1] - A[i], 1ll};
while (!xda.empty() && (xda.back().st * pom.nd) >= (pom.st * xda.back().nd)) {
pom.st += xda.back().st;
pom.nd += xda.back().nd;
xda.pop_back();
}
xda.pb(pom);
}
rep(i, m - 1) {
pll pom = {B[i + 1] - B[i], 1ll};
while (!xdb.empty() && (xdb.back().st * pom.nd) >= (pom.st * xdb.back().nd)) {
pom.st += xdb.back().st;
pom.nd += xdb.back().nd;
xdb.pop_back();
}
xdb.pb(pom);
}
int sza = xda.size();
int szb = xdb.size();
int ita = 0;
int itb = 0;
ll x = 0;
ll y = 0;
ll ans = 0ll;
while (ita < sza || itb < szb) {
if (ita == sza) {
ans += (A[x] * xdb[itb].nd);
y += xdb[itb].nd;
itb++;
}
else if (itb == szb) {
ans += (B[y] * xda[ita].nd);
x += xda[ita].nd;
ita++;
}
else {
if ((xda[ita].st * xdb[itb].nd) > (xdb[itb].st * xda[ita].nd)) {
ans += (A[x] * xdb[itb].nd);
y += xdb[itb].nd;
itb++;
}
else {
ans += (B[y] * xda[ita].nd);
x += xda[ita].nd;
ita++;
}
}
}
cout << ans << '\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... |