//https://oj.uz/problem/view/CEOI17_building
#include<bits/stdc++.h>
const int N = 1e6 + 5;
const long long inf = 1e18;
using namespace std;
struct line {
long long x, y;
long long get(long long val) { return val * x + y; }
line (long long x, long long y) : x(x), y(y) {}
line() {}
} it[4 * N];
struct LiChaoTree {
#define m ((l + r) >> 1)
void init() {
for(int i = 0; i < 4 * N; ++i) it[i].x = 0, it[i].y = inf;
}
void add_line(int node, int l, int r, line u) {
bool lef = u.get(l) < it[node].get(l);
bool mid = u.get(m) < it[node].get(m);
if(mid) swap(it[node], u);
if(l == r) return;
if(lef != mid) add_line(node << 1, l, m, u);
else add_line(node << 1 | 1, m + 1, r, u);
}
long long get(int node, int l, int r, long long u) {
if(l == r) return it[node].get(u);
if(u <= m) return min(it[node].get(u), get(node << 1, l, m, u));
return min(it[node].get(u), get(node << 1 | 1, m + 1, r, u));
}
#undef m
} A;
int n;
long long dp[N], h[N], w[N];
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) cin >> w[i];
A.init();
dp[1] = -w[1]; A.add_line(1, 0, N-1, line(-2*h[1], dp[1] + h[1] * h[1]));
for (int i = 2; i <= n; i++){
dp[i] = h[i] * h[i] - w[i] + A.get(1, 0, N-1, h[i]);
A.add_line(1, 0, N-1, line(-2*h[i], dp[i] + h[i] * h[i]));
}
for (int i = 1; i <= n; i++) dp[n] += w[i];
cout << dp[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
62968 KB |
Output is correct |
2 |
Correct |
52 ms |
62968 KB |
Output is correct |
3 |
Correct |
46 ms |
62968 KB |
Output is correct |
4 |
Correct |
47 ms |
62972 KB |
Output is correct |
5 |
Correct |
46 ms |
62968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
65268 KB |
Output is correct |
2 |
Correct |
89 ms |
65288 KB |
Output is correct |
3 |
Correct |
85 ms |
65400 KB |
Output is correct |
4 |
Correct |
84 ms |
65324 KB |
Output is correct |
5 |
Correct |
82 ms |
65304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
62968 KB |
Output is correct |
2 |
Correct |
52 ms |
62968 KB |
Output is correct |
3 |
Correct |
46 ms |
62968 KB |
Output is correct |
4 |
Correct |
47 ms |
62972 KB |
Output is correct |
5 |
Correct |
46 ms |
62968 KB |
Output is correct |
6 |
Correct |
87 ms |
65268 KB |
Output is correct |
7 |
Correct |
89 ms |
65288 KB |
Output is correct |
8 |
Correct |
85 ms |
65400 KB |
Output is correct |
9 |
Correct |
84 ms |
65324 KB |
Output is correct |
10 |
Correct |
82 ms |
65304 KB |
Output is correct |
11 |
Correct |
98 ms |
66552 KB |
Output is correct |
12 |
Correct |
101 ms |
66296 KB |
Output is correct |
13 |
Correct |
80 ms |
66464 KB |
Output is correct |
14 |
Correct |
101 ms |
66484 KB |
Output is correct |
15 |
Correct |
80 ms |
66160 KB |
Output is correct |
16 |
Correct |
81 ms |
66388 KB |
Output is correct |
17 |
Correct |
73 ms |
66396 KB |
Output is correct |
18 |
Correct |
75 ms |
66364 KB |
Output is correct |