Problem p02786 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;

#define pi 3.14159265358979323846
#define VI vector<int>
#define VLL vector<long long>
#define MAX max_element
#define MIN min_element
#define all(v) v.begin(), v.end()
const ll MOD = 1e9 + 7;
using namespace std;
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

template <typename T> T binarysearch(vector<T> A, T key) {
  ll left = 0;
  ll right = 4;
  ll mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    if (key > A[mid])
      left = mid + 1;
    else if (key < A[mid])
      right = mid;
  }
  return 0;
}

template <typename T> T finder(vector<T> vec, T number) {
  auto itr = find(vec.begin(), vec.end(), number);
  size_t index = distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}
ll frac(ll n) // 階乗
{
  if (n == 0) {
    return 1;
  }
  return (n * frac(n - 1)) % MOD;
}

template <typename T> T keta(T a) {
  ll kazu = 1;
  while (1) {
    if (a / 10 != 0) {
      a /= 10;
      kazu++;
    } else
      break;
  }
  return kazu;
} // 桁数

template <typename T> T nCr(ll n, T r) {
  ll ans = 1;
  for (int i = n; i > n - r; --i) {
    ans = ans * i;
  }
  for (int i = 1; i < r + 1; ++i) {
    ans = ans / i;
  }
  return ans;
}
/*char r[4];
int yaju[4];
char op[3];
int sum = 0;
scanf("%s", r);
for (int i = 0;i < 4;i++) {
        yaju[i] = r[i] - '0';
}
*///map < string, ll> s;

/*if (N % 2 != 0)a = N / 2;
else a = N / 2 - 1;
for (int i = 1;i <= a;i++) {
        for (int j = 0;j < N - (i * 2);j++) {
                if (S[j] != S[j + i] && S[j + i * 2] != S[j] && S[j + i] != S[j
+ i * 2]) sum--; if (sum == 0)break;
        }
}*/

int main() {
  ll H, a;
  cin >> H;
  ll ans = 1;
  ll mon = 1;
  while (1) {
    H = H / 2;
    mon *= 2;
    ans += mon;
    if (H == 1)
      break;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;

#define pi 3.14159265358979323846
#define VI vector<int>
#define VLL vector<long long>
#define MAX max_element
#define MIN min_element
#define all(v) v.begin(), v.end()
const ll MOD = 1e9 + 7;
using namespace std;
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

template <typename T> T binarysearch(vector<T> A, T key) {
  ll left = 0;
  ll right = 4;
  ll mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    if (key > A[mid])
      left = mid + 1;
    else if (key < A[mid])
      right = mid;
  }
  return 0;
}

template <typename T> T finder(vector<T> vec, T number) {
  auto itr = find(vec.begin(), vec.end(), number);
  size_t index = distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}
ll frac(ll n) // 階乗
{
  if (n == 0) {
    return 1;
  }
  return (n * frac(n - 1)) % MOD;
}

template <typename T> T keta(T a) {
  ll kazu = 1;
  while (1) {
    if (a / 10 != 0) {
      a /= 10;
      kazu++;
    } else
      break;
  }
  return kazu;
} // 桁数

template <typename T> T nCr(ll n, T r) {
  ll ans = 1;
  for (int i = n; i > n - r; --i) {
    ans = ans * i;
  }
  for (int i = 1; i < r + 1; ++i) {
    ans = ans / i;
  }
  return ans;
}
/*char r[4];
int yaju[4];
char op[3];
int sum = 0;
scanf("%s", r);
for (int i = 0;i < 4;i++) {
        yaju[i] = r[i] - '0';
}
*///map < string, ll> s;

/*if (N % 2 != 0)a = N / 2;
else a = N / 2 - 1;
for (int i = 1;i <= a;i++) {
        for (int j = 0;j < N - (i * 2);j++) {
                if (S[j] != S[j + i] && S[j + i * 2] != S[j] && S[j + i] != S[j
+ i * 2]) sum--; if (sum == 0)break;
        }
}*/

int main() {
  ll H, a;
  cin >> H;
  ll ans = 1;
  ll mon = 1;
  if (H == 1)
    ans = 1;
  else {
    while (1) {
      H = H / 2;
      mon *= 2;
      ans += mon;
      if (H == 1)
        break;
    }
  }
  cout << ans << endl;
}