#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return x;
}
const int N = 100005;
int n, k, a[N], t[N];
long long f[N], g[N], inf;
int L, R;
long long sum;
void add(int x) {
  sum += t[a[x]];
  t[a[x]]++;
}
void del(int x) {
  t[a[x]]--;
  sum -= t[a[x]];
}
long long w(int l, int r) {
  while (L > l) add(--L);
  while (R < r) add(++R);
  while (L < l) del(L++);
  while (R > r) del(R--);
  return sum;
}
void work(int l, int r, int x, int y) {
  if (l > r) return;
  int mid = (l + r) >> 1, p = 0;
  long long z;
  for (int i = min(mid, y); i >= x; i--) {
    z = w(i, mid);
    if (z + g[i - 1] < f[mid]) f[mid] = z + g[i - 1], p = i;
  }
  work(l, mid - 1, x, p);
  work(mid + 1, r, p, y);
}
int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  inf = 1ll * n * n;
  f[0] = 0;
  for (int i = 1; i <= n; i++) f[i] = inf;
  L = R = 1;
  add(1);
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j <= n; j++) g[j] = f[j], f[j] = inf;
    work(1, n, 1, n);
  }
  printf("%lld\n", f[n]);
  return 0;
}
