#include <bits/stdc++.h>
#define IO_OP ios::sync_with_stdio(0), cin.tie(0)
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7;
bool check(int a, int b, int c, int d) { // a / b >= c / d ?
assert(b > 0 && d > 0);
return 1. * a / b >= 1. * c / d;
// return 1LL * a * d >= 1LL * b * c;
}
vi go(vi a) {
vi h;
int n = SZ(a);
for(int i = 0; i < n; i++) {
if(SZ(h) >= 2 && check(a[h[SZ(h) - 1]] - a[h[SZ(h) - 2]], h[SZ(h) - 1] - h[SZ(h) - 2], a[i] - a[h[SZ(h) - 1]], i - h[SZ(h) - 1]))
h.pop_back();
h.PB(i);
}
return h;
}
signed main()
{
IO_OP;
int n, m;
cin >> n >> m;
assert(n >= 2 && m >= 2);
vi a(n), b(m);
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < m; i++)
cin >> b[i];
vi ha = go(a), hb = go(b);
ll ans = 0;
while(SZ(ha) >= 2 || SZ(hb) >= 2) {
if(SZ(hb) == 1 || (SZ(ha) >= 2 && check(a[ha[SZ(ha) - 1]] - a[ha[SZ(ha) - 2]], ha[SZ(ha) - 1] - ha[SZ(ha) - 2], b[hb[SZ(hb) - 1]] - b[hb[SZ(hb) - 2]], hb[SZ(hb) - 1] - hb[SZ(hb) - 2]))) {
ans += 1LL * (ha[SZ(ha) - 1] - ha[SZ(ha) - 2]) * b[hb.back()];
ha.pop_back();
} else {
ans += 1LL * (hb[SZ(hb) - 1] - hb[SZ(hb) - 2]) * a[ha.back()];
hb.pop_back();
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |