This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |