fork(4) download
  1. // Bear and String Distance, by Errichto
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int nax = 1e6 + 5;
  6. char s[nax];
  7.  
  8. int main() {
  9. int n, k;
  10. scanf("%d%d", &n, &k);
  11. scanf("%s", s);
  12. for(int i = 0; i < n; ++i) {
  13. // we want to change s[i]
  14. char best_letter = s[i]; // by default we don't change it at all
  15. int best_distance = 0;
  16. for(char maybe = 'a'; maybe <= 'z'; ++maybe) {
  17. int distance = abs(maybe - s[i]);
  18. // we must check if "distance <= k" because we don't want to exceed the total distance
  19. // among letters with "distance <= k" we choose the most distant one
  20. if(distance <= k && distance > best_distance) {
  21. best_distance = distance;
  22. best_letter = maybe;
  23. }
  24. }
  25. k -= best_distance; // we decrease the remaining distance
  26. s[i] = best_letter;
  27. }
  28. assert(k >= 0);
  29. // we found a correct s' only if "k == 0"
  30. if(k > 0) puts("-1");
  31. else printf("%s\n", s);
  32. return 0;
  33. }
  34.  
Runtime error #stdin #stdout 0.07s 4432KB
stdin
Standard input is empty
stdout
Standard output is empty