#include <bits/stdc++.h>
int n, m;
int A[100005];
long long dp[21][100005];
int cnt[100005], iL = 1, iR = 0;
long long ans = 0;
inline void inc(int k) { ans += cnt[A[k]]++; }
inline void dec(int k) { ans -= --cnt[A[k]]; }
inline void moveto(int L, int R) {
  while (iR < R) inc(++iR);
  while (iL > L) inc(--iL);
  while (iR > R) dec(iR--);
  while (iL < L) dec(iL++);
}
void solve(int l, int r, int L, int R, int k) {
  if (l > r) return;
  int mdl = (l + r) >> 1, best = L;
  long long bestans = 0x3f3f3f3f3f3f3f3f;
  for (int i = L; i <= R && i < mdl; i++) {
    moveto(i + 1, mdl);
    if (ans + dp[k - 1][i] < bestans) bestans = ans + dp[k - 1][i], best = i;
  }
  dp[k][mdl] = bestans;
  solve(l, mdl - 1, L, best, k);
  solve(mdl + 1, r, best, R, k);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i <= m; i++) solve(0, n, 0, n, i);
  printf("%lld\n", dp[m][n]);
}
