Problem p03994 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define SaveTime                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define mx *max_element
#define mi *min_element
#define fn for (int i = 0; i < n; i++)
#define fn1 for (int i = 1; i <= n; i++)
#define fm for (int j = 0; j < m; j++)
#define fm1 for (int j = 1; j <= m; j++)
#define fi first
#define se second
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
const int N = 1e6 + 5;
const int INF = 1e18L;
int ncr(int n, int r) {
  if (n < r)
    return 0;
  int p = 1, k = 1;
  if (n - r < r)
    r = n - r;
  if (r != 0) {
    while (r) {
      p *= n;
      k *= r;
      int g = __gcd(p, k);
      p /= g;
      k /= g;
      n--;
      r--;
    }
  } else
    p = 1;
  return p;
}
#define pi acos(-1)
int ar[N];
int tree[2 * N];
int dp[N];
int bit[N];
signed main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  SaveTime;
  int T = 1;
  // cin >> T;
  while (T--) {
    string s;
    cin >> s;
    int k;
    cin >> k;
    int n = s.size();
    int i = 0;
    while (i < n && k) {
      int c = s[i] - 'a';
      if (c) {
        if (k >= 26 - c) {
          s[i] = 'a';
          k -= (26 - c);
        }
      }
      i++;
    }

    for (int i = n - 1; k > 0 && i >= 0; i--) {
      while (k) {
        s[i] = (char)(s[i] + 1);
        if (s[i] > 'z')
          s[i] = 'a';
        --k;
      }
    }

    cout << s << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define SaveTime                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define mx *max_element
#define mi *min_element
#define fn for (int i = 0; i < n; i++)
#define fn1 for (int i = 1; i <= n; i++)
#define fm for (int j = 0; j < m; j++)
#define fm1 for (int j = 1; j <= m; j++)
#define fi first
#define se second
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
const int N = 1e6 + 5;
const int INF = 1e18L;
int ncr(int n, int r) {
  if (n < r)
    return 0;
  int p = 1, k = 1;
  if (n - r < r)
    r = n - r;
  if (r != 0) {
    while (r) {
      p *= n;
      k *= r;
      int g = __gcd(p, k);
      p /= g;
      k /= g;
      n--;
      r--;
    }
  } else
    p = 1;
  return p;
}
#define pi acos(-1)
int ar[N];
int tree[2 * N];
int dp[N];
int bit[N];
signed main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  SaveTime;
  int T = 1;
  // cin >> T;
  while (T--) {
    string s;
    cin >> s;
    int k;
    cin >> k;
    int n = s.size();
    int i = 0;
    while (i < n && k) {
      int c = s[i] - 'a';
      if (c) {
        if (k >= 26 - c) {
          s[i] = 'a';
          k -= (26 - c);
        }
      }
      i++;
    }
    s[n - 1] = (char)('a' + ((s[n - 1] - 'a' + k) % 26));
    cout << s << endl;
  }

  return 0;
}