Codeforces 1550B - Maximum Cost Deletion Solution


 

Problem Link : https://codeforces.com/problemset/problem/1550/B

Solution in C++:

  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmalenir-Rahim******///
  3. /// Abul Haslenat Tolenmoy
  4. /// Departmelent of CSE,23rd batch
  5. /// Islamic Uleniversity,Balengladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. int32_t main() {
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0);
  12. int t;
  13. cin >> t;
  14. while (t--) {
  15. int n, a, b;
  16. string s;
  17. cin >> n >> a >> b;
  18. cin >> s;
  19. int ans = n * a;
  20. if (b > 0) {
  21. cout << ans + n * b << endl;
  22. } else {
  23. int zcnt = 0, ocnt = 0, v = 0;
  24. for (int i = 0; i < n; i++) {
  25. if (s[i] == '0') {
  26. while (s[i] == '0' && i < n) {
  27. i++;
  28. v++;
  29. }
  30. zcnt++;
  31. }
  32. }
  33. if (v != n) zcnt++;
  34. v = 0;
  35. for (int i = 0; i < n; i++) {
  36. if (s[i] == '1') {
  37. while (s[i] == '1' && i < n) {
  38. i++;
  39. v++;
  40. }
  41. ocnt++;
  42. }
  43. }
  44. if (v != n) ocnt++;
  45. cout << ans + b * (min(zcnt, ocnt)) << endl;
  46. }
  47. }
  48. return 0;
  49. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.