#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007, N = 100005, D = 22;
const long long inf = 1ll << 60;
int n, m, q, dm[N * 4], s[N], lt, c[N], ls;
long long f[N][D], f0;
void solve(int x, int l, int r, int a, int b) {
  if (l > r) return;
  int mid = (l + r) >> 1;
  if (!dm[x]) {
    long long res = inf;
    for (int i = lt + 1; i <= mid; i++) {
      f0 += s[c[i]];
      s[c[i]]++;
    }
    lt = mid;
    for (int i = ls + 1; i <= a; i++) {
      s[c[i]]--;
      f0 -= s[c[i]];
    }
    for (int i = a; i <= min(mid, b); i++) {
      if (i > a) {
        s[c[i]]--;
        f0 -= s[c[i]];
      }
      if (res > f0 + f[i][q - 1]) {
        res = f0 + f[i][q - 1];
        dm[x] = i;
      }
    }
    for (int i = mid + 1; i <= b; i++) {
      s[c[i]]--;
      f0 -= s[c[i]];
    }
    ls = b;
    f[mid][q] = res;
    return;
  }
  solve(x << 1, l, mid - 1, a, dm[x]);
  solve(x << 1 | 1, mid + 1, r, dm[x], b);
}
int main() {
  scanf("%d%d", &n, &m);
  m--;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &c[i]);
    f[i][0] = f[i - 1][0] + s[c[i]];
    s[c[i]]++;
  }
  for (int j = 1; j <= m; j++) {
    memset(dm, 0, sizeof(dm));
    q = j;
    for (int i = 1; i <= D - 1; i++) {
      memset(s, 0, sizeof(s));
      lt = 0;
      ls = 0;
      f0 = 0;
      solve(1, 1, n, 1, n);
    }
  }
  printf("%I64d\n", f[n][m]);
  return 0;
}
