Help needed in my accepted solution of Chef and wedding arrangement

Here is the link too my accepted solution
https://www.codechef.com/viewsolution/36538513
Though it got accepted , I know that it will give TLE fir some test cases . LIKE
5
1000 2
All elements of a[i]=3
Here I have made a 2D dp[i][j] array where j denotes the element of the array and j denotes the no. of tables it is splitted. For each i with j=1 I am calculating the value greedily and after that for each i with j ! = 1, I am checking that in which table configuration it makes the dp[i][j] minimum and at the end I had made minimum dp[n-1][j] my answer.
Too an extent you can say that it is a kind of well implemented Greedy solution.
So I just wanted to know is there any optimisation I can do so that it becomes a correct answer .
@ssrivastava990 @ssjgz @aneee004 @galencolin @akshitm16 @everule1 @carre
Help from anyone would be highly appreciated

Anyone please help😅