#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
const long long oo = 1e18;
int freq[N], v[N], L = 1, R = 0;
long long dp[N][21], ans = 0;
void add(int l, int r) {
  while (R < r) ans += ++freq[v[++R]];
  while (R > r) ans -= freq[v[R--]]--;
  while (L < l) ans -= freq[v[L++]]--;
  while (L > l) ans += ++freq[v[--L]];
}
void calc(int l, int r, int bl, int br, int k) {
  if (l > r) return;
  int mid = (l + r) / 2, in;
  long long &ret = dp[mid][k];
  ret = oo;
  for (int i = bl; i <= min(br, mid); i++) {
    add(i, mid);
    if (ans - (R - L + 1) + dp[i - 1][k - 1] < ret) {
      ret = ans - (R - L + 1) + dp[i - 1][k - 1];
      in = i;
    }
  }
  calc(l, mid - 1, bl, in, k);
  calc(mid + 1, r, in, br, k);
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v[i]);
  }
  for (int i = 1; i <= n; i++) {
    add(1, i);
    dp[i][1] = ans - (R - L + 1);
  }
  for (int i = 2; i <= k; i++) {
    calc(1, n, 1, n, i);
  }
  cout << dp[n][k] << endl;
  return 0;
}
