#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;
#define fastIO ios::sync_with_stdio(false), cin.tie(NULL)
#define forw(i, l, r) for( int i = (l) ; i < (r) ; i++ )
#define forb(i, r, l) for( int i = (r) ; i >= (l) ; i-- )
#define log2i(x) (64 - __builtin_clzll(1ll * (x)) - 1)
#define numBit(x) (__builtin_popcountll(1ll * (x)))
#define getBit(x, i) ((x) >> (i) & 1)
#define Pi acos(-1.0l)
#define sz(x) int(x.size())
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(x) cerr << #x << " = " << x << '\n';
const int N = 1e5 + 7;
const ll inf = 1e18l;
int n;
ll h[N], w[N], dp[N];
struct Line {
ll a, b;
Line():a(0), b(inf) {}
Line(ll _a, ll _b):a(_a), b(_b) {}
ll operator ()(int x) {return a * x + b;}
}T[40 * N];
void upd(Line val, int idx, int l, int r) {
if(l + 1 == r) {
if(val(l) < T[idx](l)) T[idx] = val;
return;
}
int k = idx << 1, mid = (l + r) >> 1;
if(val.a < T[idx].a) swap(T[idx], val);
if(val(mid) < T[idx](mid)) {
swap(T[idx], val);
upd(val, k + 1, mid, r);
} else upd(val, k, l, mid);
}
ll get(int x, int idx, int l, int r) {
if(l + 1 == r) return T[idx](x);
int k = idx << 1, mid = (l + r) >> 1;
if(x < mid) return min(get(x, k, l, mid), T[idx](x));
else return min(T[idx](x), get(x, k + 1, mid, r));
}
int main() {
fastIO;
#ifndef ONLINE_JUDGE
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
#endif
cin >> n;
ll sum = 0;
forw(i, 0, n) cin >> h[i];
forw(i, 0, n) cin >> w[i], sum += w[i];
dp[0] = -w[0];
forw(i, 1, n) {
upd(Line(-2 * h[i - 1], dp[i - 1] + h[i - 1] * h[i - 1]), 1, 0, 10 * N);
dp[i] = get(h[i], 1, 0, 10 * N) + h[i] * h[i] - w[i];
}
cout << dp[n - 1] + sum;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
62832 KB |
Output is correct |
2 |
Correct |
30 ms |
62924 KB |
Output is correct |
3 |
Correct |
28 ms |
62912 KB |
Output is correct |
4 |
Correct |
29 ms |
62880 KB |
Output is correct |
5 |
Correct |
27 ms |
62932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
65220 KB |
Output is correct |
2 |
Correct |
79 ms |
65176 KB |
Output is correct |
3 |
Correct |
77 ms |
65176 KB |
Output is correct |
4 |
Correct |
65 ms |
65220 KB |
Output is correct |
5 |
Correct |
67 ms |
65176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
62832 KB |
Output is correct |
2 |
Correct |
30 ms |
62924 KB |
Output is correct |
3 |
Correct |
28 ms |
62912 KB |
Output is correct |
4 |
Correct |
29 ms |
62880 KB |
Output is correct |
5 |
Correct |
27 ms |
62932 KB |
Output is correct |
6 |
Correct |
71 ms |
65220 KB |
Output is correct |
7 |
Correct |
79 ms |
65176 KB |
Output is correct |
8 |
Correct |
77 ms |
65176 KB |
Output is correct |
9 |
Correct |
65 ms |
65220 KB |
Output is correct |
10 |
Correct |
67 ms |
65176 KB |
Output is correct |
11 |
Correct |
85 ms |
65212 KB |
Output is correct |
12 |
Correct |
102 ms |
65220 KB |
Output is correct |
13 |
Correct |
77 ms |
65220 KB |
Output is correct |
14 |
Correct |
88 ms |
65220 KB |
Output is correct |
15 |
Correct |
82 ms |
65188 KB |
Output is correct |
16 |
Correct |
61 ms |
65220 KB |
Output is correct |
17 |
Correct |
58 ms |
65252 KB |
Output is correct |
18 |
Correct |
55 ms |
65244 KB |
Output is correct |