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>
#include <random>
#ifndef LOCAL
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
using ll = long long;
using dd = double;
using ld = long double;
using uii = unsigned int;
//#define x first
//#define y second
//#define all(x) x.begin(), x.end()
//#define rall(x) x.rbegin(), x.rend()
void solve();
mt19937_64 mt(1);
int32_t main() {
#ifdef LOCAL
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
//freopen("amusing.in", "r", stdin);
//freopen("amusing.out", "w", stdout);
#endif
cout << fixed << setprecision(30);
int tests = 1;
//cin >> tests;
while (tests--) {
solve();
}
}
void solve() {
int n, m;
cin >> n >> m;
vector<ll> a(n), b(m);
vector<pair<ll, int>> bs(m);
set<int> indx;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
bs[i].first = b[i];
bs[i].second = i;
if (i < 100 || m - i < 101) {
indx.insert(i);
}
}
sort(bs.begin(), bs.end());
for (int i = 0; i < min(1000, m); i++) {
indx.insert(bs[i].second);
}
for (int i = 0; i < m; i++) {
bs[i].second *= -1;
}
sort(bs.begin(), bs.end());
for (int i = 0; i < min(1000, m); i++) {
indx.insert(-bs[i].second);
}
vector<int> ch;
for (int i : indx) {
ch.push_back(i);
}
vector<ll> dp(n, INT64_MAX), cnt(m, n - 1);
dp[n - 1] = a[n - 1] * (m - 1);
for (int i = n - 2; i >= 0; i--) {
int ind = -1;
for (int j : ch) {
ll c = dp[cnt[j]] + b[j] * (cnt[j] - i) - a[cnt[j]] * j + a[i] * j;
if (dp[i] > c) {
dp[i] = c;
ind = j;
}
}
for (int j = 0; j <= ind; j++) {
cnt[j] = i;
}
}
cout << dp[0] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |