蛮力求解
#include <iostream> using namespace::std;
using std::cout;
using std::cin; int zdjsl(int n, int height[])
{int sum = 0;int left_max[n];int right_max[n];left_max[0] = height[0];right_max[n-1] = height[n-1];for(int i=1; i<n; i++){left_max[i] = max(left_max[i-1],height[i]);}for(int i=n-2; i>=0; i--){right_max[i] = max(right_max[i+1],height[i]);}for(int i=0; i<n; i++){int temp = min(left_max[i],right_max[i])-height[i];if(temp>0){sum = sum+temp;}}return sum;
}int main()
{int n;cin >> n;int height[n];for(int i=0; i<n; i++){cin >> height[i];}std::cout << zdjsl(n, height);return 0;
}