def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]); idx +=1 M = int(input[idx]); idx +=1 K = int(input[idx]); idx +=1 L = int(input[idx]); idx +=1 edges = [] for _ in range(L): X = int(input[idx]); idx +=1 Y = int(input[idx]); idx +=1 Z = int(input[idx]); idx +=1 weight = 1 << Z # 2^Z edges.append((-weight, -Y, -X)) # Negative for descending sort # Sort by weight descending, then Y descending, then X descending edges.sort() buyer_used = [False] * (N + 1) item_used = [False] * (M + 1) total = 0 for edge in edges: weight_neg, y_neg, x_neg = edge weight = -weight_neg Y = -y_neg X = -x_neg if not buyer_used[X] and not item_used[Y]: total += weight buyer_used[X] = True item_used[Y] = True print(total) if __name__ == '__main__': main()