fork download
  1. //Coded by Sanjay Shukla
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int t,n;
  9. cin>>t;
  10. while(t--)
  11. {
  12. int p,sum=0,min_sum=0,max_sum=0;
  13. string str;
  14.  
  15. int count=0,count1=0;
  16. cin >> n >> p;
  17.  
  18. cin >> str;
  19. sort(str.begin(), str.end()); //sorting strings
  20. for(int i=0;count1<n && i<str.length();++i)
  21. {
  22. if(str[i]%p != 0)
  23. {
  24. min_sum += int(str[i]);
  25. count1++;
  26. }
  27.  
  28.  
  29. }
  30.  
  31. for(int j=str.length()-1; count<n && j>=0 ;j--)
  32. {
  33. if(str[j]%p !=0 )
  34. {
  35. max_sum += int(str[j]);
  36. count++;
  37. }
  38.  
  39. }
  40.  
  41. //Checking for base cases
  42. if ((max_sum==0 && min_sum==0 && p!= 1 ) || (count1 <n || count <n)
  43. || (n>str.length() || p==1))
  44. {
  45. cout<<"-1"<<endl;
  46. }
  47.  
  48. else
  49. cout<<min_sum<<" "<<max_sum<<endl;
  50. }
  51. }
  52.  
Success #stdin #stdout 0s 4512KB
stdin
2
2 2 
alex
5 2
john
stdout
198 198
-1