Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main():
    for line in stdin:
        n = int(line)
        count = 0

        for a in range((n if n < 1000 else 1000) + 1):
            for ab in range(a, a+1001):
                if ab > n:
                    break

                nab = n - ab
                start = nab if nab < 1000 else 1000
                end = (nab-1001) if (nab-1001) > -1 else -1
                combi = start - end
                if combi >= 0:
                    count += combi

        print(count)
    exit()


if __name__ == '__main__':
    main()

Fixed submission:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def make_combination_list():
    cmb = [0] * 4001
    count = minus = n = s = 0

    for i in range(1001):
        count += (i+1) * (i+2) // 2
        cmb[i] = cmb[4000-i] = count

    for i in range(1001, 2001):
        count += (i+1) * (i+2) // 2
        n += 1
        s += n
        minus += 4 * s
        cmb[i] = cmb[4000-i] = count - minus

    return cmb


def main():
    ans = make_combination_list()
    for line in stdin:
        print(ans[int(line)])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main():
    ans = [0] * 4001
    for line in stdin:
        n = int(line)

        if not ans[n]:
            count = 0
            for a in xrange((n if n < 1000 else 1000) + 1):
                e1 = n-a-1001 if n-a-1001 >= 0 else -1
                for nab in xrange(n-a, e1, -1):
                    ran = ((nab if nab < 1000 else 1000) -
                           ((nab-1001) if nab > 1000 else -1))
                    if ran >= 0:
                        count += ran

            ans[n] = count
        print(ans[n])
    exit()


if __name__ == '__main__':
    main()

Fixed submission:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def make_combination_list():
    cmb = [0] * 4001
    count = minus = n = s = 0

    for i in range(1001):
        count += (i+1) * (i+2) // 2
        cmb[i] = cmb[4000-i] = count

    for i in range(1001, 2001):
        count += (i+1) * (i+2) // 2
        n += 1
        s += n
        minus += 4 * s
        cmb[i] = cmb[4000-i] = count - minus

    return cmb


def main():
    ans = make_combination_list()
    for line in stdin:
        print(ans[int(line)])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def calc_combination(n):
    count = 0
    for a in xrange((n if n < 1000 else 1000) + 1):
        e1 = n-a-1001 if n-a-1001 >= 0 else -1
        for nab in xrange(n-a, e1, -1):
            ran = ((nab if nab < 1000 else 1000) -
                   ((nab-1001) if nab > 1000 else -1))
            if ran >= 0:
                count += ran
    return count


def main():
    ans = [0] * 4001
    for line in stdin:
        n = int(line)

        if not ans[n]:
            count = calc_combination(n)
            ans[n] = count
            ans[4000-n] = count
        print(ans[n])
    exit()


if __name__ == '__main__':
    main()

Fixed submission:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def make_combination_list():
    cmb = [0] * 4001
    count = minus = n = s = 0

    for i in range(1001):
        count += (i+1) * (i+2) // 2
        cmb[i] = cmb[4000-i] = count

    for i in range(1001, 2001):
        count += (i+1) * (i+2) // 2
        n += 1
        s += n
        minus += 4 * s
        cmb[i] = cmb[4000-i] = count - minus

    return cmb


def main():
    ans = make_combination_list()
    for line in stdin:
        print(ans[int(line)])


if __name__ == '__main__':
    main()