#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define endl "\n"
using namespace std;
typedef long long ll;
const int N = 1e6 + 15;
int n, k;
int a[N];
vector<int> compress;
struct line {
ll a, b;
line() : a(0), b(0) {}
line (ll a, ll b) : a(a), b(b) {}
ll calc (ll x) { return a * x + b; }
ll slope() { return a; }
};
struct liChao {
vector<line> tr;
liChao() {}
liChao (int sz) : tr(sz + 1, line(0, LLONG_MAX)) {}
void update (line f, int l, int r) {
if (l > r) return;
int mid = (l + r) >> 1;
if (l == r) {
tr[mid] = (f.calc(l) < tr[mid].calc(l) ? f : tr[mid]);
return;
}
if (f.calc(mid) < tr[mid].calc(mid)) swap(tr[mid], f);
if (f.slope() > tr[mid].slope())
update(f, l, mid - 1);
if (f.slope() < tr[mid].slope())
update(f, mid + 1, r);
}
ll query (int p, int l, int r) {
int mid = (l + r) >> 1;
ll cur = tr[mid].calc(p);
if (p == mid) return cur;
if (p < mid) return min(query(p, l, mid - 1), cur);
if (p > mid) return min(query(p, mid + 1, r), cur);
}
};
ll dp[N];
ll pref[N];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) cin >> pref[i], pref[i] += pref[i-1];
liChao solver(N);
solver.update(line(-2LL*a[1], 1LL*a[1]*a[1] - pref[1]),0,N);
for(int i = 2; i <= n; i++){
dp[i] = 1LL*a[i]*a[i] + pref[i-1] + solver.query(a[i],0,N);
solver.update(line(-2LL*a[i], 1LL*a[i]*a[i] + dp[i] - pref[i]),0,N);
}
cout << dp[n];
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t; t = 1; //cin >> t;
while(t--) solve();
}
Compilation message
building.cpp: In member function 'll liChao::query(int, int, int)':
building.cpp:54:5: warning: control reaches end of non-void function [-Wreturn-type]
54 | }
| ^
building.cpp: In function 'int main()':
building.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen(task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20048 KB |
Output is correct |
2 |
Correct |
4 ms |
20048 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20048 KB |
Output is correct |
5 |
Correct |
4 ms |
20216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
25048 KB |
Output is correct |
2 |
Correct |
37 ms |
25424 KB |
Output is correct |
3 |
Correct |
36 ms |
25260 KB |
Output is correct |
4 |
Correct |
39 ms |
25416 KB |
Output is correct |
5 |
Correct |
37 ms |
25424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20048 KB |
Output is correct |
2 |
Correct |
4 ms |
20048 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20048 KB |
Output is correct |
5 |
Correct |
4 ms |
20216 KB |
Output is correct |
6 |
Correct |
37 ms |
25048 KB |
Output is correct |
7 |
Correct |
37 ms |
25424 KB |
Output is correct |
8 |
Correct |
36 ms |
25260 KB |
Output is correct |
9 |
Correct |
39 ms |
25416 KB |
Output is correct |
10 |
Correct |
37 ms |
25424 KB |
Output is correct |
11 |
Correct |
45 ms |
25672 KB |
Output is correct |
12 |
Correct |
44 ms |
25248 KB |
Output is correct |
13 |
Correct |
37 ms |
25408 KB |
Output is correct |
14 |
Correct |
43 ms |
25416 KB |
Output is correct |
15 |
Correct |
34 ms |
25160 KB |
Output is correct |
16 |
Correct |
38 ms |
25416 KB |
Output is correct |
17 |
Correct |
20 ms |
25312 KB |
Output is correct |
18 |
Correct |
19 ms |
25436 KB |
Output is correct |