463-B Hello people.
I am writing this entry to share my approach on this problem since I have noticed that the official solution has used a much complicated solution than necessary.
We are given 'n' pylons with heights h(1,2....n). When we move from i to (i+1) the energy would be: energy += h[i]-h[i+1] Here we must never let the energy become negative integer and we want to start of with the minimum possible energy
As we move across pylons we notice that the intermediate energies cancel out. And we can successfully say that the the energy would only depend on the initial energy and the height of the current pylon (pylon i) Thus we can say that the lowest possible energy can only be possible on the tallest(max height) pylon
Now to make sure that the energy never goes below 0, we need to deduct the maximum possible height of pylon with the starting pylon Also given that we start from the ground level i.e from level 0, this simplifies to:
Minimum cost = $(maximum height of pylon possible)
Doing this we can ensure that the path is always possible and energy never becomes negative
This is my code snippet:
//Author: CelestialRex
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n;
cin>>n;
vector<int> a(n);
for (int i=0;i<n;++i)
cin>>a[i];
int x = *max_element(a.begin(),a.end());
cout<<x;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}








.