#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long INFL = 1e18;
int n, k, c[N], a[N];
long long f[N], g[N], ans;
int L = 1, R = 0;
inline void ins(int x) { ans += c[a[x]]++; }
inline void del(int x) { ans -= --c[a[x]]; }
inline long long w(int l, int r) {
  while (R < r) ins(++R);
  while (l < L) ins(--L);
  while (r < R) del(R--);
  while (L < l) del(L++);
  return ans;
}
inline void solve(int l = 1, int r = n, int ql = 0, int qr = n - 1) {
  if (l > r) return;
  int mid = l + r >> 1, k = ql;
  g[mid] = INFL;
  for (int i = ql; i <= min(mid - 1, qr); i++) {
    if (f[i] + w(i + 1, mid) < g[mid]) {
      g[mid] = f[i] + w(i + 1, mid);
      k = i;
    }
  }
  solve(l, mid - 1, ql, k);
  solve(mid + 1, r, k, qr);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  memset(f, 0x3f, sizeof f);
  f[0] = 0;
  while (k--) solve(), swap(f, g);
  printf("%lld\n", f[n]);
  return 0;
}
