Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <list>
- #include <map>
- #include <set>
- #include <deque>
- #include <stack>
- #include <queue>
- #include <algorithm>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <memory.h>
- #include <ctime>
- #include <bitset>
- #include <unordered_map>
- #include <unordered_set>
- using namespace std;
- #define ABS(a) ((a>0)?a:-(a))
- #define MIN(a,b) ((a<b)?(a):(b))
- #define MAX(a,b) ((a<b)?(b):(a))
- #define FOR(i,a,n) for (int i=(a);i<(n);++i)
- #define FI(i,n) for (int i=0; i<(n); ++i)
- #define pnt pair <int, int>
- #define mp make_pair
- #define PI 3.1415926535897
- #define MEMS(a,b) memset(a,b,sizeof(a))
- #define LL long long
- #define U unsigned
- vector<pair<pnt, int> > a;
- vector<pair<pnt, int> > b;
- bool cmp(pair<pnt, int> X, pair<pnt, int> Y)
- {
- pnt x = X.first;
- pnt y = Y.first;
- return (atan2(x.second, x.first) < atan2(y.second, y.first));
- }
- inline int getCross(int x1, int y1, int x2, int y2, int x3, int y3)
- {
- int X1 = x2 - x1;
- int Y1 = y2 - y1;
- int X2 = x3 - x1;
- int Y2 = y3 - y1;
- return X1*Y2 - X2*Y1;
- }
- int solve(int v)
- {
- int n = a.size();
- int cnt = 0;
- FOR(i, 0, n)
- {
- if (i != v)
- {
- b[cnt] = a[i];
- b[cnt].first.first -= a[v].first.first;
- b[cnt].first.second -= a[v].first.second;
- cnt++;
- }
- }
- sort(b.begin(), b.end(), cmp);
- int sum = 0;
- FOR(i, 0, b.size())
- {
- b[i].first.first += a[v].first.first;
- b[i].first.second += a[v].first.second;
- sum += b[i].second;
- }
- int who = 0;
- int cursum = 0;
- int res = 2000000000;
- FOR(i, 0, b.size())
- {
- if (who != i)
- cursum -= b[i].second;
- if (who == i)
- {
- who = MAX(who, i + 1);
- if (who == n - 1)
- who = 0;
- }
- while (who != i)
- {
- int val = getCross(a[v].first.first, a[v].first.second, b[i].first.first, b[i].first.second,
- b[who].first.first, b[who].first.second);
- if (val > 0)
- {
- cursum += b[who].second;
- who++;
- if (who == n - 1)
- who = 0;
- }
- else
- break;
- }
- int sum1 = cursum;
- int sum2 = sum - cursum - b[i].second;
- int sum3 = a[v].second;
- int sum4 = b[i].second;
- res = MIN(res, ABS(sum1 - sum2 + sum3 + sum4));
- res = MIN(res, ABS(sum1 - sum2 + sum3 - sum4));
- res = MIN(res, ABS(sum1 - sum2 - sum3 + sum4));
- res = MIN(res, ABS(sum1 - sum2 - sum3 - sum4));
- }
- return res;
- }
- int main()
- {
- #ifdef Fcdkbear
- freopen("in.txt", "r", stdin);
- //freopen("out.txt", "w", stdout);
- double beg = clock();
- #endif
- int n;
- scanf("%d", &n);
- a.resize(n);
- b.resize(n - 1);
- FOR(i, 0, n)
- scanf("%d%d%d", &a[i].first.first, &a[i].first.second, &a[i].second);
- if (n == 1)
- {
- cout << a[0].second << endl;
- return 0;
- }
- int res = 2000000000;
- FOR(i, 0, n)
- {
- res = min(res, solve(i));
- }
- cout << res << endl;
- #ifdef Fcdkbear
- double end = clock();
- fprintf(stderr, "*** Total time = %.3lf ***\n", (end - beg) / CLOCKS_PER_SEC);
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement