Hi all,
I am using the following code to solve the “Hydroelectric Dam” problem. It gives the right answer in my compiler and also on SPOJ. But somehow the solution is termed “wrong answer”. Unable to find out why its wrong? Maybe you can help.
#include
using namespace std;
int main()
{
int TestCases, Height, Volume, SumInternalBlocks;
cin >> TestCases;
int WidthArray[TestCases];
int DamArray[TestCases][10];
for(int i =0; i<TestCases; i++){
cin >> WidthArray[i];
for(int j=0; j<WidthArray[i];j++){
cin >> DamArray[i][j];
}
}
for(int i=0; i<TestCases;i++){
if(DamArray[i][0] < DamArray[i][WidthArray[i]-1]) Height = DamArray[i][0];
else Height = DamArray[i][WidthArray[i]-1];
SumInternalBlocks = 0;
for(int j=0; j<WidthArray[i];j++){
if (DamArray[i][j] > Height) DamArray[i][j] = Height;
if ( j!=0 && j != WidthArray[i]-1){
SumInternalBlocks = SumInternalBlocks + DamArray[i][j];
}
}
Volume = Height * (WidthArray[i]-2) - SumInternalBlocks;
cout<<Volume<<endl;
}
return 0;
}