#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5, MAXK = 25;
const long long INF = 1ll << 60;
int n, k, c[MAXN], a = 1, b, buc[MAXN];
long long f[MAXK][MAXN], calc;
template <typename T>
inline T read();
void solve(int, int, int, int, int);
inline void mov(int, int);
int main() {
  n = read<int>();
  k = read<int>();
  for (register int i = 1; i <= n; ++i) c[i] = read<int>();
  for (register int i = 0; i <= k; ++i)
    for (register int j = 1; j <= n; ++j) f[i][j] = INF;
  f[0][0] = 0;
  for (register int i = 1; i <= k; ++i) solve(i, 0, n - 1, 1, n);
  printf("%lld\n", f[k][n]);
  return 0;
}
template <typename T>
inline T read() {
  char tmp = getchar();
  T sum = 0;
  bool flag = false;
  while (tmp < '0' || tmp > '9') {
    if (tmp == '-') flag = true;
    tmp = getchar();
  }
  while (tmp >= '0' && tmp <= '9') {
    sum = (sum << 1) + (sum << 3) + tmp - '0';
    tmp = getchar();
  }
  return flag ? -sum : sum;
}
void solve(int q, int l, int r, int x, int y) {
  if (x > y) return;
  int mid = (x + y) >> 1, opt;
  for (register int i = min(mid - 1, r); i >= l; --i) {
    mov(i + 1, mid);
    if (f[q][mid] > f[q - 1][i] + calc) {
      opt = i;
      f[q][mid] = f[q - 1][i] + calc;
    }
  }
  solve(q, l, opt, x, mid - 1);
  solve(q, opt, r, mid + 1, y);
}
inline void mov(int L, int R) {
  while (a < L) {
    buc[c[a]]--;
    calc -= (long long)buc[c[a]];
    a++;
  }
  while (L < a) {
    a--;
    calc += (long long)buc[c[a]];
    buc[c[a]]++;
  }
  while (b < R) {
    b++;
    calc += (long long)buc[c[b]];
    buc[c[b]]++;
  }
  while (R < b) {
    buc[c[b]]--;
    calc -= buc[c[b]];
    b--;
  }
}
