CF940E. Cashback [贪心+dp]

E Cashback
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is offering you a discount.

You are given an array a of length n and an integer c.

The value of some array b of length k is the sum of its elements except for the smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14.

Among all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays.

Input
The first line contains integers n and c (1 ≤ n, c ≤ 100 000).

The second line contains n integers ai (1 ≤ ai ≤ 109) — elements of a.

Output
Output a single integer — the smallest possible sum of values of these subarrays of some partition of a.

Examples
inputCopy
3 5
1 2 3
outputCopy
6
inputCopy
12 10
1 1 10 10 10 10 10 10 9 10 10 10
outputCopy
92
inputCopy
7 2
2 3 6 4 5 7 1
outputCopy
17
inputCopy
8 4
1 3 4 5 5 3 4 1
outputCopy
23
Note
In the first example any partition yields 6 as the sum.

In the second example one of the optimal partitions is [1, 1], [10, 10, 10, 10, 10, 10, 9, 10, 10, 10] with the values 2 and 90 respectively.

In the third example one of the optimal partitions is [2, 3], [6, 4, 5, 7], [1] with the values 3, 13 and 1 respectively.

In the fourth example one of the optimal partitions is [1], [3, 4, 5, 5, 3, 4], [1] with the values 1, 21 and 1 respectively.

题意:给一个有n个元素的序列,可以任意分割成若干个序列,每个序列的价值=元素总和-序列内最小的w个数的和(其中w是序列长度/c下取整),给出n和原序列以及c的值,求总价值的最小值

题解:总价值的最小值就相当于让减去的数字之和尽可能大,而显然将序列分解成若干个长度为c的序列之和能去掉的最小值之和最大ÿ

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java实现国密SM2数字签名的示例代码: ```java import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.digests.SM3Digest; import org.bouncycastle.crypto.engines.SM2Engine; import org.bouncycastle.crypto.generators.ECKeyPairGenerator; import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECKeyGenerationParameters; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.signers.SM2Signer; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.spec.ECParameterSpec; import org.bouncycastle.util.encoders.Hex; import java.math.BigInteger; import java.security.Security; public class SM2SignDemo { public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); // 生成密钥对 ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator(); ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters( new ECDomainParameters(SM2Util.SM2_p, SM2Util.SM2_a, SM2Util.SM2_b, SM2Util.SM2_ecparams_G, SM2Util.SM2_n), SM2Util.SM2_random); keyPairGenerator.init(keyGenerationParams); AsymmetricCipherKeyPair keyPair = keyPairGenerator.generateKeyPair(); ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keyPair.getPrivate(); ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keyPair.getPublic(); // 签名 SM2Signer signer = new SM2Signer(); signer.init(true, privateKey); byte[] msg = "Hello, world!".getBytes(); byte[] z = SM2Util.getSM2Z("1234567812345678".getBytes(), publicKey.getQ()); signer.update(z, 0, z.length); signer.update(msg, 0, msg.length); BigInteger[] sig = signer.generateSignature(); String signature = Hex.toHexString(sig[0].toByteArray()) + Hex.toHexString(sig[1].toByteArray()); System.out.println("Signature: " + signature); // 验证签名 signer.init(false, publicKey); signer.update(z, 0, z.length); signer.update(msg, 0, msg.length); System.out.println("Signature verification result: " + signer.verifySignature(sig[0], sig[1])); } } class SM2Util { static final BigInteger SM2_p = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16); static final BigInteger SM2_a = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16); static final BigInteger SM2_b = new BigInteger("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16); static final BigInteger SM2_n = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16); static final BigInteger SM2_h = BigInteger.valueOf(1); static final BigInteger SM2_Gx = new BigInteger("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE171F996B8FEEF18EE", 16); static final BigInteger SM2_Gy = new BigInteger("BC3736A2F4F6779C59BDCEE36B692153D0A5D10B213A3D89B0C7C5AE0FD36B88", 16); static final ECDomainParameters SM2_ecparams_G = new ECDomainParameters(SM2_p, SM2_a, SM2_b, new org.bouncycastle.math.ec.ECPoint.SecP256K1(SM2_p, SM2_a, SM2_b, new BigInteger[]{SM2_Gx, SM2_Gy}, SM2_n, SM2_h)); static final SM3Digest SM2_256_DIGEST = new SM3Digest(); static final byte[] SM2_DEFAULT_USER_ID = "1234567812345678".getBytes(); static final SecureRandom SM2_random = new SecureRandom(); /** * 获取SM2签名中的Z值 */ static byte[] getSM2Z(byte[] userId, org.bouncycastle.math.ec.ECPoint userKey) { byte[] userIdDigest = new byte[32]; SM2_256_DIGEST.update(userId, 0, userId.length); SM2_256_DIGEST.doFinal(userIdDigest, 0); byte[] x = userKey.getXCoord().getEncoded(); byte[] y = userKey.getYCoord().getEncoded(); byte[] z = new byte[userIdDigest.length + x.length + y.length]; System.arraycopy(userIdDigest, 0, z, 0, userIdDigest.length); System.arraycopy(x, 0, z, userIdDigest.length, x.length); System.arraycopy(y, 0, z, userIdDigest.length + x.length, y.length); return z; } } ``` 在上面的示例代码中,我们使用了BouncyCastle库来实现SM2数字签名。其中,`SM2Signer`类用于签名和验签,`ECDomainParameters`类用于定义椭圆曲线参数,`SM3Digest`类用于计算摘要,`SM2Util`类用于定义一些常量和工具方法。 在签名过程中,我们需要先计算出SM2签名中的Z值,然后将Z值和待签名的消息传入`SM2Signer`类中进行签名。签名结果为两个整数,需要将它们转换为十六进制字符串拼接在一起即可得到最终的签名值。 在验签过程中,我们需要使用相同的Z值和待验签的消息来初始化`SM2Signer`类,然后将签名值传入进行验签。如果验签成功,`verifySignature`方法会返回`true`,否则返回`false`。 需要注意的是,以上示例代码仅供参考,实际使用时需要根据具体需求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值