#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const long long inf = 0x3f3f3f3f3f;
int n, K, pre, now;
int a[N];
long long f[2][N];
namespace md {
int ml, mr;
int cnt[N];
long long cost;
void add(int x) { cost += (cnt[x]++); }
void del(int x) { cost -= (--cnt[x]); }
void Move(int l, int r) {
  while (mr < r) add(a[++mr]);
  while (ml > l) add(a[--ml]);
  while (mr > r) del(a[mr--]);
  while (ml < l) del(a[ml++]);
}
}  // namespace md
using namespace md;
int hd, tl;
struct seq {
  int l, r, L, R;
} st[N * 2];
void solve(int l, int r, int L, int R) {
  if (l > r || L > R) return;
  if (L == R) {
    for (int i = l; i <= r; i++) {
      Move(L + 1, i);
      f[now][i] = f[pre][L] + cost;
    }
    return;
  }
  int mid = (l + r) >> 1, mr = min(mid - 1, R), pos = 0;
  long long mi = inf;
  for (int i = mr; i >= L; i--) {
    Move(i + 1, mid);
    if (f[pre][i] + cost <= mi) mi = f[pre][i] + cost, pos = i;
  }
  f[now][mid] = mi;
  st[++tl] = (seq){l, mid - 1, L, pos};
  st[++tl] = (seq){mid + 1, r, pos, R};
}
void calc(int l, int r, int L, int R) {
  hd = tl = 0;
  st[++tl] = (seq){l, r, L, R};
  while (hd < tl) hd++, solve(st[hd].l, st[hd].r, st[hd].L, st[hd].R);
}
int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), f[0][i] = f[0][i - 1] + (cnt[a[i]]++);
  pre = 0;
  now = 1;
  memset(cnt, 0, sizeof(cnt));
  ml = 1;
  mr = 0;
  for (int i = 2; i <= K; i++) {
    calc(i, n, i - 1, n);
    for (int j = 0; j < i; j++) f[now][j] = inf;
    swap(pre, now);
  }
  printf("%lld", f[pre][n]);
  return 0;
}
