neon365's blog

By neon365, history, 7 weeks ago, In English

Link for Problem :https://codeforces.com/problemset/problem/462/B Here is my Code:

include

include <bits/stdc++.h>

using namespace std;

int main() { long long n,k; char c; long long a[26]={0}; cin >> n >> k; for(long int i=0; i<n; i++) { cin >> c; a[c-'A']++; } sort(a,a+26); long long sum=0; for(int i=25;k>0 && i>=0; i--) { if(a[i]>=k) { sum+=(k*k); break; } sum+=(a[i]*a[i]); k-=a[i]; } cout << sum; return 0; }

 
 
 
 
  • Vote: I like it
  • -12
  • Vote: I do not like it

»
7 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Change the submission language to GNU C++11, your latest code is AC in that language.

»
7 weeks ago, # |
Rev. 3   Vote: I like it +12 Vote: I do not like it

This is the code from your latest submission, I only changed the line sum+=(long)(k*k); to sum+=(k*k);. I believe on Codeforces long has only 32 bits, so casting a 64-bit integer to a 32-bit integer might cause overflow.