#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[100005];
int posL, posR;
long long now;
long long cnt[100005];
long long dp[2][100005];
void solve(int L, int R, int l, int r) {
  if (L > R) return;
  int mid = (L + R) >> 1;
  int pos = min(mid, r);
  long long Max = 2e16;
  while (posR < mid) posR++, now += cnt[A[posR]], cnt[A[posR]]++;
  while (posL > pos + 1) posL--, now += cnt[A[posL]], cnt[A[posL]]++;
  while (posR > mid) cnt[A[posR]]--, now -= cnt[A[posR]], posR--;
  while (posL < pos + 1) cnt[A[posL]]--, now -= cnt[A[posL]], posL++;
  for (int i = (pos), i_end_ = (l); i >= i_end_; i--) {
    posL--;
    now += cnt[A[posL]];
    cnt[A[posL]]++;
    if (now + dp[0][i - 1] < Max) Max = now + dp[0][i - 1], pos = i;
  }
  dp[1][mid] = Max;
  solve(L, mid - 1, l, pos);
  solve(mid + 1, R, pos, r);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (1), i_end_ = (n); i <= i_end_; i++) scanf("%d", &A[i]);
  for (int i = (0), i_end_ = (1); i <= i_end_; i++) {
    for (int j = (0), j_end_ = (n); j <= j_end_; j++) {
      dp[i][j] = 2e16;
    }
  }
  dp[0][0] = 0;
  for (int i = (1), i_end_ = (m); i <= i_end_; i++) {
    now = 0;
    posL = 1, posR = 0;
    memset(cnt, 0, sizeof(cnt));
    solve(1, n, 1, n);
    memcpy(dp[0], dp[1], sizeof(dp[1]));
  }
  printf("%lld\n", dp[0][n]);
  return 0;
}
