Problem p03994 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define int long long int
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
#define dbg(...)                                                               \
  cerr << "> ";                                                                \
  ZZ(#__VA_ARGS__, __VA_ARGS__);
template <typename Arg1> void ZZ(const char *val1, Arg1 &&arg1) {
  cerr << val1 << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char *vals, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(vals + 1, ',');
  std::cerr.write(vals, comma - vals) << ": " << arg1 << " , ";
  ZZ(comma + 1, args...);
}

const int inf = INT_MAX;
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;

signed main() {
  IOS;
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  string s;
  cin >> s;
  int k;
  cin >> k;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'a')
      continue;
    if ('z' + 1 - s[i] <= k) {
      k -= ('z' + 1 - s[i]);
      s[i] = 'a';
    }
  }
  int len = s.length();
  while (k--) {
    s[len - 1]++;
    if (s[len - 1] > 'z')
      s[len - 1] = 'a';
  }
  cout << s << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define int long long int
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
#define dbg(...)                                                               \
  cerr << "> ";                                                                \
  ZZ(#__VA_ARGS__, __VA_ARGS__);
template <typename Arg1> void ZZ(const char *val1, Arg1 &&arg1) {
  cerr << val1 << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char *vals, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(vals + 1, ',');
  std::cerr.write(vals, comma - vals) << ": " << arg1 << " , ";
  ZZ(comma + 1, args...);
}

const int inf = INT_MAX;
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;

signed main() {
  IOS;
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  string s;
  cin >> s;
  int k;
  cin >> k;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'a')
      continue;
    if ('z' + 1 - s[i] <= k) {
      k -= ('z' + 1 - s[i]);
      s[i] = 'a';
    }
  }
  int len = s.length();
  k %= 26;
  while (k--) {
    s[len - 1]++;
    if (s[len - 1] > 'z')
      s[len - 1] = 'a';
  }
  cout << s << endl;
  return 0;
}