Submission #1307538

#TimeUsernameProblemLanguageResultExecution timeMemory
1307538chithanhnguyenBuilding Bridges (CEOI17_building)C++20
30 / 100
3093 ms3440 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1ll << (x)))

#define debug(a, l, r) for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';

const int MAXN = 1e5 + 5;
const int INF  = 1e18 + 5;
int n, h[MAXN], w[MAXN], dp[MAXN], pref[MAXN];

void init() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> h[i];
    for (int i = 1; i <= n; ++i) {
        cin >> w[i];
        pref[i] = pref[i - 1] + w[i];
    }
    fill(dp + 1, dp + n + 1, INF);
    dp[1] = 0;
}

namespace brute{
    void solve() {
        for (int i = 2; i <= n; ++i) {
            for (int j = i - 1; j >= 1; --j) {
                int cur = dp[j] + (h[i] - h[j]) * (h[i] - h[j]) + pref[i - 1] - pref[j];
                dp[i] = min(dp[i], cur);
            }
        }

        cout << dp[n];
    }
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    brute::solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...