#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 1e5 + 10, K = 21;
int n, k, a[N];
long long f[K][N];
int L, R, cnt[N];
long long sum;
inline void Add(int x) { sum += cnt[a[x]]++; }
inline void Del(int x) { sum -= --cnt[a[x]]; }
inline void F(int l, int r) {
  while (L > l) Add(--L);
  while (R < r) Add(++R);
  while (L < l) Del(L++);
  while (R > r) Del(R--);
}
inline void solve(int k, int l, int r, int L, int R) {
  if (l > r) return;
  int mid = l + r >> 1, Mid = 0;
  for (register int i = (L); i <= (min(R, mid - 1)); i++) {
    F(i + 1, mid);
    if (f[k - 1][i] + sum < f[k][mid]) f[k][mid] = f[k - 1][i] + sum, Mid = i;
  }
  solve(k, l, mid - 1, L, Mid), solve(k, mid + 1, r, Mid, R);
}
int main() {
  n = read(), k = read();
  for (register int i = (1); i <= (n); i++) a[i] = read();
  for (register int i = (0); i <= (k); i++)
    for (register int j = (0); j <= (n); j++) f[i][j] = 1e15;
  f[0][0] = 0, L = 1;
  for (register int i = (1); i <= (k); i++) solve(i, 1, n, 0, n);
  printf("%lld\n", f[k][n]);
}
