This paper derives the optimality conditions for a Mayer problem with discrete and differential inclusions with viable constraints. Applying necessary and suf lcient conditions of problems with geometric constraints, we prove optimality conditions for second order discrete inclusions. Using locally adjoint mapping, we derive Euler-Lagrange form conditions and transversality conditions for the optimality of the discrete approximation problem. Passing to the limit, we establish suf lcient conditions to the optimal problem with viable constraints. Conditions ensuring the existence of solutions to the viability problems for differential inclusions of second order have been studied in recent years. However, optimization problems of second-order differential inclusions with viable constraints considered in this paper have not been examined yet. The results presented here are motivated by practices for optimization of various fields as the mass movement model well known in traf lc balance and operations research.