You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.

At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:


	An integer x - Record a new score of x.
	"+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
	"D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
	"C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.


Return the sum of all the scores on the record. The test cases are generated so that the answer fits in a 32-bit integer.

&nbsp;
Example 1:

Input: ops = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.


Example 2:

Input: ops = ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
"5" - Add 5 to the record, record is now [5].
"-2" - Add -2 to the record, record is now [5, -2].
"4" - Add 4 to the record, record is now [5, -2, 4].
"C" - Invalidate and remove the previous score, record is now [5, -2].
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.


Example 3:

Input: ops = ["1","C"]
Output: 0
Explanation:
"1" - Add 1 to the record, record is now [1].
"C" - Invalidate and remove the previous score, record is now [].
Since the record is empty, the total sum is 0.


&nbsp;
Constraints:


	1 &lt;= ops.length &lt;= 1000
	ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104].
	For operation "+", there will always be at least two previous scores on the record.
	For operations "C" and "D", there will always be at least one previous score on the record.


class Solution:
    def calPoints(self, ops: List[str]) -> int:
        temp = []
        for i in ops:
            if i!="C" and i!="D" and i!="+":
                temp.append(int(i))
            elif i=="C":
                temp.remove(temp[len(temp)-1])
            elif i=="D":
                temp.append(2*temp[len(temp)-1])
            elif i=="+":
                temp.append(temp[len(temp)-1]+temp[len(temp)-2])
        
        
        return sum(temp)
class Solution {
    public int calPoints(String[] ops) {
        List<Integer> list = new ArrayList<Integer>();
        
        for(int i = 0; i < ops.length; i++){
            switch(ops[i]){
                case "C":
                    list.remove(list.size() - 1);
                    break;
                case "D":
                    list.add(list.get(list.size() - 1) * 2);
                    break;
                case "+":
                    list.add(list.get(list.size() - 1) + list.get(list.size() - 2));
                    break;
                default:
                    list.add(Integer.valueOf(ops[i]));
                    break;
            }
        }
        
        int finalScore = 0;
        for(Integer score: list)
            finalScore += score;
        
        return finalScore;
    }
}
class Solution {
public:
    int calPoints(vector<string>& ops) {
        stack<int>st;
        int n = ops.size();
        for(int i=0;i<n;i++){
                    if(ops[i] == "C"){
                        st.pop();
                    }
                    else if (ops[i] =="D"){
                        st.push(st.top() * 2);
                    }
                    else if(ops[i] =="+"){
                        int temp = st.top();
                        st.pop();
                        int temp2 = st.top();
                        st.push(temp);
                        st.push(temp+temp2);
                   }
                  else{
                      st.push(stoi(ops[i]));
                  }
        }
        int res = 0;
        while(!st.empty()){
            res += st.top();
            st.pop();
        }
        return res;
    }
};
//Plus sign in the below algo confirms that the data type we are getting is integer. So, instead of adding it as a string, the data type will be  added as integer
var calPoints = function(ops) {
    let stack = [];
    for(let i = 0; i < ops.length; i++){
        if(ops[i] === "C")
            stack.pop();
        else if(ops[i] === "D")
            stack.push((+stack[stack.length - 1]) * 2);
        //we have to take stack.length to get the element of stack. We cannot take i for getting the element of stack as i refers to ops and it wont give the index of previous element in stack
        else if(ops[i] ==="+")
            stack.push((+stack[stack.length - 1]) + (+stack[stack.length - 2]));
        else
        stack.push(+ops[i]);
    }
    let sum = 0;
    for(let i = 0; i < stack.length; i++){
        sum = sum + stack[i];
    }
    return sum;
};
Baseball Game