l and r of length n to store the maximum profit before and after each index, respectively.prices from left to right to fill the l array. For each index j, find the maximum profit from items with a lower index i and a lower price.prices from right to left to fill the r array. For each index j, find the maximum profit from items with a higher index k and a higher price.res to store the maximum total profit and set it to -1.i and calculate the total profit if both l[i] and r[i] are not zero. Update res with the maximum total profit found.res as the final result.