#include <bits/stdc++.h>
using namespace std;
int n, K, a[100010], tot[100010];
long long f[100003], g[100003];
inline int read() {
  int x = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -x : x;
}
void calc(int L, int R, int st, int ed, long long cost) {
  if (L > R) return;
  int M = (L + R) >> 1, p = min(M, ed), k = 0;
  for (int i = L; i <= M; i++) cost += (tot[a[i]]++);
  for (int i = st; i <= p; i++) {
    cost -= (--tot[a[i]]);
    if (g[M] > f[i] + cost) g[M] = f[k = i] + cost;
  }
  for (int i = st; i <= p; i++) cost += (tot[a[i]]++);
  for (int i = L; i <= M; i++) cost -= (--tot[a[i]]);
  calc(L, M - 1, st, k, cost);
  for (int i = L; i <= M; i++) cost += (tot[a[i]]++);
  for (int i = st; i < k; i++) cost -= (--tot[a[i]]);
  calc(M + 1, R, k, ed, cost);
  for (int i = st; i < k; i++) cost += (tot[a[i]]++);
  for (int i = L; i <= M; i++) cost -= (--tot[a[i]]);
}
int main() {
  n = read();
  K = read() - 1;
  for (int i = 1; i <= n; i++) f[i] = f[i - 1] + (tot[a[i] = read()]++);
  memset(tot, 0, sizeof tot);
  while (K-- > 0) {
    memset(g, 0x3f, sizeof g);
    calc(1, n, 1, n, 0);
    swap(f, g);
  }
  cout << f[n] << '\n';
  return 0;
}
