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 mod = 1e9 + 7;
  char buf[121], *p1 = buf, *p2 = buf;
  inline long long getc() {
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 121, stdin), p1 == p2)
               ? EOF
               : *p1++;
  }
  inline long long read() {
    long long ret = 0, f = 0;
    char ch = getc();
    while (!isdigit(ch)) {
      if (ch == '-') f = 1;
      ch = getc();
    }
    while (isdigit(ch)) {
      ret = ret * 10 + ch - 48;
      ch = getc();
    }
    return f ? -ret : ret;
  }
  template <typename T>
  inline void write(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x >= 10) write(x / 10);
    putchar(x % 10 + '0');
  }
  long long qkpow(long long a, long long b) {
    long long base = a, ans = 1;
    while (b) {
      if (b & 1) ans = 1ll * ans * base % mod;
      base = 1ll * base * base % mod;
      b >>= 1;
    }
    return ans;
  }
  long long n, m, a[4005], sta[4005], ls[4005], rs[4005], si[4005];
  long long dp[4005][4005], tmp[4005][4005];
  void dfs(long long u) {
    dp[u][0] = 0, si[u] = 1;
    dp[u][1] = (m - 1) * a[u];
    if (ls[u] && !rs[u]) {
      dfs(ls[u]);
      for (long long i = 0; i <= si[ls[u]]; i++) {
        tmp[u][i] = max(tmp[u][i], dp[ls[u]][i]);
        tmp[u][i + 1] =
            max(tmp[u][i + 1], dp[ls[u]][i] + (m - 1) * a[u] - 2 * i * a[u]);
      }
      si[u] += si[ls[u]];
      for (long long i = 1; i <= si[u]; i++) dp[u][i] = tmp[u][i];
    } else if (rs[u] && !ls[u]) {
      dfs(rs[u]);
      for (long long i = 0; i <= si[rs[u]]; i++) {
        tmp[u][i] = max(tmp[u][i], dp[rs[u]][i]);
        tmp[u][i + 1] =
            max(tmp[u][i + 1], dp[rs[u]][i] + (m - 1) * a[u] - 2 * i * a[u]);
      }
      si[u] += si[rs[u]];
      for (long long i = 1; i <= si[u]; i++) dp[u][i] = tmp[u][i];
    } else if (ls[u] && rs[u]) {
      dfs(ls[u]), dfs(rs[u]);
      for (long long i = 0; i <= si[ls[u]]; i++) {
        for (long long j = 0; j <= si[rs[u]]; j++) {
          tmp[u][i + j] =
              max(tmp[u][i + j], dp[ls[u]][i] + dp[rs[u]][j] - 2 * i * j * a[u]);
          tmp[u][i + j + 1] =
              max(tmp[u][i + j + 1], dp[ls[u]][i] + dp[rs[u]][j] +
                                         (m - 1) * a[u] - 2 * i * j * a[u] -
                                         2 * i * a[u] - 2 * j * a[u]);
        }
      }
      si[u] += si[ls[u]], si[u] += si[rs[u]];
      for (long long i = 1; i <= si[u]; i++) dp[u][i] = tmp[u][i];
    }
  }
  signed main() {
    n = read(), m = read();
    for (long long i = 1; i <= n; i++) a[i] = read();
    long long top = 0, now = 0, rt = 0;
    for (long long i = 1; i <= n; i++) {
      now = top;
      while (now && a[sta[now]] > a[i]) now--;
      if (!now)
        rt = i;
      else
        rs[sta[now]] = i;
      if (now < top) ls[i] = sta[now + 1];
      top = now;
      sta[++top] = i;
    }
    memset(dp, -0x3f, sizeof dp);
    memset(tmp, -0x3f, sizeof tmp);
    dfs(rt);
    printf("%lld\n", dp[rt][m]);
    return 0;
  }
  