#include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) FOR(i,1,n+1)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 2e3+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int xpos[maxn], ypos[maxn];
int xv[maxn], yv[maxn];
ll dp[maxn][maxn];
vector<pii> clean(vector<int> raw) {
int n = SZ(raw);
vector<int> prefmin(n+2, inf), sufmin(n+2, inf);
REP(i, n) {
prefmin[i+1] = min(prefmin[i], raw[i]);
}
for (int i = n-2; i>=0; --i) {
sufmin[i] = min(sufmin[i+1], raw[i+1]);
}
vector<pii> re;
REP(i,n) {
if (raw[i] >= prefmin[i] && raw[i] >= sufmin[i]) {
// useless
continue;
}
re.pb({raw[i], i});
}
return re;
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int H,W; cin>>H>>W;
int n,m;
{
vector<int> tmp(H);
REP(i,H) cin>>tmp[i];
vector<pii> gx = clean(tmp);
n = SZ(gx);
REP(i, n) {
xpos[i] = gx[i].s, xv[i] = gx[i].f;
}
}
{
vector<int> tmp(W);
REP(i,W) cin>>tmp[i];
vector<pii> gy = clean(tmp);
m = SZ(gy);
REP(i, m) {
ypos[i] = gy[i].s, yv[i] = gy[i].f;
}
}
bug(n,m);
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
REP(i,n) {
REP(j,m) {
if (i) MN(dp[i][j], dp[i-1][j] + yv[j] * (xpos[i] - xpos[i-1]));
if (j) MN(dp[i][j], dp[i][j-1] + xv[i] * (ypos[j] - ypos[j-1]));
}
}
cout<<dp[n-1][m-1]<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
31792 KB |
Output is correct |
2 |
Correct |
14 ms |
31772 KB |
Output is correct |
3 |
Correct |
14 ms |
31760 KB |
Output is correct |
4 |
Correct |
14 ms |
31824 KB |
Output is correct |
5 |
Correct |
13 ms |
31828 KB |
Output is correct |
6 |
Correct |
13 ms |
31704 KB |
Output is correct |
7 |
Correct |
14 ms |
31784 KB |
Output is correct |
8 |
Correct |
17 ms |
31704 KB |
Output is correct |
9 |
Correct |
15 ms |
31828 KB |
Output is correct |
10 |
Correct |
15 ms |
31800 KB |
Output is correct |
11 |
Correct |
17 ms |
31828 KB |
Output is correct |
12 |
Correct |
18 ms |
31828 KB |
Output is correct |
13 |
Correct |
18 ms |
31808 KB |
Output is correct |
14 |
Correct |
14 ms |
31776 KB |
Output is correct |
15 |
Correct |
14 ms |
31828 KB |
Output is correct |
16 |
Correct |
18 ms |
31828 KB |
Output is correct |
17 |
Correct |
13 ms |
31824 KB |
Output is correct |
18 |
Correct |
15 ms |
31700 KB |
Output is correct |
19 |
Correct |
16 ms |
31688 KB |
Output is correct |
20 |
Correct |
13 ms |
31680 KB |
Output is correct |
21 |
Correct |
14 ms |
31716 KB |
Output is correct |
22 |
Correct |
13 ms |
31700 KB |
Output is correct |
23 |
Correct |
13 ms |
31700 KB |
Output is correct |
24 |
Correct |
14 ms |
31700 KB |
Output is correct |
25 |
Correct |
17 ms |
31784 KB |
Output is correct |
26 |
Correct |
14 ms |
31700 KB |
Output is correct |
27 |
Correct |
14 ms |
31724 KB |
Output is correct |
28 |
Correct |
14 ms |
31700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
31700 KB |
Output is correct |
2 |
Correct |
14 ms |
31708 KB |
Output is correct |
3 |
Correct |
15 ms |
31832 KB |
Output is correct |
4 |
Correct |
19 ms |
32380 KB |
Output is correct |
5 |
Correct |
22 ms |
32180 KB |
Output is correct |
6 |
Correct |
21 ms |
31940 KB |
Output is correct |
7 |
Correct |
39 ms |
32588 KB |
Output is correct |
8 |
Correct |
31 ms |
32600 KB |
Output is correct |
9 |
Correct |
29 ms |
33476 KB |
Output is correct |
10 |
Correct |
31 ms |
33476 KB |
Output is correct |
11 |
Correct |
43 ms |
33408 KB |
Output is correct |
12 |
Correct |
38 ms |
33480 KB |
Output is correct |
13 |
Correct |
30 ms |
33488 KB |
Output is correct |
14 |
Correct |
45 ms |
33448 KB |
Output is correct |
15 |
Correct |
30 ms |
32932 KB |
Output is correct |
16 |
Correct |
14 ms |
31692 KB |
Output is correct |
17 |
Correct |
14 ms |
31780 KB |
Output is correct |
18 |
Correct |
14 ms |
31772 KB |
Output is correct |
19 |
Correct |
14 ms |
31720 KB |
Output is correct |
20 |
Correct |
13 ms |
31768 KB |
Output is correct |
21 |
Correct |
15 ms |
31700 KB |
Output is correct |
22 |
Correct |
14 ms |
31700 KB |
Output is correct |
23 |
Correct |
17 ms |
31700 KB |
Output is correct |
24 |
Correct |
16 ms |
31700 KB |
Output is correct |
25 |
Correct |
14 ms |
31700 KB |
Output is correct |
26 |
Correct |
14 ms |
31700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
31792 KB |
Output is correct |
2 |
Correct |
14 ms |
31772 KB |
Output is correct |
3 |
Correct |
14 ms |
31760 KB |
Output is correct |
4 |
Correct |
14 ms |
31824 KB |
Output is correct |
5 |
Correct |
13 ms |
31828 KB |
Output is correct |
6 |
Correct |
13 ms |
31704 KB |
Output is correct |
7 |
Correct |
14 ms |
31784 KB |
Output is correct |
8 |
Correct |
17 ms |
31704 KB |
Output is correct |
9 |
Correct |
15 ms |
31828 KB |
Output is correct |
10 |
Correct |
15 ms |
31800 KB |
Output is correct |
11 |
Correct |
17 ms |
31828 KB |
Output is correct |
12 |
Correct |
18 ms |
31828 KB |
Output is correct |
13 |
Correct |
18 ms |
31808 KB |
Output is correct |
14 |
Correct |
14 ms |
31776 KB |
Output is correct |
15 |
Correct |
14 ms |
31828 KB |
Output is correct |
16 |
Correct |
18 ms |
31828 KB |
Output is correct |
17 |
Correct |
13 ms |
31824 KB |
Output is correct |
18 |
Correct |
15 ms |
31700 KB |
Output is correct |
19 |
Correct |
16 ms |
31688 KB |
Output is correct |
20 |
Correct |
13 ms |
31680 KB |
Output is correct |
21 |
Correct |
14 ms |
31716 KB |
Output is correct |
22 |
Correct |
13 ms |
31700 KB |
Output is correct |
23 |
Correct |
13 ms |
31700 KB |
Output is correct |
24 |
Correct |
14 ms |
31700 KB |
Output is correct |
25 |
Correct |
17 ms |
31784 KB |
Output is correct |
26 |
Correct |
14 ms |
31700 KB |
Output is correct |
27 |
Correct |
14 ms |
31724 KB |
Output is correct |
28 |
Correct |
14 ms |
31700 KB |
Output is correct |
29 |
Correct |
13 ms |
31700 KB |
Output is correct |
30 |
Correct |
14 ms |
31708 KB |
Output is correct |
31 |
Correct |
15 ms |
31832 KB |
Output is correct |
32 |
Correct |
19 ms |
32380 KB |
Output is correct |
33 |
Correct |
22 ms |
32180 KB |
Output is correct |
34 |
Correct |
21 ms |
31940 KB |
Output is correct |
35 |
Correct |
39 ms |
32588 KB |
Output is correct |
36 |
Correct |
31 ms |
32600 KB |
Output is correct |
37 |
Correct |
29 ms |
33476 KB |
Output is correct |
38 |
Correct |
31 ms |
33476 KB |
Output is correct |
39 |
Correct |
43 ms |
33408 KB |
Output is correct |
40 |
Correct |
38 ms |
33480 KB |
Output is correct |
41 |
Correct |
30 ms |
33488 KB |
Output is correct |
42 |
Correct |
45 ms |
33448 KB |
Output is correct |
43 |
Correct |
30 ms |
32932 KB |
Output is correct |
44 |
Correct |
14 ms |
31692 KB |
Output is correct |
45 |
Correct |
14 ms |
31780 KB |
Output is correct |
46 |
Correct |
14 ms |
31772 KB |
Output is correct |
47 |
Correct |
14 ms |
31720 KB |
Output is correct |
48 |
Correct |
13 ms |
31768 KB |
Output is correct |
49 |
Correct |
15 ms |
31700 KB |
Output is correct |
50 |
Correct |
14 ms |
31700 KB |
Output is correct |
51 |
Correct |
17 ms |
31700 KB |
Output is correct |
52 |
Correct |
16 ms |
31700 KB |
Output is correct |
53 |
Correct |
14 ms |
31700 KB |
Output is correct |
54 |
Correct |
14 ms |
31700 KB |
Output is correct |
55 |
Correct |
23 ms |
32696 KB |
Output is correct |
56 |
Correct |
15 ms |
31828 KB |
Output is correct |
57 |
Correct |
15 ms |
32084 KB |
Output is correct |
58 |
Correct |
21 ms |
32220 KB |
Output is correct |
59 |
Correct |
39 ms |
33752 KB |
Output is correct |
60 |
Correct |
41 ms |
33724 KB |
Output is correct |
61 |
Correct |
42 ms |
34720 KB |
Output is correct |
62 |
Correct |
37 ms |
34628 KB |
Output is correct |
63 |
Runtime error |
17 ms |
6728 KB |
Execution killed with signal 11 |
64 |
Halted |
0 ms |
0 KB |
- |