[HackerRank] Sherlock and Cost

Problem

https://www.hackerrank.com/challenges/sherlock-and-cost/problem

Solution

Use by dynamic programming:

long cost(vector<int> B) {
    map<pair<int,int>, long> v;
    long v1 = 0;
    long v2 = 0;
    for(int stage = 1; stage <  B.size();stage ++){
            int a = 1;
            int b =  B[ B.size()-stage-1];
            int d1 = 1;
            int d2 = B[ B.size() - stage ] ;
            long vad1 = abs(d1-a)+v1;
            long vad2 = abs(d2-a)+v2;
            long vbd1 = abs(d1-b)+v1;
            long vbd2 = abs(d2-b)+v2;
            v1 = max(vad1,vad2);
            v2 = max(vbd1,vbd2); 
    }
    return max(v1, v2);
}