Trí ngu
unknown
c_cpp
3 years ago
3.0 kB
6
Indexable
Never
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define TASK "" #define ALL(x) (x).begin(), (x).end() using namespace std; template <typename T1, typename T2> bool mini(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maxi(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } const int N = 1e5 + 5; const int V = 2e6 + 1000; const int oo = 1e9; const long long ooo = 1e18; const int mod = 1e9 + 7; // 998244353; const long double pi = acos(-1); const int off = 1e6 + 200; pair <int, int> jump[N][21]; long long cost[N][21]; long long dp[N][21]; long long f[N]; int pos[V]; int s[N]; int a[N]; int n,x,y,z; long long solve(int i, int cur) { if (i == n + 1) return 0; long long &res = dp[i][cur + 10]; if (res != -1) return res; res = ooo; if (cur == 0) return res = solve(i + 1, a[i + 1]); if (cur > 0) { mini(res, solve(i + 1, a[i + 1]) + cur * x); for (int v = 1; v <= cur; v++) { int j = jump[i][v + 10].fi; if (j == 0) continue; int val = jump[i][v + 10].se; long long c = cost[i][v + 10] + (cur - v) * x; mini(res, solve(j, val) + c); } } if (cur < 0) { mini(res, solve(i + 1, a[i + 1]) + (-cur) * y); for (int v = cur; v < 0; v++) { int j = jump[i][v + 10].fi; if (j == 0) continue; int val = jump[i][v + 10].se; long long c = cost[i][v + 10] + (v - cur) * y; mini(res, solve(j, val) + c); } } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef ONLINE_JUDGE // freopen(TASK".inp","r",stdin); // freopen(TASK".out","w",stdout); #endif cin >> n >> x >> y >> z; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { int x; cin >> x; a[i] -= x; s[i] = s[i - 1] + a[i]; f[i] = f[i - 1] + i * a[i]; } for (int i = 0; i < V; i++) pos[i] = oo; for (int i = n; i >= 1; i--) { if (a[i] > 0) { for (int x = 1; x <= a[i]; x++) { int jmin = oo; for (int v = s[i] - x - 10; v <= s[i] - x; v++) mini(jmin, pos[v + off]); if (jmin == oo) continue; jump[i][x + 10].fi = jmin; jump[i][x + 10].se = s[jmin] - s[i] + x; long long &c = cost[i][x + 10]; c -= f[jmin] - f[i]; c += (long long) (s[jmin] - s[i]) * jmin; c += x * (jmin - i); c *= z; } } if (a[i] < 0) { for (int x = a[i]; x < 0; x++) { int jmin = oo; for (int v = s[i] - x; v <= s[i] - x + 10; v++) mini(jmin, pos[v + off]); if (jmin == oo) continue; jump[i][x + 10].fi = jmin; jump[i][x + 10].se = s[jmin] - s[i] + x; long long &c = cost[i][x + 10]; c -= f[jmin] - f[i]; c += (long long) (s[jmin] - s[i]) * jmin; c += x * (jmin - i); c *= -z; } } pos[s[i] + off] = i; } memset(dp, -1, sizeof(dp)); cout << solve(1, a[1]); return 0; }