#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 = 1004;
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;
map<int, bool> c;
for (int i = 1; i <= n; ++i)
{
if (c[a[i]])
continue;
aa.push_back(i);
c[a[i]] = true;
}
aa.push_back(n);
reverse(all(aa));
vector<int> bb;
c.clear();
for (int j = 1; j <= m; ++j)
{
if (c[b[j]])
continue;
bb.push_back(j);
c[b[j]] = true;
}
bb.push_back(m);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
2260 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
9 ms |
8276 KB |
Output is correct |
10 |
Correct |
10 ms |
8280 KB |
Output is correct |
11 |
Correct |
8 ms |
8276 KB |
Output is correct |
12 |
Correct |
9 ms |
8280 KB |
Output is correct |
13 |
Correct |
9 ms |
8272 KB |
Output is correct |
14 |
Correct |
10 ms |
8284 KB |
Output is correct |
15 |
Correct |
8 ms |
8276 KB |
Output is correct |
16 |
Correct |
8 ms |
8276 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 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 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2388 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
2260 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
9 ms |
8276 KB |
Output is correct |
10 |
Correct |
10 ms |
8280 KB |
Output is correct |
11 |
Correct |
8 ms |
8276 KB |
Output is correct |
12 |
Correct |
9 ms |
8280 KB |
Output is correct |
13 |
Correct |
9 ms |
8272 KB |
Output is correct |
14 |
Correct |
10 ms |
8284 KB |
Output is correct |
15 |
Correct |
8 ms |
8276 KB |
Output is correct |
16 |
Correct |
8 ms |
8276 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 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 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |