#include <bits/stdc++.h>
using namespace std;
void setIO(const string &name) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin.exceptions(istream::failbit);
}
const int inf = 0x3f3f3f3f, mod = 1e9 + 7, maxn = 1e5 + 5, maxk = 22;
long long a[maxn], dp[maxn][maxk];
long long freq[maxn];
long long lp = 1, rp = 0, cost = 0;
void update(long long l, long long r) {
  while (rp < r) {
    rp++;
    cost += freq[a[rp]];
    freq[a[rp]]++;
  }
  while (lp > l) {
    lp--;
    cost += freq[a[lp]];
    freq[a[lp]]++;
  }
  while (rp > r) {
    freq[a[rp]]--;
    cost -= freq[a[rp]];
    rp--;
  }
  while (lp < l) {
    freq[a[lp]]--;
    cost -= freq[a[lp]];
    lp++;
  }
}
void solve(long long g, long long i, long long j, long long l, long long r) {
  if (i > j) {
    return;
  }
  long long mid = (i + j) / 2, best_ind = l;
  for (long long k = l; k <= min(mid, r); ++k) {
    update(k, mid);
    long long val = dp[k - 1][g - 1] + cost;
    if (val < dp[mid][g]) {
      dp[mid][g] = val;
      best_ind = k;
    }
  }
  solve(g, i, mid - 1, l, best_ind);
  solve(g, mid + 1, j, best_ind, r);
}
signed main() {
  setIO("1");
  long long n, k;
  cin >> n >> k;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (long long i = 1; i <= k; ++i) {
    solve(i, 1, n, 1, n);
  }
  cout << *min_element(dp[n] + 1, dp[n] + k + 1) << "\n";
  return 0;
}
