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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, p[20], s[1 << 20], s1[1 << 10];

int main() {
  while (cin >> n) {
    if (!n)
      return 0;
    int n2 = n / 2;
    fill(s1, s1 + (1 << n2), 0);
    int sum = 0, dif = 1e+9;
    for (int i = 0; i < n; i++) {
      cin >> p[i];
      sum += p[i];
    }
    if (n < 11) {
      fill(s, s + (1 << n), 0);
      for (int i = 0; i < 1 << n; i++) {
        int sss = 0;
        for (int j = 0; j < n; j++)
          if ((i / (1 << j)) % 2)
            sss += p[j];
        dif = min(dif, abs(sss * 2 - sum));
      }
      cout << dif << endl;
    } else {
      for (int i = 0; i < 1 << n2; i++) {
        for (int j = 0; j < n2; j++)
          if (i >> j & 1)
            s1[i] += p[j];
      }
      sort(s1, s1 + (1 << n2));
      int ans = 1e+9;
      for (int i = 0; i < 1 << (n - n2); i++) {
        int ss = 0;
        for (int j = 0; j < n - n2; j++)
          if (i >> j & 1)
            ss += p[j + n2];
        ans = min(
            ans,
            min(abs((*lower_bound(s1, s1 + (1 << n2), (sum) / 2 - ss) + ss) *
                        2 -
                    sum),
                abs((*(lower_bound(s1, s1 + (1 << n2), (sum) / 2 - ss) - 1) +
                     ss) *
                        2 -
                    sum)));
        ans = min(
            ans,
            min(abs((*(lower_bound(s1, s1 + (1 << n2), (sum) / 2 - ss) + 1) +
                     ss) *
                        2 -
                    sum),
                abs((*(lower_bound(s1, s1 + (1 << n2), (sum) / 2 - ss) - 2) +
                     ss) *
                        2 -
                    sum)));
      }
      /*for(int i=0;i<1<<n;i++){
              for(int j=0;j<n;j++)if((i/(1<<j))%2)s[i]+=p[j];
              dif=min(dif,abs(s[i]*2-sum));
      }*/
      cout << ans << endl;
    }
    // sort(s,s+(1<<n));
    // for(int i=0;i<1<<n;i++)s[i]=s[i]*2-sum;
    /*int lb=0,ub=1<<n-1,prv=0;
    while(ub-lb>1){
            int mid=(lb+ub)/2;
            if(s[mid]==prv)break;
            if(s[mid]>=0)ub=mid;
            else lb=mid;
            prv=s[mid];
    }
    cout<<abs(s[ub])<<endl;*/
    // cout<<min(*lower_bound(s,s+(1<<n),0),abs(*(lower_bound(s,s+(1<<n),0)-1)))<<endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, p[20], s[1 << 20];

int main() {
  while (cin >> n) {
    if (!n)
      return 0;
    fill(s, s + (1 << n), 0);
    int sum = 0, dif = 1e+9;
    for (int i = 0; i < n; i++) {
      cin >> p[i];
      sum += p[i];
    }
    for (int i = 0; i < 1 << n; i++) {
      for (int j = 0; j < n; j++)
        if ((i / (1 << j)) % 2)
          s[i] += p[j];
      dif = min(dif, abs(s[i] * 2 - sum));
    }
    cout << dif << endl;
    // sort(s,s+(1<<n));
    // for(int i=0;i<1<<n;i++)s[i]=s[i]*2-sum;
    /*int lb=0,ub=1<<n-1,prv=0;
    while(ub-lb>1){
            int mid=(lb+ub)/2;
            if(s[mid]==prv)break;
            if(s[mid]>=0)ub=mid;
            else lb=mid;
            prv=s[mid];
    }
    cout<<abs(s[ub])<<endl;*/
    // cout<<min(*lower_bound(s,s+(1<<n),0),abs(*(lower_bound(s,s+(1<<n),0)-1)))<<endl;
  }
}