#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int) x.size()
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define FOR(i, a, b) for(auto i=(a); i<(b); i++)
#define ROF(i, a, b) for(auto i=(b)-1; i>=(a); i--)
#define F0R(i, n) FOR(i, 0, n)
#define R0F(i, n) ROF(i, 0, n)

using ll=long long;
using ld=long double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using vi=vector<int>;
using vl=vector<ll>;
using vpii=vector<pii>;

template<class T> bool ckmin(T&a, const T&b) {return b<a?a=b,1:0;}
template<class T> bool ckmax(T&a, const T&b) {return b>a?a=b,1:0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mxN=1<<18;
const int MOD=1e9+7; 
const ll infLL=1e18;
const ld eps=1e-6;

template<class V, int SZ> struct lazy{
    // Remember to change everything!
    const V no_op = infLL+1, ne = infLL; const V MADD = 0, VAL = 0, MSET = no_op;
    V val[2*SZ], madd[2*SZ], mset[2*SZ];
    void init() {F0R(i, 2*SZ) val[i] = VAL, mset[i] = MSET, madd[i] = MADD; }
    void build(vector<V>&v, int lo = 0, int hi = SZ, int ind = 1){
        if(lo+1<hi){
            int mid = lo+(hi-lo)/2;
            build(v, lo, mid, ind*2); build(v, mid, hi, ind*2+1);
            val[ind]=min(val[ind*2], val[ind*2+1]);
        }
        else val[ind] = (lo<siz(v)?v[lo]:ne);
    }
    V query(int L, int R, int ind = 1, int lo = 0, int hi = SZ){
        if(R <= lo || hi <= L) return ne;
        if(L <= lo && hi <= R) return val[ind];
        push(ind, lo, hi);
        int mid = lo+(hi-lo)/2;
        return min(query(L, R, ind*2, lo, mid), query(L, R, ind*2+1, mid, hi));
    }
    void set(int L, int R, V x, int ind = 1, int lo = 0, int hi = SZ){
        if(R <= lo || hi <= L) return;
        if(L <= lo && hi <= R) mset[ind] = x, val[ind] = mset[ind], madd[ind] = 0;
        else{
            push(ind, lo, hi); 
            int mid = lo+(hi-lo)/2;
            set(L, R, x, ind*2, lo, mid); set(L, R, x, ind*2+1, mid, hi);
            val[ind] = min(val[ind*2], val[ind*2+1]);
        }
    }
    void add(int L, int R, V x, int ind = 1, int lo = 0, int hi = SZ){
        if(R <= lo || hi <= L) return;
        if(L <= lo && hi <= R) {
            if(mset[ind] != no_op) mset[ind] += x;
            else madd[ind] += x;
            val[ind] += x;
        }
        else{
            push(ind, lo, hi); 
            int mid = lo+(hi-lo)/2;
            add(L, R, x, ind*2, lo, mid); add(L, R, x, ind*2+1, mid, hi);
            val[ind] = min(val[ind*2], val[ind*2+1]);
        }
    }
    void push(int ind, int lo, int hi){
        int mid = lo+(hi-lo)/2;
        if(mset[ind]!=no_op){
            set(lo, mid, mset[ind], ind*2, lo, mid);
            set(mid, hi, mset[ind], ind*2+1, mid, hi);
            mset[ind] = no_op;
        }
        else if(madd[ind] != 0){
            add(lo, mid, madd[ind], ind*2, lo, mid);
            add(mid, hi, madd[ind], ind*2+1, mid, hi);
            madd[ind] = 0;
        }
    }
};

int N, K;
int A[mxN];

lazy<ll, mxN> seg;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> N >> K;
    F0R(i, N) cin >> A[i];
    vl dp(N+1, infLL); dp[0]=0;
    F0R(i, K){
        seg.init(); seg.build(dp);
        vi lst(N+1, -1);
        vl ndp(N+1, 0);
        // F0R(j, N) cout << dp[j] << ' ';
        // cout << endl;
        F0R(j, N){
            int prev=lst[A[j]];
            lst[A[j]]=j;
            if(prev!=-1){
                seg.add(0, prev+1, j-prev);
            }
            ndp[j+1]=seg.query(0, j+1);
        }
        swap(ndp, dp);
    }
    cout << dp[N] << "\n";
    return 0; 
}


### Prompt

Please formulate a cpp solution to the following problem:

You are given an array a of n integers. Define the cost of some array t as follows:

$$$cost(t) = ∑_{x ∈ set(t) } last(x) - first(x),$$$ 

where set(t) is the set of all values in t without repetitions, first(x), and last(x) are the indices of the first and last occurrence of x in t, respectively. In other words, we compute the distance between the first and last occurrences for each distinct element and sum them up.

You need to split the array a into k consecutive segments such that each element of a belongs to exactly one segment and the sum of the cost of individual segments is minimum.

Input

The first line contains two integers n, k (1 ≤ n ≤ 35 000, 1 ≤ k ≤ min(n,100)).

The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).

Output

Output the minimum sum of the cost of individual segments.

Examples

Input


7 2
1 6 6 4 6 6 6


Output


3


Input


7 4
5 5 5 5 2 3 3


Output


1

Note

In the first example, we can divide the array into [1,6,6,4] and [6,6,6]. Cost of [1,6,6,4] will be (1-1) + (3 - 2) + (4-4) = 1 and cost of [6,6,6] will be 3-1 = 2. Total cost would be 1 + 2 = 3.

In the second example, divide the array into [5,5],[5],[5,2,3] and [3]. Total Cost would be 1 + 0 + 0 + 0 = 1.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int) x.size()
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define FOR(i, a, b) for(auto i=(a); i<(b); i++)
#define ROF(i, a, b) for(auto i=(b)-1; i>=(a); i--)
#define F0R(i, n) FOR(i, 0, n)
#define R0F(i, n) ROF(i, 0, n)

using ll=long long;
using ld=long double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using vi=vector<int>;
using vl=vector<ll>;
using vpii=vector<pii>;

template<class T> bool ckmin(T&a, const T&b) {return b<a?a=b,1:0;}
template<class T> bool ckmax(T&a, const T&b) {return b>a?a=b,1:0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mxN=1<<18;
const int MOD=1e9+7; 
const ll infLL=1e18;
const ld eps=1e-6;

template<class V, int SZ> struct lazy{
    // Remember to change everything!
    const V no_op = infLL+1, ne = infLL; const V MADD = 0, VAL = 0, MSET = no_op;
    V val[2*SZ], madd[2*SZ], mset[2*SZ];
    void init() {F0R(i, 2*SZ) val[i] = VAL, mset[i] = MSET, madd[i] = MADD; }
    void build(vector<V>&v, int lo = 0, int hi = SZ, int ind = 1){
        if(lo+1<hi){
            int mid = lo+(hi-lo)/2;
            build(v, lo, mid, ind*2); build(v, mid, hi, ind*2+1);
            val[ind]=min(val[ind*2], val[ind*2+1]);
        }
        else val[ind] = (lo<siz(v)?v[lo]:ne);
    }
    V query(int L, int R, int ind = 1, int lo = 0, int hi = SZ){
        if(R <= lo || hi <= L) return ne;
        if(L <= lo && hi <= R) return val[ind];
        push(ind, lo, hi);
        int mid = lo+(hi-lo)/2;
        return min(query(L, R, ind*2, lo, mid), query(L, R, ind*2+1, mid, hi));
    }
    void set(int L, int R, V x, int ind = 1, int lo = 0, int hi = SZ){
        if(R <= lo || hi <= L) return;
        if(L <= lo && hi <= R) mset[ind] = x, val[ind] = mset[ind], madd[ind] = 0;
        else{
            push(ind, lo, hi); 
            int mid = lo+(hi-lo)/2;
            set(L, R, x, ind*2, lo, mid); set(L, R, x, ind*2+1, mid, hi);
            val[ind] = min(val[ind*2], val[ind*2+1]);
        }
    }
    void add(int L, int R, V x, int ind = 1, int lo = 0, int hi = SZ){
        if(R <= lo || hi <= L) return;
        if(L <= lo && hi <= R) {
            if(mset[ind] != no_op) mset[ind] += x;
            else madd[ind] += x;
            val[ind] += x;
        }
        else{
            push(ind, lo, hi); 
            int mid = lo+(hi-lo)/2;
            add(L, R, x, ind*2, lo, mid); add(L, R, x, ind*2+1, mid, hi);
            val[ind] = min(val[ind*2], val[ind*2+1]);
        }
    }
    void push(int ind, int lo, int hi){
        int mid = lo+(hi-lo)/2;
        if(mset[ind]!=no_op){
            set(lo, mid, mset[ind], ind*2, lo, mid);
            set(mid, hi, mset[ind], ind*2+1, mid, hi);
            mset[ind] = no_op;
        }
        else if(madd[ind] != 0){
            add(lo, mid, madd[ind], ind*2, lo, mid);
            add(mid, hi, madd[ind], ind*2+1, mid, hi);
            madd[ind] = 0;
        }
    }
};

int N, K;
int A[mxN];

lazy<ll, mxN> seg;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> N >> K;
    F0R(i, N) cin >> A[i];
    vl dp(N+1, infLL); dp[0]=0;
    F0R(i, K){
        seg.init(); seg.build(dp);
        vi lst(N+1, -1);
        vl ndp(N+1, 0);
        // F0R(j, N) cout << dp[j] << ' ';
        // cout << endl;
        F0R(j, N){
            int prev=lst[A[j]];
            lst[A[j]]=j;
            if(prev!=-1){
                seg.add(0, prev+1, j-prev);
            }
            ndp[j+1]=seg.query(0, j+1);
        }
        swap(ndp, dp);
    }
    cout << dp[N] << "\n";
    return 0; 
}


```