description: Alice has an integer sequence a of length n and all elements are different. She will choose a subsequence of a of length m, and defines the value of a subsequence a_{b_1},a_{b_2},…,a_{b_m} as $$$∑_{i = 1}^m (m ⋅ a_{b_i}) - ∑_{i = 1}^m ∑_{j = 1}^m f(min(b_i, b_j), max(b_i, b_j)), where f(i, j) denotes \min(a_i, a_{i + 1}, \ldots, a_j)$$$.
  
  Alice wants you to help her to maximize the value of the subsequence she choose.
  
  A sequence s is a subsequence of a sequence t if s can be obtained from t by deletion of several (possibly, zero or all) elements.
  
  Input
  
  The first line contains two integers n and m (1 ≤ m ≤ n ≤ 4000).
  
  The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i < 2^{31}).
  
  Output
  
  Print the maximal value Alice can get.
  
  Examples
  
  Input
  
  
  6 4
  15 2 18 12 13 4
  
  
  Output
  
  
  100
  
  
  Input
  
  
  11 5
  9 3 7 1 8 12 10 20 15 18 5
  
  
  Output
  
  
  176
  
  
  Input
  
  
  1 1
  114514
  
  
  Output
  
  
  0
  
  
  Input
  
  
  2 1
  666 888
  
  
  Output
  
  
  0
  
  Note
  
  In the first example, Alice can choose the subsequence [15, 2, 18, 13], which has the value 4 ⋅ (15 + 2 + 18 + 13) - (15 + 2 + 2 + 2) - (2 + 2 + 2 + 2) - (2 + 2 + 18 + 12) - (2 + 2 + 12 + 13) = 100. In the second example, there are a variety of subsequences with value 176, and one of them is [9, 7, 12, 20, 18].
solution: #include <bits/stdc++.h>
  using namespace std;
  template <typename T>
  inline bool upmin(T &x, T y) {
    return y < x ? x = y, 1 : 0;
  }
  template <typename T>
  inline bool upmax(T &x, T y) {
    return x < y ? x = y, 1 : 0;
  }
  const long double eps = 1e-9;
  const long double pi = acos(-1);
  const int oo = 1 << 30;
  const long long loo = 1ll << 60;
  const int MAXN = 100005;
  const int MAXM = 4000005;
  const int MAXP = 7000000;
  const int MX = 300000;
  const int mods = 1e9 + 7;
  const int SZ = 29;
  const int inv2 = (mods + 1) >> 1;
  const int INF = 0x3f3f3f3f;
  namespace FastIO {
  constexpr int SIZE = (1 << 21) + 1;
  int num = 0, f;
  char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
                                                  *oT = obuf + SIZE - 1, c;
  inline void flush() {
    fwrite(obuf, 1, oS - obuf, stdout);
    oS = obuf;
  }
  inline void putc(char c) {
    *oS++ = c;
    if (oS == oT) flush();
  }
  inline void getc(char &c) {
    for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                         (iS == iT ? EOF : *iS++))
                       : *iS++);
         (c == ' ' || c == '\0' || c == '\n' || c == '\r') && c != EOF;
         c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                         (iS == iT ? EOF : *iS++))
                       : *iS++))
      ;
  }
  inline void reads(char *st) {
    char c;
    int n = 0;
    getc(st[++n]);
    for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                         (iS == iT ? EOF : *iS++))
                       : *iS++);
         c != ' ' && c != '\0' && c != '\n' && c != '\r' && c != EOF;
         c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                         (iS == iT ? EOF : *iS++))
                       : *iS++))
      st[++n] = c;
    st[++n] = '\0';
  }
  template <class I>
  inline void read(I &x) {
    for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                                (iS == iT ? EOF : *iS++))
                              : *iS++);
         c < '0' || c > '9';
         c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                         (iS == iT ? EOF : *iS++))
                       : *iS++))
      if (c == '-') f = -1;
    for (x = 0; c >= '0' && c <= '9';
         c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                         (iS == iT ? EOF : *iS++))
                       : *iS++))
      x = (x << 3) + (x << 1) + (c & 15);
    x *= f;
  }
  template <class I>
  inline void print(I x) {
    if (x < 0) putc('-'), x = -x;
    if (!x) putc('0');
    while (x) que[++num] = x % 10 + 48, x /= 10;
    while (num) putc(que[num--]);
  }
  inline void putstr(string st) {
    for (int i = 0; i < (int)st.size(); ++i) putc(st[i]);
  }
  struct Flusher_ {
    ~Flusher_() { flush(); }
  } io_Flusher_;
  }  // namespace FastIO
  using FastIO ::getc;
  using FastIO ::print;
  using FastIO ::putc;
  using FastIO ::putstr;
  using FastIO ::read;
  using FastIO ::reads;
  int n, m;
  long long a[MAXN];
  vector<long long> solve(int l, int r) {
    if (l > r) return vector<long long>(1);
    int mid = l;
    for (int i = l; i <= r; ++i)
      if (a[i] < a[mid]) mid = i;
    vector<long long> F, L = solve(l, mid - 1), R = solve(mid + 1, r);
    F.clear(), F.resize(min((int)L.size() + (int)R.size(), m + 1));
    for (int i = 0; i < (int)L.size(); ++i) {
      for (int j = 0; j < (int)R.size() && i + j <= m; ++j)
        upmax(F[i + j], L[i] + R[j] + a[mid] * (-i * j * 2));
      for (int j = 0; j < (int)R.size() && i + j + 1 <= m; ++j)
        upmax(F[i + j + 1],
              L[i] + R[j] + a[mid] * (m - (i + 1) * (j + 1) * 2 + 1));
    }
    return F;
  }
  signed main() {
    read(n), read(m);
    for (int i = 1; i <= n; ++i) read(a[i]);
    vector<long long> Ans = solve(1, n);
    print(Ans[m]), putc('\n');
    return 0;
  }
  