#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int n, m, a[100010];
int tot[100010], Lpos = 1, Rpos = 0;
long long f[100010], g[100010], MO;
inline int read() {
  int x = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -x : x;
}
void add(int x) { MO += (tot[x]++); }
void del(int x) { MO -= (--tot[x]); }
long long calc(int l, int r) {
  while (Lpos < l) del(a[Lpos++]);
  while (Lpos > l) add(a[--Lpos]);
  while (Rpos < r) add(a[++Rpos]);
  while (Rpos > r) del(a[Rpos--]);
  return MO;
}
void solve(int L, int R, int st, int ed) {
  if (st > ed || L > R) return;
  int M = (st + ed) >> 1;
  long long temp = inf, pos = 0;
  for (int i = L; i <= R; i++)
    if (temp > g[i] + calc(i + 1, M)) temp = g[pos = i] + calc(i + 1, M);
  f[M] = temp;
  solve(L, pos, st, M - 1);
  solve(pos, R, M + 1, ed);
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  memset(g, 0x3f, sizeof g);
  g[0] = 0;
  while (m-- > 0) {
    solve(0, n - 1, 1, n);
    swap(f, g);
  }
  cout << g[n] << '\n';
}
