1440A - Buy the String - CodeForces Solution


implementation math *800

Please click on ads to support us..

Python Code:

t = int(input())
while t:
    n,c0,c1,h = map(int,input().split())
    s = input()
    a = s.count('1') * c1
    b = s.count('0' ) * c0
    df = a+b
    if c1 > c0:
        ans1 = s.count('1') * h
        ab = ans1 + (n * c0)
        print(min(df,ab))
    elif c1 < c0:
        ans1 = s.count('0') * h
        ab = ans1 + (n*c1)
        print(min(df,ab))
    elif c1 == c0:
        print(df)
    t -= 1

C++ Code:

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

#define    debug(x)            cout<< #x << " = " << x <<endl;
#define    endl                "\n"
typedef    long long           ll;


void solve() {
    int n, a, b, h; cin >> n >> a >> b >> h;
    string s; cin >> s;
    if(a == b) {
        cout<< n * a << endl; return;
    }
    int z = 0, o = 0;
    for(auto i: s) z += i == '0', o += i == '1';

    if(a < b) {
        if(h + a < b) {
            cout<< z * a + o * (h + a) << endl; return;
        }
        cout<< z * a + o * b << endl; return;
    }

    if(a > b) {
        if(b + h < a) {
            cout<< (b + h) * z + o * b << endl; return;
        }

        cout<< a * z + b * o << endl; return;

    }
}


int32_t main() {

    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    cin >> T;
    while(T --) {
        solve();
    }
    return 0;

}


Comments

Submit
0 Comments
More Questions

567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze