比赛 NOIP2025模拟赛2 评测结果 RRRRRRRRRRRRRRRR
题目名称 桥梁建设 最终得分 0
用户昵称 wdsjl 运行时间 33.607 s
代码语言 C++ 内存使用 3.64 MiB
提交时间 2025-11-25 11:48:30
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 1010;

int h[N], w[N];
int dp[N];

signed main() {
    freopen("streetr.in","r",stdin);
    freopen("streetr.out","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> h[i];
    }
    int total_w = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> w[i];
        total_w += w[i];
    }
    memset(dp,0x3f,sizeof(dp));
    dp[1] = -w[1];
    for (int i = 2; i <= n; ++i) {
        for (int j = 1; j < i; ++j) {
            if (dp[j] != 0x3f3f3f3f) {
                int cost = (h[i] - h[j]) * (h[i] - h[j]);
                dp[i] = min(dp[i], dp[j] + cost);
            }
        }
        dp[i] -= w[i]; 
    }
    cout << dp[n] + total_w << endl;
    return 0;
}