#define PROBLEM "https://yukicoder.me/problems/no/2078" #include using namespace std; template struct ConvexHullTrick{ deque> deq; ConvexHullTrick(): deq(){ } bool check(pair line1, pair line2, pair line3){ return (line2.first - line1.first) * (line3.second - line2.second) >= (line2.second - line1.second) * (line3.first - line2.first); } T f(pair line, T x){ return line.first * x + line.second; } void add(T a, T b){ pair p = {a, b}; while((int) deq.size() >= 2 && check(deq.at((int) deq.size() - 2), deq.at((int) deq.size() - 1), p)){ deq.pop_back(); } deq.push_back(p); } T query(T x){ while((int) deq.size() >= 2 && f(deq.at(0), x) >= f(deq.at(1), x)){ deq.pop_front(); } return f(deq.at(0), x); } }; int main(){ int t; cin >> t; while(t--){ long long x, a; cin >> x >> a; vector dp(a + 1, 1LL << 60); dp[0] = 0; ConvexHullTrick CHT; for(long long i = 0; i <= a; i++){ if(i >= 1){ dp[i] = min(dp[i], CHT.query(i) + i * i + x); } CHT.add(-2 * i, i * i + dp[i]); } cout << dp[a] << "\n"; } }