#include <iostream>
//#include <vector>
using namespace std;
// Function to return the maximum water that can be stored
int maxWater(int arr[]) {
int res = 0;
int n=sizeof(arr)/sizeof(arr[0]);
for (int i = 1; i <n-1; i++) {
// Find the maximum element on its left
int left = arr[i];
for (int j = 0; j < i; j++)
left = max(left, arr[j]);
// Find the maximum element on its right
int right = arr[i];
for (int j = i + 1; j <n; j++)
right = max(right, arr[j]);
// Update the maximum water
res += (min(left, right) - arr[i]);
}
return res;
}
int main() {
int arr[] = { 2, 1, 5, 3, 1, 0, 4 };
int r= maxWater(arr);
cout<<r;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgovLyNpbmNsdWRlIDx2ZWN0b3I+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBGdW5jdGlvbiB0byByZXR1cm4gdGhlIG1heGltdW0gd2F0ZXIgdGhhdCBjYW4gYmUgc3RvcmVkCmludCBtYXhXYXRlcihpbnQgYXJyW10pIHsKICAgIGludCByZXMgPSAwOwogIGludCBuPXNpemVvZihhcnIpL3NpemVvZihhcnJbMF0pOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPG4tMTsgaSsrKSB7CgogICAgICAgIC8vIEZpbmQgdGhlIG1heGltdW0gZWxlbWVudCBvbiBpdHMgbGVmdAogICAgICAgIGludCBsZWZ0ID0gYXJyW2ldOwogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgaTsgaisrKQogICAgICAgICAgICBsZWZ0ID0gbWF4KGxlZnQsIGFycltqXSk7CgogICAgICAgIC8vIEZpbmQgdGhlIG1heGltdW0gZWxlbWVudCBvbiBpdHMgcmlnaHQKICAgICAgICBpbnQgcmlnaHQgPSBhcnJbaV07CiAgICAgICAgZm9yIChpbnQgaiA9IGkgKyAxOyBqIDxuOyBqKyspCiAgICAgICAgICAgIHJpZ2h0ID0gbWF4KHJpZ2h0LCBhcnJbal0pOwoKICAgICAgICAvLyBVcGRhdGUgdGhlIG1heGltdW0gd2F0ZXIKICAgICAgICByZXMgKz0gKG1pbihsZWZ0LCByaWdodCkgLSBhcnJbaV0pOwogICAgfQoKICAgIHJldHVybiByZXM7Cn0KCmludCBtYWluKCkgewogICAgaW50IGFycltdID0geyAyLCAxLCA1LCAzLCAxLCAwLCA0IH07CiAgIGludCByPSBtYXhXYXRlcihhcnIpOwogICBjb3V0PDxyOwogICAgcmV0dXJuIDA7Cn0=