Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programming101
Programmingoneonone

Learn everything about programming

HackerEarth The minimum cost problem solution

YASH PAL, 31 July 2024
In this HackerEarth The minimum cost problem solution you are given a binary array (array consists of 0’s and 1’s) A that contains N elements. You can perform the following operation as many times as you like:
  1.  Choose any index 1 <= i <= N and if it is currently 0, then convert it to 1 for C01 coins.
  2.  Choose any index 1 <= i <= N and if it is currently 1, then convert it to 0 for C10 coins.
Your task is to transform the given array into a special array that for every index 1 <= i < N, Ai intersection Ai+1 = 1.
HackerEarth The minimum cost problem solution

HackerEarth The minimum cost problem solution.

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
#define endl "n"
#define test ll t; cin>>t; while(t--)
typedef long long int ll;
ll solve(vector<ll>&a,ll c01,ll c10,ll st){
ll ans=0;
for(auto it:a){
if(it!=st){
ans+=(it==0?c01:c10);
}
st^=1;
}
return ans;
}
int main() {
FIO;
test
{
ll n,c01,c10;
cin>>n>>c01>>c10;
vector<ll>a(n);
for(auto &it:a) cin>>it;
ll ans=1e18;
ans=min(ans,solve(a,c01,c10,0ll));
ans=min(ans,solve(a,c01,c10,1ll));
cout<<ans<<endl;
}
return 0;
}

Second solution

t = int(input())
while t > 0:
t -= 1
c = [0, 0]
n, c[0], c[1] = map(int, input().split())
a = list(map(int, input().split()))
cost = [0, 0]
for i in range(n):
for j in range(2):
cost[j] += c[a[i]] * ((a[i] ^ i) & 1 != j)
print(min(cost))
coding problems solutions

Post navigation

Previous post
Next post

Related website

The Computer Science

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US