#include <bits/stdc++.h>
using namespace std;
const int kN = 1e5 + 5;
const long long kInf = 1e15;
int n, K, a[kN];
int now, lp, rp, cnt[kN];
long long sum, f[kN][30];
void Add(int x) {
  if (x) {
    sum += cnt[a[x]];
    cnt[a[x]]++;
  }
}
void Del(int x) {
  if (x) {
    cnt[a[x]]--;
    sum -= cnt[a[x]];
  }
}
void Move(int l, int r) {
  while (rp < r) {
    rp++, Add(rp);
  }
  while (lp > l) {
    lp--, Add(lp);
  }
  while (rp > r) {
    Del(rp), rp--;
  }
  while (lp < l) {
    Del(lp), lp++;
  }
}
void Solve(int l, int r, int L, int R) {
  int mid = (l + r) >> 1, pos;
  for (int i = L; i <= min(R, mid - 1); ++i) {
    Move(i + 1, mid);
    if (f[i][now - 1] + sum < f[mid][now]) {
      f[mid][now] = f[i][now - 1] + sum;
      pos = i;
    }
  }
  if (l < mid) {
    Solve(l, mid, L, pos);
  }
  if (r > mid) {
    Solve(mid + 1, r, pos, R);
  }
}
signed main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    Move(1, i);
    f[i][1] = sum;
  }
  for (int i = 2; i <= K; ++i) {
    for (int j = 1; j <= n; ++j) {
      f[j][i] = kInf;
      cnt[j] = 0;
    }
    sum = 0, lp = rp = 0, now = i;
    Solve(1, n, 0, n - 1);
  }
  printf("%lld\n", f[n][K]);
  return 0;
}
