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

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

int solve(int *, int, int);
int main() {
  int n, points;
  while (cin >> n >> points, n != 0 && points != 0) {
    int darts[n + 1]; // 投げない場合を考えるため、1つだけ多く取っておく
    darts[0] = 0; // 投げない場合
    for (int i = 1; i <= n; i++)
      cin >> darts[i];

    // ダーツを2回投げた時の場合を考える
    // ループを抜けた後に cnt=(n+1)*(n+1)
    int cnt = 0;
    int set[(n + 1) * (n + 1)];
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        set[cnt++] = darts[i] + darts[j];
      }
    }

    // upper_boundを使うためソート
    sort(set, set + (n + 1) * (n + 1));

    int ans = 0;
    for (int i = 0; i < (n + 1) * (n + 1); i++) {
      int aim = points - set[i]; // 目標
      if (aim < 0)
        continue;
      int last = *(upper_bound(set, set + (n + 1) * (n + 1), aim) - 1);
      if (set[i] + aim <= points)
        ans = max(ans, set[i] + last);
    }

    printf("%d\\n", solve(darts, n + 1, points));
  }

  return 0;
}

/*
int solve(int* darts, int size, int point){
  int temp[size*size];
  int k=0;
  //2本投げ終わった後の合計点数を探索
  for(int i=0; i<size; i++){
    for(int j=0; j<size; j++){
      temp[k++]= darts[i]+darts[j];
    }
  }

  sort(temp, temp+size*size);

  int ans=0;
  for(int i=0; i<size*size; i++){
    int aim = point-temp[i];
    if(aim<0) continue;
    int last = *(upper_bound(temp, temp+size*size, aim)-1);
    if(temp[i]+last<=point) ans=max(ans, temp[i]+last);
  }

  return ans;
}
*/

int solve(int *darts, int size, int points) {
  // ダーツを2回投げた時の場合を考える
  // ループを抜けた後に cnt=(n+1)*(n+1)
  int cnt = 0;
  int set[size * size];
  for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
      set[cnt++] = darts[i] + darts[j];
    }
  }

  // upper_boundを使うためソート
  sort(set, set + size * size);

  int ans = 0;
  for (int i = 0; i < size * size; i++) {
    int aim = points - set[i]; // 目標
    if (aim < 0)
      continue;
    int last = *(upper_bound(set, set + size * size, aim) - 1);
    if (set[i] + aim <= points)
      ans = max(ans, set[i] + last);
  }
  return ans;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int solve(vector<int>, int);
int main() {
  int n, points;
  vector<int> ans;
  while (cin >> n >> points, n != 0 && points != 0) {
    vector<int> darts;
    darts.push_back(0); // 投げない場合を含めておく
    for (int i = 0; i < n; i++) {
      int temp;
      cin >> temp;
      darts.push_back(temp);
    }
    ans.push_back(solve(darts, points));
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

int solve(vector<int> darts, int point) {
  vector<int> temp;
  // 2本投げ終わった後の合計点数を探索
  for (int i = 0; i < darts.size(); i++) {
    for (int j = 0; j < darts.size(); j++) {
      temp.push_back(darts[i] + darts[j]);
      sort(temp.begin(), temp.end());
    }
  }

  int ans = 0;
  for (int i = 0; i < darts.size() * darts.size(); i++) {
    int aim = point - temp[i];
    int last = *(upper_bound(temp.begin(), temp.end(), aim) - 1);
    if (temp[i] + last <= point)
      ans = max(ans, temp[i] + last);
  }

  return ans;
}