#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
long long n, m, f[22][100005], now;
long long l, r, a[100005], c[100005], res;
inline void add(long long x) {
  res -= c[x] * (c[x] - 1) / 2;
  c[x]++;
  res += c[x] * (c[x] - 1) / 2;
}
inline void del(long long x) {
  res -= c[x] * (c[x] - 1) / 2;
  c[x]--;
  res += c[x] * (c[x] - 1) / 2;
}
inline long long calc(long long ql, long long qr) {
  while (l > ql) add(a[--l]);
  while (r < qr) add(a[++r]);
  while (l < ql) del(a[l++]);
  while (r > qr) del(a[r--]);
  return res;
}
void solve(long long tl, long long tr, long long l, long long r) {
  if (tl > tr || l > r) return;
  long long mid = l + r >> 1, res = 1e18, p = 0;
  for (register long long i = (tl); i <= (tr); ++i) {
    long long t = calc(i + 1, mid) + f[now - 1][i];
    if (res > t) res = t, p = i;
  }
  f[now][mid] = res, solve(tl, p, l, mid - 1), solve(p, tr, mid + 1, r);
}
signed main() {
  memset(f, 63, sizeof f), f[0][0] = 0;
  n = read(), m = read();
  for (register long long i = (1); i <= (n); ++i) a[i] = read();
  l = 1, r = 0;
  for (register long long i = (1); i <= (m); ++i) now++, solve(0, n - 1, 1, n);
  cout << f[m][n];
  return 0;
}
