#include <bits/stdc++.h>
using namespace std;
const double pi =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
const int MR = 1e5 + 10;
const long long inf = 1e18;
long long dp[21][MR];
int A[21][MR];
int a[MR];
int freq[MR], gp, gk;
long long cost;
long long ile(int b, int e) {
  while (gp > b) {
    cost += freq[a[gp - 1]];
    freq[a[gp - 1]]++;
    gp--;
  }
  while (gp < b) {
    freq[a[gp]]--;
    cost -= freq[a[gp]];
    gp++;
  }
  while (gk > e) {
    freq[a[gk - 1]]--;
    cost -= freq[a[gk - 1]];
    gk--;
  }
  while (gk < e) {
    cost += freq[a[gk]];
    freq[a[gk]]++;
    gk++;
  }
  return cost;
}
void compute(int k, int l, int r, int Al, int Ar) {
  if (l >= r) return;
  assert(Al <= Ar);
  int m = (l + r) / 2;
  dp[k][m] = inf;
  for (int i = (max(Al, A[k - 1][m])); i < (min(Ar + 1, m)); ++i) {
    long long cnt = ile(i + 1, m + 1);
    if (dp[k][m] > dp[k - 1][i] + cnt) {
      dp[k][m] = dp[k - 1][i] + cnt;
      A[k][m] = i;
    }
  }
  compute(k, l, m, Al, A[k][m]);
  compute(k, m + 1, r, A[k][m], Ar);
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]);
  gp = 1;
  gk = 1;
  for (int i = (1); i <= (n); ++i) {
    dp[1][i] = ile(1, i + 1);
    A[1][i] = 0;
  }
  for (int i = (2); i <= (k); ++i) compute(i, i, n + 1, i - 1, n);
  cout << dp[k][n] << endl;
  return 0;
}
