#include <bits/stdc++.h>
const int maxn = 1e5 + 7;
using namespace std;
int n, k, l, r;
int a[maxn], num[maxn];
long long ans;
long long f[maxn][21];
void updata(int x, int op) {
  if (op == 1) {
    ans += (long long)num[a[x]];
    num[a[x]]++;
  } else {
    num[a[x]]--;
    ans -= (long long)num[a[x]];
  }
}
long long getsum(int x, int y) {
  while (r < y) updata(r + 1, 1), r++;
  while (r > y) updata(r, -1), r--;
  while (l < x) updata(l, -1), l++;
  while (l > x) updata(l - 1, 1), l--;
  return ans;
}
void calc(int l, int r, int ll, int rr, int d) {
  int mid = (l + r) / 2;
  int p;
  for (int i = ll; i <= min(mid - 1, rr); i++) {
    if (f[i][d - 1] + getsum(i + 1, mid) <= f[mid][d]) {
      p = i;
      f[mid][d] = f[i][d - 1] + getsum(i + 1, mid);
    }
  }
  if (l == r) return;
  calc(l, mid, ll, p, d);
  calc(mid + 1, r, p, rr, d);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= k; j++) f[i][j] = 1e15;
  }
  l = 1, r = 0;
  f[0][0] = 0;
  for (int i = 1; i <= k; i++) calc(1, n, 0, n, i);
  printf("%lld\n", f[n][k]);
}
