#include <iostream>
//#include <vector>
using namespace std;
// Function to return the maximum water that can be stored
int maxWater(int arr[]) {
int res = 0;
// For every element of the array
for (int i = 1; i < sizeof(arr); 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 <sizeof(arr); 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+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBGdW5jdGlvbiB0byByZXR1cm4gdGhlIG1heGltdW0gd2F0ZXIgdGhhdCBjYW4gYmUgc3RvcmVkCmludCBtYXhXYXRlcihpbnQgYXJyW10pIHsKICAgIGludCByZXMgPSAwOwogICAgLy8gRm9yIGV2ZXJ5IGVsZW1lbnQgb2YgdGhlIGFycmF5CiAgICBmb3IgKGludCBpID0gMTsgaSA8IHNpemVvZihhcnIpOyBpKyspIHsKCiAgICAgICAgLy8gRmluZCB0aGUgbWF4aW11bSBlbGVtZW50IG9uIGl0cyBsZWZ0CiAgICAgICAgaW50IGxlZnQgPSBhcnJbaV07CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBpOyBqKyspCiAgICAgICAgICAgIGxlZnQgPSBtYXgobGVmdCwgYXJyW2pdKTsKCiAgICAgICAgLy8gRmluZCB0aGUgbWF4aW11bSBlbGVtZW50IG9uIGl0cyByaWdodAogICAgICAgIGludCByaWdodCA9IGFycltpXTsKICAgICAgICBmb3IgKGludCBqID0gaSArIDE7IGogPHNpemVvZihhcnIpOyBqKyspCiAgICAgICAgICAgIHJpZ2h0ID0gbWF4KHJpZ2h0LCBhcnJbal0pOwoKICAgICAgICAvLyBVcGRhdGUgdGhlIG1heGltdW0gd2F0ZXIKICAgICAgICByZXMgKz0gKG1pbihsZWZ0LCByaWdodCkgLSBhcnJbaV0pOwogICAgfQoKICAgIHJldHVybiByZXM7Cn0KCmludCBtYWluKCkgewogICAgaW50IGFycltdID0geyAyLCAxLCA1LCAzLCAxLCAwLCA0IH07CiAgIGludCByPSBtYXhXYXRlcihhcnIpOwogICBjb3V0PDxyOwogICAgcmV0dXJuIDA7Cn0=