#include <bits/stdc++.h>
using namespace std;
const int maxn = 102000;
long long _sum, dp[2][maxn];
int n, K, a[maxn], cnt[maxn], _l, _r;
void ins(int val) { _sum += cnt[val]++; }
void del(int val) { _sum -= --cnt[val]; }
void adjust(int l, int r) {
  while (_l < l) del(a[_l++]);
  while (_l > l) ins(a[--_l]);
  while (_r < r) ins(a[++_r]);
  while (_r > r) del(a[_r--]);
}
void solve(int o, int l, int r, int ql, int qr) {
  if (l > r || ql > qr) return;
  int mid = (l + r) >> 1, p = min(mid, qr + 1), opt = -1;
  adjust(p, mid);
  long long res = 1e18;
  for (int q = p - 1; q >= ql; --q) {
    if (dp[o ^ 1][q] + _sum < res) res = _sum + dp[o ^ 1][q], opt = q;
    ins(a[--_l]);
  }
  dp[o][mid] = res;
  solve(o, l, mid - 1, ql, opt);
  solve(o, mid + 1, r, opt, qr);
}
int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  memset(dp, 0x3f, sizeof dp);
  dp[0][0] = 0;
  for (int p = 1; p <= K; ++p) {
    memset(cnt, 0, sizeof cnt);
    memset(dp[p & 1], 0x3f, sizeof dp[p & 1]);
    _l = 1, _r = _sum = 0;
    solve(p & 1, p, n, 0, n - 1);
  }
  printf("%lld\n", dp[K & 1][n]);
  return 0;
}
