#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 200;
const long long inf = 12344234245678;
long long dp[21][N], c[N], a[N], cur, L = 1, R;
void upd(int l, int r) {
  while (L < l) {
    c[a[L]]--;
    cur -= c[a[L]];
    L++;
  }
  while (R < r) {
    R++;
    cur += c[a[R]];
    c[a[R]]++;
  }
  while (L > l) {
    L--;
    cur += c[a[L]];
    c[a[L]]++;
  }
  while (R > r) {
    c[a[R]]--;
    cur -= c[a[R]];
    R--;
  }
}
void rec(int g, int optl, int optr, int l, int r) {
  if (l > r) return;
  int m = (l + r) / 2, best = optl;
  long long bestCur = inf;
  for (int opt = optl; opt <= min(m - 1, optr); opt++) {
    upd(opt + 1, m);
    if (dp[g - 1][opt] + cur < dp[g - 1][best] + bestCur)
      best = opt, bestCur = cur;
  }
  dp[g][m] = dp[g - 1][best] + bestCur;
  rec(g, optl, best, l, m - 1);
  rec(g, best, optr, m + 1, r);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 0; i < 21; i++)
    for (int j = 0; j < N; j++) dp[i][j] = inf;
  for (int i = 1; i <= n; i++) {
    upd(1, i);
    dp[1][i] = cur;
  }
  for (int i = 2; i <= k; i++) rec(i, 1, n, 1, n);
  cout << dp[k][n];
}
