giabinh

CatThep

Sep 28th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, len, f[102][102], a[102];
  6.  
  7. int cut(int l, int r) {
  8.     if (l+1 == r) return 0;
  9.     if (f[l][r] != -1) return(f[l][r]);
  10.  
  11.     int res = 1003;
  12.     for (int i=l+1; i<r; ++i)
  13.         res = min(res, cut(l, i) + cut(i, r) + a[r] - a[l]);
  14.     f[l][r] = res;
  15.     return(res);
  16. }
  17.  
  18. int main() {
  19.     freopen("CatThep.inp", "r", stdin);
  20.  
  21.     scanf("%d%d", &len, &n);
  22.     for (int i=1; i<=n; ++i) scanf("%d", &a[i]);
  23.     a[0] = 0; a[n+1] = len;
  24.     memset(f, -1, sizeof(f));
  25.  
  26.     printf("%d", cut(0, n+1));
  27.  
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment