#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define PI acos(-1.0)
#define FF first
#define SS second
#define eps 1e-9
// vector
#define pb push_back
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define lb lower_bound
#define ub upper_bound
// bit
#define CNTBIT(x) __builtin_popcount(x)
#define ODDBIT(x) __builtin_parity(x)
#define MASK(i) (1ll<<(i))
#define BIT(x, i) (((x)>>(i))&1)
#define SUBSET(big, small) (((big)&(small))==(small))
#define MINBIT(x) (x)&(-x)
// typedef
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef pair<int, ii> i3;
/* CODE BELOW */
const int N=1e6+7, M=1e5+7;
const int MOD=1e9+7;
const ll oo=1e9+9;
int n, m, k, t;
int a[M], p[M];
ll _dp[M];
struct node
{
ll x, y;
node(){}
node(ll x, ll y): x(x), y(y){};
ll get(ll t)
{
return x * t + y;
}
}st[N*4];
void add(ll id, ll l, ll r, node d)
{
ll mid = (l + r) / 2;
bool ok1 = d.get(mid) < st[id].get(mid);
bool ok2 = d.get(l) < st[id].get(l);
if(ok1)swap(st[id], d);
if(l+1 == r)return;
if(ok1 != ok2)add(id*2, l, mid, d);
else add(id*2+1, mid, r, d);
}
ll query(ll id, ll l, ll r, ll x)
{
ll mid = (l + r) / 2;
ll res = st[id].get(x);
if(l+1 == r)return res;
if(x < mid)return min(res, query(id*2, l, mid, x));
else return min(res, query(id*2+1, mid, r, x));
}
signed main() {
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
//fastIO;
scanf("%d", &n);
for(int i=1;i<=n;i++) scanf("%d", &a[i]);
for(int i=1;i<=n;i++) {
scanf("%d", &m);
p[i]=p[i-1]+m;
}
_dp[1]=0;
const int SUB1=1000;
#define sqr(x) 1LL*(x)*(x)
if(n<=SUB1) {
for(int i=2;i<=n;i++) {
_dp[i]=oo;
for(int j=i-1;j>0;j--) {
ll cost=sqr(a[i]-a[j])+p[i-1]-p[j];
_dp[i]=min(_dp[i], _dp[j]+cost);
}
}
printf("%lld", _dp[n]);
} else {
add(1, 1, N, {-2*a[1], sqr(a[1])-p[1]});
for(int i=2;i<=n;i++) {
_dp[i]=sqr(a[i])-(p[i]-p[i-1])+query(1, 1, N, a[i]);
add(1, 1, N, {-2*a[i], _dp[i]+sqr(a[i])});
//printf("%d %lld %lld _ %lld\n", i, _dp[i], query(1, N, a[i]), _dp[i]+p[i]);
}
printf("%lld", _dp[n]+p[n]);
}
return 0;
}
/* stuff you should look for
- int overflow, array bounds
- special/edge cases (n=1?)
- do smth instead of nothing and stay organized
- WRITE STUFF DOWN
- DONT JUST STICK ON ONE APPROACH
*/
Compilation message
building.cpp: In function 'int main()':
building.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
building.cpp:71:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | for(int i=1;i<=n;i++) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
building.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d", &m);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
2292 KB |
Output is correct |
2 |
Correct |
47 ms |
2380 KB |
Output is correct |
3 |
Correct |
50 ms |
2312 KB |
Output is correct |
4 |
Correct |
46 ms |
2172 KB |
Output is correct |
5 |
Incorrect |
43 ms |
3588 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
46 ms |
2292 KB |
Output is correct |
7 |
Correct |
47 ms |
2380 KB |
Output is correct |
8 |
Correct |
50 ms |
2312 KB |
Output is correct |
9 |
Correct |
46 ms |
2172 KB |
Output is correct |
10 |
Incorrect |
43 ms |
3588 KB |
Output isn't correct |