Candy (candy)
Description
Due to its great contribution to the maintenance of world peace, Dzx was given an unlimited amount of candy free coupons to the candy company on May 23, 2010. On this day, Dzx can choose any of the N products from the candy company to take home and enjoy. Each of the N products of the candy company contains a different number of candies. Dzx hopes that the total number of candies in the products he chooses is an integer multiple of K, so that he can evenly distribute the candies to his partners who help him maintain world peace. Of course, on the basis of meeting this condition, the more the total number of candies, the better. How much candy can Dzx take away?
Note: Dzx can only take away the entire product of the candy company.
Format
Input
The first line contains two integers \(N(1 \leq N \leq 100)\) and \(K(1 \leq K \leq 100)\)
The following \(N\) lines have 1 integer in each line, indicating the number of candies contained in the product of the candy company, which does not exceed 1,000,000
Output
The maximum total number of candies that meet the requirements. If it cannot meet the requirement of multiples of \(K\), output 0
Sample
Input
5 7
1
2
3
4
5
Output
14
Sample Explanation
Dzx's choice is \(2+3+4+5=14\), so the total number of candies is a multiple of 7, and it is the most total choice.
Sample Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int n,k,t;
int f[105][105];
int main() {
freopen("candy.in","r",stdin);
freopen("candy.out","w",stdout);
cin>>n>>k;
for(int i=1; i<=n; i++) {
cin>>t;
f[i][t%k]=t;//Store the total sugar with the same remainder.
for(int j=0; j<k; j++)
if(f[i-1][j])
f[i][(j+t)%k]=f[i-1][j]+t;
for(int j=0; j<k; j++)
f[i][j]=max(f[i][j],f[i-1][j]);
}
cout<<f[n][0]<<endl;//The result with a remainder of 0 is output. If there is no remainder with 0, then output 0 directly.
return 0;
}
Candy (candy)的更多相关文章
- Leetcode 动态规划 Candy
本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie Candy Total Accepted: 16494 Total Submissions: ...
- leetcode — candy
/** * Source : https://oj.leetcode.com/problems/candy/ * * There are N children standing in a line. ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【leetcode】Candy(hard) 自己做出来了 但别人的更好
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【leetcode】Candy
题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
随机推荐
- 【Gin-API系列】部署和监控(九)
本文是[Gin-API系列]的最后一篇文章,简单介绍如何在生产环境的部署架构和监控手段. 生产部署 部署架构 使用Nginx加Keepalived的方式搭建,可以达到高可用的效果,并可以横向扩容 如何 ...
- 新三种求数列中值SQL之效率再比拼
在 https://www.cnblogs.com/xiandedanteng/p/12677688.html 中我列举了三种求中值方案,其中日本人MICK的做法因为不适用于二百万结果集而放弃,取而 ...
- docker 停止、启动、删除镜像指令
容器 docker ps // 查看所有正在运行容器 docker stop containerId // containerId 是容器的ID docker ps -a // 查看所有容器 dock ...
- leetcode刷题-56合并区间
题目 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]] 思路 通过设置一个移 ...
- leetcode题目,个人见解1
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入 ...
- JS的全局变量无法给Ajax里的变量赋值
前阶段遇到这么一个问题,在JS定义一个全局变量,JS方法里的其他地方都能使用,偏偏ajax里无法赋值,也不是无法赋值,但赋值总是慢一拍,具体的解决方案如下图所示:
- flutter dio网络请求封装实现
flutter dio网络请求封装实现 文章友情链接: https://juejin.im/post/6844904098643312648 在Flutter项目中使用网络请求的方式大致可分为两种 ...
- 你想了解的分布式文件系统HDFS,看这一篇就够了
1.分布式文件系统 计算机集群结构 分布式文件系统把文件分布存储到多个节点(计算机)上,成千上万的计算机节点构成计算机集群. 分布式文件系统使用的计算机集群,其配置都是由普通硬件构成的,与用多个处理器 ...
- 【深入理解Linux内核架构】3.3 页表
页表:用于建立用户进程空间的虚拟地址空间和系统物理内存(内存.页帧)之间的关联. 向每个进程提供一致的虚拟地址空间. 将虚拟内存页映射到物理内存,因而支持共享内存的实现. 可以在不增加物理内存的情况下 ...
- Educational Codeforces Round 95(A-C题解)
A. Buying Torches 题目:http://codeforces.com/contest/1418/problem/A 题解:计算一个公式:1+n*(x-1)=(y+1)*k,求满足该条件 ...