Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3515 Accepted Submission(s): 1429
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int dp[1000][1000]; 6 7 int getmax(int m, int n){ 8 return m > n ? m : n; 9 } 10 int getmin(int m, int n){ 11 return m > n ? n : m; 12 } 13 14 int main(){ 15 int n, i, j, k, money[3], people[1000], sum, min, maxnum; 16 while(scanf("%d", &n) && n){ 17 //getchar(); 18 for(i = 0; i < 3; i ++){ 19 scanf("%d", &money[i]); 20 } 21 maxnum = -1; 22 for(i = 0; i < n; i ++){ 23 scanf("%d", &people[i]); 24 maxnum = getmax(maxnum, people[i]); 25 } 26 //printf("sdh"); 27 memset(dp, 0, sizeof(dp)); 28 for(i = people[0]; i <= maxnum; i ++){ 29 dp[1][i] = (money[0] + money[1]) * i; 30 } 31 for(i = 2; i <= n; i ++){ 32 for(j = people[i - 1]; j <= maxnum; j ++){ 33 min = 0xfffffff; 34 for(k = people[i - 2]; k <= maxnum; k ++){ 35 if(k <= j){ 36 min = getmin(min, dp[i - 1][k] + money[0] * (j - k) + j * money[1]); 37 } 38 else{ 39 min = getmin(min, dp[i - 1][k] + money[2] * (k - j) + j * money[1]); 40 } 41 } 42 dp[i][j] = min; 43 } 44 } 45 min = 0xfffffff; 46 for(i = people[n - 1]; i <= maxnum; i ++){ 47 min = getmin(min, dp[n][i]); 48 } 49 printf("%d\n", min); 50 } 51 return 0; 52 }