#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define INF (1LL << 60)
using namespace std;
int chmin(int& x, int y) {
return x = min(x, y);
}
vector<pair<int, int>> s_inc(const vector<pair<int, int>>& v){
vector<pair<int, int>> res;
for(auto i : v){
while(res.size() && i.fi <= res.back().fi) res.pop_back();
res.push_back(i);
}
return res;
}
int dp[1001][1001];
int solve(vector<pair<int, int>> a, vector<pair<int, int>> b){
a = s_inc(a), b = s_inc(b);
int n = a.size(), m = b.size();
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
dp[i][j] = INF;
}
}
dp[0][0] = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(i > 0) chmin(dp[i][j], dp[i - 1][j] + b[j].fi * (a[i].se - a[i - 1].se));
if(j > 0) chmin(dp[i][j], dp[i][j - 1] + a[i].fi * (b[j].se - b[j - 1].se));
}
}
return dp[n - 1][m - 1];
}
int32_t main(){
int n, m; cin >> n >> m;
vector<int> a, b;
for(int i = 0; i < n; i++){
int v; cin >> v; a.push_back(v);
}
for(int i = 0; i < m; i++){
int v; cin >> v; b.push_back(v);
}
int min_a = 0, min_b = 0;
for(int i = 1; i < n; i++){
if(a[i] < a[min_a]) min_a = i;
}
for(int i = 1; i < m; i++){
if(b[i] < b[min_b]) min_b = i;
}
vector<pair<int, int>> a1, a2, b1, b2;
for(int i = min_a; i >= 0; i--) a1.push_back({a[i] - a[min_a], min_a - i});
for(int i = min_a; i < n; i++) a2.push_back({a[i] - a[min_a], i - min_a});
for(int i = min_b; i >= 0; i--) b1.push_back({b[i] - b[min_b], min_b - i});
for(int i = min_b; i < m; i++) b2.push_back({b[i] - b[min_b], i - min_b});
int ans = (n - 1) * b[min_b] + (m - 1) * a[min_a] + solve(a1, b1) + solve(a2, b2);
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
448 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
4436 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
436 KB |
Output is correct |
16 |
Correct |
5 ms |
6212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
312 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
2132 KB |
Output is correct |
4 |
Correct |
25 ms |
3808 KB |
Output is correct |
5 |
Correct |
19 ms |
4548 KB |
Output is correct |
6 |
Correct |
8 ms |
1804 KB |
Output is correct |
7 |
Correct |
48 ms |
8988 KB |
Output is correct |
8 |
Correct |
51 ms |
8972 KB |
Output is correct |
9 |
Correct |
47 ms |
9208 KB |
Output is correct |
10 |
Correct |
52 ms |
9144 KB |
Output is correct |
11 |
Correct |
54 ms |
15412 KB |
Output is correct |
12 |
Correct |
52 ms |
9336 KB |
Output is correct |
13 |
Correct |
51 ms |
9844 KB |
Output is correct |
14 |
Correct |
59 ms |
16592 KB |
Output is correct |
15 |
Correct |
53 ms |
9240 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
312 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
308 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
448 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
4436 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
436 KB |
Output is correct |
16 |
Correct |
5 ms |
6212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
312 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
308 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
2132 KB |
Output is correct |
32 |
Correct |
25 ms |
3808 KB |
Output is correct |
33 |
Correct |
19 ms |
4548 KB |
Output is correct |
34 |
Correct |
8 ms |
1804 KB |
Output is correct |
35 |
Correct |
48 ms |
8988 KB |
Output is correct |
36 |
Correct |
51 ms |
8972 KB |
Output is correct |
37 |
Correct |
47 ms |
9208 KB |
Output is correct |
38 |
Correct |
52 ms |
9144 KB |
Output is correct |
39 |
Correct |
54 ms |
15412 KB |
Output is correct |
40 |
Correct |
52 ms |
9336 KB |
Output is correct |
41 |
Correct |
51 ms |
9844 KB |
Output is correct |
42 |
Correct |
59 ms |
16592 KB |
Output is correct |
43 |
Correct |
53 ms |
9240 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
312 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
308 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
36 ms |
5104 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
4 ms |
960 KB |
Output is correct |
58 |
Correct |
12 ms |
1964 KB |
Output is correct |
59 |
Correct |
82 ms |
9944 KB |
Output is correct |
60 |
Correct |
81 ms |
10304 KB |
Output is correct |
61 |
Correct |
82 ms |
10528 KB |
Output is correct |
62 |
Correct |
83 ms |
10608 KB |
Output is correct |
63 |
Runtime error |
104 ms |
31800 KB |
Execution killed with signal 11 |
64 |
Halted |
0 ms |
0 KB |
- |