状态转移方程有点难想。看了题解才知道的。
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
const int maxn=1050;
int n;
int a[maxn];
int f[maxn][maxn];
struct Node{
int d,v,x,y;
};
Node path[maxn][maxn];
void print(int i,int j){
if(!i) return ;
print(path[i][j].d,path[i][j].v);
int x=path[i][j].x,y=path[i][j].y;
if(a[x]&&a[y]) cout<<x<<' '<<y<<endl;
else if(a[x]) cout<<x<<endl;
else cout<<y<<endl;
}
void solve(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int m=(n+1)/2;
memset(f,0x3f,sizeof(f));
f[1][1]=max(a[2],a[3]);
f[1][2]=max(a[1],a[3]);
f[1][3]=max(a[1],a[2]);
path[1][1]={0,0,2,3};
path[1][2]={0,0,1,3};
path[1][3]={0,0,1,2};
for(int i=1;i<m;i++){
for(int j=1;j<=2*i+1&&j<=n;j++){
if(f[i+1][2*i+2]>f[i][j]+max(a[j],a[2*i+3])){
f[i+1][2*i+2]=min(f[i+1][2*i+2],f[i][j]+max(a[j],a[2*i+3]));
path[i+1][2*i+2]={i,j,j,2*i+3};
}
if(f[i+1][2*i+3]>f[i][j]+max(a[j],a[2*i+2])){
f[i+1][2*i+3]=min(f[i+1][2*i+3],f[i][j]+max(a[j],a[2*i+2]));
path[i+1][2*i+3]={i,j,j,2*i+2};
}
if(f[i+1][j]>f[i][j]+max(a[2*i+2],a[2*i+3])){
f[i+1][j]=min(f[i+1][j],f[i][j]+max(a[2*i+2],a[2*i+3]));
path[i+1][j]={i,j,2*i+2,2*i+3};
}
}
}
cout<<f[m][n+1]<<endl;
print(m,n+1);
}
int main(){
int t;t=1;
while(t--) solve();
return 0;
}