#include<bits/stdc++.h>
using namespace std;
#define PROB "BRIDGEPOL"
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) for(int i=a;i<=b;(i)++)
#define FOD(i,a,b) for(int i=a;i>=b;(i)--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define fi first
#define se second
#define mp make_pair
#define SZ(x) int(x.size())
#define bit(p,i) ((p>>i)&1)
template <class T>
inline bool minimize(T &x, const T &y){ if (x > y){x = y; return 1;} return 0; }
template <class T>
inline bool maximize(T &x, const T &y){ if (x < y){x = y; return 1;} return 0; }
const int N=1e5+9;
ll f[N],h[N],w[N];
int n;
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll get(ll x) {
//assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
void giai(){
cin>>n;
FOR(i,1,n)cin>>h[i];
FOR(i,1,n){
int x;cin>>x;
w[i]=w[i-1]+x;
}
LineContainer c;
c.add(-h[1],-h[1]*h[1]);
FOR(i,2,n){
f[i]=h[i]*h[i]+w[i-1]-c.get(-2*h[i]);
c.add(-h[i],-(h[i]*h[i]-w[i]+f[i]));
//cout<<h[i]*h[i]+w[i-1]<<" "<<f[i]<<'\n';
}
cout<<'\n';
cout<<f[n];
}
int main(){
if(fopen(PROB".inp","r")){
freopen(PROB".inp","r",stdin);
freopen(PROB".out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
giai();
}
Compilation message
building.cpp: In function 'int main()':
building.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(PROB".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
building.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(PROB".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |