#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 100005, M = 2004;
const ll INF = 1000000009000000009;
int n, m;
int a[N], b[N];
bool c[M];
ll dp[M][M];
void solv()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
for (int i = 1; i <= m; ++i)
{
cin >> b[i];
}
vector<int> aa, bb;
map<int, bool> c;
for (int i = 1; i <= n; ++i)
{
if (c[a[i]])
continue;
aa.push_back(i);
c[a[i]] = true;
}
c.clear();
for (int j = 1; j <= m; ++j)
{
if (c[b[j]])
continue;
bb.push_back(j);
c[b[j]] = true;
}
c.clear();
for (int i = n; i >= 1; --i)
{
if (c[a[i]])
continue;
aa.push_back(i);
c[a[i]] = true;
}
c.clear();
for (int j = m; j >= 1; --j)
{
if (c[b[j]])
continue;
bb.push_back(j);
c[b[j]] = true;
}
sort(all(aa));
reverse(all(aa));
sort(all(bb));
reverse(all(bb));
for (int i = 0; i < sz(aa); ++i)
{
for (int j = 0; j < sz(bb); ++j)
{
dp[i][j] = INF;
}
}
dp[0][0] = 0;
for (int i = 0; i < sz(aa); ++i)
{
for (int j = 0; j < sz(bb); ++j)
{
if (i + 1 < sz(aa))
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (aa[i] - aa[i + 1]) * 1LL * b[bb[j]]);
if (j + 1 < sz(bb))
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + (bb[j] - bb[j + 1]) * 1LL * a[aa[i]]);
}
}
cout << dp[sz(aa) - 1][sz(bb) - 1] << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
2132 KB |
Output is correct |
5 |
Correct |
2 ms |
4316 KB |
Output is correct |
6 |
Correct |
2 ms |
3024 KB |
Output is correct |
7 |
Correct |
1 ms |
2004 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
32 ms |
31792 KB |
Output is correct |
10 |
Correct |
37 ms |
31788 KB |
Output is correct |
11 |
Correct |
33 ms |
31700 KB |
Output is correct |
12 |
Correct |
32 ms |
31748 KB |
Output is correct |
13 |
Correct |
32 ms |
31772 KB |
Output is correct |
14 |
Correct |
31 ms |
31692 KB |
Output is correct |
15 |
Correct |
34 ms |
31772 KB |
Output is correct |
16 |
Correct |
31 ms |
31700 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
8660 KB |
Output is correct |
4 |
Correct |
44 ms |
32408 KB |
Output is correct |
5 |
Correct |
10 ms |
3964 KB |
Output is correct |
6 |
Correct |
11 ms |
9028 KB |
Output is correct |
7 |
Correct |
66 ms |
33292 KB |
Output is correct |
8 |
Correct |
73 ms |
33356 KB |
Output is correct |
9 |
Correct |
71 ms |
33320 KB |
Output is correct |
10 |
Correct |
68 ms |
33320 KB |
Output is correct |
11 |
Correct |
53 ms |
33236 KB |
Output is correct |
12 |
Correct |
74 ms |
33044 KB |
Output is correct |
13 |
Correct |
64 ms |
33160 KB |
Output is correct |
14 |
Correct |
51 ms |
33204 KB |
Output is correct |
15 |
Correct |
15 ms |
2000 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
2132 KB |
Output is correct |
5 |
Correct |
2 ms |
4316 KB |
Output is correct |
6 |
Correct |
2 ms |
3024 KB |
Output is correct |
7 |
Correct |
1 ms |
2004 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
32 ms |
31792 KB |
Output is correct |
10 |
Correct |
37 ms |
31788 KB |
Output is correct |
11 |
Correct |
33 ms |
31700 KB |
Output is correct |
12 |
Correct |
32 ms |
31748 KB |
Output is correct |
13 |
Correct |
32 ms |
31772 KB |
Output is correct |
14 |
Correct |
31 ms |
31692 KB |
Output is correct |
15 |
Correct |
34 ms |
31772 KB |
Output is correct |
16 |
Correct |
31 ms |
31700 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
8 ms |
8660 KB |
Output is correct |
32 |
Correct |
44 ms |
32408 KB |
Output is correct |
33 |
Correct |
10 ms |
3964 KB |
Output is correct |
34 |
Correct |
11 ms |
9028 KB |
Output is correct |
35 |
Correct |
66 ms |
33292 KB |
Output is correct |
36 |
Correct |
73 ms |
33356 KB |
Output is correct |
37 |
Correct |
71 ms |
33320 KB |
Output is correct |
38 |
Correct |
68 ms |
33320 KB |
Output is correct |
39 |
Correct |
53 ms |
33236 KB |
Output is correct |
40 |
Correct |
74 ms |
33044 KB |
Output is correct |
41 |
Correct |
64 ms |
33160 KB |
Output is correct |
42 |
Correct |
51 ms |
33204 KB |
Output is correct |
43 |
Correct |
15 ms |
2000 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Runtime error |
156 ms |
63072 KB |
Execution killed with signal 11 |
56 |
Halted |
0 ms |
0 KB |
- |