In this article, we give sharp bounds on the Hosoya index and the Merrifield-Simmons index for connected graphs of fixed size. As a consequence, we determine all connected graphs of any fixed order and size which maximize the Merrifield-Simmons index. Sharp lower bounds on the Hosoya index are known for graphs of order n and size m ∈ [n − 1, 2n − 3] ∪ n−1 2 , n 2 ; while sharp upper bounds were only known for graphs of order n and size m ≤ n + 2. We give sharp upper bounds on the Hosoya index for dense graphs with m ≥ n 2 − 2n/3. Moreover, all extreme graphs are also determined.