time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n.

It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct.

To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, …, ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored.

Write the program that determines which tasks will be performed and which will be ignored.

Input

The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks.

Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds.

Output

Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers’ ids on which this task will be performed. Otherwise, print -1.

Examples

input

4 3

1 3 2

2 2 1

3 4 3

output

6

-1

10

input

3 2

3 2 3

5 1 2

output

3

3

input

8 6

1 3 20

4 2 1

6 5 5

10 1 1

15 3 6

21 8 8

output

6

9

30

-1

15

36

Note

In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10).

In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task.

【题目链接】:http://codeforces.com/contest/747/problem/C

【题解】



一个任务一个任务地枚举

因为时间是升序的、所以不用管。

在枚举的时候看看这个任务开始的时候有哪些人是空闲的?

->如何确定某个人是否空闲???

->这个人完成任务的时间.

每让一个人接任务过后,就记录每个人任务完成的时间是什么时候.

时间复杂度O(n*q);



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXP = 1e2+10;
const int MAXQ = 1e5+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); struct abc
{
int t,k,d;
}; int last[MAXP];
abc a[MAXQ];
int n,q;
int b[MAXP];
int ans[MAXQ];
int sum[MAXP]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(q);
rep1(i,1,q)
{
rei(a[i].t);rei(a[i].k);rei(a[i].d);
}
rep1(i,1,q)
{
b[0] = 0;
sum[0] = 0;
rep1(j,1,n)
if (last[j]<a[i].t)
{
b[0]++;
b[b[0]] = j;
sum[b[0]] = sum[b[0]-1]+b[b[0]];
if (b[0]==a[i].k)
break;
}
if (b[0]==a[i].k)
{
ans[i] = sum[b[0]];
rep1(j,1,b[0])
last[b[j]] = a[i].t + a[i].d-1;
}
else
ans[i] = -1;
}
rep1(i,1,q)
printf("%d\n",ans[i]);
return 0;
}

最新文章

  1. C++ 系列:C++ 基础 001
  2. Shell编程之--“grep-awk-sed” 基础用法汇总-菜鸟入门级
  3. ssl原理,非对称加密握手,对称加密传输
  4. java io系列06之 序列化总结(Serializable 和 Externalizable)
  5. Squid 操作实践
  6. JavaScript事件委托的技术原理
  7. AIM Tech Round (Div. 2) B. Making a String 贪心
  8. Zendstudio 9.0.2 安装Aptana3 并且配置 jQuery
  9. 关于mysql binlog日志的格式说明
  10. SharePoint Solutions Deployment-PowerShell
  11. 7. Shell 函数
  12. HTML学习(六)图像
  13. VxWorks 操作系统内存布局
  14. Python Tkinter小试
  15. SoapUI工具做get请求和post请求接口测试
  16. git 本地仓库与远程仓库建立连接
  17. Git学习笔记--- diff工具 kdiff3
  18. Replicating a 2D dynamic array
  19. linux 常用命令总结(一)
  20. 爬虫2.4-scrapy框架-图片分类下载

热门文章

  1. Directx11教程(9) 增加一个TimerClass类
  2. 2019-9-2-win10-uwp-随着数字变化颜色控件
  3. docker安装 2016-11-06 19:14 299人阅读 评论(31) 收藏
  4. Python3.6正向解析与反向解析域中主机
  5. oracle函数 nls_charset_name(n1)
  6. oracle函数 LENGTHC(c1).LENGTH2(c1).LENGTH4(c1)
  7. EC Round 41 (Rated for Div. 2)主席树 E. Tufurama
  8. LRJ-Example-06-01-Uva210
  9. oracle用UNION-ALL 替换UNION ( 如果有可能的话)
  10. 本地测试读取redis和普通文件缓存的速度,redis慢一倍?