def cal_sum(n):
    if n == 0:
        return 3
    if n == 1:
        return 6
    if n == 2:
        return 10
    
    a, b, c = 3, 0, 2
    total = 3 + 0 + 2  # sum of first three perrin numbers: P(0)=3, P(1)=0, P(2)=2
    
    for i in range(3, n + 1):
        next_val = a + b
        total += next_val
        a, b, c = b, c, next_val
    
    return total