Rain Water Harvesting

#include
#include
#include
using namespace std;
int main() {
int n; cin >> n;
int a[n];
int h = INT_MIN;
for(int i = 0; i <n;i++){
cin >> a[i];
h = max(h,a[i]);
}
int grid[h][n];

// 1 - black
// 0 - white
// 2 - blue
for(int i = 0; i < n;i++){
	for(int j = 0; j < a[i] ; j++){
		grid[j][i] = 1;
	}
	for(int j = a[i]; j < h; j++){
		grid[j][i] = 0;
	}
}
int ans = 0;

for(int i =0 ; i < n; i++){
	for(int j = 0 ; j < a[i] ; j++){	
		for(int k = i+1; k < n; k++){
			if(grid[j][k]==1){
				for(int u =i+1; u < k; u++){
					grid[j][u] = 2;
					ans++;
				}
				break;
			}
		}
	}
}
cout << ans << endl;

return 0;

}

Iā€™m getting run error for test cases 6,7,8. When I download these test cases and run them I get the right answer. All other test cases are passing. Thanks for any help!