Codeforces 655B B. Mischievous Mess makers (greedy)

Source: Internet
Author: User

Topic Links:

B. Mischievous Mess Makers

Time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

It is a balmy spring afternoon, and Farmer John ' S  n  cows be ruminating Abou T link-cut cacti in their stalls. The cows, Labeled 1 through N , is arranged so The  i -th cow occupies the  i -th stall from The left. However, Elsie, after realizing that she'll forever live in the shadows beyond Bessie ' s limelight, have formed the Mischi Evous Mess makers and is plotting to disrupt this beautiful pastoral rhythm. While Farmer John takes His  K  minute long nap, Elsie and the Mess makers plan to Repeatedly choose, distinct stalls and swap the cows occupying those stalls, Making no more than One swap each minute.

Being the meticulous pranksters that they is, the mischievous Mess makers would like to know the maximum messiness attain Able in the K minutes that they has. We denote as pi the label of the cow in the I-th stall. The messiness of an arrangement of cows are defined as the number of pairs (i, J) such that c12>I < J and pi > pJ.

Input

The first line of the input contains the integers n and k (1≤ n, K ≤100 -the number of cows and the length of Farmer John ' s nap, respectively.

Output

Output A single integer, the maximum messiness this mischievous Mess makers can achieve by performing no more than K swaps.

Examplesinput
5 2
Output
10
Input
1 10
Output
0
Note

In the first sample, the mischievous Mess makers can swaps the cows in the stalls 1 and 5 during the first minute, Then the cows in stalls2 and 4 during the second minute. This reverses the arrangement of cows, giving us a total messiness of ten.

In the second sample, there are only one cow, so the maximum possible messiness is 0.

Test instructions: Ask for a maximum of K this interchange, the number of pairs (i, J) such that I < J and p i > pJ.

Idea: From the greedy know, each interchange the most out of a pair can get the largest number, each change the outermost pair, number increase 2*n-3;

AC Code:

#include <bits/stdc++.h>using namespacestd;intMain () {Long Longn,k,ans=0; CIN>>n>>K;  while(k--&&n>1) {ans+=2*n-3; N-=2; } cout<<ans<<Endl; return 0;}

Codeforces 655B B. Mischievous Mess makers (greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.