#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int inf = 1e15;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int h, w; cin >> h >> w;
vector <int> a(h), b(w);
for ( auto &u: a ) cin >> u;
for ( auto &u: b ) cin >> u;
auto get = [&](vector <int> a){
int n = a.size();
vector <int> prfmin(n + 1), sufmin(n + 2);
prfmin[0] = sufmin[n + 1] = inf;
for ( int i = 0; i < n; i++ ){
prfmin[i + 1] = min(prfmin[i], a[i]);
}
for ( int i = n; i > 0; i-- ){
sufmin[i] = min(sufmin[i + 1], a[i - 1]);
}
vector <int> p;
for ( int i = 0; i < n; i++ ){
if ( max(prfmin[i], sufmin[i + 2]) <= a[i] ){
continue;
} p.pb(i);
}
return p;
};
auto idxA = get(a); h = idxA.size();
auto idxB = get(b); w = idxB.size();
vector <vector<int>> dp(h, vector <int> (w, inf));
dp[0][0] = 0;
for ( int i = 0; i < h; i++ ){
for ( int j = !i; j < w; j++ ){
int opt = inf;
if ( i > 0 ) chmin(opt, b[idxB[j]] * (idxA[i] - idxA[i - 1]) + dp[i - 1][j]);
if ( j > 0 ) chmin(opt, a[idxA[i]] * (idxB[j] - idxB[j - 1]) + dp[i][j - 1]);
dp[i][j] = opt;
}
}
cout << dp[h - 1][w - 1];
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
8284 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 |
456 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 |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
456 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
4 ms |
3164 KB |
Output is correct |
5 |
Correct |
5 ms |
3156 KB |
Output is correct |
6 |
Correct |
2 ms |
1608 KB |
Output is correct |
7 |
Correct |
9 ms |
4952 KB |
Output is correct |
8 |
Correct |
11 ms |
4956 KB |
Output is correct |
9 |
Correct |
10 ms |
5104 KB |
Output is correct |
10 |
Correct |
11 ms |
5076 KB |
Output is correct |
11 |
Correct |
28 ms |
34000 KB |
Output is correct |
12 |
Correct |
9 ms |
5160 KB |
Output is correct |
13 |
Correct |
9 ms |
5164 KB |
Output is correct |
14 |
Correct |
30 ms |
33780 KB |
Output is correct |
15 |
Correct |
13 ms |
5352 KB |
Output is correct |
16 |
Correct |
0 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 |
344 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
8284 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 |
456 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 |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
456 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
600 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
2908 KB |
Output is correct |
32 |
Correct |
4 ms |
3164 KB |
Output is correct |
33 |
Correct |
5 ms |
3156 KB |
Output is correct |
34 |
Correct |
2 ms |
1608 KB |
Output is correct |
35 |
Correct |
9 ms |
4952 KB |
Output is correct |
36 |
Correct |
11 ms |
4956 KB |
Output is correct |
37 |
Correct |
10 ms |
5104 KB |
Output is correct |
38 |
Correct |
11 ms |
5076 KB |
Output is correct |
39 |
Correct |
28 ms |
34000 KB |
Output is correct |
40 |
Correct |
9 ms |
5160 KB |
Output is correct |
41 |
Correct |
9 ms |
5164 KB |
Output is correct |
42 |
Correct |
30 ms |
33780 KB |
Output is correct |
43 |
Correct |
13 ms |
5352 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
344 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 |
6 ms |
3932 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
2 ms |
1372 KB |
Output is correct |
59 |
Correct |
14 ms |
6120 KB |
Output is correct |
60 |
Correct |
13 ms |
6124 KB |
Output is correct |
61 |
Correct |
14 ms |
6236 KB |
Output is correct |
62 |
Correct |
14 ms |
6252 KB |
Output is correct |
63 |
Runtime error |
330 ms |
1048576 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |