#include <iostream>
using namespace std;
/*
dp[1] = 0
Let dp[i] = the minimum cost to connect pillar 1 and pillar i
dp[i] = min{dp[j] + sq(h[i] - h[j]) + w_sum[i-1] - w_sum[j] | j=1..i-1}
= min{dp[j] + sq(h[i]) - 2*h[i]*h[j] + sq(h[j]) + w_sum[i-1] - w_sum[j]}
= sq(h[i]) + w_sum[i-1] + min{ (-2*h[j]) * h[i] + (dp[j] + sq(h[j]) - w_sum[j]) }
A * X + B
*/
const long long INF = 1'000'000'000'000'000'000LL;
long long sq(long long x)
{
return x*x;
}
int main()
{
int n;
cin >> n;
long long h[n+1];
for(int i = 1; i <= n; i++) cin >> h[i];
long long w[n+1], w_sum[n+1];
w_sum[0] = 0;
for(int i = 1; i <= n; i++)
{
cin >> w[i];
w_sum[i] = w_sum[i-1] + w[i];
}
long long dp[n+1];
dp[1] = 0;
for(int i = 2; i <= n; i++)
{
dp[i] = INF;
for(int j = 1; j < i; j++)
dp[i] = min(dp[i], dp[j] + sq(h[i] - h[j]) + w_sum[i-1] - w_sum[j]);
}
cout << dp[n] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3055 ms |
4392 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Execution timed out |
3055 ms |
4392 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |