#include <bits/stdc++.h>
using namespace std;
const long long mxN = 100000;
const long long mxK = 21;
long long n, k;
long long a[mxN], dp[mxN][mxK], freq[mxN];
long long cl = 1, cr = 0, cval = 0;
void getVal(long long l, long long r) {
  while (cl < l) {
    freq[a[cl]]--;
    cval -= freq[a[cl]];
    ++cl;
  }
  while (cl > l) {
    --cl;
    cval += freq[a[cl]];
    ++freq[a[cl]];
  }
  while (cr < r) {
    ++cr;
    cval += freq[a[cr]];
    ++freq[a[cr]];
  }
  while (cr > r) {
    --freq[a[cr]];
    cval -= freq[a[cr]];
    --cr;
  }
}
void solve(long long ck, long long l, long long r, long long ql, long long qr) {
  long long m = (l + r) / 2;
  if (dp[m][ck] != -1e18) return;
  if (l > r) return;
  dp[m][ck] = dp[m][ck - 1];
  long long ans = 1e18, ind = -1;
  for (long long i = min(m - 1, qr); i >= ql; --i) {
    getVal(i + 1, m);
    if (cval + dp[i][ck - 1] < ans) {
      ans = dp[i][ck - 1] + cval;
      ind = i;
    }
  }
  if (ind != -1) {
    dp[m][ck] = min(dp[m][ck], ans);
  } else
    return;
  if (l == r) return;
  solve(ck, l, m, ql, ind);
  solve(ck, m + 1, r, ind, qr);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  long long amt[n];
  fill(amt, amt + n, 0);
  long long cur = 0;
  for (long long i = 0; i < n; ++i) {
    cur += amt[a[i]];
    ++amt[a[i]];
    dp[i][1] = cur;
  }
  for (long long i = 2; i <= k; ++i) {
    for (long long j = 0; j < n; ++j) {
      dp[j][i] = -1e18;
    }
    solve(i, 0, n - 1, 0, n - 1);
  }
  cout << max(0LL, dp[n - 1][k]) << "\n";
}
