#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen("kyoto.inp", "r")){
freopen("kyoto.inp", "r", stdin);
freopen("kyoto.out", "w", stdout);
}
#else
auto start = chrono::steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
const int N = 1e5 + 5;
#define int long long
int n, m, a[N], b[N], extra, p[N], q[N], sa[N], sb[N];
bool check1(int x, int y, int z) { return (sa[z] - sa[y]) * (y - x) >= (sa[y] - sa[x]) * (z - y); }
bool check2(int x, int y, int z) { return (sb[z] - sb[y]) * (y - x) >= (sb[y] - sb[x]) * (z - y); }
bool check3(int x, int y, int z, int w){ return (sa[y] - sa[x]) * (w - z) >= (sb[w] - sb[z]) * (y - x); }
void Hollwo_Pelw(){
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++)
cin >> b[i];
extra += a[n] * (m - 1);
extra += b[m] * (n - 1);
for (int i = 1; i <= n; i++)
a[i] -= a[n];
for (int i = 1; i <= m; i++)
b[i] -= b[m];
-- n, -- m;
// memset(dp, 0x3f, sizeof dp);
// dp[0][0] = 0;
// for (int i = 0; i <= n; i++)
// for (int j = 0; j <= m; j++) {
// if (i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + b[j + 1]);
// if (j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[i + 1]);
// }
// cout << dp[n][m] + extra;
for (int i = 1; i <= n; i++) extra -= a[i];
for (int i = 1; i <= m; i++) extra -= b[i];
for (int i = 1; i <= n; i++) a[i] -= a[i + 1];
for (int i = 1; i <= m; i++) b[i] -= b[i + 1];
for (int i = 1; i <= n; i++) sa[i] = sa[i-1] + a[i];
for (int i = 1; i <= m; i++) sb[i] = sb[i-1] + b[i];
for(int i = 0; i <= n; p[++ p[0]] = i++)
while(p[0] >= 2 && check1(p[p[0] - 1], p[p[0]], i)) -- p[0];
for(int i = 0; i <= m; q[++ q[0]] = i++)
while(q[0] >= 2 && check2(q[q[0] - 1], q[q[0]], i)) -- q[0];
int ans = extra;
for(int i = 1, j = 1, k = 1; i < p[0] || j < q[0]; )
if(j == q[0] || (i < p[0] && check3(p[i], p[i + 1], q[j], q[j + 1])))
for(int t = p[i++] + 1; t <= p[i]; ++ t, ++ k) ans += 1LL * a[t] * k;
else
for(int t = q[j++] + 1; t <= q[j]; ++ t, ++ k) ans += 1LL * b[t] * k;
cout << ans << endl;
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:15:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen("kyoto.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
kyoto.cpp:16:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("kyoto.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
244 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
8 ms |
1880 KB |
Output is correct |
5 |
Correct |
9 ms |
1728 KB |
Output is correct |
6 |
Correct |
7 ms |
916 KB |
Output is correct |
7 |
Correct |
30 ms |
4044 KB |
Output is correct |
8 |
Correct |
18 ms |
4164 KB |
Output is correct |
9 |
Correct |
18 ms |
4180 KB |
Output is correct |
10 |
Correct |
18 ms |
3912 KB |
Output is correct |
11 |
Correct |
17 ms |
3972 KB |
Output is correct |
12 |
Correct |
18 ms |
4088 KB |
Output is correct |
13 |
Correct |
18 ms |
4100 KB |
Output is correct |
14 |
Correct |
16 ms |
4152 KB |
Output is correct |
15 |
Correct |
16 ms |
4296 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
244 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
8 ms |
1880 KB |
Output is correct |
33 |
Correct |
9 ms |
1728 KB |
Output is correct |
34 |
Correct |
7 ms |
916 KB |
Output is correct |
35 |
Correct |
30 ms |
4044 KB |
Output is correct |
36 |
Correct |
18 ms |
4164 KB |
Output is correct |
37 |
Correct |
18 ms |
4180 KB |
Output is correct |
38 |
Correct |
18 ms |
3912 KB |
Output is correct |
39 |
Correct |
17 ms |
3972 KB |
Output is correct |
40 |
Correct |
18 ms |
4088 KB |
Output is correct |
41 |
Correct |
18 ms |
4100 KB |
Output is correct |
42 |
Correct |
16 ms |
4152 KB |
Output is correct |
43 |
Correct |
16 ms |
4296 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
18 ms |
2584 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
468 KB |
Output is correct |
58 |
Correct |
4 ms |
980 KB |
Output is correct |
59 |
Correct |
26 ms |
5368 KB |
Output is correct |
60 |
Correct |
25 ms |
3836 KB |
Output is correct |
61 |
Correct |
26 ms |
4076 KB |
Output is correct |
62 |
Correct |
24 ms |
3492 KB |
Output is correct |
63 |
Correct |
28 ms |
5364 KB |
Output is correct |
64 |
Correct |
37 ms |
4512 KB |
Output is correct |
65 |
Correct |
47 ms |
5336 KB |
Output is correct |
66 |
Correct |
25 ms |
5212 KB |
Output is correct |
67 |
Correct |
30 ms |
5604 KB |
Output is correct |