#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;
#define task "test"
#define fastIO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i, l, r) for( ll i = (l) ; i < (r) ; i++ )
#define forb(i, r, l) for( ll i = (r) ; i >= (l) ; i-- )
#define sz(x) (int)x.size()
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define numBit(x) __builtin_popcount(x)
#define lb lower_bound
#define ub upper_bound
#define ar array
#define endl '\n'
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
const int N = 2e5 + 7;
const ll inf = 0x3f3f3f3f;
int n;
ll h[N], w[N], dp[N];
ll sum(int i, int j) {
return w[j] - w[i - 1];
}
ll sqr(int x) {
return 1LL * x * x;
}
int main() {
// fastIO;
scanf("%d", &n);
forw(i, 1, n + 1) scanf("%lld", &h[i]);
forw(i, 1, n + 1) scanf("%lld", &w[i]), w[i] += w[i - 1];
memset(dp, 0x3f, sizeof dp);
dp[1] = 0;
forw(i, 1, n + 1) forb(j, i - 1, 1)
dp[i] = min(dp[i], dp[j] + sqr(h[i] - h[j]) + sum(j + 1, i - 1));
cout << dp[n] << endl;
}
/*
Subtask 1: dp[i] = max(dp[j] + (hi - hj) ^ 2 + sum(w[i + 1]...w[j])
Subtask 2: Chọn 2 cây cầu sao cho h[1] + sum(2, i - 1) + h[i] + sum(i + 1, j - 1) + h[j] + sum(j + 1, n - 1) + h[n]
*/
Compilation message
building.cpp: In function 'int main()':
building.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
building.cpp:51:26: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | forw(i, 1, n + 1) scanf("%lld", &h[i]);
| ~~~~~^~~~~~~~~~~~~~~
building.cpp:52:26: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | forw(i, 1, n + 1) scanf("%lld", &w[i]), w[i] += w[i - 1];
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
2 ms |
1868 KB |
Output is correct |
5 |
Correct |
3 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3084 ms |
4404 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
2 ms |
1868 KB |
Output is correct |
5 |
Correct |
3 ms |
1868 KB |
Output is correct |
6 |
Execution timed out |
3084 ms |
4404 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |