qyliang/CodeNanoFix huggingface dataset:
You are given N blocks, each with a weight, solidness, and value. You need to build a tower by stacking some of the blocks vertically. The tower must satisfy the condition that the sum of the weights of the blocks stacked above each block is not greater than its solidness. Find the maximum possible sum of the values of the blocks in the tower. Constraints: N (1 ≤ N ≤ 10^3), weights and solidness (1 ≤ w_i, s_i ≤ 10^4), values (1 ≤ v_i ≤ 10^9). Input format: N followed by N lines of weight, solidness, and value. Output the maximum possible sum of the values.

# Buggy version:
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

from itertools import accumulate
from functools import lru_cache

M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
 
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]


n = val()
l = []
for i in range(n):l.append(li())

l.sort(key = lambda x:x[1])

dp = [0] * (20001)

for i in l:
    for j in range(i[1], -1, -1):
        dp[j + i[0]] = max(dp[j + i[0]], dp[j] + i[-1])

print(max(dp))




# Fixed version:
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

from itertools import accumulate
from functools import lru_cache

M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
 
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]


n = val()
l = []
for i in range(n):l.append(li())

l.sort(key = lambda x:x[1] + x[0])

dp = [0] * (20001)

for i in l:
    for j in range(i[1], -1, -1):
        dp[j + i[0]] = max(dp[j + i[0]], dp[j] + i[-1])

print(max(dp))

# Buggy version:
n=int(input());ans=0;p=[0]*22222
for w,s,v in sorted([list(map(int,input().split()))for _ in[0]*n],key=lambda a:a[0]+a[1]):
 for j in range(s+1):
  p[j+w]=max(p[j+w],p[j]+v);
  ans=max(ans,p[j+w]);
print(ans)


# Fixed version:
n=int(input());
ans=0;
p=[0]*22222;
for w,s,v in sorted([list(map(int,input().split()))for _ in[0]*n],key=lambda a:a[0]+a[1]):
 for j in range(s,-1,-1):
  p[j+w]=max(p[j+w],p[j]+v);
  ans=max(ans,p[j+w]);
print(ans)
You are given a grid with H horizontal rows and W vertical columns. Some squares are wall squares, and the rest are empty. Taro starts at (1, 1) and needs to reach (H, W) by moving right or down to an adjacent empty square. Find the number of Taro's paths from (1, 1) to (H, W), modulo 10^9 + 7. The input format is H, W, N, followed by N pairs of coordinates (r_i, c_i). Output the number of paths.

# Buggy version:
import sys
h, w, n = [int(i) for i in sys.readline().split()]
m = 1000000007
fact = [1, 1]
for i in range(2, 200002):
    fact.append(fact[-1] * i)
inv = [pow(i, m - 2, m) for i in fact]
f = lambda i: fact[i[0] + i[1]] * inv[i[0]] * inv[i[1]] % m
dp = [0] * (n + 1)
p = [[int(i) - 1 for i in sys.readline().split()] for j in range(n)]
p = sorted(p + [[h - 1, w - 1]])
for i in range(n + 1):
    h1, w1 = p[i]
    tot = f([h1, w1])
    for j in range(i):
        if p[j][1] > w1:
            continue
        tot = (tot - dp[j] * f([h1 - p[j][0], w1 - p[j][1]]) % m) % m
    dp[i] = tot
print(dp[-1])

# Fixed version:
import sys
h, w, n = [int(i) for i in sys.stdin.readline().split()]
m = 1000000007
fact = [1, 1]
for i in range(2, 200002):
    fact.append(fact[-1] * i % m)
inv = [pow(i, m - 2, m) for i in fact]
f = lambda i: fact[i[0] + i[1]] * inv[i[0]] * inv[i[1]] % m
dp = [0] * (n + 1)
p = [[int(i) - 1 for i in sys.stdin.readline().split()] for j in range(n)]
p = sorted(p + [[h - 1, w - 1]])
for i in range(n + 1):
    h1, w1 = p[i]
    tot = f([h1, w1])
    for j in range(i):
        if p[j][1] > w1:
            continue
        tot = (tot - dp[j] * f([h1 - p[j][0], w1 - p[j][1]]) % m) % m
    dp[i] = tot
print(dp[-1])
Score: 800 points
Problem Statement:
There are N positive integers A_1, A_2, ..., A_N. Takahashi can perform the following operation on these integers any number of times: Choose 1 ≤ i ≤ N and multiply the value of A_i by -2. He would like to make A_1 ≤ A_2 ≤ ... ≤ A_N holds. Find the minimum number of operations required. If it is impossible, print -1.
Constraints:
1 ≤ N ≤ 200000
1 ≤ A_i ≤ 10^9
Input:
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output:
Print the answer.
Sample Input 1:
4
3 1 4 1
Sample Output 1:
3
One possible solution is:
- Choose i=4 and multiply the value of A_4 by -2. A_1, A_2, A_3, A_4 are now 3, 1, 4, -2.
- Choose i=1 and multiply the value of A_1 by -2. A_1, A_2, A_3, A_4 are now -6, 1, 4, -2.
- Choose i=4 and multiply the value of A_4 by -2. A_1, A_2, A_3, A_4 are now -6, 1, 4, 4.
Sample Input 2:
5
1 2 3 4 5
Sample Output 2:
0
A_1 ≤ A_2 ≤ ... ≤ A_N holds before any operation is performed.
Sample Input 3:
8
657312726 129662684 181537270 324043958 468214806 916875077 825989291 319670097
Sample Output 3:
7

# Buggy version:
from math import log,ceil
N=int(input())
A=list(map(int,input().split()))
inf=float("inf")
p=[0]*(N+1)
n=[0]*(N+1)
Stack_p=[(-inf,N-1)]
Stack_n=[(-inf,0)]
for i in range(N-1)[::-1]:
  k=ceil(log(A[i]/A[i+1],4))
  p[i]=p[i+1]
  t=i
  if k>=1:
    while k>0:
      s,m=Stack_p.pop()
      p[i]+=k*(m-t)
      if k+s>=0:
        k+=s
      else:
        k=0
        Stack_p.append((k+s,m))       
  elif k<0:
    Stack_p.append((k,i))
for i in range(2,N+1):
  k=ceil(log(A[i-1]/A[i-2],4))
  n[i]=n[i-1]
  t=i-1
  if k>=1:
    while k>0:
      print(Stack_n,k)
      s,m=Stack_n.pop()
      n[i]+=k*(t-m)
      if k+s>=0:
        k+=s
      else:
        k=0
        Stack_n.append((k+s,m))       
  elif k<0:
    Stack_n.append((k,i-1))
#print(p)
#print(n)
ans=inf
for i in range(N+1):
  ans=min(ans,i+2*n[i]+2*p[i])
print(ans)

# Fixed version:
from math import log,ceil
N=int(input())
A=list(map(int,input().split()))
inf=float("inf")
p=[0]*(N+1)
n=[0]*(N+1)
Stack_p=[(-inf,N-1)]
Stack_n=[(-inf,0)]
for i in range(N-1)[::-1]:
  k=ceil(log(A[i]/A[i+1],4))
  p[i]=p[i+1]
  t=i
  if k>=1:
    while k>0:
      #print(Stack_p,k,t,p)
      s,m=Stack_p.pop()
      p[i]+=k*(m-t)
      if k+s>=0:
        k+=s
        t=m
      else:
        Stack_p.append((k+s,m))   
        k=0
  elif k<0:
    Stack_p.append((k,i))
for i in range(2,N+1):
  k=ceil(log(A[i-1]/A[i-2],4))
  n[i]=n[i-1]
  t=i-1
  if k>=1:
    while k>0:
     # print(Stack_n,k,t,n)
      s,m=Stack_n.pop()
     # print(s,m)
      n[i]+=k*(t-m)
      t=m
      if k+s>=0:
        k+=s
      elif k+s<0:
        Stack_n.append((k+s,m))  
        k=0
     # print(Stack_n)
  elif k<0:
    Stack_n.append((k,i-1))
#print(p)
#print(n)
ans=inf
for i in range(N+1):
  ans=min(ans,i+2*n[i]+2*p[i])
print(ans)
You are given N points (x_i, y_i) on a two-dimensional plane. Find the sum of scores for all possible sets S that form convex polygons. The score of a set S is defined as 2^(n-|S|), where n is the number of points inside the convex hull of S. Print the sum modulo 998244353. Constraints: 1≤N≤200, 0≤x_i,y_i<10^4 (1≤i≤N), x_i and y_i are integers. Input format: N, followed by N lines of x and y coordinates. Output: Print the sum of scores modulo 998244353. Example: Input: 4, 0 0, 0 1, 1 0, 1 1. Output: 5.

# Buggy version:
# E
from collections import Counter

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

M = 998244353

res = pow(2, N, M)
# 0 points
res -= 1
# 1 points
res -= N

# all lines
line_cnt = [0]*(N+1)

for i in range(N):
    xi, yi = xy_list[i]
    angle_list = []
    for j in range(N):
        xj, yj = xy_list[j]
        if xj != xi:
            angle_list.append((yj-yi)/(xj-xi))
        elif yj != xj:
            angle_list.append(10000.0)
        
    # count points in same line
    cnt_i = Counter(angle_list)
    for k in cnt_i.values():
        line_cnt[k+1] += 1

for i in range(2, N+1):
    cnt = line_cnt[i] // i
    res -= cnt*(pow(2, i, M)-i-1)

res = res % M
print(res)

# Fixed version:
# E
from collections import Counter

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

M = 998244353

res = pow(2, N, M)
# 0 points
res -= 1
# 1 points
res -= N

# all lines
line_cnt = [0]*(N+1)

for i in range(N):
    xi, yi = xy_list[i]
    angle_list = []
    for j in range(N):
        xj, yj = xy_list[j]
        if xj != xi:
            angle_list.append((yj-yi)/(xj-xi))
        elif yj != yi:
            angle_list.append(10000.0)
        
    # count points in same line
    cnt_i = Counter(angle_list)
    for k in cnt_i.values():
        line_cnt[k+1] += 1

for i in range(2, N+1):
    cnt = line_cnt[i] // i
    res -= cnt*(pow(2, i, M)-i-1)

res = res % M
print(res)
You are given a row of N balls, each initially labeled with an integer Ai. When a spell is cast, all balls with the current number k vanish simultaneously. The objective is to make all balls vanish by casting the spell some number of times. If this is not initially possible, the minimum number of modifications to the integers on the balls is sought. The integers on the balls may change by themselves in M such changes. After each change, the minimum number of modifications needed to achieve the objective before the next change is to be found. Constraints: 1 ≤ N, M, Ai, Xj, Yj ≤ 200000. In the test set worth 500 points, N, M ≤ 200. Input format: N M, followed by A1...AN, then X1 Y1, X2 Y2, ..., XM YM. Output format: M lines, each containing the minimum necessary number of modifications. Examples are provided.

# Buggy version:
import sys
N,M=map(int,sys.stdin.readline().split())
E=N;c=[0]*300000;l=[0]*300000
A=list(map(int,sys.stdin.readline().split()))
def Z(e):
	global E
	p=e-c[e]
	c[e]+=1
	if p:
		if not l[p]: E-=1
		l[p]+=1
for e in A:Z(e)
A=[0]+A
for m in range(M):
	x,y=map(int,sys.stdin.readline().split())
	p=A[x]-c[A[x]]+1
	c[A[x]]-=1
	if p:
		l[p]-=1
		if not l[p]: E+=1
	A[x]=y
	Z(A[x])
	print(E)

# Fixed version:
import sys
N,M=map(int,sys.stdin.readline().split())
E=N;c=[0]*300000;l=[0]*300000
A=list(map(int,sys.stdin.readline().split()))
def Z(e):
	global E
	p=e-c[e]
	c[e]+=1
	if p>0:
		if not l[p]: E-=1
		l[p]+=1
for e in A:Z(e)
A=[0]+A
for m in range(M):
	x,y=map(int,sys.stdin.readline().split())
	p=A[x]-c[A[x]]+1
	c[A[x]]-=1
	if p>0:
		l[p]-=1
		if not l[p]: E+=1
	A[x]=y
	Z(A[x])
	print(E)
In this problem, there is a tree with N vertices numbered 1 to N. The edges of the tree are represented by (x_i, y_i). Alice and Bob play a game on this tree. Starting from Alice, they take turns removing an existing edge, disconnecting it into two separate connected components, and then removing the component that does not contain Vertex 1. A player loses when unable to perform the operation. Determine the winner assuming both players play optimally.

Constraints:
- 2 ≤ N ≤ 100000
- 1 ≤ x_i, y_i ≤ N
- The given graph is a tree.

Input:
The input is given in the format:
N
x_1 y_1
x_2 y_2
...
x_{N-1} y_{N-1}

Output:
Print "Alice" if Alice wins; print "Bob" if Bob wins.

Sample Input 1:
5
1 2
2 3
2 4
4 5

Sample Output 1:
Alice

Sample Input 2:
5
1 2
2 3
1 4
4 5

Sample Output 2:
Bob

Sample Input 3:
6
1 2
2 4
5 1
6 3
3 2

Sample Output 3:
Alice

Sample Input 4:
7
1 2
3 7
4 6
2 3
2 4
1 5

Sample Output 4:
Bob

# Buggy version:
import sys
sys.setrecursionlimit(k:=2*n)
n=int(input())
T=[[]for _ in"_"*k]
for _ in range(n-1):a,b=map(int,input().split());T[a]+=b,;T[b]+=a,
def d(v,p):
 r=0
 for s in T[v]:
  if s!=p:r^=d(s,v)+1
 return r
print("ABloibc e"[d(1,1)<1::2])

# Fixed version:
import sys
sys.setrecursionlimit(9**9)
n=int(input())
T=[[]for _ in"_"*(n+1)]
for _ in range(n-1):a,b=map(int,input().split());T[a]+=b,;T[b]+=a,
def d(v,p):
 r=0
 for s in T[v]:
  if s!=p:r^=d(s,v)+1
 return r
print("ABloibc e"[d(1,1)<1::2])
You are given an integer sequence of length n, a_1, ..., a_n. Perform n operations on an empty sequence b. The i-th operation appends a_i to the end of b and then reverses the order of the elements in b. Find the sequence b obtained after these n operations.

Constraints:
1 ≤ n ≤ 2×10^5
0 ≤ a_i ≤ 10^9
n and a_i are integers.

Input:
The input is given in the following format:
n
a_1 a_2 ... a_n

Output:
Print n integers in a line with spaces in between. The i-th integer should be b_i.

Sample Input 1:
4
1 2 3 4

Sample Output 1:
4 2 1 3

Sample Input 2:
3
1 2 3

Sample Output 2:
3 1 2

Sample Input 3:
1
1000000000

Sample Output 3:
1000000000

Sample Input 4:
6
0 6 7 6 7 0

Sample Output 4:
0 6 6 0 7 7

# Buggy version:
from sys import stdin

N = int(stdin.readline().rstrip())
A = stdin.readline().rstrip().split()

B = A[::-1]

ans = ""

if N % 2 == 0:
  for i in range(0,N,2):
    ans += B[i]
  for i in range(0,N,2):
    ans += A[i]
  
else:
  for i in range(0,N,2):
    ans += B[i]
  for i in range(1,N,2):
    ans += B[i]

print(*list(ans))

# Fixed version:
from sys import stdin

N = int(stdin.readline().rstrip())
A = stdin.readline().rstrip().split()

B = A[::-1]

ans = []

if N % 2 == 0:
  for i in range(0,N,2):
    ans += [B[i]]
  for i in range(0,N,2):
    ans += [A[i]]
  
else:
  for i in range(0,N,2):
    ans += [B[i]]
  for i in range(1,N,2):
    ans += [A[i]]

print(*list(ans))

# Buggy version:
N = int(input())
A = list(map(int, input().split()))

ans = []

l1 = A[0::2]
l2 = A[1::2]
if N % 2 == 0:
    l2 = l2[::-1]
    ans = l2+l1
else:
    l1 = l1[::-1]
    ans = l1+l2

print(" ".join(ans))


# Fixed version:
N = int(input())
A = list(input().split())

ans = []

l1 = A[0::2]
l2 = A[1::2]
if N % 2 == 0:
    l2 = l2[::-1]
    ans = l2+l1
else:
    l1 = l1[::-1]
    ans = l1+l2

print(" ".join(ans))

# Buggy version:
N = int(input())
s = input().split()

# b = ''
# for ss in s:
#     b += ss
#     print(b)
#     b = b[::-1]
# print(" ".join(b))

ans = []
if N % 2 == 1:
    for ss in s[::-1][0::2]:
        ans.append(ss[::-1])
    for ss in s[1::2]:
        ans.append(ss)
else:
    for ss in s[::-1][0::2]:
        ans.append(ss[::-1])
    for ss in s[0::2]:
        ans.append(ss)
print(" ".join(ans))


# Fixed version:
N = int(input())
s = input().split()

# b = ''
# for ss in s:
#     b += ss
#     print(b)
#     b = b[::-1]
# print(" ".join(b))

ans = []
if N % 2 == 1:
    for ss in s[::-1][0::2]:
        ans.append(ss)
    for ss in s[1::2]:
        ans.append(ss)
else:
    for ss in s[::-1][0::2]:
        ans.append(ss)
    for ss in s[0::2]:
        ans.append(ss)
print(" ".join(ans))

# Buggy version:
input()
n=[x for x in input().split()]
nb=n[::-1]
bs=n[::2]
if len(nb)%2==0:
    be=nb[::2]
else:
    be=nb[::2]
if len(n)==1:
    print(n[0])
else:
    print(" ".join(be+bs))

# Fixed version:
input()
n=[x for x in input().split()]
nb=n[::-1]
bs=n[::2]
if len(nb)%2==0:
    be=nb[::2]
else:
    be=nb[1::2]
if len(n)==1:
    print(n[0])
else:
    ans=be+bs
    if len(n)%2!=0:
        ans=ans[::-1]
    print(" ".join(ans))
You are given a sequence of non-negative integers. You can perform an operation where you replace an element with the XOR of all elements. Your goal is to match the sequence with another given sequence. Determine if this is achievable and find the minimum number of operations needed. Constraints: 2 ≤ N ≤ 10^5, 0 ≤ a_i, b_i < 2^30. Input format: N, followed by sequences a and b. Output the minimum number of operations or -1 if not achievable.

# Buggy version:
n,a,b,ax,bx=int(input()),list(map(int,input().split()))+[0],list(map(int,input().split()))+[0],0,0
na,nb=a,b
for i in range(n):
	a[n]^=a[i]
	b[n]^=b[i]
a,b=sorted(a),sorted(b)
if sum([a[i]!=b[i] for i in range(n+1)]):
	print("-1")
	exit()
f=dict()
def find(x):
	if f[x]==x:
		return x
	else:
		f[x]=find(f[x])
		return f[x]
ans=0
for i in range(n):
	if na[i]!=nb[i]:
		f[na[i]]=na[i]
f[a[n]]=a[n]
for i in range(n):
	if na[i]!=nb[i]:
		ans+=1
		f[find(nb[i])]=find(na[i])
for i,j in f:
	if i==j:
		ans+=1
print(ans-1)

# Fixed version:
n,a,b=int(input()),list(map(int,input().split()))+[0],list(map(int,input().split()))+[0]
for i in range(n):
	a[n]^=a[i]
	b[n]^=b[i]
na,nb=sorted(a),sorted(b)
if na!=nb:
	print("-1")
	exit()
f=dict()
def find(x):
	if f[x]==x:
		return x
	else:
		f[x]=find(f[x])
		return f[x]
ans=0
for i in range(n):
	if a[i]!=b[i]:
		f[a[i]]=a[i]
f[a[n]]=a[n]
for i in range(n):
	if a[i]!=b[i]:
		ans+=1
		f[find(b[i])]=find(a[i])
for i in f:
	if i==f[i]:
		ans+=1
print(ans-1)
Title: Maximum Sum

Problem:
Given a sequence of n integers a1, a2, ..., an and a positive integer k (1 ≤ k ≤ n), create a program to output the maximum value of the sum Si = ai + ai+1 + ... + ai+k-1 (1 ≤ i ≤ n - k + 1).

Input:
The input consists of multiple datasets. Each dataset is given in the following format and ends with a line containing two zeros.
- The first line contains two positive integers n (1 ≤ n ≤ 100000) and k (1 ≤ k ≤ n) separated by a space.
- Starting from the second line, each line i (1 ≤ i ≤ n) contains the i-th term of the sequence ai (-10000 ≤ ai ≤ 10000).
Among the test data for evaluation, 60% satisfy n ≤ 5000 and k ≤ 1000.
The number of datasets does not exceed 5.

Output:
For each dataset, output the maximum value of Si on a separate line.

Example:
Input
5 3
2
5
-4
10
3
0 0

Output
11

# Buggy version:
def func(n,k):
    SUM = 0
    ans = 0
    A = [int(input()) for _ in range(n)]
    for i in range(k-1):
        SUM += A[i]
    for i in range(k,len(A)):
        ans = max(ans,SUM+A[i]-A[i-k])
        SUM = SUM+A[i]-A[i-k]
    print(ans)

for _ in range(10):
    n,k = [int(s) for s in input().split()]
    if n == 0 and k ==0:
        break
    else:
        func(n,k)


# Fixed version:
def func(n,k):
    SUM = 0
    ans = 0
    A = [int(input()) for _ in range(n)]
    for i in range(k):
        SUM += A[i]
    for i in range(k,len(A)):
        ans = max(ans,SUM+A[i]-A[i-k])
        SUM = SUM+A[i]-A[i-k]
    print(ans)

for _ in range(10):
    n,k = [int(s) for s in input().split()]
    if n == 0 and k ==0:
        break
    else:
        func(n,k)

# Buggy version:
def main():
	while True:
		a = []
		n, k = [int(x) for x in input().split()]
		if n == 0 and k == 0: break
		for i in range(n): a.append(int(input()))
		l = [0]
		for i in range(1, n - k + 2): l.append(a[i + k - 2] - a[i - 1])
		maxsum = 0
		maxi = 0
		s = 0
		for i, x in enumerate(l):
			s += x
			if s >= maxsum:
				maxsum = s
				maxi = i
		print(sum(a[i - 1:i + k - 1]))
main()

# Fixed version:
def main():
	while True:
		a = []
		n, k = [int(x) for x in input().split()]
		if n == 0 and k == 0: break
		for i in range(n): a.append(int(input()))
		l = [0]
		for i in range(1, n - k + 1): l.append(a[i + k - 1] - a[i - 1])
		maxsum = 0
		maxi = 0
		s = 0
		for i, x in enumerate(l):
			s += x
			if s >= maxsum:
				maxsum = s
				maxi = i
		print(sum(a[maxi:maxi + k]))
main()

# Buggy version:
while 1:
    n,k = [int(i) for i in input().split()]
    if not n and k:
        break
    a = [int(input()) for i in range(n)]
    b = [0]*len(a)

    for i in range(n):
        b[i] = b[i-1] + a[i]
    b = b + [0]

    c = [b[i + k - 1] - b[i - 1] for i in range(n - k + 1)]

    print(max(c))


# Fixed version:
while 1:
    n,k = [int(i) for i in input().split()]
    if not (n and k):
        break
    a = [int(input()) for i in range(n)]
    b = [0]*len(a)

    for i in range(n):
        b[i] = b[i-1] + a[i]
    b = b + [0]

    c = [b[i + k - 1] - b[i - 1] for i in range(n - k + 1)]

    print(max(c))
Oldest Ruins

Problem:
Long ago, there was a settlement where many people lived. The people built various buildings of different shapes and sizes. However, these structures have been lost, and the only clues to their locations are found in literature and the pillars discovered at the ruins.

The literature describes a temple. When viewed from above, the temple is precisely a square, with pillars at each of its four corners. It is unknown which direction the temple faced, and it is also unclear whether there were pillars on the edges or inside. Archaeologists believed that among the pillars found at the ruins, the one with the largest area forming a square must be the temple.

Given the coordinates of the pillars, write a program to find the largest square formed by four pillars and output its area. Note that the sides of the square may not be parallel to the coordinate axes.

Example:
In the example below, there are 10 pillars. Four pillars at coordinates (4, 2), (5, 2), (5, 3), (4, 3) form a square, as do four pillars at coordinates (1, 1), (4, 0), (5, 3), (2, 4). The larger square is formed by the latter set of pillars, with an area of 10.

Input:
The input consists of multiple datasets. Each dataset is given in the following format and ends with a line containing a single zero.

The first line contains the number of pillars found at the ruins, n.

The next n lines each contain the x and y coordinates of a pillar, separated by a space.

Each pillar appears only once.

n is an integer satisfying 1 ≤ n ≤ 3000, and the x and y coordinates of the pillars are integers between 0 and 5000.

Output:
For each dataset, output a single integer. If there exists a square formed by four pillars, output the area of the largest such square. If no such square exists, output 0.

Input/Output Example:
Input:
10
9 4
4 3
1 1
4 2
2 4
5 8
4 0
5 3
0 5
5 2
10
9 4
4 3
1 1
4 2
2 4
5 8
4 0
5 3
0 5
5 2
0

Output:
10
10

# Buggy version:
n = int(input())

poles = [tuple(map(int, input().split())) for _ in range(n)]
poles_set = set(poles)

max_square = 0
for i in range(n):
    x1, y1 = poles[i]
    for j in range(i, n):
        x2, y2 = poles[j]
        dx, dy = x2 - x1, y2 - y1
        if ((x2 + dy, y2 - dx) in poles_set and (x1 + dy, y1 - dx) in poles_set) \
                or ((x2 - dy, y2 + dx) in poles_set and (x1 - dy, y1 + dx) in poles_set):
            edge = dx ** 2 + dy ** 2
            if max_square < edge:
                max_square = edge
print(max_square)

# Fixed version:
while True:
    n = int(input())
    if not n:
        break

    poles = [tuple(map(int, input().split())) for _ in range(n)]
    poles_set = set(poles)

    max_square = 0
    for i in range(n):
        x1, y1 = poles[i]
        for j in range(i, n):
            x2, y2 = poles[j]
            dx, dy = x2 - x1, y2 - y1
            if ((x2 + dy, y2 - dx) in poles_set and (x1 + dy, y1 - dx) in poles_set) \
                    or ((x2 - dy, y2 + dx) in poles_set and (x1 - dy, y1 + dx) in poles_set):
                edge = dx ** 2 + dy ** 2
                if max_square < edge:
                    max_square = edge
    print(max_square)