Submission #468997

#TimeUsernameProblemLanguageResultExecution timeMemory
468997paga2004Building Bridges (CEOI17_building)C++14
30 / 100
34 ms19500 KiB
#include <bits/stdc++.h>

#ifdef LOCAL
#define dbg(x) cerr << "dgb: " << x << "\n";
#else
#define dbg(x)
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,tune=native")
#endif

#define int int_fast64_t

using namespace std;

const int INF = 1e17;
vector<int> hs;
vector<int> w;
int n;
vector<vector<int>> memo(1001, vector<int>(1001, INF));

int f(int i, int l) {
  if (memo[i][l] != INF)
    return memo[i][l];
  if (i == 0)
    return (hs[l] - hs[0]) * (hs[l] - hs[0]);
  int best = f(i - 1, i) + (hs[i] - hs[l]) * (hs[i] - hs[l]);
  if (i != n - 1)
    best = min(best, f(i - 1, l) + w[i]);

  return memo[i][l] = best;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;
  hs.resize(n);
  w.resize(n);
  for (int &x : hs)
    cin >> x;
  for (int &x : w)
    cin >> x;

  cout << f(n - 1, n - 1) << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...