Runtime error in DBOY

Hello,
Can somebody help me with the given problem below.


Here’s link to my solution.
I’m getting illegal memory access error.
http://www.codechef.com/viewsolution/4762523

Thanks in advance.

in the line:

for(i=1;i<=n;i++) cin>>h[i];

you have earlier declared a size of n array : --> int h[n],k[n];

in c and c++ arrays are 0 indexed. Hence valid indexes are 0 to n-1. When you are attempting to access h[n] in the loop it will give segmentation fault or illegal memory access!!