May Lunchtime Charges question getting SIGSEGV

Can anyone tell me what’s the problem in my Code?
Question:- CodeChef: Practical coding for everyone

My Code:- CodeChef: Practical coding for everyone

I think this is a valid test input:

1
1 5
1
1 1 1 1 1

If so, it causes an out-of-bounds access in your solution:

[simon@simon-laptop][17:53:36]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling ad17ya-CHARGES.cpp
+ g++ -std=c++14 ad17ya-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
ad17ya-CHARGES.cpp: In function ‘void solve()’:
ad17ya-CHARGES.cpp:13:24: warning: unused variable ‘sum’ [-Wunused-variable]
     ll n, k, dist = 0, sum = 0;
                        ^~~
+ set +x
Successful
[simon@simon-laptop][17:53:40]
[~/devel/hackerrank/otherpeoples]>echo "1
1 5
1
1 1 1 1 1" | ./a.out
ad17ya-CHARGES.cpp:21:30: runtime error: index 1 out of bounds for type 'char [*]'
ad17ya-CHARGES.cpp:21:17: runtime error: index 1 out of bounds for type 'char [*]'
0
0
0
0
0
1 Like