Working with boxes
Difficulty: Hard

A warehouse contains N boxes in a single line. The administrator of the warehouse wants to merge them to form a single stack. 

The workers can take 2 consecutive non-empty stacks of boxes i and i+1 (A single box is a stack as well) and merge them into a new stack at position i. This is counted as one operation. The time in minutes for the operation is the total weight (W1) multiplied by the number of boxes (N1) in one stack, plus, the total weight (W2) multiplied by the number of boxes (N2) in the other stack: W1 * N1 + W2 * N2.   
Two operations cannot happen at the same time. 

The administrator sets a deadline of T minutes, so the work must be finished in T minutes or less. The workers can use a crane to work faster. This crane will reduce the time taken for every operation by C minutes, if you pay to the operator a non-negative amount of dollars(C). So this operation takes time: W1 * N1 + W2 * N2 - C.

What is the minimum value of C that will ensure that the work is finished in no more than T minutes? 

**Update**  
If the operation time < 0 at any point of time, the the time taken is 0.  

**Input Format**  
The first line contains 2 numbers N and T . Next follows a line with N integers Wi   - the weight of each box.  

**Constraints**  

$1 \le N \le 100$  
$1 \le T \le 10^6$  
$1 \le W_i \le 100$  

**Output Format**  
Output just one line, the minimum value of C that will ensure that the work is finished in T minutes or less.

**Sample Input**

    4 15
    1 1 2 2

**Sample Output**

    1

**Explanation**  
<br>for C = 0:
<br>The optimal way to make a singe stack:
<br>**Step 1:** Stacking 1st and 2nd boxes in time 1*1+1*1. creates a new line of stacks with weights 2 2 2 and amount of boxes 2 1 1.

**Step 2:** Stacking 2nd and 3rd boxes in time 2*1 + 2*1 creates a new line of stacks with weights 2 4 and amount of boxes 2 2.

**Step 3:** Stacking 1st and 2nd boxes in time 2*2 + 4*2 creates a single stack.
The total time is 2 + 4 + 12 = 18. The dead line is 15 minutes and it is not reached.

for C = 1:
<br>**Step 1:** Stacking 1st and 2nd boxes in time 1*1+1*1 - 1 creates a new line of stacks with weights 2 2 2 and amount of boxes 2 1 1.

**Step 2:** Stacking 2nd and 3rd boxes in time 2*1 + 2*1 - 1 creates a new line of stacks with weights 2 4 and amount of boxes 2 2.

**Step 3:** Stacking 1st and 2nd boxes in time 2*2 + 4*2 - 1 creates a single stacks.
The total time is 1+3+11 = 15. Finally the deadline is reached for C = 1.

 

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    #include <map>
    #include <set>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <bitset>
    #include <vector>
    #include <ctime>
    #include <cmath>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cassert>
    #include <numeric>
    #include <iomanip>
    #include <sstream>
    #include <fstream>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long       LL;
    typedef pair<int, int>  PII;
    typedef pair<LL, LL>    PLL;
    typedef vector<PLL>  	VPLL;
    typedef vector<int>     VI;
    typedef vector<LL>      VL;
    typedef vector<PII>     VPII;
    typedef vector<VI>      VVI;
    typedef vector<VPII>    VVPII;
    #define MM(a,x) memset(a,x,sizeof(a));
    #define ALL(x)  (x).begin(), (x).end()
    #define P(x)	 cerr<<"["#x<<" = "<<(x)<<"]"<<endl;
    #define P2(x,y)	 cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<"]"<<endl;
    #define P3(x,y,z)cerr<<"["#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<"]"<<endl;
    #define PP(x,i)	 cerr<<"["#x<<i<<" = "<<x[i]<<"]"<<endl;
    #define TM(a,b)	 cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
    #define UN(v)   sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
    #define mp make_pair
    #define pb push_back
    #define x first
    #define y second
    struct _ {_() {ios_base::sync_with_stdio(0);}} _;
    template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
    template<class T> string tostring(T x, int len = 0, char c = '0') {stringstream ss; ss << x; string r = ss.str(); if(r.length() < len) r = string(len - r.length(), c) + r; return r;}
    template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w, ' '); if(a != b && (n == 0 || ++c % n)) cout << s; else cout << "\n"; cout.flush();}}
    template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
    template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
    const LL linf = 0x3f3f3f3f3f3f3f3fLL;
    const int inf = 0x3f3f3f3f;
    const int mod = int(1e9) + 7;
    const int N = 111;

    int n, T;
    int w[N];
    int s[N];

    int dp[N][N];

    int check(int c) {
        MM(dp, 0x3f);
        for(int i = 0; i < n; i++) dp[i][i] = 0;
        for(int l = 2; l <= n; l++)
            for(int i = 0; i < n; i++) {
                int j = i + l - 1;
                if(j >= n) break;

                for(int k = i; k < j; k++) {
                    int cost = (s[k + 1] - s[i]) * (k - i + 1) + (s[j + 1] - s[k + 1]) * (j - k);
                    cost = max(cost - c, 0);
                    chmin(dp[i][j], dp[i][k] + dp[k + 1][j] + cost);
                }
            }
        return dp[0][n - 1] <= T;
    }

    int main() {
        cin >> n >> T;
        assert(1 <= n && n <= 100);
        assert(1 <= T && T <= 1e6);
        for(int i = 0; i < n; i++) {
            cin >> w[i];
            assert(1 <= w[i] && w[i] <= 100);
            s[i + 1] = s[i] + w[i];
        }

        int L = 0, R = inf;

        while(L < R) {
            int M = (L + R) / 2;

            if(check(M)) {
                R = M;
            } else {
                L = M + 1;
            }
        }

        cout << L << endl;

        return 0;
    }