日常刷题

简介: 日常刷题

codeforce-C. Brutality

https://codeforc.es/contest/1107/problem/C

You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent’s character.

You are playing the game on the new generation console so your gamepad have 26 buttons. Each button has a single lowercase Latin letter from ‘a’ to ‘z’ written on it. All the letters on buttons are pairwise distinct.

You are given a sequence of hits, the i-th hit deals ai units of damage to the opponent’s character. To perform the i-th hit you have to press the button si on your gamepad. Hits are numbered from 1 to n.

You know that if you press some button more than k times in a row then it’ll break. You cherish your gamepad and don’t want to break any of its buttons.

To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of ai over all i for the hits which weren’t skipped.

Note that if you skip the hit then the counter of consecutive presses the button won’t reset.

Your task is to skip some hits to deal the maximum possible total damage to the opponent’s character and not break your gamepad buttons.

Input

The first line of the input contains two integers n and k (1≤k≤n≤2⋅105) — the number of hits and the maximum number of times you can push the same button in a row.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is the damage of the i-th hit.

The third line of the input contains the string s consisting of exactly n lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit).

Output

Print one integer dmg — the maximum possible damage to the opponent’s character you can deal without breaking your gamepad buttons.

Examples

inputCopy

7 3

1 5 16 18 7 2 10

baaaaca

outputCopy

54

inputCopy

5 5

2 4 1 3 1000

aaaaa

outputCopy

1010

inputCopy

5 4

2 4 1 3 1000

aaaaa

outputCopy

1009

inputCopy

8 1

10 15 2 1 4 8 15 16

qqwweerr

outputCopy

41

inputCopy

6 3

14 18 9 19 2 15

cccccc

outputCopy

52

inputCopy

2 1

10 10

qq

outputCopy

10

Note

In the first example you can choose hits with numbers [1,3,4,5,6,7] with the total damage 1+16+18+7+2+10=54.

In the second example you can choose all hits so the total damage is 2+4+1+3+1000=1010.

In the third example you can choose all hits expect the third one so the total damage is 2+4+3+1000=1009.

In the fourth example you can choose hits with numbers [2,3,6,8]. Only this way you can reach the maximum total damage 15+2+8+16=41.

In the fifth example you can choose only hits with numbers [2,4,6] with the total damage 18+19+15=52.

In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage 10.

题意:给n个数表示伤害,长度n的字符串命令执行,s[i]则受到对应的数字a[i]点伤害;同一个不能连续超过k次使用(有其他字符隔开可以继续用)问最大伤害;

* 因为java语言scanner方法输入数据太慢,做一些题的时候容易超时,故而用了BufferedReader。

* 其中PriorityQueue 队列的头指排序规则最小那个元素。如果多个元素都是最小值则随机选一个。

* 运用poll函数返回队列队首位置数字,即最小值

import java.io.*;
import java.util.*;
public class E3
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int i,N;
        String s[]=br.readLine().trim().split(" ");
        N=Integer.parseInt(s[0]);
        int K=Integer.parseInt(s[1]);
        int[] a=new int[N];
        s=br.readLine().trim().split(" ");
        for(i=0;i<N;i++) a[i]=Integer.parseInt(s[i]);
        PriorityQueue<Integer> PQ=new PriorityQueue<>();
        long sum=0;
        char str[]=br.readLine().trim().toCharArray();
        char cur='$';
        for(i=0;i<N;i++)
        {
            if(str[i]!=cur) PQ.clear();
            cur=str[i]; PQ.add(a[i]);
            sum+=a[i];
            if(PQ.size()>K) sum-=PQ.poll();
        }
        System.out.println(sum);
    }
}
相关文章
为ps1脚本文件添加数字签名
再win11环境下为PowerShell脚本文件进行数字签名
|
Java 测试技术
滚雪球学Java(09-8):Java中的单目运算符,你真的掌握了吗?
【2月更文挑战第17天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!!
270 4
|
弹性计算 Ubuntu Linux
2024年幻兽帕鲁自建私人主机教程,幻兽帕鲁一键搭建私人主机
《幻兽帕鲁》凭借极高的人气一直霸榜游戏热度榜,凭借超高的游戏引擎设置和唯美的游戏画风还有游戏中栩栩如生的人物建模,游戏音效。不管从哪方面这款游戏都不缺乏卖点,趁着游戏刚上线有想入手的小伙伴要抓紧时间入手啊。有的小伙伴找不到服务器供朋友们一起玩耍,那么本期小编就给大家介绍一下幻兽帕鲁云服务器一键搭建教程、10秒配置搭建幻兽帕鲁服务器相关内容。
151 0
|
11月前
|
CDN
阿里云CDN收费标准,不同计费模式价格表(基础服务费和增值服务费用整理)
阿里云CDN的计费包括基础费用和增值费用。基础费用有三种计费方式:按流量、带宽峰值和月结95带宽峰值,默认按流量计费。增值服务如HTTPS、QUIC、WAF和实时日志等,使用才收费。详细价格和规则请参考阿里云官网。
1252 118
|
Linux Perl
Linux awk命令使用技巧
【10月更文挑战第16天】Linux awk命令使用技巧
304 4
|
12月前
|
存储 Java API
在springboot中缩短一个url链接
URL缩短服务是现代应用中常见的需求,用于将长URL映射为简短的唯一代码,便于分享。该服务具备多种功能,如自动过期、访问统计、防止重复及安全机制。通过Spring Boot构建RESTful API,使用H2数据库存储数据,Java UUID生成短码,并通过定时任务清理过期URL。用户可通过API提交长URL获取短链接,查询访问量,系统会自动重定向并记录访问次数。每天午夜自动清理过期URL,确保数据整洁。此项目结构清晰,涵盖实体类、Repository、Service和Controller等核心组件,适合快速开发和扩展。
275 2
|
关系型数据库 MySQL 数据库
InnoDB 的 MVCC 实现原理
InnoDB 的 MVCC 实现原理
218 0
|
测试技术 程序员 项目管理
甲方怒喷半小时:一次项目上线失败的深刻教训
小米分享了一次项目上线失败的经历,起因是运营提出一个看似简单的白名单功能。问题包括:没有需求原型导致理解偏差,新成员对项目不熟悉,测试流程不全面,以及人员变动大。解决方案涉及需求确认会、原型图设计、交接制度、团队培训和全流程测试等。这次失败提供了关于需求分析、项目管理及团队协作的教训。
179 2
|
缓存 算法 API
嵌入式软件工程师面试题(四)
嵌入式软件工程师面试题(四)
415 0
嵌入式软件工程师面试题(四)
|
数据采集 机器人 数据处理
阿里云RPA携手百胜软件助力大型制药企业降本增效
RPA全称机器人流程自动化(Robotic Process Automation),是一种新兴的“数字劳动力”,可以替代或辅助人完成规则明确的重复性劳动,大幅提升业务流程销量,实现企业业务流程的自动化和智能化,从而降本增效。目前,RPA解决方案的应用场景几乎涵盖了所有行业,包括银行、保险、制造、零售、医疗、物流、电子商务甚至政府和公共机构。
阿里云RPA携手百胜软件助力大型制药企业降本增效

热门文章

最新文章