#include <bits/stdc++.h>
using namespace std;
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937 Rand(seed);
uniform_int_distribution<long long> range(0, 1ll << 32);
inline void ucin() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
template <class T>
inline void chkmax(T &x, const T &y) {
  if (x < y) x = y;
}
template <class T>
inline void chkmin(T &x, const T &y) {
  if (x > y) x = y;
}
const int N = 4e5 + 10;
const long long INF = 1e18;
int n, k, cnt[N], a[N], L = 1, R = 0;
long long g[N], f[N], cur;
void ins(int u) { cur += cnt[u]++; }
void del(int u) { cur -= --cnt[u]; }
void gto(int l, int r) {
  while (R < r) ins(a[++R]);
  while (L > l) ins(a[--L]);
  while (R > r) del(a[R--]);
  while (L < l) del(a[L++]);
}
void solve(int l, int r, int ql, int qr) {
  if (l > r) return;
  int mid = (l + r) >> 1, qmid = 0;
  f[mid] = INF;
  for (int i = ql; i <= min(qr, mid - 1); ++i) {
    gto(i + 1, mid);
    long long c = g[i] + cur;
    if (f[mid] > c) {
      f[mid] = c;
      qmid = i;
    }
  }
  solve(l, mid - 1, ql, qmid);
  solve(mid + 1, r, qmid, qr);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  fill(g + 1, g + n + 1, INF);
  g[0] = 0;
  for (int i = 1; i <= k; ++i) {
    f[0] = INF;
    solve(1, n, 0, n - 1);
    copy(f, f + n + 1, g);
  }
  printf("%lld\n", f[n]);
  return 0;
}
