Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- using namespace std;
- #define forn(i, n) for(int i = 0; i < (int) (n); i++)
- #define forsn(i, s, n) for(int i = (s); i < (int) (n); i++)
- #define SQR(x) ((x)*(x))
- const int INF = 200000000;
- int dp[1001][1001];
- int cost[1001][1001];
- int n, b;
- int calc(int s, int m){
- if(m == 0) return cost[s][m];
- if(dp[s][m] == -1){
- dp[s][m] = INF;
- forsn(i, s+1, n)
- dp[s][m] = min(dp[s][m], cost[s][i] + calc(i, m-1));
- }
- return dp[s][n];
- }
- int main(){
- #ifdef ACM
- freopen("test.in", "r", stdin);
- #endif
- while(scanf("%d %d", &n, &b) && !(n == 0 || b == 0)){
- int depot[1001];
- forn(i, n) scanf("%d", &depot[i]);
- int acum[1001], sacum[1001];
- acum[0] = sacum[0] = 0;
- forsn(i, 1, n+1){
- acum[i] += depot[i-1];
- sacum[i] += depot[i-1]*depot[i-1];
- }
- forn(i, n+1) forsn(j, i+1, n+1)
- cost[i][j] = (SQR(acum[j]-acum[i]) - (sacum[j]-sacum[i]))/2;
- cout << calc(0, b) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment