#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const int N = 1e5+5;
ll a[N], b[N];
vector<int> stka, stkb;
inline bool chk(ll a[], int x, int y, int z)
{ return !((z-y)*(a[y]-a[x]) < (y-x)*(a[z]-a[y])); }
inline bool chk2(int x, int y, int l, int r)
{ return (r-l)*(a[y]-a[x]) < (y-x)*(b[r]-b[l]); }
int main(void)
{ IO
ll n, i, m, j, ans=0;
cin >> n >> m;
for (i=1; i <= n; ++i) cin >> a[i];
for (i=1; i <= m; ++i) cin >> b[i];
for (i=1; i <= n; ++i)
{
while (SZ(stka) >= 2 && chk(a, stka[SZ(stka)-2], stka[SZ(stka)-1], i))
stka.pop_back();
stka.pb(i);
}
for (i=1; i <= m; ++i)
{
while (SZ(stkb) >= 2 && chk(b, stkb[SZ(stkb)-2], stkb[SZ(stkb)-1], i))
stkb.pop_back();
stkb.pb(i);
}
for (i=0, j=0; i < SZ(stka)-1 || j < SZ(stkb)-1; )
{
if (i < SZ(stka)-1 && j < SZ(stkb)-1)
{
if (chk2(stka[i], stka[i+1], stkb[j], stkb[j+1]))
{
ans += (stka[i+1]-stka[i]) * b[stkb[j]];
++i;
} else {
ans += (stkb[j+1]-stkb[j]) * a[stka[i]];
++j;
}
} else if (i < SZ(stka)-1) {
ans += (stka[i+1]-stka[i]) * b[stkb[j]];
++i;
} else { // if j < SZ(stkb)-1
ans += (stkb[j+1]-stkb[j]) * a[stka[i]];
++j;
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
464 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
856 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
352 KB |
Output is correct |
4 |
Correct |
5 ms |
1372 KB |
Output is correct |
5 |
Correct |
4 ms |
1260 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
17 ms |
2628 KB |
Output is correct |
8 |
Correct |
12 ms |
2648 KB |
Output is correct |
9 |
Correct |
12 ms |
2656 KB |
Output is correct |
10 |
Correct |
12 ms |
2656 KB |
Output is correct |
11 |
Correct |
10 ms |
2656 KB |
Output is correct |
12 |
Correct |
14 ms |
2656 KB |
Output is correct |
13 |
Correct |
14 ms |
2652 KB |
Output is correct |
14 |
Correct |
11 ms |
2708 KB |
Output is correct |
15 |
Correct |
11 ms |
2912 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
0 ms |
360 KB |
Output is correct |
18 |
Correct |
0 ms |
356 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
464 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
856 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
352 KB |
Output is correct |
32 |
Correct |
5 ms |
1372 KB |
Output is correct |
33 |
Correct |
4 ms |
1260 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
17 ms |
2628 KB |
Output is correct |
36 |
Correct |
12 ms |
2648 KB |
Output is correct |
37 |
Correct |
12 ms |
2656 KB |
Output is correct |
38 |
Correct |
12 ms |
2656 KB |
Output is correct |
39 |
Correct |
10 ms |
2656 KB |
Output is correct |
40 |
Correct |
14 ms |
2656 KB |
Output is correct |
41 |
Correct |
14 ms |
2652 KB |
Output is correct |
42 |
Correct |
11 ms |
2708 KB |
Output is correct |
43 |
Correct |
11 ms |
2912 KB |
Output is correct |
44 |
Correct |
0 ms |
360 KB |
Output is correct |
45 |
Correct |
0 ms |
360 KB |
Output is correct |
46 |
Correct |
0 ms |
356 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
9 ms |
1884 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
480 KB |
Output is correct |
58 |
Correct |
3 ms |
856 KB |
Output is correct |
59 |
Correct |
18 ms |
3932 KB |
Output is correct |
60 |
Correct |
18 ms |
3928 KB |
Output is correct |
61 |
Correct |
18 ms |
3932 KB |
Output is correct |
62 |
Correct |
18 ms |
3836 KB |
Output is correct |
63 |
Correct |
16 ms |
4312 KB |
Output is correct |
64 |
Correct |
18 ms |
3932 KB |
Output is correct |
65 |
Correct |
18 ms |
3932 KB |
Output is correct |
66 |
Correct |
16 ms |
3932 KB |
Output is correct |
67 |
Correct |
17 ms |
3932 KB |
Output is correct |