### NYOJ 737 石子合並（一） （區間DP+平行四邊形優化）

``#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL long long#define pi acos(-1.0)//#pragma comment(linker, "/STACK:1024000000")const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-3;const int MAXN=40000+10;int dp[300][300], sum[300];int main(){        int n, i, j, k, len, x;        while(scanf("%d",&n)!=EOF) {                sum[0]=0;                memset(dp,INF,sizeof(dp));                for(i=1; i<=n; i++) {                        scanf("%d",&x);                        sum[i]=sum[i-1]+x;                        dp[i][i]=0;                }                for(len=2; len<=n; len++) {                        for(i=1; i<=n-len+1; i++) {                                j=i+len-1;                                for(k=i+1; k<=j; k++) {                                        dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k][j]+sum[j]-sum[i-1]);                                }                        }                }                printf("%d\n",dp[1][n]);        }        return 0;}``

``#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL long long#define pi acos(-1.0)//#pragma comment(linker, "/STACK:1024000000")const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-3;const int MAXN=40000+10;int dp[300][300], sum[300], s[300][300];int main(){        int n, i, j, k, len, x;        while(scanf("%d",&n)!=EOF){                sum[0]=0;                memset(dp,INF,sizeof(dp));                for(i=1;i<=n;i++){                        scanf("%d",&x);                        sum[i]=sum[i-1]+x;                        dp[i][i]=0;                        s[i][i]=i;                }                for(len=2;len<=n;len++){                        for(i=1;i<=n-len+1;i++){                                j=i+len-1;                                for(k=s[i][j-1];k<=s[i+1][j];k++){                                        if(dp[i][j]>dp[i][k-1]+dp[k][j]+sum[j]-sum[i-1]){                                                dp[i][j]=dp[i][k-1]+dp[k][j]+sum[j]-sum[i-1];                                                s[i][j]=k;                                        }                                }                        }                }                printf("%d\n",dp[1][n]);        }        return 0;}``