Problem p02943 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define lld long double
#define MOD 998244353LL
#define CAP 10000000
#define inf 1000000000000000000LL
#define pii pair<ll, ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v) v.begin(), v.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll power(ll x, ll y, ll md = MOD) {
  ll res = 1;
  x %= md;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % md;
    x = (x * x) % md;
    y = y >> 1;
  }
  return res % md;
}
#define dbg(x) cout << #x << ":" << x << endl;

#define int ll

ll min1() { return inf; }
ll max1() { return -inf; }

template <typename... Args> int min1(int a, Args... args) {
  return min(a, min1(args...));
}
template <typename... Args> int max1(int a, Args... args) {
  return max(a, max1(args...));
}

struct node {
  std::vector<ll> v, w;
};

string ans;
char mn;
int len;
string mmn;
int ff = 0;

void dfs(string s, int c, int k, int mul) {
  if (ff)
    return;
  if (c == k) {
    if (ans > s)
      ans = s;
    return;
  }
  int n = s.size();
  string t = s;
  reverse(t.begin(), t.end());
  string u = s + t;

  int st = 0;
  int end = n;

  if (c != k - 1 && c > 1) {
    for (int i = n - 1; i > -1; i--) {
      if (s[i] == mn) {
        int num = (n - i) * 2;
        st = i + num - n;
        if (st < 0)
          st = 0;
        end = st;
      } else
        break;
    }
  }

  for (int i = st; i <= end; i++) {
    string temp = u.substr(i, n);
    if (c == k - 1) {
      int cnt = 0;
      for (int i = 0; i < n; i++) {
        if (mn == temp[i])
          cnt++;
        else
          break;
      }
      if (cnt >= mul)
        dfs(temp, c + 1, k, mul * 2);
      continue;
    }
    int cnt = 0;
    for (int i = n - 1; i > -1; i--) {
      if (mn == temp[i])
        cnt++;
      else
        break;
    }
    int pp = cnt;
    for (int i = 0; i < k - 2 - c; i++) {
      pp *= 2;
    }
    if (pp >= n) {
      string temp = "";
      for (int i = 0; i < n; i++)
        temp += mn;
      ans = mn;
      ff = 1;
      return;
    }
    if (cnt >= mul)
      dfs(temp, c + 1, k, mul * 2);
  }
}

signed main() {
  fast;

  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;

  mn = s[0];
  len = 1;

  int cur = 1;
  for (int i = 1; i < n; i++) {
    if (s[i] == s[i - 1]) {
      cur++;
    } else {
      if (s[i - 1] < mn) {
        mn = s[i - 1];
        len = cur;
      } else if (s[i - 1] == mn) {
        len = max(len, cur);
      }
      cur = 1;
    }
  }
  int f = 0;
  if (s[n - 1] < mn) {
    mn = s[n - 1];
    len = cur;
  } else if (s[n - 1] == mn) {
    if (cur > len) {
      len = cur;
      f = 1;
    }
  }

  if (k + f > 14 || len == n) {
    for (int i = 0; i < n; i++)
      cout << mn;
    return 0;
  } else {
    int pp = len;
    for (int i = 0; i < k - 1 + f; i++) {
      pp *= 2;
      if (pp >= n) {
        for (int i = 0; i < n; i++)
          cout << mn;
        return 0;
      }
    }
  }

  mmn = s;

  ans = s;

  dfs(s, 0, k, len * (1 + f));
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define lld long double
#define MOD 998244353LL
#define CAP 10000000
#define inf 1000000000000000000LL
#define pii pair<ll, ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v) v.begin(), v.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll power(ll x, ll y, ll md = MOD) {
  ll res = 1;
  x %= md;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % md;
    x = (x * x) % md;
    y = y >> 1;
  }
  return res % md;
}
#define dbg(x) cout << #x << ":" << x << endl;

#define int ll

ll min1() { return inf; }
ll max1() { return -inf; }

template <typename... Args> int min1(int a, Args... args) {
  return min(a, min1(args...));
}
template <typename... Args> int max1(int a, Args... args) {
  return max(a, max1(args...));
}

struct node {
  std::vector<ll> v, w;
};

string ans;
char mn;
int len;
string mmn;
int ff = 0;

void dfs(string s, int c, int k, int mul) {
  if (ff)
    return;
  if (c == k) {
    if (ans > s)
      ans = s;
    return;
  }
  int n = s.size();
  string t = s;
  reverse(t.begin(), t.end());
  string u = s + t;

  int st = 0;
  int end = n;

  if (c != k - 1 && c != 0) {
    for (int i = n - 1; i > -1; i--) {
      if (s[i] == mn) {
        int num = (n - i) * 2;
        st = i + num - n;
        if (st < 0)
          st = 0;
        end = st;
      } else
        break;
    }
  }

  for (int i = st; i <= end; i++) {
    string temp = u.substr(i, n);
    if (c == k - 1) {
      int cnt = 0;
      for (int i = 0; i < n; i++) {
        if (mn == temp[i])
          cnt++;
        else
          break;
      }
      if (cnt >= mul)
        dfs(temp, c + 1, k, mul * 2);
      continue;
    }
    int cnt = 0;
    for (int i = n - 1; i > -1; i--) {
      if (mn == temp[i])
        cnt++;
      else
        break;
    }
    int pp = cnt;
    for (int i = 0; i < k - 2 - c; i++) {
      pp *= 2;
    }
    if (pp >= n) {
      string temp = "";
      for (int i = 0; i < n; i++)
        temp += mn;
      ans = mn;
      ff = 1;
      return;
    }
    if (cnt >= mul)
      dfs(temp, c + 1, k, mul * 2);
  }
}

signed main() {
  fast;

  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;

  mn = s[0];
  len = 1;

  int cur = 1;
  for (int i = 1; i < n; i++) {
    if (s[i] == s[i - 1]) {
      cur++;
    } else {
      if (s[i - 1] < mn) {
        mn = s[i - 1];
        len = cur;
      } else if (s[i - 1] == mn) {
        len = max(len, cur);
      }
      cur = 1;
    }
  }
  int f = 0;
  if (s[n - 1] < mn) {
    mn = s[n - 1];
    len = cur;
  } else if (s[n - 1] == mn) {
    if (cur > len) {
      len = cur;
      f = 1;
    }
  }

  if (k + f > 14 || len == n) {
    for (int i = 0; i < n; i++)
      cout << mn;
    return 0;
  } else {
    int pp = len;
    for (int i = 0; i < k - 1 + f; i++) {
      pp *= 2;
      if (pp >= n) {
        for (int i = 0; i < n; i++)
          cout << mn;
        return 0;
      }
    }
  }

  mmn = s;

  ans = s;

  dfs(s, 0, k, len * (1 + f));
  cout << ans;
}