We provide a formula for the partition function of five-dimensional N = 1 gauge theories on M 4 × S 1 , topologically twisted along M 4 in the presence of general background magnetic fluxes, where M 4 is a toric Kähler manifold. The result can be expressed as a contour integral of the product of copies of the K-theoretic Nekrasov's partition function, summed over gauge magnetic fluxes. The formula generalizes to five dimensions the topologically twisted index of three-and four-dimensional field theories. We analyze the large N limit of the partition function and some related quantities for two theories: N = 2 SYM and the USp(2N ) theory with N f flavors and an antisymmetric matter field. For P 1 × P 1 × S 1 , which can be easily generalized to Σ g 2 × Σ g 1 × S 1 , we conjecture the form of the relevant saddle point at large N . The resulting partition function for N = 2 SYM scales as N 3 and is in perfect agreement with the holographic results for domain walls in AdS 7 × S 4 . The large N partition function for the USp(2N ) theory scales as N 5/2 and gives a prediction for the entropy of a class of magnetically charged black holes in massive type IIA supergravity. arXiv:1808.06626v3 [hep-th] 27 Nov 2018 14 See [87, sect. 4.2] for a more complete explanation..(3.96) 26 The free energy on Σ g2 × S 3 as a function of ∆ was explicitly computed in field theory in [67] after the completion of this work. The result in [67] perfectly agrees with (3.95).