#include <bits/stdc++.h>
const int maxn = 2e5 + 10;
int cnt[maxn];
long long dp[maxn];
long long tmp[maxn];
int a[maxn];
int n;
using namespace std;
int l1, r1;
long long now;
void Seg(int x, int y) {
  for (; l1 < x; now -= (--cnt[a[l1++]]))
    ;
  for (; x < l1; now += (cnt[a[--l1]]++))
    ;
  for (; r1 > y; now -= (--cnt[a[r1--]]))
    ;
  for (; y > r1; now += (cnt[a[++r1]]++))
    ;
}
void slove(int l, int r, int L, int R) {
  if (l > r) return;
  int mid = (l + r) >> 1;
  Seg(L, mid);
  int P = L, pp = L;
  for (int p = L; p <= min(mid, R); p++) {
    Seg(p, mid);
    if (now + dp[p - 1] < tmp[mid]) {
      tmp[mid] = now + dp[p - 1];
      pp = P = p;
    } else if (now + dp[p - 1] == tmp[mid]) {
      P = p;
    }
  }
  slove(l, mid - 1, L, pp);
  slove(mid + 1, r, P, R);
}
int main() {
  int k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (int i = 0; i < k; i++) {
    cnt[0] = 1;
    memset(tmp, 0x3f, sizeof(tmp));
    tmp[0] = 0;
    slove(1, n, 1, n);
    memcpy(dp, tmp, sizeof(dp));
  }
  printf("%lld", dp[n]);
}
