#include <bits/stdc++.h>
using namespace std;
int n, m, a[500010];
int cnt[500010], l, r;
long long sum;
inline void add(int x) { sum += cnt[x]++; }
inline void del(int x) { sum -= --cnt[x]; }
void move(int L, int R) {
  while (r < R) add(a[++r]);
  while (l > L) add(a[--l]);
  while (r > R) del(a[r--]);
  while (l < L) del(a[l++]);
}
long long f[500010], g[500010];
void solve(int l, int r, int L, int R) {
  if (l > r) return;
  int m = l + r >> 1, p;
  for (int i = L; i <= min(R, m - 1); ++i) {
    move(i + 1, m);
    if (g[i] + sum < f[m]) f[m] = g[i] + sum, p = i;
  }
  solve(l, m - 1, L, p);
  solve(m + 1, r, p, R);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  cnt[a[1]] = 1;
  l = r = 1;
  for (int i = 0; i < 500010; ++i) g[i] = 100000000000000000ll;
  g[0] = 0;
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j < 500010; ++j) f[j] = 100000000000000000ll;
    solve(1, n, 0, n - 1);
    for (int j = 1; j <= n; ++j) g[j] = f[j];
  }
  cout << f[n] << endl;
  return 0;
}
