http://icpc.upc.edu.cn/problem.php?cid=1673&pid=2
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
struct str
{
int l,s[505];
str()
{
l = 0;
memset(s, 0, sizeof(s));
}
} f[100][100], mm[100];
int a[100],n,m;
str ans;
str operator + (const str& a, const str& b){
str c;
int& l = c.l = max(a.l, b.l);
for (int i=1;i<=l;i++)
{
c.s[i] += a.s[i] + b.s[i];
c.s[i+1] += c.s[i] / 10000;
c.s[i] %= 10000;
}
if(c.s[l+1] > 0)
c.l++;
return c;
}
str operator * (const int& a, const str& b){
str c;
int &l=c.l=b.l;
for (int i=1;i<=l;i++)
{
c.s[i] += b.s[i] * a;
c.s[i+1] += c.s[i] / 10000;
c.s[i] %= 10000;
}
while(c.s[c.l+1] > 0)
{
c.l++;
c.s[c.l+1] += c.s[c.l] /10000;
c.s[c.l] %= 10000;
}
return c;
}
str max(str a, str b)
{
if(a.l > b.l)
return a;
else if(a.l < b.l)
return b;
else
{
for(int i = a.l; i >= 1; i--)
{
if(a.s[i] > b.s[i])
return a;
else if(a.s[i] < b.s[i])
return b;
}
return a;
}
}
int main()
{
mm[0].s[1] = 1;
mm[0].l = 1;
for (int i=1; i<=80; i++)
mm[i] = 2 * mm[i-1];
scanf("%d%d",&n,&m);
for (int k=1; k<=n; k++)
{
memset(f, 0, sizeof(f));
for (int i=1; i<=m; i++)
scanf("%d", &a[i]);
for (int i=1; i<=m; i++)
for(int j = m; j >= i; j--)
f[i][j] = max(f[i-1][j] +
a[i-1] * mm[m-j+i-1], f[i][j+1] + a[j+1] * mm[m-j+i-1]);
str max1;
for (int i=1;i<=m;i++)
max1 = max(max1, f[i][i] + a[i] * mm[m]);
ans =ans+max1;
}
int l=ans.l;
printf("%d", ans.s[l]);
for(int i = l - 1; i >= 1; i--)
printf("%04d", ans.s[i]);
printf("\n");
}