题解 | #称砝码#

称砝码

https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c

n_weights = int(input())
weights = [int(w) for w in input().split()]
nums = [int(i) for i in input().split()]

# credits to wfj
def count_different_weights(n, weight_lst, count_lst):
    exist_weights = {0}
    for w, c in zip(weight_lst, count_lst):
        need_try_to_add_one = exist_weights
        for i in range(c):
            need_try_to_add_more = []
            for wi in need_try_to_add_one:
                wi += w
                if wi not in exist_weights:
                    need_try_to_add_more.append(wi)
            need_try_to_add_one = need_try_to_add_more
            exist_weights.update(need_try_to_add_one)
    return len(exist_weights)

result = count_different_weights(n_weights, weights, nums)
print(result)

算法学习分析与整理 文章被收录于专栏

个人学习算法的文档整理与思考,举一反三,相爱相杀。

全部评论

相关推荐

2025-11-19 18:44
已编辑
成都理工大学 Java
程序员花海:我面试过100+校招生,大厂后端面试不看ACM,竞赛经历含金量低于你有几份大厂实习 这个简历整体来看不错 可以海投
如何写一份好简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务