#include <bits/stdc++.h>
using namespace std;
inline long long f(int x) { return ((long long)x * (x - 1)) / 2; }
const int N = 1e5 + 20;
const int K = 21;
long long dp[N][K];
int ar[N], cnt[N];
long long ans = 0;
int cum = 1, fisting = 1;
inline void upd(int l, int r) {
  while (cum > l) {
    --cum;
    ans -= f(cnt[ar[cum]]);
    ++cnt[ar[cum]];
    ans += f(cnt[ar[cum]]);
  }
  while (cum < l) {
    ans -= f(cnt[ar[cum]]);
    --cnt[ar[cum]];
    ans += f(cnt[ar[cum]]);
    ++cum;
  }
  while (fisting < r) {
    ++fisting;
    ans -= f(cnt[ar[fisting]]);
    ++cnt[ar[fisting]];
    ans += f(cnt[ar[fisting]]);
  }
  while (fisting > r) {
    ans -= f(cnt[ar[fisting]]);
    --cnt[ar[fisting]];
    ans += f(cnt[ar[fisting]]);
    --fisting;
  }
}
void DC(int k, int l, int r, int L, int R) {
  if (l > r) return;
  int m = l + r >> 1;
  upd(min(R, m), m);
  pair<long long, long long> best = {1e18, 1e18};
  for (int j = min(R, m); j >= L; --j) {
    upd(j, m);
    if (best > make_pair(dp[j - 1][k - 1] + ans, (long long)j)) {
      best = make_pair(dp[j - 1][k - 1] + ans, j);
    }
  }
  dp[m][k] = best.first;
  DC(k, l, m - 1, L, best.second);
  DC(k, m + 1, r, best.second, R);
}
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) cin >> ar[i];
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans -= f(cnt[ar[i]]);
    ++cnt[ar[i]];
    ans += f(cnt[ar[i]]);
    dp[i][1] = ans;
  }
  for (int i = 2; i <= n; ++i) --cnt[ar[i]];
  for (int i = 2; i <= k; ++i) DC(i, 1, n, 1, n);
  cout << dp[n][k];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  while (q--) solve();
}
