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>
#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 = 1e5 + 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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |