#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int maxn = 1e5 + 10;
long long dp[maxn][20];
long long cnt[maxn];
long long a[maxn];
long long cost;
int N, K, L, R;
void query(int l, int r) {
  while (L < l) {
    --cnt[a[L]];
    cost -= cnt[a[L]];
    ++L;
  }
  while (L > l) {
    --L;
    cost += cnt[a[L]];
    ++cnt[a[L]];
  }
  while (R < r) {
    ++R;
    cost += cnt[a[R]];
    ++cnt[a[R]];
  }
  while (R > r) {
    --cnt[a[R]];
    cost -= cnt[a[R]];
    --R;
  }
}
void solve(int l, int r, int x, int y, int k) {
  if (l > r) return;
  int mid = (l + r) >> 1;
  int pos = -1;
  dp[mid][k] = inf;
  for (int i = x; i <= min(y, mid); ++i) {
    query(i, mid);
    if (cost + dp[i - 1][k - 1] < dp[mid][k]) {
      dp[mid][k] = cost + dp[i - 1][k - 1];
      pos = i;
    }
  }
  solve(l, mid - 1, x, pos, k);
  solve(mid + 1, r, pos, y, k);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> K;
  memset(cnt, 0, sizeof(cnt));
  dp[0][1] = 0;
  cost = 0;
  for (int i = 1; i <= N; ++i) {
    cin >> a[i];
    cost += cnt[a[i]]++;
    dp[i][1] = cost;
  }
  L = 1, R = N;
  for (int i = 2; i <= K; ++i) {
    solve(1, N, 1, N, i);
  }
  cout << dp[N][K] << endl;
}
