#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long dp[22][100009], ara[100009], cnt[100009], cost = 0;
long long curStart = 0, curStop = -1;
void add(long long val) {
  cost -= (cnt[val] * (cnt[val] - 1)) / 2;
  cnt[val]++;
  cost += (cnt[val] * (cnt[val] - 1)) / 2;
}
void del(long long val) {
  cost -= (cnt[val] * (cnt[val] - 1)) / 2;
  cnt[val]--;
  cost += (cnt[val] * (cnt[val] - 1)) / 2;
}
void calculateCost(long long start, long long stop) {
  while (curStart > start) add(ara[--curStart]);
  while (curStop < stop) add(ara[++curStop]);
  while (curStart < start) del(ara[curStart++]);
  while (curStop > stop) del(ara[curStop--]);
}
void solve(long long lo, long long hi, long long left, long long right,
           long long k) {
  if (lo > hi) return;
  long long mid = (lo + hi) / 2, idx = 0;
  dp[k][mid] = inf;
  for (long long i = min(mid - 1, right); i >= left; i--) {
    long long now = i + 1;
    calculateCost(now, mid);
    if (dp[k - 1][i] + cost < dp[k][mid]) {
      dp[k][mid] = dp[k - 1][i] + cost;
      idx = i;
    }
  }
  solve(lo, mid - 1, left, idx, k);
  solve(mid + 1, hi, idx, right, k);
}
int main() {
  long long n, k;
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) scanf("%lld", &ara[i]);
  for (long long i = 0; i <= n; i++)
    for (long long j = 0; j <= 20; j++) dp[j][i] = inf;
  dp[0][0] = 0;
  for (long long i = 1; i <= k; i++) {
    solve(1, n, 0, n, i);
  }
  cout << dp[k][n] << endl;
  return 0;
}
