Please help me in giving me ideas of how to approach this problem. My idea is a greedy approach.

The problem is:

You are working in Samara, Russia for a few days, Each day has a new pay per unit of work and a new cost per unit of food. Working 1 unit costs 1 unit of energy, and eating 1 unit of food adds 1 unit of energy. Here
are some specifications of your employment:

+You arrive with no money, but with energy. You can never have more energy than you arrive with, and it can never be negative.

+You can do any amount of work every day (possibly not do any work at all), limited only by your energy. You cannot work when your energy is zero.

+You can eat any amount of food every day (possibly not have any food at all), limited by the money you have. You cannot eat when the money you have is zero.

+You can eat food at the end of the day, and cannot return to work after eating. You can return to work on the next day.
Your true goal is to return home with as much money as possible. Compute the maximum amount of money you can take home.

For example, consider a 3 day stay where pay per unit work for each day is as follows: earning=[7, 2, 4]. The cost of food is cost=[7, 3, 6]. You start with e=5 units of energy.

*First day: 1 unit work is worth 1, and 1 unit food costs 1. There is no financial incentive to go to work this day.

*Second day: 1 unit work earns 2, and 1 unit food costs 3, Thus you spend more to eat than total earning so there is no financial incentive to go to work on this day.

*Third day: You earn 4 units per unit of work. The cost of food is irrelevant this day, as you are leaving for the home straight from work. You spend all of your energy working, collect your pay: 5 x 4 = 20 units of
money and go home without buying dinner.

Function Description
Complete the function calculateProļ¬t in the editor below. The function must return an integer that represents the maximum earnings that can be taken home at the end of your stay.

My solution so far(needs to be improved):

```
function calculateProfit(n, earning, cost, e) {
// Write your code here
let sum = 0
let ef = e;
let count = 0;
let max = 0;
for (let i = 0; i < n; i++){
if (i != n - 1) {
console.log("next day " + ef + " " + ef * earning[i + 1] + "--" + ef * cost[i]);
if (earning[i] > cost[i]) {
sum += ef * earning[i];
e = 0;
max = 0;
if (ef * earning[i + 1] > ef * cost[i] && sum > 0) {
//console.log(e);
sum -= ef * cost[i];
e = ef;
}
}
else {
count++;
max = Math.max(max, earning[i]);
}
}
else {//last day
if (earning[i] <= cost[i]) {
count++;
}
max = Math.max(max, earning[i]);
if (e > 0)
sum += ef * max;
}
console.log(i, "-", sum," max=",max);
}
console.log("count",count);
if (count == n) {
earning.sort();
sum = earning[n-1] * ef;
}
return sum;
}
```