
946 So.2d 99 (2006)
Richard McDUFFIE, Appellant,
v.
STATE of Florida, Appellee.
No. 2D06-2996.
District Court of Appeal of Florida, Second District.
December 29, 2006.
WHATLEY, Judge.
Richard McDuffie appeals the summary denial of his motion to correct illegal sentence filed pursuant to Florida Rule of Criminal Procedure 3.800(a). We reverse the postconviction court's summary denial of McDuffie's claim that his forty-five-year prison sentence for second-degree murder with a firearm was illegal and remand for further proceedings.
On January 11, 1995, in circuit court case 93-887, McDuffie pleaded no contest pursuant to a stipulated upward departure negotiated plea agreement to the lesser offense of second-degree murder with a firearm, a violation of sections 782.04(2) and 775.087, Florida Statutes (1991). On May 11, 1995, the trial court sentenced McDuffie to forty-five years' prison. This court affirmed the judgment and sentence. See McDuffie v. State, 681 So.2d 285 (Fla. 2d DCA 1996) (table decision).
In considering the rule 3.800(a) claim, the postconviction court found that McDuffie pleaded no contest to second-degree murder, a violation of section 782.04, Florida Statutes (1991), a felony of the first-degree punishable by imprisonment for a term of years not exceeding life or as provided in section 775.082. Therefore, the postconviction court found that McDuffie's sole reliance on section 775.082 was misplaced as section 782.04 specifically provided that second-degree murder is punishable by imprisonment for a term of years not exceeding life.
*100 However, McDuffie pleaded nolo contendere to second-degree murder with a firearm, a violation of sections 782.04(2) and 775.087, Florida Statutes (1991). Pursuant to section 782.04(2), second-degree murder is "a felony of the first degree, punishable by imprisonment for a term of years not exceeding life or as provided in s. 775.082, s. 775.083, or s. 775.084." Section 775.087, Florida Statutes (1991), states:
(1) Unless otherwise provided by law, whenever a person is charged with a felony, except a felony in which the use of a weapon or firearm is an essential element, and during the commission of such felony the defendant carries, displays, uses, threatens, or attempts to use any weapon or firearm, or during the commission of such felony the defendant commits an aggravated battery, the felony for which the person is charged shall be reclassified as follows:
(a) In the case of a felony of the first degree, to a life felony.
Therefore, McDuffie's second-degree murder conviction required reclassification to a life felony due to McDuffie's use of a firearm.
Section 775.082(3)(a), Florida Statutes (1991), states that a person who has been convicted of a life felony committed on or after October 1, 1983, may be punished by "a term of imprisonment for life or by a term of imprisonment not exceeding 40 years." Therefore, McDuffie's forty-five-year prison sentence exceeds the forty-year statutory maximum under section 775.082(3)(a). "A trial court cannot impose an illegal sentence even pursuant to a plea bargain." Ferguson v. State, 804 So.2d 411, 412 (Fla. 4th DCA 2001). Consequently, McDuffie's forty-five-year prison sentence is illegal and must be reversed. See Ramos v. State, 931 So.2d 1023 (Fla. 3d DCA 2006) (holding that fifty-year sentence for life felony was illegal where section 775.082(3)(a), Florida Statutes (1989), proscribed punishment by life imprisonment or for a term not exceeding forty years); Miranda v. State, 832 So.2d 937, 941 (Fla. 3d DCA 2002) (holding that "for a 1993 crime date, the offense of second-degree murder with a weapon is classified as a life felony" with a maximum penalty of "life imprisonment or a term of years not exceeding forty" and, therefore, the fifty-year-prison sentence exceeds the legal maximum); Ferguson, 804 So.2d at 412 (reversing forty-five-year sentence for second-degree murder, a first-degree felony that became a life felony due to the possession of a firearm, which could be punished by a term not exceeding forty years); Wright v. State, 600 So.2d 11, 12 (Fla. 4th DCA 1992) (reversing sixty-year sentences for two first-degree felonies that became life felonies due to the possession of a firearm, which could be punished by a term not exceeding forty years).
McDuffie's forty-five-year prison sentence for second-degree murder with a firearm is illegal. See Ferguson, 804 So.2d at 412. However, McDuffie's forty-five-year prison sentence was the result of a stipulated upward departure pursuant to a negotiated plea agreement. Therefore, McDuffie may be resentenced within the statutory maximum of forty years only with the State's agreement. See Lang v. State, 931 So.2d 922, 922 (Fla. 2d DCA 2005). If the State does not agree to such a sentence, McDuffie must be allowed to withdraw his plea. See id.
The court notes that the trial court also failed to impose the three-year mandatory minimum pursuant to section 775.087(2)(a)(1) when McDuffie was originally sentenced. Whether such a mandatory minimum is required to be imposed on remand is contingent on how the State elects to proceed. Therefore, on remand, *101 the postconviction court shall impose any applicable mandatory minimum pursuant to section 775.087.
Reversed and remanded with instructions.
SALCINES and VILLANTI, JJ., Concur.
