#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
inline void write(int x) {
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int N = 100005;
const long long INF = (long long)N * N;
int nl, nr, cnt[N];
long long now;
int n, a[N];
inline void modify(int l, int r) {
  while (nl > l) {
    --nl;
    now += cnt[a[nl]];
    ++cnt[a[nl]];
  }
  while (nr < r) {
    ++nr;
    now += cnt[a[nr]];
    ++cnt[a[nr]];
  }
  while (nl < l) {
    --cnt[a[nl]];
    now -= cnt[a[nl]];
    ++nl;
  }
  while (nr > r) {
    --cnt[a[nr]];
    now -= cnt[a[nr]];
    --nr;
  }
}
long long f[N], g[N];
inline void solve(int l, int r, int pl, int pr) {
  if (l > r) return;
  int p = -1, mid = l + r >> 1;
  f[mid] = INF;
  for (int i = pl; i <= pr && i < mid; ++i) {
    modify(i + 1, mid);
    if (now + g[i] < f[mid]) p = i, f[mid] = now + g[i];
  }
  solve(l, mid - 1, pl, p);
  solve(mid + 1, r, p, pr);
}
int k;
int main() {
  int i;
  read(n), read(k);
  f[0] = 0;
  for (i = 1; i <= n; ++i) read(a[i]), f[i] = INF;
  nl = nr = cnt[a[1]] = 1;
  while (k--) memcpy(g, f, sizeof(g)), solve(1, n, 0, n);
  cout << f[n] << '\n';
  return 0;
}
