#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <unordered_map>
#define ll long long
#define reg register
#define fo(a,b,c) for(reg ll a=b;a<=c;a++)
#define re(a,b,c) for(reg ll a=b;a>=c;a--)
#define pii pair<ll,ll>
#define fi first
#define se second
#define mod 998244353
using namespace std;
inline ll gi(){
ll x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
ll n,k;
ll a[100005],dp[100005][22],z,y,ans,t[100005];
ll ck(ll mz,ll my)
{
while(y<my)
{
y++;
ans+=t[a[y]];
t[a[y]]++;
}
while(z<mz)
{
t[a[z]]--;
ans-=t[a[z]];
z++;
}
while(y>my)
{
t[a[y]]--;
ans-=t[a[y]];
y--;
}
while(z>mz)
{
z--;
ans+=t[a[z]];
t[a[z]]++;
}
// cout<<z<<" "<<y<<" "<<ans<<'\n';
return ans;
}
void sol(ll num,ll l,ll r,ll pl,ll pr)
{
ll mid=(l+r)/2;
ll id=0,zs=999999999999999;
fo(i,pl,min(mid,pr))
{
// cout<<i+1<<" "<<mid<<'\n';
ll zzz=dp[i][num-1]+ck(i+1,mid);
if(zzz<zs)
{
id=i;
zs=zzz;
}
}
dp[mid][num]=zs;
if(l==r) return;
sol(num,l,mid,pl,id);
sol(num,mid+1,r,id,pr);
}
int main()
{
t[0]=1;
n=gi(),k=gi();
fo(i,1,n)
{
dp[i][0]=999999999999999;
a[i]=gi();
}
fo(i,1,k)
{
sol(i,1,n,0,n);
/* fo(j,1,n)
{
cout<<dp[j][i]<<" ";
}
cout<<'\n';*/
}
cout<<dp[n][k];
return 0;
}
559. Maximum Depth of N-ary Tree | 821. Shortest Distance to a Character |
1441. Build an Array With Stack Operations | 1356. Sort Integers by The Number of 1 Bits |
922. Sort Array By Parity II | 344. Reverse String |
1047. Remove All Adjacent Duplicates In String | 977. Squares of a Sorted Array |
852. Peak Index in a Mountain Array | 461. Hamming Distance |
1748. Sum of Unique Elements | 897. Increasing Order Search Tree |
905. Sort Array By Parity | 1351. Count Negative Numbers in a Sorted Matrix |
617. Merge Two Binary Trees | 1450. Number of Students Doing Homework at a Given Time |
700. Search in a Binary Search Tree | 590. N-ary Tree Postorder Traversal |
589. N-ary Tree Preorder Traversal | 1299. Replace Elements with Greatest Element on Right Side |
1768. Merge Strings Alternately | 561. Array Partition I |
1374. Generate a String With Characters That Have Odd Counts | 1822. Sign of the Product of an Array |
1464. Maximum Product of Two Elements in an Array | 1323. Maximum 69 Number |
832. Flipping an Image | 1295. Find Numbers with Even Number of Digits |
1704. Determine if String Halves Are Alike | 1732. Find the Highest Altitude |