wls there are n quadratic function Fi (x) = aix2 + bix + ci (1 ≤ i ≤ n).
Now he wants = 1xi = m and x is a positive integer under the conditions required to Σni = 1Fi (xi at Σni ) minimum.
This request is a minimum.
Input
The first line two positive integers n, m.
N following lines of three integers a, b, c represent quadratic term of the quadratic function, the first term, the constant term coefficient.
N-m ≤ ≤ ≤. 1 100, 000
. 1 ≤ A ≤. 1, 000
-1, 000 ≤ B, C ≤. 1, 000
the Output
line represents an integer answer.
The Input the Sample
2. 3
. 1. 1. 1
2 2 2
the Sample the Output
13 is
Ideas:
because it is the subject of the request xi must be a positive integer, then each have a minimum xi is 1, so we give each xi value of 1,
At the same time, we use the stack to maintain a quadratic function for each current F (xi + 1) - F (xi) Maintenance Why this number?
Because the current value of the corresponding function value xi is F (xi) we want the sum xi = m if M> n is certainly worth to give some of the quadratic function xi increase the value, then we pass the maintenance of this information,
Every greedy to add a function that allows to increase the value xi xi + 1 most answers contribution is minimal.
This process is repeated until the sum xi = m
See details Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node
{
ll a,b,c;
ll x;
ll val;
bool operator < (const node & t) const
{
return val>t.val;
}
};
priority_queue<node> heap;
int n;
int m;
ll gao(ll a,ll b, ll c ,ll x)
{
return a*x*x+b*x+c;
}
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n>>m;
ll a,b,c;
ll ans=0ll;
repd(i,1,n)
{
cin>>a>>b>>c;
node temp;
temp.a=a;
temp.b=b;
temp.c=c;
temp.x=1;
temp.val=gao(a,b,c,2)-gao(a,b,c,1);
heap.push(temp);
}
m-=n;
while(m--)
{
node temp=heap.top();
heap.pop();
temp.x++;
temp.val=gao(temp.a,temp.b,temp.c,temp.x+1)-gao(temp.a,temp.b,temp.c,temp.x);
heap.push(temp);
}
while(!heap.empty())
{
node temp=heap.top();
heap.pop();
ans+=gao(temp.a,temp.b,temp.c,temp.x);
}
cout<<ans<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}