#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

template<typename T>
struct DP2
{
	const int N1, N2;
	vector<T> data;
	DP2(int N1, int N2, const T& t = T())
		: N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2)
		{ return data[ (i1*N2)+i2 ]; }
	void swap(DP2& rhs)
		{ data.swap(rhs.data); }
};

class SpellCards { public:
	int maxDamage(vector <int> level, vector <int> damage)
	{
		static const int IMPOSSIBLE = -99999999;

		int N = level.size();
		DP2<int> dp(N, N+1, IMPOSSIBLE);
		for(int w=1; w<=N; ++w)
		for(int s=0; s<N; ++s) {
			int L = level[s];
			int D = damage[s];

			DP2<int> ip(L+1, w, IMPOSSIBLE);
			ip(1,0) = D;
			for(int c=1; c<=L; ++c)
			for(int k=1; k<w; ++k) {
				int sk = (s+k) % N;

				int v = ip(c-1,k-1);
				for(int p=1; p<=k; ++p) {
					int sp = (s+p) % N;
					v = max(v, ip(c,p-1) + dp(sp, k-p+1));
				}
				ip(c,k) = v;
			}

			dp(s,w) = ip(L, w-1);
		}

		int theBest = 0;
		for(int s=0; s<N; ++s)
		{
			vector<int> best(N+1);
			for(int k=0; k<N; ++k) {
				best[k+1] = best[k];
				for(int p=0; p<=k; ++p)
					best[k+1] = max(best[k+1], best[p] + dp((s+p)%N, k-p+1));
			}
			theBest = max(theBest, best[N]);
		}
		return theBest;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, SpellCards().maxDamage(level, damage));}
int main(){

CASE(0)
	int level_[] = {1,1,1};
	  vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int damage_[] = {10,20,30};
	  vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); 
	int _ = 60; 
END
CASE(1)
	int level_[] = {3,3,3};
	  vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int damage_[] = {10,20,30};
	  vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); 
	int _ = 30; 
END
CASE(2)
	int level_[] = {4,4,4};
	  vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int damage_[] = {10,20,30};
	  vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); 
	int _ = 0; 
END
CASE(3)
	int level_[] = {50,1,50,1,50};
	  vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int damage_[] = {10,20,30,40,50};
	  vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); 
	int _ = 60; 
END
CASE(4)
	int level_[] = {2,1,1};
	  vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int damage_[] = {40,40,10};
	  vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); 
	int _ = 80; 
END
CASE(5)
	int level_[] = {1,2,1,1,3,2,1};
	  vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int damage_[] = {10,40,10,10,90,40,10};
	  vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); 
	int _ = 170; 
END
CASE(6)
	int level_[] = {1,2,2,3,1,4,2};
	  vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int damage_[] = {113,253,523,941,250,534,454};
	  vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); 
	int _ = 1918; 
END
CASE(7)
	int level_[] = {4,8,50,24,4,32,6,39,3,2,4,4,39,39,10,3,42,36,38,18,6,18,41,15,28,40,45,26,40,30,25,46,18,35,29,25,48,10,18,25,46,33,30,26,36,8,20,35,28,29};
	  vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int damage_[] = {8577,2855,8317,7176,295,7028,5317,7099,4642,29,2481,7249,5387,6266,6241,4550,9178,2375,1037,4438,4189,5886,5285,3583,2874,8484,4725,2096,7560,4057,988,5914,3460,1305,6223,2305,6229,2719,2404,6454,278,5242,2948,9607,1302,4263,3366,4925,8381,3692};
	  vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); 
	int _ = -2; 
END
CASE(8)
	int level_[] = {25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25};
	  vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int damage_[] = {8577,2855,8317,7176,295,7028,5317,7099,4642,29,2481,7249,5387,6266,6241,4550,9178,2375,1037,4438,4189,5886,5285,3583,2874,8484,4725,2096,7560,4057,988,5914,3460,1305,6223,2305,6229,2719,2404,6454,278,5242,2948,9607,1302,4263,3366,4925,8381,3692};
	  vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); 
	int _ = -2; 
END
}
// END CUT HERE
