PORTLAND, Maine — A former chief of the Maine State Police will serve four years in prison after pleading guilty Tuesday to charges that he repeatedly sexually assaulted a young girl earlier this year.

Under the terms of a plea agreement, Andrew Demers, 74, of New Gloucester, pleaded guilty to Class B unlawful sexual contact and Cumberland County District Attorney Stephanie Anderson agreed to drop a Class A charge of gross sexual assault.

Justice Thomas Warren sentenced Demers to five years in prison with all but four years suspended, and three years of probation. Demers must turn himself in at the Cumberland County Jail at 9 a.m. on Nov. 11.

Demers, who served 26 years with the Maine State Police and held the position of chief from 1987 to 1993, was arrested on March 17 and initially charged with Class B unlawful sexual contact with a person younger than 12. But in April, a Cumberland County grand jury indicted Demers on the more severe count of Class A gross sexual assault and Class B unlawful sexual contact.

He pleaded not guilty to the charges in April.

But on Tuesday, Demers told Warren that he was guilty of the lesser charge. His attorney, Walt McKee, acknowledged in a sentencing memo that after his arrest, Demers attempted suicide by overdosing on medication prescribed for a heart condition. While in the hospital, he confessed “to touching the victim and having her touch him” between 5 and 10 times in February and March 2014.

McKee asked the judge to consider factors, including Demers’ “exemplary” career and his admission of guilt, that prevented the need for the child to testify. He asked Warren to allow Demers to serve any sentence in county jail because of the potential dangers the former law enforcement chief could face if sent to the state prison.

Among those to speak on Demers’ behalf were several former law enforcement officers, an adult granddaughter, and the defendant’s clinical psychologist. The psychologist told Warren that Demers suffered from post-traumatic stress disorder from his law enforcement career. He also said Demers would be “the most highly-coveted and prized [target]” of prison violence, adding, “I don’t believe the Department of Corrections can keep Andy alive for long.”

Demers began sobbing as an adult granddaughter told Warren of spending nights at her grandparents’ house as a child, “and each night we’d go to sleep singing the Marine Corps hymn.”

The district attorney argued for a nine-year sentence, telling the judge that a forensic evaluation found “no diminished capacity.” Anderson said Demers’ background in law enforcement warranted more time because “[he] must have known probably better than anyone the devastating impact that child sexual abuse has upon a victim and their family. And yet he did it over and over.”

She pointed to a key piece of evidence, a two-minute videotape of the victim, taken by the victim’s father on a cell phone.

Anderson said the two were in the car one day when the girl began describing the sexual acts. The father stopped the car and asked the child to repeat what she’d said while he videotaped it.

The district attorney drew gasps from Demers’ friends and family gathered in the courtroom when she suggested that as a law enforcement officer for 40 years, “if he really wanted to kill himself, he could have. You don’t kill yourself by taking heart medication.”

Demers sobbed when he addressed the judge, saying, “Your honor, since my arrest I have been torn with shame and humiliation with the realization of my behavior and the pain I’ve brought to my family, friends, and most of all, to this sweet little girl.”

“I’ve built my life around integrity and honor and in a few moments, I destroyed it all,” he continued.

Of his suicide attempt, Demers said, “I believe the Lord spared my life so I might be able to help heal the people I’ve hurt so badly.”

In pronouncing sentence, Warren noted aggravating factors, including the gravity of the offense and the number of times it occurred, the extremely young age of the victim, and the fact that Demers’ had created “a very, very sexualized relationship” with the child.

But Warren said “significant” mitigating factors included his “sterling” career, the number of character references from “a highly respected group of law enforcement officials.”

In acknowledging the risk of violence Demers might encounter in prison, Warren said the Department of Corrections would be well aware of the situation.

Following the sentence, Anderson said Warren “is a thoughtful judge” who “understands and knows the law. This was one of many appropriate sentences.”

Demers declined to comment. McKee said, “This certainly was not what we had hoped for, but four years is a heck of a lot better than 10.”

To reach a sexual assault advocate, call the Statewide Sexual Assault Crisis and Support Line at 800-871-7741, TTY 888-458-5599. This free and confidential 24-hour service is accessible from anywhere in Maine. Calls are automatically routed to the closest sexual violence service provider.