#include <bits/stdc++.h>

#define ll long long
#define el cout << '\n'
#define ii pair<ll, ll>
#define fi first
#define se second

using namespace std;

const int maxn = 1e5;
const int maxk = 20;
const ll INF = 1e18;

int n, k, l = 1, r = 0;
ll a[maxn + 10], dp[maxn + 10][maxk + 10], cnt[maxn + 10], quantity = 0;

void add(int p)
{
    quantity += cnt[a[p]];
    cnt[a[p]]++;
}
void del(int p)
{
    cnt[a[p]]--;
    quantity -= cnt[a[p]];
}
ll cost(int ql, int qr)
{
    while (l < ql) del(l++);
    while (l > ql) add(--l);
    while (r < qr) add(++r);
    while (r > qr) del(r--);
    return quantity;
}
void dnc(int j, int l, int r, int optl, int optr)
{
    if (l > r) return ;
    int m = l + r >> 1;
    ii best = {INF, -1};

    for (int z = optl; z <= min(m, optr); z++)
        best = min(best, {dp[z - 1][j - 1] + cost(z, m), z});

    dp[m][j] = best.fi;
    dnc(j, l, m - 1, optl, best.se);
    dnc(j, m + 1, r, best.se, optr);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen("YET.INP", "r"))
    {
        freopen("YET.INP", "r", stdin);
        freopen("YET.OUT", "w", stdout);
    }

    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= k; j++)
            dp[i][j] = INF;
    dp[0][0] = 0;
//    for (int j = 1; j <= k; j++)
//        for (int i = 1; i <= n; i++)
//            for (int z = 1; z <= i; z++)
//                dp[i][j] = min(dp[i][j], dp[z - 1][j - 1] + cost(z, i));
//
    for (int j = 1; j <= k; j++)
        dnc(j, 1, n, 1, n);
//    for (int j = 1; j <= k; j++)
//    {
//        for (int i = 1; i <= n; i++)
//            cout << dp[i][j] << ' ';
//        el;
//    }
    cout << dp[n][k];
}
