#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    long   depth;
    long  time;
} Station;

void heapify(long *heap, long size, long i)
{
    long minimum = i;
    long l = 2 * i + 1;
    long r = 2 * i + 2;


    if (l < size && heap[l] < heap[minimum])
        minimum = l;
    if (r < size && heap[r] < heap[minimum])
        minimum = r;
    if (minimum != i)
    {
        long temp = heap[minimum];
        heap[minimum] = heap[i];
        heap[i] = temp;
        heapify(heap, size, minimum);
    }
}

long compare(const void *a, const void *b)
{
    Station *stationA = (Station *)a;
    Station *stationB = (Station *)b;
    return stationA->depth - stationB->depth;
}

long deleteMin(long *heap, long *size)
{
    if (*size == 0)
        return -1;
    long min = heap[0];
    heap[0] = heap[--(*size)];
    heapify(heap, *size, 0);
    return min;
}

void insert(long heap[], long *size, long value)
{
    heap[(*size)++] = value;
    for (long i = *size / 2 - 1; i >= 0; i--)
    {
        heapify(heap, *size, i);
    }
}

void func(Station stations[], long num_stations, long ini_oxygen, long final_depth, long additional_oxygen)
{
    //printf("inside func func\n");
    qsort(stations, num_stations, sizeof(Station), compare);
    long heap[num_stations];
    long heap_size = 0;
    long current_time = 0, max_depth = ini_oxygen;

    for (long i = 0; i < num_stations; ++i)
    {
        while (max_depth < stations[i].depth && heap_size > 0)
        {
            long refill_time = deleteMin(heap, &heap_size);
            current_time += refill_time;
            max_depth += additional_oxygen;
        }

        if (max_depth < stations[i].depth)
            break;
        
       
        heap[heap_size] = stations[i].time;
        long child = heap_size;
        long parent = (child-1)/2;
        while (child > 0 && heap[child] < heap[parent])
         {//printf("inside inside inside while loop\n");
            long temp = heap[parent];
            heap[parent] = heap[child];
            heap[child] = temp;
            child=parent;
            parent=(child-1)/2;
        }

        heap_size++;

        if (max_depth >= final_depth)
        { 
            printf("%d\n", current_time);
            return;
        }
    }

    while (heap_size > 0 && max_depth < final_depth)
    {
        long refill_time = deleteMin(heap, &heap_size);
current_time += refill_time;
        max_depth += additional_oxygen;
    }

    if (max_depth >= final_depth)
    {
        printf("%ld", current_time);
    }
    else
    {
        printf("-1 %ld", max_depth);
    }
}


int main()
{
    long num_stations, ini_oxygen, final_depth, additional_oxygen;
    scanf("%ld %ld %ld %ld", &num_stations, &ini_oxygen, &final_depth, &additional_oxygen);

    Station stations[num_stations];
    for (long i = 0; i < num_stations; ++i)
    {
        scanf("%ld", &stations[i].depth);
    }
    for (long i = 0; i < num_stations; ++i)
    {
        scanf("%ld", &stations[i].time);
    }

    func(stations, num_stations, ini_oxygen, final_depth, additional_oxygen);
    return 0;
}
modify this code to solve this
Deep Sea Adventure 2
Submit solution
My submissions
All submissions
Best submissions
Points:35 (partial)
Time limit:1.0s
Memory limit:128M
Author:
admin
Problem type
Allowed languages
C
Problem Statement
Luckily Percy made it safely to the caves located deep on the ocean floor. Now he gets to explore the various chambers present there. However, he again has an oxygen issue. As per safety guidelines, each chamber has a certain minimum oxygen level limit that the diver needs to have before entering it. At the same time, after visiting each chamber, Percy either loses certain amount of oxygen (due to consumption) or ends up gaining some (due to presence of air pockets). Percy is curious and wants to know whether it will be possible for him to visit all the chambers without running out of oxygen.

Percy initially starts with 
 amount of oxygen. There are 
 chambers, where the 
th chamber has a minimum oxygen requirement of 
i and after visiting the chamber, his oxygen level changes by 
i amount.

You have to help Percy again by letting him know whether it is possible for him to visit all the chambers in any order without running out of oxygen. He can visit one chamber only once.

Input Format
The first line of input contains integers 
 and 
, denoting the number of chambers and the initial oxygen supply respectively.

The second line contains 
 integers representing the minimum oxygen requirement 
i of each chamber.

The third line contains 
 integers representing the changee 
i in oxygen level after Percy visits a chamber.

Constraints
There are two sets of constraints, which differ only in the range of 
. The points of this question are split based on the value of this range.

Set 1


i 

i 

Set 2


i 

i 

Output Format
You need to output "YES" or "NO" (case sensitive) based on whether it is possible for him to visit all the chambers or not.

Sample Test Case 1:
Input:
Copy
3 5
3 6 1
-4 -3 6
Output:
Copy
YES
Explanation:
Percy wants to visit all the 
 chambers. He initially has 
 units of oxygen. He goes to the first chamber and is left with 
 unit. Then he goes to the third chamber and is left with 
 units. Lastly, he visits the second chamber and is left with 
 units of oxygen.

Sample Test Case 2:
Input:
Copy
2 7
7 3
-4 -3
Output:
Copy
YES
Explanation:
Percy wants to visit all the 
 chambers. He initially has 
 units of oxygen. He goes to the first chamber and is left with 
 units. Then he visits the second chamber and is left with 
 units of oxygen.

Sample Test Case 3:
Input:
Copy
2 5
2 2
-3 -4
Output:
Copy
NO
Explanation:
Percy wants to visit all the 
 chambers. He initially has 
 units of oxygen. One way he can try is going to the first chamber in the first go and be left with 
 units, with which he can enter the second chamber, but then run out of oxygen. The other way is he can go to second chamber in the first attempt, but this leaves him with only 
 unit afterwards, with which he cannot enter the first chamber.