The code below implements the algorithm described to solve the optimization problem:

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

    for i in range(1, 2**n):
        subset_p_sum = 0
        subset_w_sum = 0
        subset = []

        for j in range(n):
            if (i >> j) & 1:
                subset_p_sum += p[j]
                subset_w_sum += w[j]
                subset.append(j)

        if subset_w_sum <= W and subset_p_sum > max_sum:
            max_sum = subset_p_sum
            selected_elements = subset

    return max_sum, selected_elements

# Example usage
p = [5, 3, 7, 2]
w = [2, 8, 3, 1]
W = 8

max_sum, selected_elements = knapsack_optimization(p, w, W)
print("Maximum sum of selected elements in p:", max_sum)
print("Indices of the selected elements:", selected_elements)
```

This algorithm is known as the "Subset Sum Problem" or a variation of the "Knapsack Problem". It is a classic problem in combinatorial optimization where the objective is to find a subset of elements that satisfies certain constraints (in this case, the sum of weights being below a threshold) while optimizing another criteria (in this case, maximizing the sum of profits).

Let me know if you need further assistance!