#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100010;
const int Maxk = 22;
const int inf = 2147483647;
const double pi = acos(-1.0);
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
int n, k, a[Maxn], cnt[Maxn];
long long f[Maxn][Maxk], cost;
int cl, cr;
void w(int l, int r) {
  while (cr < r) cr++, cost += cnt[a[cr]], cnt[a[cr]]++;
  while (cl > l) cl--, cost += cnt[a[cl]], cnt[a[cl]]++;
  while (cr > r) cnt[a[cr]]--, cost -= cnt[a[cr]], cr--;
  while (cl < l) cnt[a[cl]]--, cost -= cnt[a[cl]], cl++;
}
void solve(int o, int l, int r, int L, int R) {
  if (l > r) return;
  int mid = l + r >> 1, p;
  f[mid][o] = 1e18;
  for (int i = min(mid - 1, R); i >= L; i--) {
    w(i + 1, mid);
    if (f[i][o - 1] + cost < f[mid][o]) f[mid][o] = f[i][o - 1] + cost, p = i;
  }
  solve(o, l, mid - 1, L, p);
  solve(o, mid + 1, r, p, R);
}
int main() {
  memset(cnt, 0, sizeof(cnt));
  n = read(), k = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  long long t = 0;
  for (int i = 1; i <= n; i++) t += cnt[a[i]], f[i][1] = t, cnt[a[i]]++;
  for (int i = 1; i <= n; i++) cnt[a[i]] = 0;
  cost = 0, cl = 1, cr = 0;
  for (int i = 2; i <= k; i++) solve(i, i, n, 1, n);
  printf("%lld", f[n][k]);
}
