#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 999;
long long f[maxn], ansnow, a[maxn], n, m, buc[maxn], L, R;
long long C(long long n) {
  if (n <= 1) return 0;
  return n * (n - 1) / 2;
}
void upd(long long x, long long fl) {
  ansnow -= C(buc[x]);
  buc[x] += fl;
  ansnow += C(buc[x]);
}
long long calc(long long l, long long r) {
  while (L > l) upd(a[L - 1], 1), --L;
  while (R < r) upd(a[R + 1], 1), ++R;
  while (L < l) upd(a[L], -1), ++L;
  while (R > r) upd(a[R], -1), --R;
  return ansnow;
}
void solve(long long ql, long long qr, long long l, long long r) {
  if (l > r) return;
  long long mid = l + r >> 1, mn = n * n, pos = ql;
  for (long long i = (ql); i <= (min(qr, mid)); ++i) {
    long long tmp = f[i - 1] + calc(i, mid);
    if (tmp < mn) mn = tmp, pos = i;
  }
  solve(pos, qr, mid + 1, r);
  solve(ql, pos, l, mid - 1);
  f[mid] = mn;
}
signed main() {
  cin >> n >> m;
  L = 1, R = 0;
  for (long long i = (1); i <= (n); ++i) cin >> a[i], f[i] = calc(1, i);
  for (long long i = (1); i <= (m - 1); ++i) solve(1, n, 1, n);
  cout << f[n];
}
