tcbpg

Untitled

Aug 21st, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. #define forn(i, n) for(int i = 0; i < (int) (n); i++)
  7. #define forsn(i, s, n) for(int i = (s); i < (int) (n); i++)
  8. #define SQR(x) ((x)*(x))
  9.  
  10. const int INF = 200000000;
  11. int dp[1001][1001];
  12. int cost[1001][1001];
  13.  
  14. int n, b;
  15.  
  16. int calc(int s, int m){
  17. if(m == 0) return cost[s][m];
  18.  
  19. if(dp[s][m] == -1){
  20. dp[s][m] = INF;
  21. forsn(i, s+1, n)
  22. dp[s][m] = min(dp[s][m], cost[s][i] + calc(i, m-1));
  23. }
  24.  
  25. return dp[s][n];
  26. }
  27.  
  28. int main(){
  29. #ifdef ACM
  30. freopen("test.in", "r", stdin);
  31. #endif
  32.  
  33. while(scanf("%d %d", &n, &b) && !(n == 0 || b == 0)){
  34. int depot[1001];
  35.  
  36. forn(i, n) scanf("%d", &depot[i]);
  37.  
  38. int acum[1001], sacum[1001];
  39.  
  40. acum[0] = sacum[0] = 0;
  41. forsn(i, 1, n+1){
  42. acum[i] += depot[i-1];
  43. sacum[i] += depot[i-1]*depot[i-1];
  44. }
  45.  
  46. forn(i, n+1) forsn(j, i+1, n+1)
  47. cost[i][j] = (SQR(acum[j]-acum[i]) - (sacum[j]-sacum[i]))/2;
  48.  
  49. cout << calc(0, b) << endl;
  50. }
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment