#include <bits/stdc++.h>
#define pll pair<long long, long long>
#define x first
#define y second
using namespace std;
const int N = 1<<20;
const int M = 1e5 + 10;
const int K = 1e6;
const long long INF = 1e18;
bool chk[N<<1];
int n;
long long sum[M], h[M], dp[M];
pll seg[N<<1];
long long get( pll a, long long x ) { return a.x * x + a.y; }
void update( pll li, int l = 0, int r = K, int idx = 1 ) {
if( !chk[idx] ) {
chk[idx] = true, seg[idx] = li;
return ;
}
if( get( li, l ) > get( seg[idx], l ) && get( li, r ) > get( seg[idx], r ) ) return ;
if( get( li, l ) < get( seg[idx], l ) && get( li, r ) < get( seg[idx], r ) ) return void( seg[idx] = li );
int mid = ( l + r ) >> 1;
if( get( li, l ) < get( seg[idx], l ) ) swap( seg[idx], li );
if( l == r ) return ;
if( get( li, mid ) < get( seg[idx], mid ) ) swap( seg[idx], li ), update( li, l, mid, idx<<1 );
else update( li, mid+1, r, idx<<1|1 );
}
long long query( long long x, int l = 0, int r = K, int idx = 1 ) {
if( !chk[idx] ) return INF;
if( l == r ) return get( seg[idx], x );
int mid = ( l + r ) >> 1;
if( x <= mid ) return min( query( x, l, mid, idx<<1 ), get( seg[idx], x ) );
else return min( get( seg[idx], x ), query( x, mid + 1, r, idx<<1|1 ) );
}
int main()
{
scanf("%d",&n);
for( int i = 1 ; i <= n ; i++ ) scanf("%lld",&h[i]);
for( int i = 1 ; i <= n ; i++ ) {
scanf("%lld",&sum[i]);
sum[i] += sum[i-1];
}
for( int i = 1 ; i <= n ; i++ ) {
if( i != 1 ) dp[i] = query( h[i] ) + h[i] * h[i] + sum[i-1];
//printf("%d %lld %lld %lld\n",i,query( h[i] ), sum[i-1],dp[i]);
update( pll( -2LL*h[i], h[i]*h[i] - sum[i] + dp[i] ) );
}
//printf("\n");
printf("%lld\n",dp[n]);
return 0;
}
Compilation message
building.cpp: In function 'int main()':
building.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
building.cpp:45:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for( int i = 1 ; i <= n ; i++ ) scanf("%lld",&h[i]);
~~~~~^~~~~~~~~~~~~~
building.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&sum[i]);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
3064 KB |
Output is correct |
2 |
Correct |
64 ms |
3192 KB |
Output is correct |
3 |
Correct |
56 ms |
3064 KB |
Output is correct |
4 |
Correct |
55 ms |
2808 KB |
Output is correct |
5 |
Correct |
43 ms |
4860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
57 ms |
3064 KB |
Output is correct |
7 |
Correct |
64 ms |
3192 KB |
Output is correct |
8 |
Correct |
56 ms |
3064 KB |
Output is correct |
9 |
Correct |
55 ms |
2808 KB |
Output is correct |
10 |
Correct |
43 ms |
4860 KB |
Output is correct |
11 |
Correct |
55 ms |
2936 KB |
Output is correct |
12 |
Correct |
56 ms |
3064 KB |
Output is correct |
13 |
Correct |
48 ms |
2808 KB |
Output is correct |
14 |
Correct |
56 ms |
2936 KB |
Output is correct |
15 |
Correct |
45 ms |
6264 KB |
Output is correct |
16 |
Correct |
44 ms |
4856 KB |
Output is correct |
17 |
Correct |
29 ms |
2680 KB |
Output is correct |
18 |
Correct |
28 ms |
2680 KB |
Output is correct |