#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool umax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
long long dp[2][100009], res;
int arr[100009], cnt[100009], L = 1, R = 1;
void add(int x) {
  res += cnt[arr[x]];
  cnt[arr[x]]++;
}
void rem(int x) {
  cnt[arr[x]]--;
  res -= cnt[arr[x]];
}
long long cost(int l, int r) {
  while (L < l) rem(L++);
  while (R <= r) add(R++);
  while (L > l) add(--L);
  while (R - 1 > r) rem(--R);
  return res;
}
void solve(int block, int l, int r, int optl, int optr) {
  if (l > r) return;
  int mid = (l + r) >> 1, who;
  for (int i = optl; i <= min(mid, optr); i++)
    if (umin(dp[block][mid], dp[block ^ 1][i - 1] + cost(i, mid))) who = i;
  solve(block, l, mid - 1, optl, who);
  solve(block, mid + 1, r, who, optr);
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", arr + i);
  for (int i = 1; i <= n; i++) dp[1][i] = cost(1, i);
  for (int i = 2; i <= k; i++) {
    for (int j = 1; j <= n; j++) dp[i & 1][j] = 1000000000007;
    solve(i & 1, i, n, 1, n);
  }
  printf("%lld\n", dp[k & 1][n]);
  return 0;
}
