Problem p02948 (C++)

Buggy submission (status: Time Limit Exceeded):
// abc137_d.cpp
// Sat Aug 10 20:49:51 2019

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return a.first > b.first;
  }
}

bool first_compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.first != b.first) {
    return a.first > b.first;
  } else {
    return a.second > b.second;
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<P> pair;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    pair.push_back(make_pair(a, b));
  }

  sort(pair.begin(), pair.end(), compare_by_b);

  ll day[m + 1];
  fill(day, day + m + 1, 0);
  ll smallestday = 1;
  ll biggestday = m;
  int flag = 0;
  for (int i = 0; i < n; i++) {
    for (int j = max(pair[i].first, smallestday); j <= m; j++) {
      if (day[j] == 0) {
        day[j] = pair[i].second;

        if (j == smallestday) {
          int smallflag = 1;
          for (int k = smallestday + 1; k <= m; k++) {
            if (day[k] == 0) {
              smallestday = k;
              smallflag = 0;
              break;
            }
          }
          if (smallflag) {
            flag = 1;
          }
        }

        if (j == biggestday) {
          for (int k = biggestday - 1; k >= 1; k--) {
            if (day[k] == 0) {
              biggestday = k;
              break;
            }
          }
        }
        break;
      }
    }
    if (flag) {
      break;
    }
  }

  ll ans = 0;
  for (int i = 1; i <= m; i++) {
    ans += day[i];
    //		cout << day[i] << " ";
  }

  cout << ans << endl;
}

Fixed submission:
// abc137_d.cpp
// Sat Aug 10 20:49:51 2019

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return a.first > b.first;
  }
}

bool first_compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.first != b.first) {
    return a.first > b.first;
  } else {
    return a.second > b.second;
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<P> pair;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    pair.push_back(make_pair(a, b));
  }

  sort(pair.begin(), pair.end(), compare_by_b);

  ll day[m + 1];
  fill(day, day + m + 1, 0);
  ll smallestday = 1;
  ll biggestday = m;
  int flag = 0;
  for (int i = 0; i < n; i++) {
    if (pair[i].first > biggestday) {
      continue;
    }
    for (int j = max(pair[i].first, smallestday); j <= biggestday; j++) {
      if (day[j] == 0) {
        day[j] = pair[i].second;

        if (j == smallestday) {
          int smallflag = 1;
          for (int k = smallestday + 1; k <= m; k++) {
            if (day[k] == 0) {
              smallestday = k;
              smallflag = 0;
              break;
            }
          }
          if (smallflag) {
            flag = 1;
          }
        }

        if (j == biggestday) {
          for (int k = biggestday - 1; k >= 1; k--) {
            if (day[k] == 0) {
              biggestday = k;
              break;
            }
          }
        }
        break;
      }
    }
    if (flag) {
      break;
    }
  }

  ll ans = 0;
  for (int i = 1; i <= m; i++) {
    ans += day[i];
    //		cout << day[i] << " ";
  }

  cout << ans << endl;
}