#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false
const int MAXH = 100 * 1000 + 17;
int a[MAXH];
int kola[MAXH];
int popa[MAXH];
int b[MAXH];
int kolb[MAXH];
int popb[MAXH];
set<pair<ld, int>> s;
int main () {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int h, w;
cin >> h >> w;
for (int i = 1; i <= h; i ++) {
cin >> a[i];
}
for (int i = 1; i <= w; i ++) {
cin >> b[i];
}
for (int i = 0; i < h; i ++) {
kola[i] = i + 1;
popa[i + 1] = i;
}
for (int i = 0; i < w; i ++) {
kolb[i] = i + 1;
popb[i + 1] = i;
}
for (int i = 1; i < h; i ++) {
s.insert({a[i + 1] - a[i], i});
}
for (int i = 1; i < w; i ++) {
s.insert({b[i + 1] - b[i], i + h});
}
ll wyn = 0;
while (!s.empty()) {
pair<ld, int> el = *s.begin();
s.erase(el);
int i = el.nd;
if (i < h) {
int p = popa[i];
int k = kola[i];
kola[p] = k;
popa[k] = p;
if (p == 0) {
wyn += ll(b[kolb[0]]) * ll(k - i);
} else {
s.erase({ld(a[i] - a[p])/ld(i - p), p});
s.insert({ld(a[k] - a[p])/ld(k - p), p});
}
} else {
i -= h;
int p = popb[i];
int k = kolb[i];
kolb[p] = k;
popb[k] = p;
if (p == 0) {
wyn += ll(a[kola[0]]) * ll(k - i);
} else {
s.erase({ld(b[i] - b[p])/ld(i - p), p + h});
s.insert({ld(b[k] - b[p])/ld(k - p), p + h});
}
}
}
cout << wyn << "\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... |