orazma

d.cpp

Apr 25th, 2026
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <map>
  5. #include <climits>
  6. #include <set>
  7. #include <numeric>
  8. #include <cmath>
  9. #include <string>
  10. #include <cstdlib>
  11. #include <queue>
  12.  
  13. #define f first
  14. #define s second
  15. #define pb push_back
  16. #define mp make_pair
  17. #define ll long long
  18. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  19. using namespace std;
  20.  
  21. const int MAXN = 110;
  22. const ll INF = 1e18;
  23. ll dp[MAXN][MAXN], a[MAXN];
  24. int main() {
  25.     IOS;
  26.  
  27.     int L, n;
  28.  
  29.     cin >> L >> n;
  30.     a[1] = 0;
  31.     for (int i = 2; i <= n + 1; i++){
  32.         cin >> a[i];
  33.     }
  34.     a[n + 2] = L;
  35.     n += 2;
  36.  
  37.     for (int len = 1; len <= n; len++){
  38.         int x = 1;
  39.         int y = len;
  40.  
  41.  
  42.         while (x <= n && y <= n){
  43.             dp[x][y] = INF;
  44.             if (len <= 2){
  45.                 dp[x][y] = 0;
  46.             }
  47.             else{
  48.                 //cout << "!! " << x << ' ' << y << "\n";
  49.                 for (int k = x + 1; k < y; k++){
  50.                     dp[x][y] = min(dp[x][y], dp[x][k] + dp[k][y] + a[y] - a[x]);
  51.                 }
  52.             }
  53.             x++;
  54.             y++;
  55.         }
  56.     }
  57. /*
  58.     for (int i = 1; i <= n; i++){
  59.         for (int j = 1; j <= n; j++){
  60.             cout << dp[i][j] << ' ';
  61.         }
  62.         cout << "\n";
  63.     }
  64. */
  65.     cout << dp[1][n];
  66.  
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment