zoj2059

双塔dp

#include<iostream>
#include<cmath>
using namespace std;
int heigh[2005],hh[2005];
int main()
{
  int i,n,h;
  while(cin>>n&&n>=0)
  {
    for(i=1;i<=2000;i++) heigh[i]=hh[i]=-1;
    heigh[0]=hh[0]=0;
    while(n--)
    {
      cin>>h;
      for(i=0;i<=2000;i++)
      {
        if(heigh[i]!=-1)
        {
          if(h+i<=2000) hh[h+i]=max(hh[h+i],heigh[i]);
          if(h<i) hh[i-h]=max(hh[i-h],heigh[i]+h);
          else hh[h-i]=max(hh[h-i],heigh[i]+i);
        }
      }
      for(i=0;i<=2000;i++) heigh[i]=hh[i];
    }
    if(heigh[0]!=0) cout<<heigh[0]<<endl;
    else cout<<"Sorry"<<endl;
  }
}

你可能感兴趣的:(zoj2059)