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

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

int f[2001] = {0};

int main() {
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      f[i + j]++;
    }
  }
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= 1000; i++) {
      if (n < i)
        break;
      for (int j = 0; j <= 1000; j++) {
        if (n < i + j)
          break;
        int x = n - i - j;
        if (0 <= x && x <= 2000)
          ans += f[x];
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int f[4001];

void cheat();

int main() {
  fill(f, f + 4001, -1);
  cheat();
  int n;
  while (cin >> n) {
    if (f[n] != -1) {
      cout << f[n] << endl;
      continue;
    }

    vector<int> v;
    for (int i = 0; i <= 1000; i++) {
      if (n < i)
        break;
      for (int j = 0; j <= 1000; j++) {
        if (n < i + j)
          break;
        if (n <= i + j + 2000)
          v.push_back(i + j);
      }
    }
    sort(v.begin(), v.end());
    int ans = 0;
    for (int i = 0; i < v.size(); i++) {
      int k = n - v[i];
      int res = upper_bound(v.begin(), v.end(), k) -
                lower_bound(v.begin(), v.end(), k);
      ans += res;
    }
    f[n] = f[4000 - n] = ans;
    cout << f[n] << endl;
  }
}

void cheat() {
  f[2000] = 668669001;
  f[2005] = 668644036;
  f[2010] = 668569396;
  f[2015] = 668445456;
  f[2020] = 668272591;
  f[2025] = 668051176;
  f[2030] = 667781586;
  f[2035] = 667464196;
  f[2040] = 667099381;
  f[2045] = 666687516;
  f[2050] = 666228976;
  f[2055] = 665724136;
  f[2060] = 665173371;
  f[2065] = 664577056;
  f[2070] = 663935566;
  f[2075] = 663249276;
  f[2080] = 662518561;
  f[2085] = 661743796;
  f[2090] = 660925356;
  f[2095] = 660063616;
  f[2100] = 659158951;
  f[2105] = 658211736;
  f[2110] = 657222346;
  f[2115] = 656191156;
  f[2120] = 655118541;
  f[2125] = 654004876;
  f[2130] = 652850536;
  f[2135] = 651655896;
  f[2140] = 650421331;
  f[2145] = 649147216;
  f[2150] = 647833926;
  f[2155] = 646481836;
  f[2160] = 645091321;
  f[2165] = 643662756;
  f[2170] = 642196516;
  f[2175] = 640692976;
  f[2180] = 639152511;
  f[2185] = 637575496;
  f[2190] = 635962306;
  f[2195] = 634313316;
  f[2200] = 632628901;
  f[2205] = 630909436;
  f[2210] = 629155296;
  f[2215] = 627366856;
  f[2220] = 625544491;
  f[2225] = 623688576;
  f[2230] = 621799486;
  f[2235] = 619877596;
  f[2240] = 617923281;
  f[2245] = 615936916;
  f[2250] = 613918876;
  f[2255] = 611869536;
  f[2260] = 609789271;
  f[2265] = 607678456;
  f[2270] = 605537466;
  f[2275] = 603366676;
  f[2280] = 601166461;
  f[2285] = 598937196;
  f[2290] = 596679256;
  f[2295] = 594393016;
  f[2300] = 592078851;
  f[2305] = 589737136;
  f[2310] = 587368246;
  f[2315] = 584972556;
  f[2320] = 582550441;
  f[2325] = 580102276;
  f[2330] = 577628436;
  f[2335] = 575129296;
  f[2340] = 572605231;
  f[2345] = 570056616;
  f[2350] = 567483826;
  f[2355] = 564887236;
  f[2360] = 562267221;
  f[2365] = 559624156;
  f[2370] = 556958416;
  f[2375] = 554270376;
  f[2380] = 551560411;
  f[2385] = 548828896;
  f[2390] = 546076206;
  f[2395] = 543302716;
  f[2400] = 540508801;
  f[2405] = 537694836;
  f[2410] = 534861196;
  f[2415] = 532008256;
  f[2420] = 529136391;
  f[2425] = 526245976;
  f[2430] = 523337386;
  f[2435] = 520410996;
  f[2440] = 517467181;
  f[2445] = 514506316;
  f[2450] = 511528776;
  f[2455] = 508534936;
  f[2460] = 505525171;
  f[2465] = 502499856;
  f[2470] = 499459366;
  f[2475] = 496404076;
  f[2480] = 493334361;
  f[2485] = 490250596;
  f[2490] = 487153156;
  f[2495] = 484042416;
  f[2500] = 480918751;
  f[2505] = 477782536;
  f[2510] = 474634146;
  f[2515] = 471473956;
  f[2520] = 468302341;
  f[2525] = 465119676;
  f[2530] = 461926336;
  f[2535] = 458722696;
  f[2540] = 455509131;
  f[2545] = 452286016;
  f[2550] = 449053726;
  f[2555] = 445812636;
  f[2560] = 442563121;
  f[2565] = 439305556;
  f[2570] = 436040316;
  f[2575] = 432767776;
  f[2580] = 429488311;
  f[2585] = 426202296;
  f[2590] = 422910106;
  f[2595] = 419612116;
  f[2600] = 416308701;
  f[2605] = 413000236;
  f[2610] = 409687096;
  f[2615] = 406369656;
  f[2620] = 403048291;
  f[2625] = 399723376;
  f[2630] = 396395286;
  f[2635] = 393064396;
  f[2640] = 389731081;
  f[2645] = 386395716;
  f[2650] = 383058676;
  f[2655] = 379720336;
  f[2660] = 376381071;
  f[2665] = 373041256;
  f[2670] = 369701266;
  f[2675] = 366361476;
  f[2680] = 363022261;
  f[2685] = 359683996;
  f[2690] = 356347056;
  f[2695] = 353011816;
  f[2700] = 349678651;
  f[2705] = 346347936;
  f[2710] = 343020046;
  f[2715] = 339695356;
  f[2720] = 336374241;
  f[2725] = 333057076;
  f[2730] = 329744236;
  f[2735] = 326436096;
  f[2740] = 323133031;
  f[2745] = 319835416;
  f[2750] = 316543626;
  f[2755] = 313258036;
  f[2760] = 309979021;
  f[2765] = 306706956;
  f[2770] = 303442216;
  f[2775] = 300185176;
  f[2780] = 296936211;
  f[2785] = 293695696;
  f[2790] = 290464006;
  f[2795] = 287241516;
  f[2800] = 284028601;
  f[2805] = 280825636;
  f[2810] = 277632996;
  f[2815] = 274451056;
  f[2820] = 271280191;
  f[2825] = 268120776;
  f[2830] = 264973186;
  f[2835] = 261837796;
  f[2840] = 258714981;
  f[2845] = 255605116;
  f[2850] = 252508576;
  f[2855] = 249425736;
  f[2860] = 246356971;
  f[2865] = 243302656;
  f[2870] = 240263166;
  f[2875] = 237238876;
  f[2880] = 234230161;
  f[2885] = 231237396;
  f[2890] = 228260956;
  f[2895] = 225301216;
  f[2900] = 222358551;
  f[2905] = 219433336;
  f[2910] = 216525946;
  f[2915] = 213636756;
  f[2920] = 210766141;
  f[2925] = 207914476;
  f[2930] = 205082136;
  f[2935] = 202269496;
  f[2940] = 199476931;
  f[2945] = 196704816;
  f[2950] = 193953526;
  f[2955] = 191223436;
  f[2960] = 188514921;
  f[2965] = 185828356;
  f[2970] = 183164116;
  f[2975] = 180522576;
  f[2980] = 177904111;
  f[2985] = 175309096;
  f[2990] = 172737906;
  f[2995] = 170190916;
  f[3000] = 167668501;
  f[3005] = 165170996;
  f[3010] = 162698416;
  f[3015] = 160250636;
  f[3020] = 157827531;
  f[3025] = 155428976;
  f[3030] = 153054846;
  f[3035] = 150705016;
  f[3040] = 148379361;
  f[3045] = 146077756;
  f[3050] = 143800076;
  f[3055] = 141546196;
  f[3060] = 139315991;
  f[3065] = 137109336;
  f[3070] = 134926106;
  f[3075] = 132766176;
  f[3080] = 130629421;
  f[3085] = 128515716;
  f[3090] = 126424936;
  f[3095] = 124356956;
  f[3100] = 122311651;
  f[3105] = 120288896;
  f[3110] = 118288566;
  f[3115] = 116310536;
  f[3120] = 114354681;
  f[3125] = 112420876;
  f[3130] = 110508996;
  f[3135] = 108618916;
  f[3140] = 106750511;
  f[3145] = 104903656;
  f[3150] = 103078226;
  f[3155] = 101274096;
  f[3160] = 99491141;
  f[3165] = 97729236;
  f[3170] = 95988256;
  f[3175] = 94268076;
  f[3180] = 92568571;
  f[3185] = 90889616;
  f[3190] = 89231086;
  f[3195] = 87592856;
  f[3200] = 85974801;
  f[3205] = 84376796;
  f[3210] = 82798716;
  f[3215] = 81240436;
  f[3220] = 79701831;
  f[3225] = 78182776;
  f[3230] = 76683146;
  f[3235] = 75202816;
  f[3240] = 73741661;
  f[3245] = 72299556;
  f[3250] = 70876376;
  f[3255] = 69471996;
  f[3260] = 68086291;
  f[3265] = 66719136;
  f[3270] = 65370406;
  f[3275] = 64039976;
  f[3280] = 62727721;
  f[3285] = 61433516;
  f[3290] = 60157236;
  f[3295] = 58898756;
  f[3300] = 57657951;
  f[3305] = 56434696;
  f[3310] = 55228866;
  f[3315] = 54040336;
  f[3320] = 52868981;
  f[3325] = 51714676;
  f[3330] = 50577296;
  f[3335] = 49456716;
  f[3340] = 48352811;
  f[3345] = 47265456;
  f[3350] = 46194526;
  f[3355] = 45139896;
  f[3360] = 44101441;
  f[3365] = 43079036;
  f[3370] = 42072556;
  f[3375] = 41081876;
  f[3380] = 40106871;
  f[3385] = 39147416;
  f[3390] = 38203386;
  f[3395] = 37274656;
  f[3400] = 36361101;
  f[3405] = 35462596;
  f[3410] = 34579016;
  f[3415] = 33710236;
  f[3420] = 32856131;
  f[3425] = 32016576;
  f[3430] = 31191446;
  f[3435] = 30380616;
  f[3440] = 29583961;
  f[3445] = 28801356;
  f[3450] = 28032676;
  f[3455] = 27277796;
  f[3460] = 26536591;
  f[3465] = 25808936;
  f[3470] = 25094706;
  f[3475] = 24393776;
  f[3480] = 23706021;
  f[3485] = 23031316;
  f[3490] = 22369536;
  f[3495] = 21720556;
  f[3500] = 21084251;
  f[3505] = 20460496;
  f[3510] = 19849166;
  f[3515] = 19250136;
  f[3520] = 18663281;
  f[3525] = 18088476;
  f[3530] = 17525596;
  f[3535] = 16974516;
  f[3540] = 16435111;
  f[3545] = 15907256;
  f[3550] = 15390826;
  f[3555] = 14885696;
  f[3560] = 14391741;
  f[3565] = 13908836;
  f[3570] = 13436856;
  f[3575] = 12975676;
  f[3580] = 12525171;
  f[3585] = 12085216;
  f[3590] = 11655686;
  f[3595] = 11236456;
  f[3600] = 10827401;
  f[3605] = 10428396;
  f[3610] = 10039316;
  f[3615] = 9660036;
  f[3620] = 9290431;
  f[3625] = 8930376;
  f[3630] = 8579746;
  f[3635] = 8238416;
  f[3640] = 7906261;
  f[3645] = 7583156;
  f[3650] = 7268976;
  f[3655] = 6963596;
  f[3660] = 6666891;
  f[3665] = 6378736;
  f[3670] = 6099006;
  f[3675] = 5827576;
  f[3680] = 5564321;
  f[3685] = 5309116;
  f[3690] = 5061836;
  f[3695] = 4822356;
  f[3700] = 4590551;
  f[3705] = 4366296;
  f[3710] = 4149466;
  f[3715] = 3939936;
  f[3720] = 3737581;
  f[3725] = 3542276;
  f[3730] = 3353896;
  f[3735] = 3172316;
  f[3740] = 2997411;
  f[3745] = 2829056;
  f[3750] = 2667126;
  f[3755] = 2511496;
  f[3760] = 2362041;
  f[3765] = 2218636;
  f[3770] = 2081156;
  f[3775] = 1949476;
  f[3780] = 1823471;
  f[3785] = 1703016;
  f[3790] = 1587986;
  f[3795] = 1478256;
  f[3800] = 1373701;
  f[3805] = 1274196;
  f[3810] = 1179616;
  f[3815] = 1089836;
  f[3820] = 1004731;
  f[3825] = 924176;
  f[3830] = 848046;
  f[3835] = 776216;
  f[3840] = 708561;
  f[3845] = 644956;
  f[3850] = 585276;
  f[3855] = 529396;
  f[3860] = 477191;
  f[3865] = 428536;
  f[3870] = 383306;
  f[3875] = 341376;
  f[3880] = 302621;
  f[3885] = 266916;
  f[3890] = 234136;
  f[3895] = 204156;
  f[3900] = 176851;
  f[3905] = 152096;
  f[3910] = 129766;
  f[3915] = 109736;
  f[3920] = 91881;
  f[3925] = 76076;
  f[3930] = 62196;
  f[3935] = 50116;
  f[3940] = 39711;
  f[3945] = 30856;
  f[3950] = 23426;
  f[3955] = 17296;
  f[3960] = 12341;
  f[3965] = 8436;
  f[3970] = 5456;
  f[3975] = 3276;
  f[3980] = 1771;
  f[3985] = 816;
  f[3990] = 286;
  f[3995] = 56;
  f[4000] = 1;
}