The problem described is a variation of the Knapsack Problem, specifically the 0/1 Knapsack Problem where we are trying to maximize the total value of selected items (sum of p_i) while ensuring that the total weight of selected items (sum of w_i) does not exceed a given weight limit W.

Here is a Python code that solves this problem:

```python
def knapsack(p, w, W):
    n = len(p)
    dp = [[0 for _ in range(W+1)] for _ in range(n+1)]

    for i in range(1, n+1):
        for j in range(1, W+1):
            if w[i-1] > j:
                dp[i][j] = dp[i-1][j]
            else:
                dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i-1]] + p[i-1])

    selected = []
    i = n
    j = W
    while i > 0 and j > 0:
        if dp[i][j] != dp[i-1][j]:
            selected.append(i-1)
            j -= w[i-1]
        i -= 1

    return dp[n][W], selected[::-1]

# Example usage
p = [60, 100, 120]
w = [10, 20, 30]
W = 50
max_sum, selected_elements = knapsack(p, w, W)

print("Maximum sum of selected elements in p:", max_sum)
print("Indices of selected elements:", selected_elements)
```

The algorithm implemented in the code is known as the Dynamic Programming Approach for the 0/1 Knapsack Problem.