-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_Minimum_sum.cpp
More file actions
50 lines (43 loc) · 895 Bytes
/
Find_Minimum_sum.cpp
File metadata and controls
50 lines (43 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// @author: Abhimanyu Maurya
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
//fast i/o
bool ib = ios_base::sync_with_stdio(0);
bool it = cin.tie(0);
bool ot = cout.tie(0);
int main()
{
int t, k, freq[26];
cin>>t;
while (t--)
{
memset(freq,0,sizeof(freq));
cin>>k;
string s;
cin>>s;
for (int i = 0; s[i]!='\0' ; i++)
{
freq[s[i]-'a']++;
}
sort(freq,freq+26,greater<int>());
while (k>0 and freq[0]>0)
{
freq[0]--;
k--;
sort(freq, freq + 26, greater<int>());
}
int sum = 0;
for (int i = 0; i <26; i++)
{
if(freq[i]>0)
{
sum+=(freq[i]*freq[i]);
}
else break;
}
cout<<sum<<"\n";
}
return 0;
}