#include <bits/stdc++.h>
using namespace std;
long long DP[100005][25], arr[100005], freq[100005], l = 1, r, ans;
void alter_range(long long l1, long long r1) {
  while (l1 < l) {
    l--;
    ans += freq[arr[l]]++;
  }
  while (r < r1) {
    r++;
    ans += freq[arr[r]]++;
  }
  while (l < l1) {
    ans -= --freq[arr[l]];
    l++;
  }
  while (r1 < r) {
    ans -= --freq[arr[r]];
    r--;
  }
}
void make_layer(long long layer, long long l1, long long r1, long long a,
                long long b) {
  if (l1 > r1) return;
  long long i, j, mid = (l1 + r1) >> 1, &curr = DP[mid][layer], best = mid,
                  better;
  for (i = a; i <= min(b, mid); i++) {
    alter_range(i, mid);
    better = DP[i - 1][layer - 1] + ans;
    if (curr > better) {
      curr = better;
      best = i;
    }
  }
  make_layer(layer, l1, mid - 1, a, best);
  make_layer(layer, mid + 1, r1, best, b);
}
int main() {
  long long i, j, n, k;
  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++) scanf("%lld", &arr[i]);
  for (i = 0; i <= n; i++)
    for (j = 0; j <= k; j++) DP[i][j] = 1e18;
  DP[0][0] = 0;
  for (i = 1; i <= k; i++) make_layer(i, 1, n, 1, n);
  printf("%lld\n", DP[n][k]);
  return 0;
}
