이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> a, b;
void solve(vector<int> r, vector<int> c)
{
    int h = r.size(), w = c.size();
    vector<vector<ll> > dp(h, vector<ll>(w));
    for(int i = 0; i < h; i++)
    {
        for(int j = 0; j < w; j++)
        {
            if(i == 0 && j == 0) continue;
            dp[i][j] = 1e15;
            if(i != 0) dp[i][j] = min(dp[i][j], dp[i - 1][j] + b[c[j]] * (r[i] - r[i - 1]));
            if(j != 0) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[r[i]] * (c[j] - c[j - 1]));
        }
    }
    cout << dp[h - 1][w - 1];
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    a.resize(h + 1);
    b.resize(w + 1);
    for(int i = 1; i <= h; i++) cin >> a[i];
    for(int i = 1; i <= w; i++) cin >> b[i];
    vector<int> ro, co;
    for(int i = 1; i <= h; i++)
    {
        if(ro.empty() || a[ro.back()] > a[i]) ro.push_back(i);
    }
    for(int i = 1; i <= w; i++)
    {
        if(co.empty() || b[co.back()] > b[i]) co.push_back(i);
    }
    vector<int> r2, c2;
    for(int i = h; i > ro.back(); i--)
    {
        if(r2.empty() || a[r2.back()] > a[i]) r2.push_back(i);
    }
    for(int i = w; i > co.back(); i--)
    {
        if(c2.empty() || b[c2.back()] > b[i]) c2.push_back(i);
    }
    reverse(r2.begin(), r2.end());
    reverse(c2.begin(), c2.end());
    for(auto x : r2) ro.push_back(x);
    for(auto x : c2) co.push_back(x);
    solve(ro, co);
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |