This algorithm is called the "Knapsack Problem" or "0-1 Knapsack Problem". Here is a Python implementation:

```python
def knapsack_problem(p, w, W):
    n = len(p)
    max_sum = 0
    max_indices = []

    for i in range(1 << n):
        sum_w = 0
        sum_p = 0
        indices = []
        for j in range(n):
            if i & (1 << j):
                sum_w += w[j]
                sum_p += p[j]
                indices.append(j)
        if sum_w <= W and sum_p > max_sum:
            max_sum = sum_p
            max_indices = indices

    return max_sum, max_indices
```

This algorithm uses dynamic programming to find the maximum sum of p_i and the corresponding indices. The time complexity is O(2^n).