#include <bits/stdc++.h>
inline long long read() {
  long long x = 0;
  char c = getchar(), f = 1;
  for (; c < '0' || '9' < c; c = getchar())
    if (c == '-') f = -1;
  for (; '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
inline void write(long long x) {
  static char buf[20];
  int len = 0;
  if (x < 0) putchar('-'), x = -x;
  for (; x; x /= 10) buf[len++] = x % 10 + '0';
  if (!len)
    putchar('0');
  else
    while (len) putchar(buf[--len]);
}
inline void writesp(long long x) {
  write(x);
  putchar(' ');
}
inline void writeln(long long x) {
  write(x);
  putchar('\n');
}
int a[200010], cnt[200010];
long long f[30][200010];
int n, k, L, R;
long long cur;
long long calc(int l, int r) {
  while (l < L) cur += cnt[a[--L]], ++cnt[a[L]];
  while (R < r) cur += cnt[a[++R]], ++cnt[a[R]];
  while (L < l) --cnt[a[L]], cur -= cnt[a[L++]];
  while (r < R) --cnt[a[R]], cur -= cnt[a[R--]];
  return cur;
}
void solve(int k, int l, int r, int x, int y) {
  int mid = (l + r) >> 1, pos = x;
  long long val = f[k - 1][x - 1] + calc(x, mid);
  for (int i = x + 1; i <= y && i <= mid; i++) {
    long long tmp = f[k - 1][i - 1] + calc(i, mid);
    if (tmp < val) val = tmp, pos = i;
  }
  f[k][mid] = val;
  if (l < mid) solve(k, l, mid - 1, x, pos);
  if (mid < r) solve(k, mid + 1, r, pos, y);
}
int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  memset(f, 0x3f, sizeof(f));
  f[0][0] = 0;
  memset(cnt, 0, sizeof(cnt));
  L = 1;
  R = 0;
  cur = 0;
  for (int i = 1; i <= k; i++) solve(i, 1, n, 1, n);
  writeln(f[k][n]);
  return 0;
}
