1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include<bits/stdc++.h> using namespace std; typedef double db; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; #define fi first #define se second #define pb push_back #define sz(x) ((int)(x).size()) #define all(x) begin(x),end(x) #define rep(i,l,r) for(int i=(l);i<(r);++i) #define per(i,l,r) for(int i=(r)-1;i>=(l);--i) #define dd(x) cout << #x << "=" << x << ", " #define de(x) cout << #x << "=" << x << endl
const int N = 1e5 + 7; int n, m, A[N], B[N]; struct Node { int num; ll sum; Node() {} Node(int _num, ll _sum) { num = _num, sum = _sum; } Node operator+(const Node &p) const { return Node(num + p.num, sum + p.sum); } bool operator<(const Node &p) const { return sum * p.num < p.sum * num; } } a[N], b[N]; int gao(int n, int *r, Node *a) { int top = 0; rep(i, 0, n) { scanf("%d", r + i); Node v(1, r[i]); while (top > 0 && a[top - 1] < v) v = a[--top] + v; a[top++] = v; } return top; } int main() { scanf("%d%d", &n, &m); int la = gao(n, A, a), lb = gao(m, B, b); n = m = 0; ll ans = 0; for (int i = 0, j = 0; i < la || j < lb; ) { while (i < la && (j == lb || !(a[i] < b[j]))) { rep(k, n, n + a[i].num) ans += 1ll * (k + m + 1) * A[k]; n += a[i++].num; } while (j < lb && (i == la || !(b[j] < a[i]))) { rep(k, m, m + b[j].num) ans += 1ll * (k + n + 1) * B[k]; m += b[j++].num; } } printf("%lld\n", ans); return 0; }
|