NICOSIA, Cyprus (AP) -- Cyprus clinched a last-minute solution to avert imminent financial meltdown early Monday after it agreed to slash its oversized banking sector and inflict hefty losses on wealthy depositors in troubled banks to secure a 10 billion euro ($13 billion) bailout.

The deal, described by the country's politicians as "painful," was agreed with euro finance ministers in Brussels just in time. The European Central Bank had threatened to cut off crucial emergency assistance to the Cyprus's embattled banks after Monday if no agreement was reached.

Without that funding, Cyprus' banks would have collapsed, dragging the economy down with them and threatening the small Mediterranean island's membership of the 17-strong group of European Union countries that use the euro -- all of which would have sent the EU's markets spinning.

"The result that was found is right," German Chancellor Angela Merkel said. "It also makes those who helped cause these undesirable developments play their part. That is how it should be."

Germany has long insisted Cypriot banks, which attracted foreign investors with high interest rates, needed to contribute to the bailout.

"I think that a fair sharing of the burden was achieved," she said. "On one hand, the banks have to take responsibility for themselves. That is what we have always said: we do not want taxpayers to have to rescue banks, we want banks to rescue themselves."

Markets in Europe reacted positively, opening sharply higher, and the euro was back near $1.30.

The mood in Nicosia was more somber, however.

"This decision is painful for the Cypriot people. This decision was a defeat of solidarity, of social cohesion, which are fundamental freedoms, fundamental principles of the European Union," Parliament President Yiannakis Omirou told AP.

"So as soon as possible we have to prepare our economy to go out from the mechanism and the troika," he said, referring to the bailout agreement and the three-member delegation from the European Commission, International Monetary Fund, and ECB, who oversee implementation of bailout measures.

Banks in Cyprus have been closed for more than a week in Cyprus while politicians wrangled on how to raise 5.8 billion euros ($7.5 billion) to qualify for the rescue. An alternative was needed after the country's lawmakers resoundingly defeated the initial plan which would have seized up to 10 percent of funds in people's accounts in all banks.

While cash has been available through ATMs, many machines have quickly run out. Daily withdrawal limits of 100 euros ($130) were imposed on ATMs of the country's two troubled lenders, Laiki and Bank of Cyprus, on Sunday. All banks were scheduled to reopen Tuesday -- although a final decision on that was expected later Monday by the Central Bank.

Some form of capital controls will almost certainly be imposed once the banks open to prevent a potential bank run -- although the details remained unclear Monday afternoon. Lawmakers passed a bill last week allowing authorities to restrict financial transactions in times of crisis.

Ordinary Cypriots were trying to be optimistic.

"We believe in our people. People will work hard so we can stand on our feet again," said Nicosia resident Nicos Andreou Theodorou.

Under the new plan, the bulk of the funds will be raised by forcing losses on wealthy savers in two of the country's banks, with the remainder coming from tax increases and privatizations.

Laiki, the country's second-largest bank, will be restructured, with all bond-holders and people with more than 100,000 euros in their accounts facing significant losses. The bank will be dissolved immediately into a bad bank containing its uninsured deposits and toxic assets, with the guaranteed deposits being transferred to the nation's biggest lender, Bank of Cyprus.

Deposits at Bank of Cyprus above 100,000 euros will be frozen until it becomes clear whether or to what extent they will also be forced to take losses. Those funds will eventually be converted into bank shares. German Finance Minister Wolfgang Schaeuble said he expected a bit more than 50 percent of savings at Bank of Cyprus will be involved in the swap.

It is not yet clear how severe the losses would be to Laiki's large bank deposit holders, but the euro finance ministers noted the restructure expected to yield 4.2 billion euros ($5.4 billion) overall. Analysts have estimated investors might lose up to 40 percent of their money.

The plan agreed Monday does not need extra approval from Cyprus's parliament because the losses are part of a restructuring of the island's banks -- which would come under legislation passed last week -- and not a tax.

"Today's agreement should ... prevent a further escalation of the eurozone crisis and much more dramatic damage that would have followed from a Cypriot eurozone exit," said Reinhard Cluse, Managing Director of UBS Investment Bank.

However, Cluse said there is a risk that Cyprus' debt load may become unsustainable as the country is expected to enter a deep recession due to the cuts to its banking sector.

To further secure Cyprus's economy, the size of the country's banking sector -- worth up to eight times the country's gross domestic product of about 18 billion euros ($23.3 billion) -- must be drastically reduced , said Jeroen Dijsselbloem, who chairs the meetings of the eurozone's finance ministers.

The international creditors said the country's business model of attracting foreign investors, including many Russians, with low taxes and lax financial regulation had backfired and needed to be reformed. The country would also have to cut its budget, implement structural reforms and privatize state assets.

Russia's prime minister on Monday slammed the deal, saying the agreement was tantamount to theft: "In my opinion, the stealing of what has been stolen continues there."

Russian citizens hold as much as 20 billion euros ($26 billion) in Cypriot banks. Russian Deputy Prime Minister Igor Shuvalov also expressed concerns over the impact of Monday's decision.

"Despite all the assurances that we're receiving from the European Commission we fear that this (decision) could affect the stability of the euro, the stability of the eurozone and would send shockwaves to deteriorate the situation on the whole," he said.

Several national parliaments in eurozone countries such as Germany must approve the deal, which might take another few weeks. EU officials said they expect the whole program to be approved by mid-April.