Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.4 kB
14
Indexable
Never
// Author : RAJAT SHAHI (@blood_of_dragons)
// The wheels just keeps on turning... The drummers began to drum...

#include<bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include"/Users/rajat/Desktop/CPP codes/debug.h"
#endif

#ifdef ONLINE_JUDGE
#define debug(...) 42
#endif

#define endl '\n'
const int mod = 1e9 + 7;
#define rep(i,a,b) for(auto i=a;i<=b;i++)
#define per(i,a,b) for(auto i=a;i>=b;i--)
using ll = long long;
using vll = vector<long long>;
const int def_size = 100007;

const long long limit = 1e18;

int main()
{
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(ios::badbit | ios::failbit);

  long long n, x;
  string name;

  cin >> n >> x;
  cin >> name;

  long long ans = x;

  for (int i = 0; i < n; i++)
  {
    if (name[i] == 'U' and ans > 1)
    {
      ans = ans / 2;
    }
    else if (name[i] == 'R' and (2 * ans + 1) <= (limit))
    {
      ans = 2 * ans + 1;
    }
    else if (name[i] == 'L' and (2 * ans) <= (limit))
    {
      ans = 2 * ans;
    }
    else
    {

      long long curr_index = i;

      long long cnt_up = 0, cnt_down = 0;

      for (int j = curr_index; j < n; j++)
      {
        if (name[i] == 'U') cnt_up++;
        else cnt_down++;

        if ((cnt_up == cnt_down) or (j == n - 1))
        {
          i = j;
          break;
        }
      }
    }
  }

  cout << ans << endl;
}