#include <bits/stdc++.h>
using namespace std;
struct func1 {
long long a, b;
long long operator() ( long long x ) {
return a * x + b;
}
};
const int MAX_N = 1e5;
const int MAX_H = 1e6;
long long h[MAX_N], w[MAX_N], dp[MAX_N];
func1 maxFunc[4 * MAX_H];
void addLine( int v, int l, int r, func1 f ) {
int mid = (l + r) / 2;
if ( maxFunc[v].a < f.a )
swap( maxFunc[v], f );
if ( f( mid ) < maxFunc[v]( mid ) ) {
swap( maxFunc[v], f );
if ( l != r )
addLine( v * 2 + 1, l, mid, f );
} else {
if ( l != r )
addLine( v * 2 + 2, mid + 1, r, f );
}
}
long long getMin( int v, int l, int r, int x ) {
if ( l == r )
return maxFunc[v]( x );
int mid = (l + r) / 2;
if ( x <= mid )
return min( maxFunc[v]( x ), getMin( v * 2 + 1, l, mid, x ) );
return min( maxFunc[v]( x ), getMin( v * 2 + 2, mid + 1, r, x ) );
}
int main() {
int n;
cin >> n;
for ( int i = 0; i < n; i++ )
cin >> h[i];
for ( int i = 0; i < n; i++ )
cin >> w[i];
for ( int i = 1; i < n; i++ )
w[i] += w[i - 1];
for ( int v = 0; v < 4 * MAX_H; v++ )
maxFunc[v] = { (long long)1e7, (long long)1e12 };
addLine( 0, 1, MAX_H, { -2 * h[0], dp[0] - w[0] + h[0] * h[0] } );
for ( int i = 1; i < n; i++ ) {
dp[i] = getMin( 0, 1, MAX_H, h[i] ) + w[i - 1] + h[i] * h[i];
addLine( 0, 1, MAX_H, { -2 * h[i], dp[i] - w[i] + h[i] * h[i] } );
}
cout << dp[n - 1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
64344 KB |
Output is correct |
2 |
Correct |
10 ms |
64092 KB |
Output is correct |
3 |
Correct |
10 ms |
64288 KB |
Output is correct |
4 |
Correct |
13 ms |
64348 KB |
Output is correct |
5 |
Correct |
11 ms |
64092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
65388 KB |
Output is correct |
2 |
Correct |
74 ms |
66388 KB |
Output is correct |
3 |
Correct |
70 ms |
66228 KB |
Output is correct |
4 |
Correct |
81 ms |
66316 KB |
Output is correct |
5 |
Correct |
65 ms |
66492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
64344 KB |
Output is correct |
2 |
Correct |
10 ms |
64092 KB |
Output is correct |
3 |
Correct |
10 ms |
64288 KB |
Output is correct |
4 |
Correct |
13 ms |
64348 KB |
Output is correct |
5 |
Correct |
11 ms |
64092 KB |
Output is correct |
6 |
Correct |
69 ms |
65388 KB |
Output is correct |
7 |
Correct |
74 ms |
66388 KB |
Output is correct |
8 |
Correct |
70 ms |
66228 KB |
Output is correct |
9 |
Correct |
81 ms |
66316 KB |
Output is correct |
10 |
Correct |
65 ms |
66492 KB |
Output is correct |
11 |
Correct |
80 ms |
66388 KB |
Output is correct |
12 |
Correct |
75 ms |
66392 KB |
Output is correct |
13 |
Correct |
70 ms |
66388 KB |
Output is correct |
14 |
Correct |
81 ms |
66384 KB |
Output is correct |
15 |
Correct |
60 ms |
66252 KB |
Output is correct |
16 |
Correct |
61 ms |
66128 KB |
Output is correct |
17 |
Correct |
60 ms |
66212 KB |
Output is correct |
18 |
Correct |
56 ms |
66388 KB |
Output is correct |