#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long a[200005];
long long cnt[200005], f[200005][21];
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long l = 1, r, tmp;
inline long long calc(long long x, long long y) {
  ++x;
  while (l < x) tmp -= --cnt[a[l++]];
  while (l > x) tmp += cnt[a[--l]]++;
  while (r < y) tmp += cnt[a[++r]]++;
  while (r > y) tmp -= --cnt[a[r--]];
  return tmp;
}
long long level;
void solve(long long l, long long r, long long bl, long long br) {
  if (l > r || bl > br) return;
  long long mid = l + r >> 1, b;
  for (register long long i = bl; i <= min(br, mid); ++i)
    if (calc(i, mid) + f[i][level - 1] < f[mid][level])
      f[mid][level] = calc(i, mid) + f[i][level - 1], b = i;
  solve(l, mid - 1, bl, b);
  solve(mid + 1, r, b, br);
}
int main() {
  n = read(), m = read();
  for (register long long i = 1; i <= n; ++i) a[i] = read();
  for (register long long i = 0; i <= n; ++i)
    for (register long long j = 0; j <= m; ++j) f[i][j] = 1919810114514;
  f[0][0] = 0;
  for (level = 1; level <= m; ++level) solve(1, n, 0, n - 1);
  printf("%lld", f[n][m]);
  return 0;
}
