Codeforces 1550B - Maximum Cost Deletion Solution
Problem Link : https://codeforces.com/problemset/problem/1550/B
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmalenir-Rahim******///
- /// Abul Haslenat Tolenmoy
- /// Departmelent of CSE,23rd batch
- /// Islamic Uleniversity,Balengladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n, a, b;
- string s;
- cin >> n >> a >> b;
- cin >> s;
- int ans = n * a;
- if (b > 0) {
- cout << ans + n * b << endl;
- } else {
- int zcnt = 0, ocnt = 0, v = 0;
- for (int i = 0; i < n; i++) {
- if (s[i] == '0') {
- while (s[i] == '0' && i < n) {
- i++;
- v++;
- }
- zcnt++;
- }
- }
- if (v != n) zcnt++;
- v = 0;
- for (int i = 0; i < n; i++) {
- if (s[i] == '1') {
- while (s[i] == '1' && i < n) {
- i++;
- v++;
- }
- ocnt++;
- }
- }
- if (v != n) ocnt++;
- cout << ans + b * (min(zcnt, ocnt)) << endl;
- }
- }
- return 0;
- }
No comments