#include <bits/stdc++.h>
#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(iter(a), greater<>())
#define eb emplace_back
#define pob pop_back()
#define ef emplace_front
#define pof pop_front()
#define mp make_pair
#define F first
#define S second
#define printv(a, b) { \
for(auto pv : a) b << pv << " "; \
b << "\n"; \
}
using namespace std;
typedef long long ll;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
const ll MOD = 1000000007;
void topos(ll& a){
a = (a % MOD + MOD) % MOD;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pll> a(n), b(m);
for(int i = 0; i < n; i++) cin >> a[i].F, a[i].S = i;
for(int i = 0; i < m; i++) cin >> b[i].F, b[i].S = i;
lsort(a);
lsort(b);
auto solve = [&](vector<pll>& v){
deque<pll> r;
for(pll i : v){
if(r.empty()){
r.eb(i);
continue;
}
if(r.front().S < i.S && i.S < r.back().S) continue;
if(i.S < r.front().S){
if(i.F == r.front().F && r.size() > 1) r.pof;
r.ef(i);
}
if(r.back().S < i.S){
if(i.F == r.back().F && r.size() > 1) r.pob;
r.eb(i);
}
}
v.clear();
for(pll i : r) v.eb(i);
};
solve(a);
solve(b);
n = a.size(); m = b.size();
vector<vector<ll>> dp(n, vector<ll>(m, 1LL << 60));
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].F * (a[i].S - a[i - 1].S));
if(j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[i].F * (b[j].S - b[j - 1].S));
}
}
cout << dp[n - 1][m - 1] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
11 ms |
8264 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
380 KB |
Output is correct |
16 |
Correct |
12 ms |
8148 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 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 |
316 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
2772 KB |
Output is correct |
4 |
Correct |
15 ms |
2132 KB |
Output is correct |
5 |
Correct |
20 ms |
3668 KB |
Output is correct |
6 |
Correct |
6 ms |
972 KB |
Output is correct |
7 |
Correct |
38 ms |
4180 KB |
Output is correct |
8 |
Correct |
47 ms |
4076 KB |
Output is correct |
9 |
Correct |
40 ms |
5064 KB |
Output is correct |
10 |
Correct |
47 ms |
4916 KB |
Output is correct |
11 |
Correct |
67 ms |
35660 KB |
Output is correct |
12 |
Correct |
35 ms |
5344 KB |
Output is correct |
13 |
Correct |
35 ms |
5360 KB |
Output is correct |
14 |
Correct |
83 ms |
35028 KB |
Output is correct |
15 |
Correct |
24 ms |
4316 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 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 |
316 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
320 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
11 ms |
8264 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
380 KB |
Output is correct |
16 |
Correct |
12 ms |
8148 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 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 |
316 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
4 ms |
2772 KB |
Output is correct |
32 |
Correct |
15 ms |
2132 KB |
Output is correct |
33 |
Correct |
20 ms |
3668 KB |
Output is correct |
34 |
Correct |
6 ms |
972 KB |
Output is correct |
35 |
Correct |
38 ms |
4180 KB |
Output is correct |
36 |
Correct |
47 ms |
4076 KB |
Output is correct |
37 |
Correct |
40 ms |
5064 KB |
Output is correct |
38 |
Correct |
47 ms |
4916 KB |
Output is correct |
39 |
Correct |
67 ms |
35660 KB |
Output is correct |
40 |
Correct |
35 ms |
5344 KB |
Output is correct |
41 |
Correct |
35 ms |
5360 KB |
Output is correct |
42 |
Correct |
83 ms |
35028 KB |
Output is correct |
43 |
Correct |
24 ms |
4316 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 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 |
316 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
320 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
19 ms |
2376 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
468 KB |
Output is correct |
58 |
Correct |
6 ms |
1108 KB |
Output is correct |
59 |
Correct |
47 ms |
5208 KB |
Output is correct |
60 |
Correct |
43 ms |
5108 KB |
Output is correct |
61 |
Correct |
47 ms |
6348 KB |
Output is correct |
62 |
Correct |
50 ms |
6516 KB |
Output is correct |
63 |
Runtime error |
567 ms |
1048576 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |