Abhay and his plan -Editorial || PLANS

PROBLEM LINK: Contest Page | CodeChef

Problem Code: Contest Page | CodeChef

Practice: Error Page | CodeChef

Contest : Contest Page | CodeChef

Author: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
Tester: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
Editorialist: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9

DIFFICULTY:

Med-Hard

PROBLEM:

Abhay is a bad boy and plans to steal things from a houses. Every house has a different amount of wealth to steal from. Abhay is very strict with his rules. He plans to rob only alternative houses. Help Abhay in doing so by taking care of his privacy and in a way that he does not get caught. You are given an integer vector showcasing the wealth of houses, your motive is to help Abhay figure out the maximum wealth he can steal going by his rules.

EXPLANATION:

Traverse and store maximum while comparing.

SOLUTION:

C++:

#include <bits/stdc++.h>
using namespace std;
#define max(a, b) ((a)>(b)?(a):(b))
int mob(int num[], int n) {
int a = 0;
int b = 0;
for (int i=0; i<n; i++)
{
if (i%2==0)
{
a = max(a+num[i], b);
}
else
{
b = max(a, b+num[i]);
}
}
return max(a, b);
}

int main(){
int test;
while(cin >> test){
vector nums;
for(int i = 0; i < test; i++){
int num;
cin >> num;
nums.push_back(num);
}
cout << mob(nums.data(), test) << endl;
}
}