def count_weights(weights):
# 初始化一个空集合,用于记录不同的重量
unique_weights = set()
# 初始情况下,只有 0 是一种不同的重量
unique_weights.add(0)
# 遍历每个砝码的重量
for weight in weights:
# 复制一份当前已有的不同重量集合
current_weights = set(unique_weights)
# 将当前已有的不同重量和当前砝码的重量进行组合,并添加到集合中
for w in current_weights:
unique_weights.add(w + weight)
unique_weights.add(abs(w - weight)) # 考虑天平两边放置砝码的情况
# 返回不同重量的种类数
return len(unique_weights) - 1 if 0 in unique_weights else len(unique_weights)
if __name__ == '__main__':
n = int(input())
list1 = list(map(int,input().split()))
print(count_weights(list1))
该代码最终通过所有测试用例,得到该题全分