#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define FOR(i,N) for(i=0;i<(N);++i)
#define FORe(i,N) for(i=1;i<=(N);++i)
#define FORr(i,a,b) for(i=(a);i<(b);++i)
#define FORrev(i,N) for(i=(N);i>=0;--i)
#define F0R(i,N) for(int i=0;i<(N);++i)
#define F0Re(i,N) for(int i=1;i<=(N);++i)
#define F0Rr(i,a,b) for(ll i=(a);i<(b);++i)
#define F0Rrev(i,N) for(int i=(N);i>=0;--i)
#define all(v) (v).begin(),(v).end()
#define dbgLine cerr<<" LINE : "<<__LINE__<<"\n"
#define ldd long double
using namespace std;
const int Alp = 26;
const int __PRECISION = 9;
const int inf = 1e9 + 8;
const ldd PI = acos(-1);
const ldd EPS = 1e-7;
const ll MOD = 1e9 + 7;
const ll MAXN = 1e5 + 5;
const ll ROOTN = 320;
const ll LOGN = 18;
const ll INF = 1e18 + 1022;
int N;
ll dp[MAXN], pre[MAXN], h[MAXN];
struct line
{
mutable ll m, c, p, isline;
bool operator < (const line& o) const { return ((o.isline) ? (m < o.m) : (p < o.p));}
};
struct CHT : multiset<line>
{
ll mode;
CHT() { mode = 1;}
inline void set_mode(string s){if(s[1] == 'i' or s[1] == 'I') mode = -1;}
ll div(ll a, ll b) {return (a/b - ((a^b)<0 && a%b));}
bool isect(iterator x, iterator y)
{
if(y == end()) {x->p = INF; return false;}
if(x->m == y->m) x->p = (x->c > y->c) ? INF : -INF;
else x->p = div((y->c - x->c),(x->m - y->m));
return x->p >= y->p;
}
void add(ll sl, ll in)
{
auto z = insert({mode*sl, mode*in, 0, 1}), y = z++, x = y;
while(isect(y, z)) z = erase(z);
if(x != begin() and isect(--x, y)) isect(x, y = erase(y));
while((y=x) != begin() and (--x)->p >= y->p) isect(x, erase(y));
}
ll query(ll x)
{
assert(!empty());
auto l = *lower_bound({0, 0, x, 0});
return mode*(l.m * x + l.c);
}
}box;
signed main()
{
/*
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
*/
ll w;
cin>>N;
F0R(i, N)
cin>>h[i];
F0R(i, N)
cin>>w, pre[i] = w + ((i == 0) ? 0 : pre[i-1]);
box.set_mode("min");
dp[0] = 0;
box.add( - 2*h[0], h[0]*h[0] + dp[0] - pre[0]);
F0Re(i, N-1)
{
dp[i] = (h[i] * h[i] + pre[i-1]) + box.query(h[i]);
box.add(- 2*h[i], h[i]*h[i] + dp[i] - pre[i]);
}
//F0R(i, N)
// cout<<"dp["<<i<<"] = "<<dp[i]<<'\n';
cout<<dp[N-1]<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
3756 KB |
Output is correct |
2 |
Correct |
136 ms |
3788 KB |
Output is correct |
3 |
Correct |
141 ms |
3832 KB |
Output is correct |
4 |
Correct |
123 ms |
3592 KB |
Output is correct |
5 |
Correct |
125 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
136 ms |
3756 KB |
Output is correct |
7 |
Correct |
136 ms |
3788 KB |
Output is correct |
8 |
Correct |
141 ms |
3832 KB |
Output is correct |
9 |
Correct |
123 ms |
3592 KB |
Output is correct |
10 |
Correct |
125 ms |
5112 KB |
Output is correct |
11 |
Correct |
151 ms |
3900 KB |
Output is correct |
12 |
Correct |
141 ms |
3780 KB |
Output is correct |
13 |
Correct |
132 ms |
3832 KB |
Output is correct |
14 |
Correct |
146 ms |
4088 KB |
Output is correct |
15 |
Correct |
137 ms |
11384 KB |
Output is correct |
16 |
Correct |
124 ms |
5116 KB |
Output is correct |
17 |
Correct |
114 ms |
3764 KB |
Output is correct |
18 |
Correct |
116 ms |
3768 KB |
Output is correct |