結果
| 問題 | 
                            No.370 道路の掃除
                             | 
                    
| コンテスト | |
| ユーザー | 
                             tnakao0123
                         | 
                    
| 提出日時 | 2016-07-12 23:57:28 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 1,279 bytes | 
| コンパイル時間 | 826 ms | 
| コンパイル使用メモリ | 86,608 KB | 
| 実行使用メモリ | 6,824 KB | 
| 最終ジャッジ日時 | 2024-10-14 17:00:17 | 
| 合計ジャッジ時間 | 1,917 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 34 | 
ソースコード
/* -*- coding: utf-8 -*-
 *
 * 370.cc: No.370 道路の掃除 - yukicoder
 */
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;
/* constant */
const int MAX_M = 1000;
const int INF = 1 << 30;
/* typedef */
/* global variables */
int ps[MAX_M + 1], ms[MAX_M + 1];
bool zero = false;
/* subroutines */
/* main */
int main() {
  int n, m;
  cin >> n >> m;
  int pn = 0, mn = 0;
  for (int i = 0; i < m; i++) {
    int di;
    cin >> di;
    if (di > 0) ps[++pn] = di;
    else if (di < 0) ms[++mn] = -di;
    else zero = true;
  }
  //printf("pn=%d,mn=%d\n", pn, mn);
  if (zero) n--;
  sort(ps, ps + pn + 1);
  sort(ms, ms + mn + 1);
  int mind = INF, maxi = min(n, mn);
  for (int i = 0, j = n; i <= maxi; i++, j--)
    if (j <= pn) {
      int d0 = ms[i] * 2 + ps[j];
      int d1 = ms[i] + ps[j] * 2;
      //printf("ms[%d]=%d,ps[%d]=%d:%d,%d\n", i, ms[i], j, ps[j], d0, d1);
      if (mind > d0) mind = d0;
      if (mind > d1) mind = d1;
    }
  printf("%d\n", mind);
  return 0;
}
            
            
            
        
            
tnakao0123