-
Notifications
You must be signed in to change notification settings - Fork 0
/
CODEM4.cpp
62 lines (62 loc) · 1.16 KB
/
CODEM4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define FOR(i,j,k) for(int i=j;i<k;i++)
using namespace std;
int array[100],sum;
int dp[100][100];
int max(int a,int b)
{
return a > b ? a : b;
}
int min(int a,int b)
{
return a < b ? a : b;
}
// if both plays optimals.....
int dpMIN(int i,int j)
{
if(i>j)
return 0;
if(dp[i][j]!=-1)
return dp[i][j];
int ans = max(array[i]+min( dpMIN(i+2,j) , dpMIN(i+1,j-1) ) , array[j] + min(dpMIN(i,j-2),dpMIN(i+1,j-1)));
//cout << ans << " ";
return dp[i][j]= ans;
}
int dpMAX(int i,int j)
{
if(i>j)
return 0;
if(dp[i][j]!=-1)
return dp[i][j];
int ans = max(array[i] + max(dpMAX(i+1,j-1),dpMAX(i+2,j)),array[j] + max(dpMAX(i+1,j-1),dpMAX(i,j-2)));
dp[i][j] = ans;
return ans;
}
int main()
{
int t,n;
si(t);
while(t--)
{
si(n);
sum=0;
memset(dp,-1,sizeof(dp));
FOR(i,0,n)
{
si(array[i]);
sum+=array[i];
}
int ans1 = dpMAX(0,n-1);
memset(dp,-1,sizeof(dp));
int ans2 = dpMIN(0,n-1);
/*
FOR(i,0,n)
FOR(j,0,n)
cout << dp[i][j] << " ";
cout << endl;
*/
cout << ans1 << " " << ans2 << endl;
}
return 0;
}