Salvens

E

Jul 29th, 2023
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int long long
  6.  
  7. const long long INF = 1e18 + 7;
  8. const int MAXN = 1e2 + 100;
  9. const int N = 1e5 + 10;
  10.  
  11. int beam, n;
  12. vector<int> a;
  13. array<array<int, MAXN>, MAXN> dp;
  14.  
  15. int rec(int l, int r) {
  16.     if (dp[l][r] != INF) {
  17.         return dp[l][r];
  18.     }
  19.     int ans = INF;
  20.     for (int m = l + 1; m < r; ++m) {
  21.         ans = min(ans, rec(l, m) + rec(m, r) + a[r] - a[l]);
  22.     }
  23.     return dp[l][r] = ans;
  24. }
  25.  
  26. signed main() {
  27.     ios_base::sync_with_stdio(false);
  28.     cin.tie(nullptr);
  29.     cout.tie(nullptr);
  30.     cin >> beam >> n;
  31.     a.resize(n + 2);
  32.     a[0] = 0;
  33.     for (int i = 1; i <= n; ++i) {
  34.         cin >> a[i];
  35.     }
  36.     a[n + 1] = beam;
  37.     n += 2;
  38.     for (int i = 0; i < n; ++i) {
  39.         for (int j = 0; j < n; ++j) {
  40.             dp[i][j] = INF;
  41.         }
  42.     }
  43.     for (int i = 0; i < n - 1; ++i) {
  44.         dp[i][i + 1] = 0;
  45.     }
  46.     cout << rec(0, n - 1) << '\n';
  47. }
Advertisement
Add Comment
Please, Sign In to add comment