#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 55, inf = 2e18;
long long a[maxn], RR, LL, C, cnt[maxn], dp[maxn], pd[maxn], n, k;
long long cost(long long L, long long R) {
  while (RR < R) {
    RR++;
    C += cnt[a[RR]];
    cnt[a[RR]]++;
  }
  while (LL > L) {
    LL--;
    C += cnt[a[LL]];
    cnt[a[LL]]++;
  }
  while (RR > R) {
    cnt[a[RR]]--;
    C -= cnt[a[RR]];
    RR--;
  }
  while (LL < L) {
    cnt[a[LL]]--;
    C -= cnt[a[LL]];
    LL++;
  }
  return C;
}
void f(long long L, long long R, long long l, long long r) {
  if (R <= L) return;
  long long mid = (L + R) / 2;
  long long A = 0;
  for (long long i = l; i <= min(r, mid - 1); i++) {
    if (pd[i] + cost(i, mid - 1) < dp[mid]) {
      dp[mid] = pd[i] + cost(i, mid - 1);
      A = i;
    }
  }
  if (R == L + 1) return;
  f(L, mid, l, A);
  f(mid + 1, R, A, r);
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    pd[i] = dp[i] = inf;
  }
  cnt[a[0]]++;
  pd[0] = 0;
  for (long long i = 0; i < k; i++) {
    f(0, n + 1, 0, n);
    for (long long j = 0; j <= n; j++) pd[j] = dp[j], dp[j] = inf;
  }
  cout << pd[n];
}
