Many regions in the world are protected against flooding by a dike, which may be either natural or artificial. We deal with a model for finding the optimal heights of such a dike in the future. It minimizes the sum of the investments costs for upgrading the dike in the future and the expected costs due to flooding. The model is highly nonlinear, nonconvex, and infinite-dimensional. Despite this, the model can be solved analytically if there is no backlog in maintenance. If there is a backlog in maintenance, then the optimal solution can be found by minimizing a convex function over a finite interval. However, if the backlog becomes extremely large we show that the model breaks down. Our model has been used in The Netherlands to define legal safety standards for the coming decades.