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;
  const long long MAXN = 4010;
  const long long INF = 2e9;
  const double eps = 1e-6;
  const double PI = acos(-1);
  long long n, m, rt, a[MAXN];
  long long p[MAXN], ls[MAXN], rs[MAXN], siz[MAXN];
  long long stk[MAXN], top, dp[MAXN][MAXN];
  template <typename T>
  inline bool read(T &a) {
    a = 0;
    char c = getchar();
    long long f = 1;
    while (c < '0' || c > '9') {
      if (c == '-') f = -1;
      c = getchar();
    }
    while (c >= '0' && c <= '9') {
      a = a * 10 + (c ^ 48);
      c = getchar();
    }
    return a *= f, true;
  }
  template <typename A, typename... B>
  inline bool read(A &x, B &...y) {
    return read(x) && read(y...);
  }
  void DFS(long long x) {
    siz[x] = 1, dp[x][0] = 0, dp[x][1] = m * a[x] - a[x];
    if (ls[x]) {
      DFS(ls[x]);
      for (long long i = siz[x]; i >= 0; --i) {
        for (long long j = 1; j <= siz[ls[x]]; ++j)
          dp[x][i + j] =
              max(dp[x][i + j], dp[x][i] + dp[ls[x]][j] - 2 * i * j * a[x]);
      }
      siz[x] += siz[ls[x]];
    }
    if (rs[x]) {
      DFS(rs[x]);
      for (long long i = siz[x]; i >= 0; --i) {
        for (long long j = 1; j <= siz[rs[x]]; ++j)
          dp[x][i + j] =
              max(dp[x][i + j], dp[x][i] + dp[rs[x]][j] - 2 * i * j * a[x]);
      }
      siz[x] += siz[rs[x]];
    }
  }
  signed main() {
    read(n), read(m);
    for (long long i = 1; i <= n; ++i) read(a[i]);
    for (long long i = 1; i <= n; ++i) {
      while (top && a[stk[top]] > a[i]) ls[i] = stk[top--];
      if (top) rs[stk[top]] = i;
      stk[++top] = i;
    }
    rt = stk[1];
    DFS(rt);
    printf("%lld\n", dp[rt][m]);
    return 0;
  }
  