Rahul and Chemistry
Difficulty: Hard

Rahul hates Chemistry and he wants to get his experiment done as quickly as possible.		
The experiment is to combine adjacent elements until you get one element with minimum cost. Cost of combining elements is explained later.		
So you are given an array with $N$ elements where $w_i$ is the weight of the ith element and the elements are arranged linearly.				
Rahul can fuse any two 2 neighbouring elements at a time and the cost of fusing $i^{th}$ element and $(i+1)^{th}$ element is $max(w_i,w_{i+1})$ and the weight of the new element is $w_i + w_{i+1}$.		
Now tell Rahul the minumum cumulative cost  of fusing all the elements into $1$ element. Since the answer can be large  print answer modulo $1000000007$.		
		
**Input Format:**		
The first line would contain $N$ the number of elements.		
Next line would contain $N$ space separated integers where the $i^{th}$ integer denotes $w_i$ weight of $i^{th}$ element.		

**Output Format:**		
A single line containing the required answer i.e the minimum cumulative cost modulo $1000000007$ to merge all elements into $1$ element.

**Constraints:**		  
$1 \le N \le 500$		
$0 \le w_i \le 1000000$		


**Sample Input #00**		
  
    3
    1 4 2
	
**Sample Output #00**		

    9
    	
**Explanation**  
_Sample #00:_		  
There are $2$ possible ways to combine the elements.		
		
-	 Combine $2^{nd}$ and $3^{rd}$ element. Cost = $max(4,2) = 4$. Weight of new element = $6$. Array now looks like		
	{1, 6}		
   Combine 1st and 2nd element. Cost= max(1,6) = 6. Total Cost=4+6=10.

-	 Combine $1^{st}$ and $2^{nd}$ element. Cost = $max(1,4) = 4$. Weight of new element = $5$. Array now looks like		
	{5, 2}		
   Combine 1st and 2nd element. Cost= max(5,2) = 5. Total Cost=4+5=9.
So the answer is 9.

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<vector>
    #include<queue>
    #include<map>
    #include<set>
    #include<algorithm>
    #include<cstring>
    #include<numeric>
    #include<string>
    #include<functional>
    #include<complex>

    using namespace std;

    #define ll long long int 
    #define loop(i,a,b) for(int i=a;i<b;i++)

    #define mod 1000000007
    #define inf 999999999999999999
    int n;
    ll dp[510][510];
    ll a[510];
    ll cs[510];
    void Scan()
    {	
        cin>>n;
        loop(i,0,n) cin>>a[i];
        loop(i,0,n)
            loop(j,0,n)
                dp[i][j]=-1;
        return;
    }

    void pre()
    {
        cs[0]=0;
        for(int i=1;i<=n;i++)
            cs[i]=cs[i-1]+a[i-1];
    }

    ll Solve(int i,int j)
    {
        if(dp[i][j]!=-1) return dp[i][j];
        if(i==j) return dp[i][j]=0;
        ll res=inf;
        for(int k=i;k<j;k++)
        {
            ll p=Solve(i,k);
            p+=Solve(k+1,j); if(p>=mod) p%=mod;
            p+=max((cs[k+1]-cs[i]),(cs[j+1]-cs[k+1])); if(p>=mod) p%=mod;
            res=min(res,p);
        }
        return dp[i][j]=res;
    }

    void Out()
    {
        pre();
        cout<<Solve(0,n-1)<<endl;
        return;
    }
    int main()
    {
        Scan();
        Out();
        return 0;
    }