Input Format
The first line contains integer , the number of elements in the set .
The second line contains  space separated elements of set . All of the elements are non-negative integers, less than or equal to 9.
The third line contains integer , the number of commands.
The next  lines contains either pop, remove and/or discard commands followed by their associated value.
Constraints

Output Format
Print the sum of the elements of set  on a single line.
hackerrank set discard remove pop problem solution in python

Problem solution in python 2 programming language.

n = int(raw_input())
s = set([int(x) for x in raw_input().strip().split()]) 
for _ in range(int(raw_input())):

    a = list(raw_input().strip().split())

    if a[0] == 'pop':
        s.pop()
    elif a[0] == 'discard':
        s.discard(int(a[1]))
    else:
        s.remove(int(a[1]))

print sum(s)

Problem solution in python 3 programming language.

n = int(input())
s = set([int(x) for x in input().strip().split()]) 
for _ in range(int(input())):

    a = list(input().strip().split())

    if a[0] == 'pop':
        s.pop()
    elif a[0] == 'discard':
        s.discard(int(a[1]))
    else:
        s.remove(int(a[1]))

print sum(s)

second solution

n = int(input())
s = set(map(intinput().split()))
N = int(input())
lists = []
for i in range(N):
    x = input().split()
    lists.append(x)
for i in range(N):
    if lists[i][0] == 'pop':
        if len(s) == 0:
            continue
        else:
            s.pop()
    if lists[i][0] == 'remove':
        x = lists[i][1]
        if int(x) in s:
            s.remove(int(x))
        else:
            continue
    if lists[i][0] == 'discard':
        x = lists[i][1]
        if int(x) in s:
            s.discard(int(x))
        else:
            continue
print(sum(s))