#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e16;
int n, k;
int x, y;
int a[100005];
int b[100005];
long long dp[22][100005];
long long tmp;
inline void add(int &x) {
  tmp += b[a[x]];
  b[a[x]]++;
}
inline void del(int &x) {
  b[a[x]]--;
  tmp -= b[a[x]];
}
void solve(int j, int L, int R, int l, int r) {
  if (L > R) return;
  int m = (L + R) / 2;
  while (y < m) {
    y++;
    add(y);
  }
  while (y > m) {
    del(y);
    y--;
  }
  while (x < l) {
    del(x);
    x++;
  }
  while (x > l) {
    x--;
    add(x);
  }
  dp[j][m] = inf;
  int pos = -1;
  for (int i = l; i <= r && i < m; i++) {
    del(x);
    x++;
    long long v = dp[j - 1][i] + tmp;
    if (v < dp[j][m]) {
      dp[j][m] = v;
      pos = i;
    }
  }
  solve(j, L, m - 1, l, pos);
  solve(j, m + 1, R, pos, r);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[0][i] = inf;
  }
  for (int j = 1; j <= k; j++) {
    memset(b, 0, sizeof(b));
    x = 1;
    y = 0;
    tmp = 0;
    solve(j, 1, n, 0, n);
  }
  cout << dp[k][n] << endl;
  return 0;
}
