#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e15;
void strat1(int h,int w){
vector<int> a(h+1);
vector<int> b(w+1);
for(int i=1;i<=h;i++)cin>>a[i];
for(int i=1;i<=w;i++)cin>>b[i];
vector<vector<int>> DP(h+1,vector<int>(w+1,INF));
DP[1][1]=0;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(j!=1)DP[i][j]=min(DP[i][j],DP[i][j-1]+a[i]);
if(i!=1)DP[i][j]=min(DP[i][j],DP[i-1][j]+b[j]);
}
}
cout << DP[h][w] << '\n';
exit(0);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int h,w;
cin >> h >> w;
if(h<=1000 and w<=1000)strat1(h,w);
vector<int> a(h+1);
vector<int> b(w+1);
for(int i=1;i<=h;i++)cin>>a[i];
for(int i=1;i<=w;i++)cin>>b[i];
vector<int> acti={0},actj={0};
{
vector<int> maxidx(1001),minidx(1001);
for(int i=1;i<=h;i++)maxidx[a[i]]=i;
for(int i=h;i;i--)minidx[a[i]]=i;
for(int i=1;i<=h;i++)if(i==minidx[a[i]] or i==maxidx[a[i]])acti.emplace_back(i);
}
{
vector<int> maxidx(1001),minidx(1001);
for(int i=1;i<=w;i++)maxidx[b[i]]=i;
for(int i=w;i;i--)minidx[b[i]]=i;
for(int i=1;i<=w;i++)if(i==minidx[b[i]] or i==maxidx[b[i]])actj.emplace_back(i);
}
int sh = acti.size()-1;
int sw = actj.size()-1;
vector<vector<int>> DP(sh+1,vector<int>(sw+1,INF));
DP[1][1]=0;
for(int i=1;i<=sh;i++){
for(int j=1;j<=sw;j++){
if(j!=1)DP[i][j]=min(DP[i][j],DP[i][j-1]+(a[acti[i]]*(actj[j]-actj[j-1])));
if(i!=1)DP[i][j]=min(DP[i][j],DP[i-1][j]+(b[actj[j]]*(acti[i]-acti[i-1])));
}
}
cout << DP[sh][sw] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
8172 KB |
Output is correct |
10 |
Correct |
6 ms |
8180 KB |
Output is correct |
11 |
Correct |
8 ms |
8284 KB |
Output is correct |
12 |
Correct |
5 ms |
8284 KB |
Output is correct |
13 |
Correct |
5 ms |
8284 KB |
Output is correct |
14 |
Correct |
6 ms |
8280 KB |
Output is correct |
15 |
Correct |
5 ms |
8152 KB |
Output is correct |
16 |
Correct |
5 ms |
8284 KB |
Output is correct |
17 |
Correct |
5 ms |
8284 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 |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
2984 KB |
Output is correct |
4 |
Correct |
41 ms |
31472 KB |
Output is correct |
5 |
Correct |
5 ms |
2852 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
43 ms |
33360 KB |
Output is correct |
8 |
Correct |
32 ms |
33496 KB |
Output is correct |
9 |
Correct |
35 ms |
33372 KB |
Output is correct |
10 |
Correct |
34 ms |
33360 KB |
Output is correct |
11 |
Correct |
50 ms |
33260 KB |
Output is correct |
12 |
Correct |
46 ms |
33012 KB |
Output is correct |
13 |
Correct |
36 ms |
33020 KB |
Output is correct |
14 |
Correct |
35 ms |
33372 KB |
Output is correct |
15 |
Correct |
17 ms |
1884 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
356 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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
8172 KB |
Output is correct |
10 |
Correct |
6 ms |
8180 KB |
Output is correct |
11 |
Correct |
8 ms |
8284 KB |
Output is correct |
12 |
Correct |
5 ms |
8284 KB |
Output is correct |
13 |
Correct |
5 ms |
8284 KB |
Output is correct |
14 |
Correct |
6 ms |
8280 KB |
Output is correct |
15 |
Correct |
5 ms |
8152 KB |
Output is correct |
16 |
Correct |
5 ms |
8284 KB |
Output is correct |
17 |
Correct |
5 ms |
8284 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 |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
2984 KB |
Output is correct |
32 |
Correct |
41 ms |
31472 KB |
Output is correct |
33 |
Correct |
5 ms |
2852 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
43 ms |
33360 KB |
Output is correct |
36 |
Correct |
32 ms |
33496 KB |
Output is correct |
37 |
Correct |
35 ms |
33372 KB |
Output is correct |
38 |
Correct |
34 ms |
33360 KB |
Output is correct |
39 |
Correct |
50 ms |
33260 KB |
Output is correct |
40 |
Correct |
46 ms |
33012 KB |
Output is correct |
41 |
Correct |
36 ms |
33020 KB |
Output is correct |
42 |
Correct |
35 ms |
33372 KB |
Output is correct |
43 |
Correct |
17 ms |
1884 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
352 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
356 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 |
Runtime error |
14 ms |
2912 KB |
Execution killed with signal 11 |
56 |
Halted |
0 ms |
0 KB |
- |