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;
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... |