Learning With Clock
Problem Description
Deva recently learned about angles in his mathematics class and is eager to explore them further! He began learning about angles using a wall clock.

He chose an ancient wall clock for his learning. This clock has only two hands: the hour hand and the minute hand. The hour hand moves to the next hour only after the minute hand completes a full rotation, staying at the current hour until then. The task is to form a given angle between the two hands of the clock. The rules for forming the given angle are as follows -

Starting from the given position, you can move both hands, but in opposite directions without crossing each other. For example, if you move the hour hand clockwise, you must move the minute hand counterclockwise, and vice versa, without them crossing over each other.
The hour hand can only be moved from one hour to the next or previous hour; it cannot be positioned between the hours. However, there are no such restrictions for the minute hand.
There is a cost attached to moving either the hour hand or minute hand. Likewise, the direction in which each of them is moved also has different costs attached to it as follows.
The cost of moving a hand clockwise incurs cost of A rupees
The cost of moving a hand anti-clockwise incurs cost of B rupees.
The cost of moving hour hand is X rupees
The cost of moving minutes hand is Y rupees.
All costs are for per degree.
You can form the given angle in any direction i.e. it can either be exterior angle or interior angle. Refer figures in example below for better understanding.
For example, if the time is 12:15 and one needs to form an angle of 300 degrees, one can achieve this by moving the minute hand 30 degrees in anti-clockwise direction (i.e., from the 3 to the 2), if the resulting cost is low, as shown in Figure 2 below.
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@67af833b:image1.png


Fig. 1

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@67af833b:image2.png


Fig.2

Given the initial time, an integer Q representing the number of queries, and four integers A, B, X, and Y as defined above, an angle will be provided in each query that one needs to form between the hands of the clock. For each query, determine the minimum cost required to form the specified angle and print the total minimum cost as the output.

Constraints
0 <= Angle <= 360 degrees and Angle will be an integer

1 <= A, B, X and Y <= 10

A, B, X and Y are unique.

1 <= Q <=20

Input
The first line contains the initial time in the format hours:minutes.

The second line contains an integer Q, representing the number of queries.

The third line contains four space delimited integers A, B, X, and Y respectively, as defined above.

The next Q lines each contain a single integer, representing the angle that you need to form between the hands of the clock.

Output
A single integer representing the sum of minimum cost for processing each query.

Time Limit (secs)
1

Examples
Example 1

Input

2:35

4

1 2 3 4

200

160

60

130

Output

670

Explanation

The initial time is 2:35, resembling the image below on the clock.

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@67af833b:image3.png

Fig. 3

The first query is to form 200 degrees between the two hands. For this, we can set up the hands like below.

Keep the hours hand in same position and move the minutes hand 10 degrees in clockwise direction. This will incur a cost of 10 * 4 * 1 = 40 rupees (10 degrees * cost of moving minutes hand * cost of moving clockwise).

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@67af833b:image4.png

Fig. 4

Next query is to form 160 degrees between the two hands which is already present as shown below. Thus, we need not make any changes, in this query. Hence no cost incurred.

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@67af833b:image5.png

Fig. 5

Third query is to form 60 degrees between the two hands. For this, we can set up the hands like below.

Move the hours hand 90 degrees in clockwise direction and move the minutes hand 10 degrees in anti-clockwise direction. This will incur a cost of (90 * 3 * 1) + (10 * 4 * 2) = 350 rupees.

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@67af833b:image6.png

Fig. 6

Fourth query is to form 130 degrees between the two hands. For this, we can set up the hands like below.

Keep the hour hand in the same position and move the minutes hand 70 degrees in clockwise direction. This will incur a cost of 70 * 4 * 1 = 280 rupees.

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@67af833b:image7.png

Fig. 7

Hence the total cost incurred is 40 + 0 + 350 + 280 = 670. Hence output is 670.

Example 2

Input

8:25

1

5 8 3 9

68

Output

990

Explanation

The initial time is 8:25, resembling the image below on the clock.

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@67af833b:image8.png

Fig. 8

The only query is to form 68 degrees between the two hands. For this, we can set up the hands like below.

Remain the hour hand in the same position and move the minutes hand 22 degrees in clockwise direction. This will incur a cost of 22 * 9 * 5 = 990 rupees (22 degrees * cost of moving minutes hand * cost of moving clockwise).

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@67af833b:image9.png

Fig. 9

Hence, the total cost incurred is 990 rupees. Hence, the output is 990.
you gave me this solution : 
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct CS {
    int hr_dig;
    int min_grg;
};

CS pT(const string& t_s) {
    CS state;
    size_t colon = t_s.find(':');
    if (colon == string::npos) {
        state.hr_dig = 0;
        state.min_grg = 0;
        return state;
    }
    int hour = stoi(t_s.substr(0, colon));
    int minute = stoi(t_s.substr(colon + 1));
    state.hr_dig = (hour % 12) * 30;
    state.min_grg = (minute % 60) * 6;
    return state;
}

pair<ll, CS> cmC(const CS& curr_st, int desired_angle, 
                                    int A, int B, int X, int Y) {
    ll m_c = LLONG_MAX;
    CS o_s = curr_st;
    int D = desired_angle > 180 ? 360 - desired_angle : desired_angle;

    for(int H_new = 0; H_new < 360; H_new += 30){
        if(H_new == curr_st.hr_dig){
            continue;
        }
        vector<int> mnpos = { (H_new + D) % 360, (H_new - D + 360) % 360 };

        for(auto M_new : mnpos){
            int movement_H_cw = (H_new - curr_st.hr_dig + 360) % 360;
            int movement_M_ccw = (curr_st.min_grg - M_new + 360) % 360;
            ll cost1 = 0;
            if(movement_H_cw != 0){
                cost1 += (ll)movement_H_cw * A * X;
            }
            cost1 += (ll)movement_M_ccw * B * Y;

            int mhc = (curr_st.hr_dig - H_new + 360) % 360;
            int movement_M_cw = (M_new - curr_st.min_grg + 360) % 360;
            ll cost2 = 0;
            if(mhc != 0){
                cost2 += (ll)mhc * B * X;
            }
            cost2 += (ll)movement_M_cw * A * Y;

            ll curr_cost = min(cost1, cost2);

            if(curr_cost < m_c){
                m_c = curr_cost;
                o_s.hr_dig = H_new;
                o_s.min_grg = M_new;
            }
        }
    }

    for(int H_new = 0; H_new < 360; H_new += 30){
        int a_d = abs(H_new - curr_st.min_grg);
        a_d = min(a_d, 360 - a_d);
        if(a_d == D){
            int M_CW = (H_new - curr_st.hr_dig + 360) % 360;
            ll cost_CW = 0;
            if(M_CW != 0){
                cost_CW = (ll)M_CW * A * X;
            }
            int moc_ccw = (curr_st.hr_dig - H_new + 360) % 360;
            ll cost_CCW = 0;
            if(moc_ccw != 0){
                cost_CCW = (ll)moc_ccw * B * X;
            }
            ll curr_cost = min(cost_CW, cost_CCW);

            if(curr_cost < m_c){
                m_c = curr_cost;
                o_s.hr_dig = H_new;
                o_s.min_grg = curr_st.min_grg;
            }
        }
    }

    vector<int> dis_mw = { (curr_st.hr_dig + D) % 360, (curr_st.hr_dig - D + 360) % 360 };

    for(auto M_new : dis_mw){
        int M_CW = (M_new - curr_st.min_grg + 360) % 360;
        ll cost_CW = (ll)M_CW * A * Y;

        int moc_ccw = (curr_st.min_grg - M_new + 360) % 360;
        ll cost_CCW = (ll)moc_ccw * B * Y;

        ll curr_cost = min(cost_CW, cost_CCW);

        if(curr_cost < m_c){
            m_c = curr_cost;
            o_s.hr_dig = curr_st.hr_dig;
            o_s.min_grg = M_new;
        }
    }

    return {m_c, o_s};
}

ll cal(CS& curr_st, const vector<int>& q, 
               int A, int B, int X, int Y) {
    ll ans = 0;

    for(auto D_orig : q){
        int D = D_orig;
        if(D > 180) D = 360 - D;
        auto result = cmC(curr_st, D, A, B, X, Y);
        ll cost = result.first;
        CS new_state = result.second;
        ans += cost;
        curr_st = new_state;
    }

    return ans;
}

void solve() {
    string t_s;
    cin >> t_s;
    CS curr_st = pT(t_s);

    int Q;
    cin >> Q;
    int A, B, X, Y;
    cin >> A >> B >> X >> Y;

    vector<int> q(Q);
    for(auto &d : q) cin >> d;

    ll ans = cal(curr_st, q, A, B, X, Y);

    cout << ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    solve();
}
make it optimum