Problem - 1440A - CodeForces

Buy The String

要求

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

题目

You are given four integers n, c0, c1 and h and a binary string s of length n.
A binary string is a string consisting of characters 0 and 1.
You can change any character of the string s (the string should be still binary after the change). You should pay h coins for each change.
After some changes (possibly zero) you want to buy the string. To buy the string you should buy all its characters. To buy the character 0 you should pay c0 coins, to buy the character 1 you should pay c1 coins.
Find the minimum number of coins needed to buy the string.

Input

The first line contains a single integer t (1≤ t ≤10) — the number of test cases. Next 2t lines contain descriptions of test cases.

The first line of the description of each test case contains four
integers n, c0, c1, h (1≤ n,c0,c1,h ≤1000).

The second line of the description of each test case contains the
binary string s of length n.

Output

For each test case print a single integer — the minimum number of coins needed to buy the string.

Example

input
6
3 1 1 1
100
5 10 100 1
01010
5 10 1 1
11111
5 1 10 1
11111
12 2 1 10
101110110101
2 100 1 10
00

output
3
52
5
10
16
22

Note

In the first test case, you can buy all characters and pay 3 coins, because both characters 0 and 1 costs 1 coin.
In the second test case, you can firstly change 2- nd and 4- th symbols of the string from 1 to 0 and pay 2 coins for that. Your string will be 00000. After that, you can buy the string and pay 510=50 coins for that. The total number of coins paid will be 2+50=52.

我的代码

#include "bits/stdc++.h"
using namespace std;

int main(){
	int n,c0,c1,h;
	int t;
	int sum = 0;
	string s;
	cin>>t;
	for(int i = 0 ; i < t ; i ++){
		int n0 = 0 , n1 = 0 ;
		cin>>n>>c0>>c1>>h;
		cin>>s;
		for(int j = 0 ; j < n ; j ++){
			if(s[j] == '0'){
				n0 ++ ;
			}
			n1 = n - n0 ;
		}//算出1和0的个数
			if(c0 > c1 + h){//0换1
				
				sum = n0 * h + n * c1 ;
				
			}else if(c1 > c0 + h){//1换0
				
				sum = n1 * h + n * c0 ;
				
			}else{//小于等于都不用换
				sum = n0 * c0 + n1 * c1 ;
			}
			cout<<sum<<endl;
	}
	return 0 ;
}

原题链接

Problem - 1440A - CodeForces

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值