Problem p03994 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define rep(i, a) for (int i = 0; (i) < (int)(a); (i)++)
#define reps(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
#define rrep(i, a) for (int i = (int)a - 1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v)                                                              \
  sort(all(v));                                                                \
  (v).erase(unique(all(v)), v.end())
#define CIN(type, x)                                                           \
  type x;                                                                      \
  cin >> x
#define TRUE__ "YES"
#define FALSE__ "NO"
#define PRINT(f)                                                               \
  if ((f)) {                                                                   \
    cout << (TRUE__) << endl;                                                  \
  } else {                                                                     \
    cout << FALSE__ << endl;                                                   \
  }
#define RS resize
#define CINV(v, N)                                                             \
  do {                                                                         \
    v.RS(N);                                                                   \
    rep(i, N) cin >> v[i];                                                     \
  } while (0);
#define RCINV(v, N)                                                            \
  do {                                                                         \
    v.RS(N);                                                                   \
    rrep(i, N) cin >> v[i];                                                    \
  } while (0);

#define MOD 1000000007

void init();
void solve();

signed main() {
  init();
  solve();
}

string s;
int K;

void init() { cin >> s >> K; }

void update(int pos) {
  if (s[pos] == 'a')
    return;
  int t = 'z' - s[pos] + 1;
  if (t <= K) {
    K -= t;
    s[pos] = 'a';
  }
}

void solve() {
  rep(i, s.length()) update(i);
  while (K--) {
    s.back()++;
    if (s.back() > 'z')
      s.back() = 'a';
  }
  cout << s << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define rep(i, a) for (int i = 0; (i) < (int)(a); (i)++)
#define reps(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
#define rrep(i, a) for (int i = (int)a - 1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v)                                                              \
  sort(all(v));                                                                \
  (v).erase(unique(all(v)), v.end())
#define CIN(type, x)                                                           \
  type x;                                                                      \
  cin >> x
#define TRUE__ "YES"
#define FALSE__ "NO"
#define PRINT(f)                                                               \
  if ((f)) {                                                                   \
    cout << (TRUE__) << endl;                                                  \
  } else {                                                                     \
    cout << FALSE__ << endl;                                                   \
  }
#define RS resize
#define CINV(v, N)                                                             \
  do {                                                                         \
    v.RS(N);                                                                   \
    rep(i, N) cin >> v[i];                                                     \
  } while (0);
#define RCINV(v, N)                                                            \
  do {                                                                         \
    v.RS(N);                                                                   \
    rrep(i, N) cin >> v[i];                                                    \
  } while (0);

#define MOD 1000000007

void init();
void solve();

signed main() {
  init();
  solve();
}

string s;
int K;

void init() { cin >> s >> K; }

void update(int pos) {
  if (s[pos] == 'a')
    return;
  int t = 'z' - s[pos] + 1;
  if (t <= K) {
    K -= t;
    s[pos] = 'a';
  }
}

void solve() {
  rep(i, s.length()) update(i);
  K %= 26;
  while (K--) {
    s.back()++;
    if (s.back() > 'z')
      s.back() = 'a';
  }
  cout << s << endl;
}