#include <iostream>
using namespace std;
bool FindPit(int* _Array, int _Size, int& inout_From, int& out_To, int& out_Max)
{
out_To = inout_From;
out_Max = 0;
int lv_Max = _Array[inout_From];
bool lv_LFind = true;
for(int i = inout_From; i < _Size; ++i)
{
if(lv_LFind)
{
if(_Array[i] >= lv_Max)
{
lv_Max = _Array[i];
inout_From = i;
}
else
lv_LFind = false;
}
if(!lv_LFind)
{
if(_Array[i] >= out_Max)
{
out_Max = _Array[i];
out_To = i;
}
if(out_Max >= lv_Max)
{
out_Max = lv_Max;
return true;
}
}
}
return out_To > inout_From;
}
int Volume(const int* _Array,const int _From,const int _To,const int Max)
{
int lv_Volume = 0;
for(int i = _From; i < _To; i++)
lv_Volume += (Max - _Array[i]) > 0 ? (Max - _Array[i]) : 0;
return lv_Volume;
}
int Volume(int* _Array, int _Size)
{
int lv_Volume = 0;
int lv_From = 0;
int lv_To;
int lv_Max;
while(FindPit(_Array, _Size, lv_From, lv_To, lv_Max))
{
auto lv_Vol = Volume(_Array, lv_From, lv_To, lv_Max);
lv_Volume += lv_Vol;
lv_From = lv_To;
}
return lv_Volume;
}
int main()
{
// 2,5,1,3,8,2,1,7,7,6 ans 17
// 2,5,1,2,3,4,7,7,6 ans 10
// 2,5,1,3,1,2,1,7,7,6 ans 17
int lv_Array[] = {1, 2, 3, 2, 5, 3, 4};
auto lv_Volume = Volume(lv_Array, sizeof(lv_Array)/sizeof(*lv_Array));
if( lv_Volume == 2)
cout << "PASS";
else
cout << "FAIL";
return 0;
}
CiNpbmNsdWRlIDxpb3N0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmJvb2wgRmluZFBpdChpbnQqIF9BcnJheSwgaW50IF9TaXplLCBpbnQmIGlub3V0X0Zyb20sIGludCYgb3V0X1RvLCBpbnQmIG91dF9NYXgpCnsKCW91dF9UbyA9IGlub3V0X0Zyb207CglvdXRfTWF4ID0gMDsKCQoJaW50IGx2X01heCA9IF9BcnJheVtpbm91dF9Gcm9tXTsKCWJ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