voidgeta(){ for (int i = 1; i <= n; ++i) { pii p = {i, a[i]}; for (; topa >= 2 && mul(sub(p, stka[topa]), sub(stka[topa], stka[topa - 1])) >= 0; --topa) {} stka[++topa] = p; } }
voidgetb(){ for (int i = 1; i <= m; ++i) { pii p = {i, b[i]}; for (; topb >= 2 && mul(sub(p, stkb[topb]), sub(stkb[topb], stkb[topb - 1])) >= 0; --topb) {} stkb[++topb] = p; } }
voiddickdreamer(){ std::cin >> n >> m; for (int i = 1; i <= n; ++i) std::cin >> a[i]; for (int i = 1; i <= m; ++i) std::cin >> b[i]; geta(), getb(); int x = 1, y = 1; i64 ans = 0; for (int i = 1, j = 1; x < n || y < m;) { bool nxt = 0; if (x == n || y < m && mul(sub(stkb[j + 1], stkb[j]), sub(stka[i + 1], stka[i])) >= 0) nxt = 1; if (!nxt) { ans += 1ll * b[y] * (stka[i + 1].first - stka[i].first); x += stka[i + 1].first - stka[i].first; ++i; } else { ans += 1ll * a[x] * (stkb[j + 1].first - stkb[j].first); y += stkb[j + 1].first - stkb[j].first; ++j; } } std::cout << ans << '\n'; }