Satisfiability Modulo the Theory of Costs: Foundations and Applications