Problem:
Problem G:
Gather on the Clock
There is a self-playing game called
Gather on the Clock
.
At the beginning of a game, a number of cards are placed on a ring. Each card is labeled by a
value.
In each step of a game, you pick up any one of the cards on the ring and put it on the next one
in clockwise order. You will gain the score by the difference of the two values. You should deal
with the two cards as one card in the later steps. You repeat this step until only one card is left
on the ring, and the score of the game is the sum of the gained scores.
Your task is to write a program that calculates the maximum score for the given initial state,
which specifies the values of cards and their placements on the ring.
The figure shown below is an example, in which the initial state is illustrated on the left, and
the subsequent states to the right. The picked cards are 1, 3 and 3, respectively, and the score
of this game is 6.
Figure 6: An illustrative play of Gather on the Clock
Input
The input consists of multiple test cases. The first line contains the number of cases. For each
case, a line describing a state follows. The line begins with an integer
n
(2 ≤
n
≤ 100), the
number of cards on the ring, and then
n
numbers describing the values of the cards follow. Note
that the values are given in clockwise order. You can assume all these numbers are non-negative
and do not exceed 100. They are separated by a single space character.
Output
For each case, output the maximum score in a line.
Sample Input
2
4 0 1 2 3
6 1 8 0 3 5 9
Output for the Sample Input
6
34

Submitted solutions:

code_snippet: #include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int Q, n;
int main() {
	cin >> Q;
	while (Q--) {
		cin >> n;
		vector<int> a(n);
		for (int i = 0; i < n; i++) cin >> a[i];
		int ret = 0;
		for (int i = 0; i < n; i++) {
			vector<vector<int> > dp(n, vector<int>(n + 1));
			for (int j = 2; j <= n; j++) {
				for (int k = 0; k <= n - j; k++) {
					int r = k + j;
					for (int l = k + 1; l < r; l++) {
						dp[k][r] = max(dp[k][r], dp[k][l] + dp[l][r] + abs(a[k] - a[l]));
					}
				}
			}
			ret = max(ret, dp[0][n]);
			vector<int> b(n);
			for (int j = 0; j < n; j++) b[j] = a[(j + 1) % n];
			a = b;
		}
		cout << ret << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 1400
memory_kb: 3224
final_rank: 19

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;
string root_sts = "C ,C#,D ,D#,E ,F ,F#,G ,G#,A ,A#,B ";

int solve(int l, int r, const vector<int>&nums, vector<vector<int>>&dp) {
	if (dp[l][r] != -1) {
	}
	else {
		if(l==r)dp[l][r]=0;
		else if(l+1==r)dp[l][r]=0;
		else {
			vector<int>memo(r-l);
			memo[0]=0;
			for (int x = 0; x < r - l-1; ++x) {
				for (int num = 1; num <= r-l-x-1; ++num) {
					int plus=abs(nums[l]-nums[l+x+1])+
						solve(l+x+1,l+x+num+1,nums,dp);
					memo[x+num]=max(memo[x+num],
						memo[x]+plus);
				}
			}
			dp[l][r]=memo[r-l-1];
		}
	}
	return dp[l][r];
}

int main() {
	int Q;cin>>Q;
	while (Q--) {
		int N;cin>>N;
		vector<int>nums(N);
		for(int i=0;i<N;++i)cin>>nums[i];
		nums.insert(nums.end(),nums.begin(),nums.end());
		vector<vector<int>>dp(2*N,vector<int>(2*N,-1));
		int ans=0;
		for (int i = 0; i < N; ++i) {
			ans=max(ans,solve(i,i+N,nums,dp));
		}
		cout<<ans<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 7480
memory_kb: 3196
final_rank: 20

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

void func(){

	int N;
	scanf("%d",&N);

	int table[N];
	for(int i = 0; i < N; i++)scanf("%d",&table[i]);

	int dp[N][N];

	for(int i = 0; i < N; i++){
		for(int k = 0; k < N; k++){
			dp[i][k] = 0;
		}
	}

	for(int length = 1; length <= N-1; length++){
		for(int i = 0; i < N; i++){
			for(int k = 0; k < length; k++){
				dp[i][length] = max(dp[i][length],dp[i][k]+dp[(i+k+1)%N][length-k-1]+abs(table[i]-table[(i+k+1)%N]));
			}
		}
	}

	int ans = 0;
	for(int i = 0; i < N; i++){
		ans = max(ans,dp[i][N-1]);
	}

	printf("%d\n",ans);

}

int main(){
	int num_case;
	scanf("%d",&num_case);

	for(int i = 0; i < num_case; i++)func();

	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 3172
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
bool used[111][111];
int dp[111][111];
int n;
int a[111];
int dfs(int l,int r){
  if(used[l][r]) return dp[l][r];
  if(l+1==r) return 0;
  used[l][r]=1;
  int res=0;
  for(int i=1;(l+i)%n!=r;i++){
    int m=(l+i)%n;
    res=max(res,dfs(l,m)+dfs(m,r)+abs(a[l]-a[m]));
  }
  //cout<<l<<" "<<r<<":"<<res<<endl;
  return dp[l][r]=res;
}
signed main(){
  int T;
  cin>>T;
  while(T--){
    cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    memset(used,0,sizeof(used));
    memset(dp,0,sizeof(dp));
    int ans=0;
    for(int i=0;i<n;i++) ans=max(ans,dfs(i,i));
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 520
memory_kb: 3208
final_rank: 18

code_snippet: #include<bits/stdc++.h>

using namespace std;

int dp[101][101],a[100];


int main(){
  int T;
  cin>>T;
  while(T--){
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
      cin>>a[i];

    memset(dp,0,sizeof(dp));
    int ans=0;
    for(int i=1;i<n;i++){
      for(int j=0;j<n;j++){
	for(int k=j;k<j+i;k++){
	  ans=max(ans,dp[j][(j+i)%n]=max(dp[j][(j+i)%n],dp[j][k%n]+dp[(k+1)%n][(j+i)%n]+abs(a[j]-a[(k+1)%n])));
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 3120
final_rank: 15

code_snippet: #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int n, a[107], dp[107][107], K[107][107];
int main() {
	int t; cin >> t;
	for (int p = 0; p < t; p++) {
		for (int i = 0; i < 11449; i++)dp[i / 107][i % 107] = 0;
		cin >> n; if (n == 0)break;
		for (int i = 0; i < n; i++)cin >> a[i];
		int maxn = 0;
		for (int u = 0; u < n; u++) {
			int G = a[0];
			for (int v = 1; v < n; v++)a[v - 1] = a[v];
			a[n - 1] = G;
			for (int v = 0; v < 11449; v++)dp[v / 107][v % 107] = 0;
			for (int v = 0; v < n; v++) {
				for (int w = 0; w < n; w++)K[v][w] = abs(a[v] - a[w]);
			}
			for (int v = 1; v <= n; v++) {
				for (int w = 0; w < n; w++) {
					int I = w, J = v + w;
					if (I >= J || J >= n)continue;
					for (int k = I; k < J; k++) {
						dp[I][J] = max(dp[I][J], dp[I][k] + dp[k + 1][J] + K[I][k + 1]);
					}
				}
			}
			maxn = max(maxn, dp[0][n - 1]);
		}
		cout << maxn << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 2790
memory_kb: 1248
final_rank: 13

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
#define MAX_N 100
 
int N,arr[MAX_N];
int dp[MAX_N][MAX_N];
 
int rec(int L,int R){
    if(L == R) return dp[L][R] = 0;
    if(dp[L][R] >= 0) return dp[L][R];
    int res = 0;
    int i = L;
    while(i != R){
	int j = i%N,k = (i+1)%N;
	res = max(res,rec(L,j)+rec(k,R)+abs(arr[L]-arr[k]));
	i++; i %= N;
    }
    return dp[L][R] = res;
}
 
int main(){
    int Tc;
    cin >> Tc;
    while(Tc--){
	cin >> N;
	for(int i = 0 ; i < N ; i++){
	    cin >> arr[i];
	}
	memset(dp,-1,sizeof(dp));
	int res = 0;
	for(int i = 0 ; i < N ; i++){
	    int j = (i+N-1)%N;
	    res = max(res,rec(i,j));
	}
	cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 460
memory_kb: 3100
final_rank: 17

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <complex>
#include <assert.h>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;

static const double EPS = 1e-12;

static const int tx[] = {+0,+1,+0,-1};
static const int ty[] = {-1,+0,+1,+0};

int dp[101][101];

int main(){
  int num_of_tests;
  while(~scanf("%d",&num_of_tests)){
    for(int test_i = 0; test_i < num_of_tests; test_i++){
      int num_of_cards;
      scanf("%d",&num_of_cards);
      vector<int> cards;
      for(int card_i = 0; card_i < num_of_cards; card_i++){
        int card;
        scanf("%d",&card);
        cards.push_back(card);
      }

      memset(dp,0,sizeof(dp));
      for(int len = 1; len <= cards.size(); len++){
        for(int pos = 0; pos < cards.size(); pos++){
          for(int mid = 0; mid < len; mid++){
            dp[pos][len] = max(dp[pos][len],
                               dp[pos][mid]
                               + dp[(pos + mid) % cards.size()][len - mid]
                               + abs(cards[pos] - cards[(pos + mid) % cards.size()]));
          }
        }
      }

      int res = 0;
      for(int pos = 0; pos < cards.size(); pos++){
        res = max(dp[pos][cards.size()],res);
      }
      printf("%d\n",res);
    }
  }
}
accuracy: 1
time_ms: 650
memory_kb: 1256
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define MAX_N 100

int N,arr[MAX_N];
int dp[MAX_N][MAX_N];

int rec(int L,int R){
  if(L == R){ return dp[L][R] = 0; }
  if(dp[L][R] >= 0){ return dp[L][R]; }
  int res = 0;
  int i = L;
  while(i != R){
    int j = i%N,k = (i+1)%N;
    res = max(res,rec(L,j)+rec(k,R)+abs(arr[L]-arr[k]));
    i++; i %= N;
  }
  return dp[L][R] = res;
}

int main(){
  int Tc;
  cin >> Tc;
  while(Tc--){
    cin >> N;
    for(int i = 0 ; i < N ; i++){
      cin >> arr[i];
    }
    memset(dp,-1,sizeof(dp));
    int res = 0;
    for(int i = 0 ; i < N ; i++){
      int j = (i+N-1)%N;
      res = max(res,rec(i,j));
    }
    cout << res << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 710
memory_kb: 1212
final_rank: 7

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int b[110];
int dp[110][110];
int tmp[110][110];
int n;
inline int ABS(int a){return max(a,-a);}
int ad[110][110];
int solve(int l,int r){
	if(~dp[l][r])return dp[l][r];
	if(l==r)return dp[l][r]=0;
	int sz=(r-l+n)%n;
	for(int i=0;i<=sz;i++){
		tmp[l][i]=-99999999;
	}
	tmp[l][0]=0;
	for(int i=0;i<sz;i++){
		dp[l][ad[l][i]]=tmp[l][i];
		int t=ABS(b[l]-b[ad[l+1][i]]);
		for(int j=sz-i;j>=1;j--){
			tmp[l][i+j]=max(tmp[l][i+j],tmp[l][i]+t+solve(ad[l][i+1]%n,ad[l][i+j]));
		}
	}
	//printf("%d %d: %d\n",l,r,tmp[(r-l+n)%n]);
	return dp[l][r]=tmp[l][sz];
}
int main(){
	int t;scanf("%d",&t);
	while(t--){
		int a;scanf("%d",&a);for(int i=0;i<a;i++)scanf("%d",b+i);
		n=a;
		for(int i=0;i<110;i++)for(int j=0;j<110;j++)ad[i][j]=(i+j)%n;
		int ret=0;
		for(int i=0;i<a;i++)for(int j=0;j<a;j++)dp[i][j]=-1;
		for(int i=0;i<a;i++){
			ret=max(ret,solve(i,(i+a-1)%a));
		}
		printf("%d\n",ret);
	}
}
accuracy: 1
time_ms: 6670
memory_kb: 1156
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
 
int main() {
  int T;
  cin >> T;
  while(T--) {
    int n;
    cin >> n;
    int a[n];
    for(int i=0; i<n; i++) cin >> a[i];
    int dp[n][n];
    memset(dp,0,sizeof(dp));
    for(int i=0; i<n; i++) dp[i][(i+1)%n]=abs(a[i]-a[(i+1)%n]);
    int ans=0;
    for(int i=2; i<n; i++) {
      for(int j=0; j<n; j++) {
    for(int k=j; k<i+j+1; k++) {
      int x=(i+j)%n;
      if(k==j) dp[j][x]=max(dp[j][x],dp[j][(i+j-1)%n]+abs(a[j]-a[x]));
      else dp[j][x]=max(dp[j][x],dp[j][k%n]+dp[k%n][x]);
      ans=max(ans,dp[j][x]);
    }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 1204
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;

const int INF = 1000000000;

int main(){
    int T;
    cin >> T;
    while(T--) {
        int n;
        cin >> n;
        vector<int> a(n);
        REP(i, n) cin >> a[i];

        int dp[101][101] = {};
        REP(i, n) REP(j, n) dp[i][j] = -INF;
        REP(i, n) dp[i][i] = 0;
        for(int l = 1; l < n; l++) {
            for(int i = 0; i < n; i++) {
                int j = (i + l) % n;
                int max_k = 0;
                for(int h = 0; h < l; h++) {
                    int k = (i + h) % n;
                    int k2 = (i + h + 1) % n;
                    int val = dp[i][k] + dp[k2][j] + abs(a[i] - a[k2]);
                    if(dp[i][j] < val) {
                        max_k = k;
                        dp[i][j] = val;
                    }
                }
                //printf("dp[%d][%d] = %d (dp[%d][%d] + dp[%d][%d])\n", i, j, dp[i][j], i, max_k, (max_k + 1) % n, j);
            }
        }

        int ans = -INF;
        REP(i, n) ans = max(ans, dp[i][(i + n - 1) % n]);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 1240
final_rank: 5

code_snippet: #include<iostream>
#include<algorithm>

using namespace std;

int mem[200][200];
bool p[200][200];
int a[200];

int rec(int f,int l){
  if(l-f==1)return 0;
  if(p[l][f]++)return mem[l][f];
  mem[l][f]=0;
  for(int m=f+1;m<l;m++){
    mem[l][f]=max(mem[l][f],rec(f,m)+rec(m,l)+abs(a[f]-a[m]));
  }
  return mem[l][f];
}

int main(){
  int T;
  cin>>T;
  while(T--){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
      cin>>a[i];
      a[i+n]=a[i];
    }
    int ans=0;
    fill(p[0],p[200],0);
    for(int i=0;i<n;i++){
      ans=max(ans,rec(i,i+n));
    }
    cout<<ans<<endl;
  }
}
accuracy: 1
time_ms: 440
memory_kb: 1360
final_rank: 9

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>

#define curr(P, i) P[(i) % P.size()]
#define next(P, i) P[(i+1) % P.size()]
#define prev(P, i) P[(i+P.size()-1) % P.size()]
#define Next(a,i) ((i)+1)%(a).size()
#define Curr(a,i) (i)%(a).size()
#define Prev(a, i) (i+a.size()-1) % a.size()

using namespace std;

int main(void){

  int n,m,x,dp[101][101];
  vector<int>a;

  cin >> n;

  while(n--){
    a.clear();

    for(int i=0;i<101;i++)
      for(int j=0;j<101;j++)dp[i][j]=0;

    cin >> m;
    for(int i=0;i<m;i++){
      cin >> x;
      a.push_back(x);
    }
    
    for(int i=0;i<m;i++){
      for(int j=0;j<m;j++){
	for(int k=0;k<i;k++){
	  dp[Curr(a,j)][Curr(a,i)]=max(dp[j][i],dp[Curr(a,j)][Curr(a,k)]+dp[Next(a,j+k)][Prev(a,i-k)]+abs(curr(a,j)-next(a,j+k))); 
	}
      }
    }
    int ans=0;
    for(int i=0;i<m;i++)
      for(int j=0;j<m;j++)
      ans=max(ans,dp[i][j]);
    
    cout << ans << endl;
    
  }

  return 0;
}
accuracy: 1
time_ms: 920
memory_kb: 1256
final_rank: 12

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>

#define curr(P, i) P[(i) % P.size()]
#define next(P, i) P[(i+1) % P.size()]
#define prev(P, i) P[(i+P.size()-1) % P.size()]
#define Next(a,i) ((i)+1)%(a).size()
#define Curr(a,i) (i)%(a).size()
#define Prev(a, i) (i+a.size()-1) % a.size()

using namespace std;

int main(void){

  int n,m,x,dp[101][101];
  vector<int>a;

  cin >> n;

  while(n--){
    a.clear();

    for(int i=0;i<101;i++)
      for(int j=0;j<101;j++)dp[i][j]=0;

    cin >> m;
    for(int i=0;i<m;i++){
      cin >> x;
      a.push_back(x);
    }
    
    for(int i=1;i<m;i++){
      for(int j=0;j<m;j++){
	for(int k=0;k<i;k++){
	  dp[Curr(a,j)][Curr(a,i)]=max(dp[j][i],dp[Curr(a,j)][Curr(a,k)]+dp[Next(a,j+k)][Prev(a,i-k)]+abs(curr(a,j)-next(a,j+k))); 
	}
      }
    }
    int ans=0;
    for(int i=0;i<m;i++)
      for(int j=0;j<m;j++)
      ans=max(ans,dp[i][j]);
    
    cout << ans << endl;
    
  }

  return 0;
}
accuracy: 1
time_ms: 930
memory_kb: 1252
final_rank: 10

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>

#define curr(P, i) P[(i) % P.size()]
#define next(P, i) P[(i+1) % P.size()]
#define prev(P, i) P[(i+P.size()-1) % P.size()]
#define Next(a,i) ((i)+1)%(a).size()
#define Curr(a,i) (i)%(a).size()
#define Prev(a, i) (i+a.size()-1) % a.size()

using namespace std;

int main(void){

  int n,m,x,dp[101][101];
  vector<int>a;

  cin >> n;

  while(n--){
    a.clear();

    for(int i=0;i<101;i++)
      for(int j=0;j<101;j++)dp[i][j]=0;

    cin >> m;
    for(int i=0;i<m;i++){
      cin >> x;
      a.push_back(x);
    }

    //for(int i=0;i<m;i++)dp[i][Next(a,i)]=abs(curr(a,i)-next(a,i));
    
    for(int k=1;k<m;k++){
      for(int i=0;i<m;i++){
	for(int j=0;j<k;j++){
	  dp[Curr(a,i)][Curr(a,k)]=max(dp[i][k],dp[Curr(a,i)][Curr(a,j)]+dp[Next(a,i+j)][Prev(a,k-j)]+abs(curr(a,i)-next(a,i+j)));
						  
	}
	
      }
    }
    int ans=0;
    for(int i=0;i<m;i++)
      for(int j=0;j<m;j++)
      ans=max(ans,dp[i][j]);
    
    cout << ans << endl;
    
  }

  return 0;
}
accuracy: 1
time_ms: 930
memory_kb: 1252
final_rank: 10

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstdio>
using namespace std;

int main()
{
  int N;
  scanf("%d",&N);
  while(N-- > 0)
    {
      int n;
      cin >> n;
      int dp[n][n];
      int vec[n];
      for(int i=0;i<n;i++)
	{
	  cin >> vec[i];
	  for(int j=0;j<n;j++)
	    dp[i][j] = 0;
	}
      
      int mex = 0;
      for(int i=0;i<n;i++)
	dp[i][(i+1)%n] = abs(vec[i]-vec[(i+1)%n]),mex = max(mex,dp[i][(i+1)%n]);
      
      for(int i=2;i<n;i++)//幅
	{
	  for(int j=0;j<n;j++)//index
	    {
	      for(int k=j;k<=j+i-1;k++)//中間地点
		{
		  if(k == j)
		    {
		      dp[j][(j+i)%n] = max(dp[j][(j+i)%n],dp[j][(j+i-1)%n]+abs(vec[j]-vec[(j+i)%n]));
		      mex = max(mex,dp[j][(j+i)%n]);
		      continue;
		    }
		  dp[j][(j+i)%n] = max(dp[j][(j+i)%n],
				       dp[k%n][(j+i)%n]+dp[j][k%n]);
		}
	      mex = max(mex,dp[j][(j+i)%n]);
	    }
	}

      cout << mex << endl;


    }
  return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 1276
final_rank: 6

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
  int N;
  cin >> N;
  while(N-- > 0)
    {
      int n;
      cin >> n;
      int dp[n][n];
      vector<int> vec(n);
      for(int i=0;i<n;i++)
	{
	  cin >> vec[i];
	  for(int j=0;j<n;j++)
	    dp[i][j] = 0;
	}

      int mex = 0;
      for(int i=0;i<n;i++)
	dp[i][(i+1)%n] = abs(vec[i]-vec[(i+1)%n]),mex = max(mex,dp[i][(i+1)%n]);

      for(int i=2;i<n;i++)//幅
	{
	  for(int j=0;j<n;j++)//index
	    {
	      for(int k=j;k<=j+i-1;k++)//中間地点
		{
		  if(k == j)
		    {
		      dp[j][(j+i)%n] = max(dp[j][(j+i)%n],dp[j][(j+i-1)%n]+abs(vec[j]-vec[(j+i)%n]));
		      mex = max(mex,dp[j][(j+i)%n]);
		      continue;
		    }
		  dp[j][(j+i)%n] = max(dp[j][(j+i)%n],
				       dp[k%n][(j+i)%n]+dp[j][k%n]);
		}
	      mex = max(mex,dp[j][(j+i)%n]);
	    }
	}

      cout << mex << endl;


    }
  return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 1232
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>

using namespace std;

main(){
  int T, dp[100][100], n, d[100], s;
  cin >> T;
  while(T--){
    cin >> n;
    for(int i=0;i<n;i++) cin >> d[i];
    fill(dp[0], dp[n], 0);
    for(int k=1;k<n;k++){
      for(int i=0;i<n;i++){
        for(int j=0;j<k;j++){
          s = (i+k)%n;
          dp[i][s] = max(dp[i][s], dp[i][(i+j)%n] + dp[(i+j+1)%n][s] + abs(d[i] - d[(i+j+1)%n]));
        }
      }
    }
    int ans = 0;
    for(int i=0;i<n;i++) ans = max(ans, dp[i][(i+n-1)%n]);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 340
memory_kb: 1196
final_rank: 4

code_snippet: #include <iostream>
#include <algorithm>

using namespace std;

main(){
  int T, dp[100][100], n, d[100], s, t;
  cin >> T;
  while(T--){
    cin >> n;
    for(int i=0;i<n;i++) cin >> d[i];
    fill(dp[0], dp[n], 0);
    for(int k=1;k<n;k++){
      for(int i=0;i<n;i++){
        for(int j=0;j<k;j++){
          s = (i+k)%n;
          t = (i+j+1)%n;
          dp[i][s] = max(dp[i][s], dp[i][(i+j)%n] + dp[t][s] + abs(d[i] - d[t]));
        }
      }
    }
    int ans = 0;
    for(int i=0;i<n;i++) ans = max(ans, dp[i][(i+n-1)%n]);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 330
memory_kb: 1196
final_rank: 3