-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPickIt.java
47 lines (44 loc) · 1.17 KB
/
PickIt.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Class4;
import java.util.*;
/**
*
* @author EricaWang
*/
public class PickIt {
/**
* @param args the command line arguments
*/
static int [][] dp = new int [205][205];
static int [] a = new int [205];
public static int fun(int l, int r){
if(dp[l][r]!=0){
return dp[l][r];
}
if(l+1==r){
return dp[l][r]=0;
}
for(int k = l+1; k<r; k++){
dp[l][r]=Math.max(dp[l][r],fun(l,k)+fun(k,r)+a[l]+a[k]+a[r]);
}
return dp[l][r];
}
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
int n = input.nextInt();
while(n!=0){
dp = new int [205][205];
a = new int [205];
for(int i = 1; i<=n; i++){
a[i]=input.nextInt();
}
System.out.println(fun(1,n));
n = input.nextInt();
}
}
}