Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
// #define TEST
// //*******************************************************************************************************************************************

//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

// void deleteX(list<uint64_t> &list, int x) {
//     for(auto itr=list.begin(); itr!=list.end(); itr++ ) {
//         if(*itr==x) {
//             list.erase(itr);
//             break;
//         }
//     }
// }

int main(void) {
  list<uint64_t> list;
  uint64_t n;

  cin >> n;
  string order;
  uint64_t x;

  for (size_t i = 1; i <= n; i++) {

    if (!(cin >> order))
      break;

    //        dout << order << " " << x << endl;

    if (order[0] == 'i') {
      cin >> x;
      list.insert(list.begin(), x);
      //            list.push_front(x);
      continue;
    }
    if (order.size() == 6) {
      cin >> x;
      //            deleteX(list, x);
      auto it = find(list.begin(), list.end(), x);
      if (it != list.end())
        list.erase(it);
      continue;
    }
    if (order.size() == 11) {
      list.pop_front();
      continue;
    }
    if (order.size() == 10) {
      list.pop_back();
      continue;
    }
  }

  // display list
  //    dout << "---- display list ---- \\n";
  for (auto itr = list.begin(); itr != list.end(); itr++) {
    if (itr == list.begin()) {
      cout << *itr;
    } else {
      cout << " " << *itr;
    }
  }
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
// #define TEST
// //*******************************************************************************************************************************************

//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

// void deleteX(list<uint64_t> &list, int x) {
//     for(auto itr=list.begin(); itr!=list.end(); itr++ ) {
//         if(*itr==x) {
//             list.erase(itr);
//             break;
//         }
//     }
// }

int main(void) {
  list<uint64_t> list;
  uint64_t n;

  cin >> n;

  for (size_t i = 1; i <= n; i++) {
    string order;
    uint64_t x;

    if (!(cin >> order))
      break;

    //        dout << order << " " << x << endl;

    if (order[0] == 'i') {
      cin >> x;
      list.insert(list.begin(), x);
      //            list.push_front(x);
      continue;
    }
    if (order.size() == 6) {
      cin >> x;
      //            deleteX(list, x);
      auto it = find(list.begin(), list.end(), x);
      if (it != list.end())
        list.erase(it);
      continue;
    }
    if (order.size() == 11) {
      list.pop_front();
      continue;
    }
    if (order.size() == 10) {
      list.pop_back();
      continue;
    }
  }

  // display list
  //    dout << "---- display list ---- \\n";
  for (auto itr = list.begin(); itr != list.end(); itr++) {
    if (itr == list.begin()) {
      cout << *itr;
    } else {
      cout << " " << *itr;
    }
  }
  cout << endl;

  return 0;
}