alexjercan/bugnet huggingface dataset:
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    N, M, *a = map(int, open(0).read().split())
    div = 1000000007
    broken = set(a)

    comb_list = [0] * (N + 1)
    comb_list[0] = 1
    if 1 not in broken:
        comb_list[1] = 1

    for i in range(2, N + 1):
        if i in broken:
            continue
        comb_list[i] = (comb_list[i - 1] + comb_list[i - 2]) % div

    print(comb_list[-1])


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

n, m = map(int, input().split())
a = [int(input()) for i in range(m)]

f = [0] * (n + 1)
f[0] = 1

for i in range(1, n + 1):
    if i in a:
        continue
    f[i] = (f[i - 1] + f[i - 2]) % 1000000007

print(f[n])
Problem p03013 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())

stairs = []
is_continuous = False
for i in range(m):
    stairs.append(int(input()))
    if i > 0 and stairs[i] - 1 == stairs[i - 1]:
        is_continuous = True
        break

if is_continuous:
    print(0)
else:
    countup_num = [0] * (n + 1)
    countup_num[0] = 1
    countup_num[1] = 1
    for i in range(2, n + 1):
        countup_num[i] = (countup_num[i - 1] + countup_num[i - 2]) % 1000000007
    res = 1
    diff = 0
    now = 0
    for i in range(m):
        diff = stairs[i] - now - 1
        now = stairs[i] + 1
        res *= (countup_num[diff]) % 1000000007
        # print(res, now)
    if m == 0:
        res = countup_num[n]
    else:
        diff = n - stairs[-1] - 1
        res *= (countup_num[diff]) % 1000000007

    res %= 1000000007
    # print(countup_num)
    print(res)


Error: 0

Fixed submission:

n, m = map(int, input().split())

stairs = []
is_continuous = False
for i in range(m):
    stairs.append(int(input()))
    if i > 0 and stairs[i] - 1 == stairs[i - 1]:
        is_continuous = True
        break

if is_continuous:
    print(0)
else:
    countup_num = [0] * (n + 1)
    countup_num[0] = 1
    countup_num[1] = 1
    for i in range(2, n + 1):
        countup_num[i] = (countup_num[i - 1] + countup_num[i - 2]) % 1000000007
    res = 1
    diff = 0
    now = 0
    for i in range(m):
        diff = stairs[i] - now - 1
        now = stairs[i] + 1
        res *= (countup_num[diff]) % 1000000007
        # print(res, now)
    diff = n - stairs[-1] - 1
    res *= (countup_num[diff]) % 1000000007

    res %= 1000000007
    # print(countup_num)
    print(res)
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    [N, M] = list(map(lambda x: int(x), inputs[0].split()))
    broke_steps = {}
    for b in map(lambda x: int(x), inputs[1:]):
        broke_steps[b] = True

    SSSS = 1000000007

    patterns = [1]
    for step in range(1, N + 1):
        if step in broke_steps:
            ap = 0
        elif step == 1:
            ap = 1
        else:
            ap = (patterns[step - 1] + patterns[step - 2]) % SSSS
        patterns.append(ap)

    return patterns[-1]


if __name__ == "__main__":
    first = input()
    [N, M] = list(map(lambda x: int(x), first.split()))
    inputs_str = inputs(M)
    inputs_str.insert(0, first)
    ret = solve(inputs_str)
    print(ret)


Error: TLE

Fixed submission:

def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    [N, M] = list(map(lambda x: int(x), inputs[0].split()))
    broke_steps = list(map(lambda x: int(x), inputs[1:]))

    SSSS = 1000000007

    patterns = [1]
    for step in range(1, N + 1):
        if step in broke_steps:
            ap = 0
        elif step == 1:
            ap = 1
        else:
            ap = (patterns[step - 1] + patterns[step - 2]) % SSSS
        patterns.append(ap)

    return patterns[-1]


if __name__ == "__main__":
    first = input()
    [N, M] = list(map(lambda x: int(x), first.split()))
    inputs_str = inputs(M)
    inputs_str.insert(0, first)
    ret = solve(inputs_str)
    print(ret)
Problem p03013 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())

s = [0] * (n + 1)
s[-1] = 1
for _ in range(m):
    a = int(input())
    s[a] = None

if s[0] is None and s[1] is None:
    print(0)
    exit()

for i in range(n - 1, -1, -1):
    if s[i] is None:
        continue
    if i + 1 < len(s) and s[i + 1] is not None:
        s[i] += s[i + 1]
    if i + 2 < len(s) and s[i + 2] is not None:
        s[i] += s[i + 2]

print(s[0] % 1000000007)


Error: 0

Fixed submission:

n, m = map(int, input().split())

s = [0] * (n + 1)
s[-1] = 1
for _ in range(m):
    a = int(input())
    s[a] = None

if s[0] is None and s[1] is None:
    print(0)
    exit()

for i in range(n - 1, -1, -1):
    if s[i] is None:
        continue
    if i + 1 < len(s) and s[i + 1] is not None:
        s[i] += s[i + 1]
    if i + 2 < len(s) and s[i + 2] is not None:
        s[i] += s[i + 2]

print(s[0] % 1_000_000_007)
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

mod = 10**9 + 7

n, m = list(map(int, input().split()))
a = set([int(input()) for _ in range(m)])

dp = {}
for i in range(n + 1):
    dp[i] = 1
dp[1] = 0 if 1 in a else 1
for i in range(2, n + 1):
    if i in a:
        dp[i] = 0
    else:
        dp[i] = dp[i - 1] % mod + dp[i - 2] % mod
        dp[i] %= mod

print(dp[n] % mod)


Error: TLE

Fixed submission:

mod = 10**9 + 7

n, m = list(map(int, input().split()))
a = [int(input()) for _ in range(m)]

dp = {}
for i in range(n + 1):
    dp[i] = 1
dp[1] = 0 if 1 in a else 1
for i in range(2, n + 1):
    if i in a:
        dp[i] = 0
    else:
        dp[i] = dp[i - 1] % mod + dp[i - 2] % mod
        dp[i] %= mod

print(dp[n] % mod)
Problem p03013 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
b = [0, 1]
c = []
ans = 1

for _ in range(n):
    b.append(b[-1] + b[-2])

if m == 0:
    ans = b[n + 1]
else:
    c.append(a[0])
    for i in range(m - 1):
        c.append(a[i + 1] - a[i] - 1)
    c.append(n - a[m - 1])

    for i in c:
        ans *= b[i]

print(ans % 1000000007)


Error: 0

Fixed submission:

n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
b = [0, 1]
c = []
ans = 1

for _ in range(n):
    b.append(b[-1] + b[-2])

c.append(a[0])
for i in range(m - 1):
    c.append(a[i + 1] - a[i] - 1)
c.append(n - a[m - 1])

for i in c:
    ans *= b[i]

print(ans % 1000000007)
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
import sys

sys.setrecursionlimit(1000000)


memo = [0] * (10**5 + 1)


def count(n):
    if n < 2:
        return 1
    if memo[n] != 0:
        return memo[n]
    memo[n] = count(n - 1) + count(n - 2)
    return memo[n]


n, m, *a = map(int, open(0).read().split())
if sum(a[i + 1] == a[i] + 1 for i in range(m - 1)) > 0:
    print(0)
    exit()

a.append(n + 1)
c = -1
ans = 1
for i in a:
    ans = ans * count(i - c - 2) % (10**9 + 7)
    c = i
print(ans)


Error: TLE

Fixed submission:

#!/usr/bin/env python3
import sys

sys.setrecursionlimit(1000000)


def count(n):
    if n < 2:
        return 1
    return count(n - 1) + count(n - 2)


n, m, *a = map(int, open(0).read().split())
if sum(a[i + 1] == a[i] + 1 for i in range(m - 1)) > 0:
    print(0)
    exit()

a.append(n + 1)
c = -1
ans = 1
for i in a:
    ans = ans * count(i - c - 2) % (10**9 + 7)
    c = i
print(ans)
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

def generate_range_combinations(n):
    C = [1] * n
    for i in range(2, n):
        C[i] = (C[i - 1] + C[i - 2]) % DIV
    return C


def solve(n, m, A):
    from collections import defaultdict

    if m == 0:
        C = generate_range_combinations(n + 1)
        return C[-1]

    D = defaultdict(lambda: 0)
    for i in range(m - 1):
        d = A[i + 1] - A[i] - 1
        if d == 0:
            return 0
        D[d] += 1
    D[A[0]] += 1
    D[n - A[-1]] += 1

    ans = 1
    C = generate_range_combinations(max(D.keys()))
    for k, v in D.items():
        ans *= C[k - 1] ** v
        ans %= DIV
    return ans


DIV = 1000000007
n, m = map(int, input().split())
A = [int(input()) for _ in range(m)]
print(solve(n, m, A))


Error: TLE

Fixed submission:

from math import factorial

n, m = map(int, input().split())
A = [0] * m
for i in range(m):
    A[i] = int(input())
B = []
if m > 1:
    for i in range(m - 1):
        if A[i + 1] - A[i] == 1:
            print(0)
            exit()
if m:
    B.append(A[0])
    A.append(n + 1)
    for i in range(len(A) - 2, -1, -1):
        B.append(A[i + 1] - A[i] - 1)
else:
    B.append(n + 1)
div = 1000000007
ans = 1
for b in B:
    if b < 3:
        continue
    c = b - 1
    two_max = c // 2
    a = 0
    for two in range(two_max + 1):
        one = c - (2 * two)
        a += factorial(one + two) // (factorial(one) * factorial(two))
    ans *= a
    ans %= div
print(ans)
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = list(map(int, input().split()))
a = [int(input()) for i in range(M)]
a.append(0)
dp = [0, 1]
a_flag = 0

for i in range(1, N + 1):
    if i == a[a_flag]:
        dp.append(0)
        a_flag += 1
        continue
    dp.append(dp[i - 1] + dp[i])

print(dp[N + 1] % 1000000007)


Error: TLE

Fixed submission:

N, M = list(map(int, input().split()))
a = [int(input()) for i in range(M)]

dp = [0, 1]

for i in range(1, N + 1):
    if i in a:
        dp.append(0)
        continue
    dp.append(dp[i - 1] + dp[i])

print(dp[N + 1] % 1000000007)
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    N, M = map(int, input().split())
    A = set([int(input()) for _ in range(M)])

    mod = 10**9 + 7
    dp = [0] * (N + 1)
    dp[0] = 1

    for i in range(1, N + 1):
        if i in A:
            continue

        if i == 1:
            dp[1] = dp[0]
            continue

        dp[i] = dp[i - 1] + dp[i - 2]
        dp[i] %= mod

    print(dp[N])


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    N, M = map(int, input().split())
    A = [int(input()) for _ in range(M)]

    mod = 10**9 + 7
    dp = [0] * (N + 1)
    dp[0] = 1

    for i in range(1, N + 1):
        if i in A:
            continue

        if i == 1:
            dp[1] = dp[0]
            continue

        dp[i] = dp[i - 1] + dp[i - 2]
        dp[i] %= mod

    print(dp[N])


if __name__ == "__main__":
    main()
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

input = sys.stdin.readline


def binary_search(l, value):
    low = 0
    high = len(l) - 1
    while low <= high:
        mid = (low + high) // 2
        if l[mid] == value:
            return True
        elif l[mid] < value:
            low = mid + 1
        else:
            high = mid - 1
    return False


N, M = map(int, input().split())
A = [int(input()) for _ in range(M)]

A.append(10**6)
A.append(10**6)
B = [0] * (N + 1)
B[0] = 1
if binary_search(A, 1) is True:
    B[1] = 0
else:
    B[1] = 1
mod = 10**9 + 7

for i in range(2, N + 1):
    if i != 2 and binary_search(A, i - 1) is True and binary_search(A, i - 2) is True:
        print(0)
        exit()
    elif binary_search(A, i - 2) is True:
        B[i] = B[i - 1]
    elif binary_search(A, i - 1) is True:
        B[i] = B[i - 2]
    else:
        B[i] = B[i - 1] + B[i - 2]
    B[i] %= mod
print(B[N] % mod)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = [int(input()) for _ in range(M)]

B = [0] * (N + 1)
B[0] = 1
if 1 not in A:
    B[1] = 1
else:
    B[1] = 0
mod = 10**9 + 7

for i in range(2, N + 1):
    if i != 2 and i - 1 in A and i - 2 in A:
        print(0)
        exit()
    elif i - 2 in A:
        B[i] = B[i - 1]
    elif i - 1 in A:
        B[i] = B[i - 2]
    else:
        B[i] = B[i - 1] + B[i - 2]
    B[i] %= mod
print(B[N] % mod)
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
import sys

sys.setrecursionlimit(1000000)
n, m, *a = map(int, open(0).read().split())
dp = [1] + [-1] * n
dp[1] = 1
for i in a:
    dp[i] = 0


def count(n):
    if dp[n] != -1:
        return dp[n]
    dp[n] = (count(n - 1) + count(n - 2)) % (10**9 + 7)
    return dp[n]


print(count(n))


Error: TLE

Fixed submission:

#!/usr/bin/env python3
import sys

sys.setrecursionlimit(1000000)

n, m, *a = map(int, open(0).read().split())
dp = [0] * (n + 1)
ok = [True] * (n + 1)

for i in a:
    ok[i] = False


def count(n):
    if ok[n]:
        if n < 2:
            return 1
        if dp[n] != 0:
            return dp[n]
        dp[n] = (count(n - 1) + count(n - 2)) % (10**9 + 7)
        return dp[n]
    else:
        return 0


print(count(n))
Problem p03013 (Python) — buggy submission, verdict: Runtime Error

N, M = map(int, input().split())
a = set([int(input()) for _ in range(M)])

MOD = 1000000007

dp = [0] * (N + 1)
dp[0] = 1

if 1 not in a:
    dp[1] = 1
else:
    dp[1] = 0

for i in range(2, N + 1):
    if i in a:
        continue
    dp[i] = dp[i - 1] + dp[i - 2]
    dp[i] %= MOD

print(dp[N])


Error: WA

Fixed submission:

N, M = map(int, input().split())
a = set([int(input()) for _ in range(M)])

MOD = 1000000007

dp = [0] * (N + 1)
dp[0] = 1

if 1 not in a:
    dp[1] = 1
else:
    dp[1] = 0

for i in range(2, N + 1):
    print(f"i: {i}, dp: {dp}")

    if i in a:
        continue
    dp[i] = dp[i - 1] + dp[i - 2]
    dp[i] %= MOD
print(f"i: {i}, dp: {dp}")

print(dp[N])
Problem p03013 (Python) — buggy submission, verdict: Runtime Error

N, M = map(int, input().split())
steps = [True] * (N + 1)
for _ in range(M):
    steps[int(input())] = False
dp = [0] * (N + 1)
dp[0] = 1
for prev in range(N):
    for now in range(prev + 1, min(N, prev + 2) + 1):
        if steps[now]:
            dp[now] += dp[prev]
            dp[now] %= 10**9 + 7
print(dp[N])


Error: 0

Fixed submission:

N, M = map(int, input().split())
steps = [True] * (N + 1)
for _ in range(M):
    steps[int(input())] = False
dp = [0] * (N + 1)
dp[0] = 1
for prev in range(N):
    for now in range(prev + 1, min(N, prev + 2) + 1):
        if steps[now]:
            dp[now] += dp[prev]
            dp[now] %= 1_000_000_007
print(dp[N])
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

MOD = 1000000007
N = 100001

fibo = [1, 1] + [0] * N
for i in range(2, N):
    fibo[i] = (fibo[i - 1] + fibo[i - 2]) % MOD

n, m = list(map(int, input().split(" ")))
a_list = [int(input()) for _ in range(m)]
is_ngs = [0] * (n + 2)
for a in a_list:
    is_ngs[a] = 1
for i in range(n):
    if is_ngs[i] and is_ngs[i + 1]:
        print(0)
        exit()
cnts = []
cnt = 0

for i in range(n + 1):
    if is_ngs[i]:
        cnts.append(cnt)
        cnt = 0
        continue
    cnt += 1
cnts.append(cnt)
ans = 1
for c in cnts:
    if c != 0:
        ans *= fibo[c - 1]
# print(is_ngs)
# print(cnts)
print(ans % MOD)


Error: TLE

Fixed submission:

MOD = 1000000007
N = 100001

fibo = [1, 1] + [0] * N
for i in range(2, N):
    fibo[i] = (fibo[i - 1] + fibo[i - 2]) % MOD

n, m = list(map(int, input().split(" ")))
a_list = [int(input()) for _ in range(m)]
ngs = [i in a_list for i in range(n)]
ngs.append(False)
for i in range(n):
    if ngs[i] and ngs[i + 1]:
        print(0)
        exit()
cnts = []
cnt = 0

for ng in ngs:
    if ng:
        cnts.append(cnt)
        cnt = 0
        continue
    cnt += 1
cnts.append(cnt)
ans = 1
for c in cnts:
    if c != 0:
        ans *= fibo[c - 1]
# print(ngs)
# print(cnts)
print(ans % MOD)
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    N, M = map(int, input().split())
    A = [0] * (N + 2)
    for _ in range(M):
        A[int(input())] = 1
    dp = [0] * (N + 2)
    dp[-2] = 1
    for i in range(N - 1, -1, -1):
        if A[i] == 1:
            continue
        dp[i] = dp[i + 1] + dp[i + 2]
    print(dp[0] % 1000000007)


main()


Error: TLE

Fixed submission:

def main():
    N, M = map(int, input().split())
    A = [int(input()) for _ in range(M)]
    dp = [0] * (N + 2)
    dp[-2] = 1
    for i in range(N - 1, -1, -1):
        if i in A:
            continue
        elif i + 1 in A:
            dp[i] = dp[i + 2]
        elif i + 2 in A:
            dp[i] = dp[i + 1]
        elif i + 1 in A and i + 2 in A:
            break
        else:
            dp[i] = dp[i + 1] + dp[i + 2]
    print(dp[0] % 1000000007)


main()
Problem p03013 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())
hole_ids = {int(input()) - 1: 0 for _ in range(m)}
X = 1000000007


def solve(n, hole_ids):
    nums = []
    # nums = [0] * n
    for i in range(n):
        if i in hole_ids:
            nums.append(0)
            # nums[i] = 0
            continue

        if i == 0:
            nums.append(1)
            # nums[i] = 1
        elif i == 1:
            # 1つ前から1段上がるから2つ前から2個飛ばし
            nums.append(nums[-1] + 1)
            # nums[i] = nums[i-1] + 1
        else:
            nums.append((nums[-1] % X + nums[-2] % X) % X)
            # nums[i] = (nums[i-1] + nums[i-2]) % X
    return nums[-1]


print(solve(n, hole_ids))


Error: EOFError: EOF when reading a line
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03013/Python/s744106332.py", line 29, in <module>
    n, m = map(int, input().split())
EOFError: EOF when reading a line


Fixed submission:

n, m = map(int, input().split())
hole_ids = {int(input()) - 1: 0 for _ in range(m)}
X = 1000000007


def solve(n, hole_ids):
    nums = []
    # nums = [0] * n
    for i in range(n):
        if i in hole_ids:
            nums.append(0)
            # nums[i] = 0
            continue

        if i == 0:
            nums.append(1)
            # nums[i] = 1
        elif i == 1:
            # 1つ前から1段上がるから2つ前から2個飛ばし
            nums.append(nums[-1] + 1)
            # nums[i] = nums[i-1] + 1
        else:
            nums.append((nums[-1] % X + nums[-2] % X) % X)
            # nums[i] = (nums[i-1] + nums[i-2]) % X
    return nums[-1]


print(solve(n, hole_ids))
n, m = map(int, input().split())
hole_ids = {int(input()) - 1: 0 for _ in range(m)}
X = 1000000007


def solve(n, hole_ids):
    nums = []
    # nums = [0] * n
    for i in range(n):
        if i in hole_ids:
            nums.append(0)
            # nums[i] = 0
            continue

        if i == 0:
            nums.append(1)
            # nums[i] = 1
        elif i == 1:
            # 1つ前から1段上がるから2つ前から2個飛ばし
            nums.append(nums[-1] + 1)
            # nums[i] = nums[i-1] + 1
        else:
            nums.append((nums[-1] % X + nums[-2] % X) % X)
            # nums[i] = (nums[i-1] + nums[i-2]) % X
    return nums[-1]


print(solve(n, hole_ids))
Problem p03013 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = [int(s) for s in input().split()]
A = []
for _ in range(M):
    A.append(int(input()))
A = set(A)

counts = [0] * (N + 1)
for i in range(N + 1):
    if i == 0:
        counts[i] = 1
    elif i == 1:
        if i not in A:
            counts[i] = 1
    else:
        if i not in A:
            for j in [i - 2, i - 1]:
                if j not in A:
                    counts[i] += counts[j]
                    counts[i] %= 1000000007

print(counts[N])


Error: TLE

Fixed submission:

N, M = [int(s) for s in input().split()]
A = []
for _ in range(M):
    A.append(int(input()))

counts = [0] * (N + 1)
for i in range(N + 1):
    if i == 0:
        counts[i] = 1
    elif i == 1:
        if i not in A:
            counts[i] = 1
    else:
        for j in [i - 2, i - 1]:
            if j not in A and i not in A:
                counts[i] += counts[j]
                counts[i] %= 1000000007

print(counts[N])
Problem p03014 (Python) — buggy submission, verdict: Time Limit Exceeded

H, W = map(int, input().split())
MAP = [list(input()) for _ in range(H)]
L = [[0] * W for _ in range(H)]
R = [[0] * W for _ in range(H)]
D = [[0] * W for _ in range(H)]
U = [[0] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        if MAP[i][j] == "#":
            L[i][j] = 0
        elif j == 0:
            L[i][j] = 1
        else:
            L[i][j] = L[i][j - 1] + 1

        if MAP[i][W - j - 1] == "#":
            R[i][W - j - 1] = 0
        elif j == 0:
            R[i][W - j - 1] = 1
        else:
            R[i][W - j - 1] = R[i][W - j] + 1

for j in range(W):
    for i in range(H):
        if MAP[i][j] == "#":
            U[i][j] = 0
        elif i == 0:
            U[i][j] = 1
        else:
            U[i][j] = U[i - 1][j] + 1

        if MAP[H - i - 1][j] == "#":
            D[H - i - 1][j] = 0
        elif i == 0:
            D[H - i - 1][j] = 1
        else:
            D[H - i - 1][j] = D[H - i][j] + 1
result = 0
for i in range(H):
    for j in range(W):
        result = max(result, U[i][j] + D[i][j] + L[i][j] + R[i][j] - 3)

print(result)


Error: TLE

Fixed submission:

H, W = map(int, input().split())
MAP = [[] for _ in range(H)]
L = [[[] for h in range(W)] for _ in range(H)]
R = [[[] for h in range(W)] for _ in range(H)]
D = [[[] for h in range(W)] for _ in range(H)]
U = [[[] for h in range(W)] for _ in range(H)]

for i in range(H):
    MAP[i] = list(input())

for i in range(H):
    for j in range(W):
        if MAP[i][j] == "#":
            L[i][j] = 0
        elif j == 0:
            L[i][j] = 1
        else:
            L[i][j] = L[i][j - 1] + 1

        if MAP[i][W - j - 1] == "#":
            R[i][W - j - 1] = 0
        elif j == 0:
            R[i][W - j - 1] = 1
        else:
            R[i][W - j - 1] = R[i][W - j] + 1

for j in range(W):
    for i in range(H):
        if MAP[i][j] == "#":
            U[i][j] = 0
        elif i == 0:
            U[i][j] = 1
        else:
            U[i][j] = U[i - 1][j] + 1

        if MAP[H - i - 1][j] == "#":
            D[H - i - 1][j] = 0
        elif i == 0:
            D[H - i - 1][j] = 1
        else:
            D[H - i - 1][j] = D[H - i][j] + 1
result = 0
for i in range(H):
    for j in range(W):
        result = max(result, U[i][j] + D[i][j] + L[i][j] + R[i][j] - 3)

print(result)
Problem p03014 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

input = sys.stdin.readline
import numpy as np


h, w = map(int, input().split())

GRID = []
for _ in range(h):
    S = [0] + [0 if c == "#" else 1 for c in input().strip()] + [0]
    GRID.append(S)
GRID.insert(0, [0] * (w + 2))
GRID.append([0] * (w + 2))
grid = np.array(GRID)

top = np.zeros((h + 2, w + 2), dtype=np.int)
bottom = np.zeros((h + 2, w + 2), dtype=np.int)
left = np.zeros((h + 2, w + 2), dtype=np.int)
right = np.zeros((h + 2, w + 2), dtype=np.int)

for i in range(1, h + 1):
    top[i] = (top[i - 1] + 1) * grid[i]
for i in range(h, 0, -1):
    bottom[i] = (bottom[i + 1] + 1) * grid[i]
for j in range(1, w + 1):
    left[:, j] = (left[:, j - 1] + 1) * grid[:, j]
for j in range(w, 0, -1):
    right[:, j] = (right[:, j + 1] + 1) * grid[:, j]

column = (top + bottom - 1) * grid
row = (left + right - 1) * grid
lighted = (column + row - 1) * grid

ans = lighted.max()
print(ans)


Error: TLE

Fixed submission:

h, w = map(int, input().split())
GRID = [input() for _ in range(h)]

HORIZONTAL = [[0] * w for _ in range(h)]
for i in range(h):
    COUNTS = []
    count = 0
    for j in range(w):
        if GRID[i][j] == ".":
            count += 1
        else:
            COUNTS.append(count)
            count = 0
    COUNTS.append(count)
    k = 0
    for j in range(w):
        if GRID[i][j] == ".":
            HORIZONTAL[i][j] = COUNTS[k]
        else:
            k += 1

VERTICAL = [[0] * w for _ in range(h)]
for j in range(w):
    COUNTS = []
    count = 0
    for i in range(h):
        if GRID[i][j] == ".":
            count += 1
        else:
            COUNTS.append(count)
            count = 0
    COUNTS.append(count)
    k = 0
    for i in range(h):
        if GRID[i][j] == ".":
            VERTICAL[i][j] = COUNTS[k]
        else:
            k += 1

LIGHTED = []
for i in range(h):
    for j in range(w):
        LIGHTED.append(HORIZONTAL[i][j] + VERTICAL[i][j] - 1)

ans = max(LIGHTED)
print(ans)