///huynhocute123///
#include<bits/stdc++.h>
using namespace std;
#define S second
#define F first
#define pii pair<int,int>
#define piii pair<int,pair<int,int>>
#define pb push_back
#define pi M_PI
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = b; i >= a; --i)
#define ALL(v) v.begin(),v.end()
#define ll long long
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
//random_device rd;
//mt19937 rng(rd());
//#define int long long
const int maxN = 1e5 + 9 ;
const int modd = 1e9 + 7;
const int base = 2309;
const int MAX = 1e9+9;
void minimize(int &u, int v){
if(v < u) u = v;
}
void maximize(int &u, int v){
if(v > u) u = v;
}
int n, k, t, m, res, a[maxN], f[maxN], b[maxN],dp[maxN];
int l, r;
bool vis[maxN];
vector<int> e[maxN];
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 query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
}line ;
void solve(){
cin >> n;
FOR(i ,1 , n)cin >> a[i];
FOR(i, 1, n){
cin >> b[i];
f[i] = f[i-1] + b[i];
}
// L[i] = { -2*a[i], dp[i] + a[i] * a[i] - f[i] };
// dp[i] = a[i] *S[id].a + S[id].b + a[i]*a[i] + f[i-1];
/// dp[i] = QUERRY() + a[i]^ 2 + f[i-1]
line.add(2 * a[1], - (a[1] * a[1] - f[1]) );
FOR(i , 2, n){
dp[i] = -line.query(a[i]) +a[i] * a[i] + f[i-1];
line.add(2 * a[i] , f[i] - a[i] * a[i] - dp[i] );
}
cout << dp[n];
}
signed main(){
// freopen("name.inp","r",stdin);
// freopen("name.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
inp("task.inp");
t = 1;
// cin >> t;
while( t-- )solve();
}
Compilation message
building.cpp: In function 'int main()':
building.cpp:14:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | #define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
building.cpp:88:5: note: in expansion of macro 'inp'
88 | inp("task.inp");
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
4188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Incorrect |
16 ms |
4188 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |