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

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> *points;
vector<int> *twice;

void deldup(vector<int> *vec) {
  vector<int>::iterator it = vec->begin();
  bool is_straight = false;
  int save = -1;

  while (it != vec->end()) {
    if (*it == save) {
      vec->erase(it++);
      is_straight = true;
      continue;
    } else {
      save = *it;
      if (is_straight) {
        vec->erase(it++);
        is_straight = false;
        continue;
      }
      is_straight = false;
    }
    it++;
  }
}

int search(vector<int> *target, int standard) {
  int ind;
  int min = 0;
  int max = target->size() - 1;

  while (1) {
    ind = (min + max) / 2;

    if (max - min <= 1)
      return target->at(min);

    if (target->at(ind) <= standard) {
      min = ind;
    } else {
      max = ind;
    }
  }
}

int main() {
  while (1) {
    int i;
    int n, m;
    int max = 0;

    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      return 0;

    points = new vector<int>();
    twice = new vector<int>();

    points->push_back(0);
    for (i = 0; i < n; i++) {
      int k;
      scanf("%d", &k);
      points->push_back(k);
    }

    vector<int>::iterator it1 = points->begin();
    while (it1 != points->end()) {
      vector<int>::iterator it2 = points->begin();
      while (it2 != points->end()) {
        twice->push_back(*it1 + *it2);
        it2++;
      }
      it1++;
    }

    sort(points->begin(), points->end());
    sort(twice->begin(), twice->end());

    vector<int>::reverse_iterator rit1 = twice->rbegin();
    while (rit1 != twice->rend()) {
      if (*rit1 <= m) {
        int got = *rit1 + search(twice, m - *rit1);
        if (max < got)
          max = got;
      }
      rit1++;
    }

    printf("%d\\n", max);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> *points;
vector<int> *twice;

void deldup(vector<int> *vec) {
  vector<int>::iterator it = vec->begin();
  bool is_straight = false;
  int save = -1;

  while (it != vec->end()) {
    if (*it == save) {
      vec->erase(it++);
      is_straight = true;
      continue;
    } else {
      save = *it;
      if (is_straight) {
        vec->erase(it++);
        is_straight = false;
        continue;
      }
      is_straight = false;
    }
    it++;
  }
}

int search(vector<int> *target, int standard) {
  int ind;
  int min = 0;
  int max = target->size() - 1;

  while (1) {
    ind = (min + max) / 2;

    if (min + 1 == max)
      return target->at(min);

    if (target->at(ind) <= standard) {
      min = ind;
    } else {
      max = ind;
    }
  }
}

int main() {
  while (1) {
    int i;
    int n, m;
    int max = 0;

    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      return 0;

    points = new vector<int>();
    twice = new vector<int>();

    points->push_back(0);
    for (i = 0; i < n; i++) {
      int k;
      scanf("%d", &k);
      points->push_back(k);
    }

    vector<int>::iterator it = points->begin();
    while (it != points->end()) {
      vector<int>::iterator it2 = points->begin();
      while (it2 != points->end()) {
        twice->push_back(*it + *it2);
        it2++;
      }
      it++;
    }

    sort(twice->begin(), twice->end());
    deldup(twice);

    vector<int>::reverse_iterator it1 = twice->rbegin();
    while (it1 != twice->rend()) {
      if (*it1 <= m) {
        int got = *it1 + search(twice, m - *it1);
        if (max < got)
          max = got;
      }
      it1++;
    }

    printf("%d\\n", max);
  }
}