#include <bits/stdc++.h>
int get() {
  char c;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  int s = c == '-' ? -1 : 1, k = ~s ? c - '0' : 0;
  for (; isdigit(c = getchar()); k = k * 10 + c - '0')
    ;
  return s * k;
}
using namespace std;
const int N = 100010;
int n, k, a[N], cnt[N], p = 1, q = 0;
long long sum, _f[21][N], *f0, *f;
void solve(int l, int r, int x, int y) {
  int mid = (l + r) >> 1, pos = min(mid, y);
  for (; p < pos; sum -= --cnt[a[p++]])
    ;
  for (; q < mid; sum += cnt[a[++q]]++)
    ;
  for (; q > mid; sum -= --cnt[a[q--]])
    ;
  for (; p >= x; sum += cnt[a[--p]]++)
    if (f0[p - 1] + sum < f[mid]) pos = p, f[mid] = f0[p - 1] + sum;
  if (l < mid) solve(l, mid - 1, x, pos);
  if (mid < r) solve(mid + 1, r, pos, y);
}
int main() {
  n = get(), k = get();
  for (int i = 1; i <= n; ++i) a[i] = get();
  memset(_f, 63, sizeof(_f)), _f[0][0] = 0;
  for (int i = 1; i <= k; ++i) f0 = _f[i - 1], f = _f[i], solve(1, n, 1, n);
  printf("%I64d", f[n]);
  return 0;
}
